-
-
Notifications
You must be signed in to change notification settings - Fork 792
feat(graphql_analyze): implement useLoneExecutableDefinition #8617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Netail
wants to merge
1
commit into
biomejs:main
Choose a base branch
from
Netail:feat/use-lone-executable-definition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`useLoneExecutableDefinition`](https://biomejs.dev/linter/rules/use-lone-executable-definition/). Require queries, mutations, subscriptions or fragments to be located in separate files. | ||
|
|
||
| **Invalid:** | ||
|
|
||
| ```graphql | ||
| query Foo { | ||
| id | ||
| } | ||
|
|
||
| fragment Bar on Baz { | ||
| id | ||
| } | ||
| ``` |
141 changes: 81 additions & 60 deletions
141
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
crates/biome_graphql_analyze/src/lint/nursery/use_lone_executable_definition.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_graphql_syntax::{AnyGraphqlDefinition, GraphqlRoot}; | ||
| use biome_rowan::{AstNode, AstNodeList, TextRange}; | ||
| use biome_rule_options::use_lone_executable_definition::UseLoneExecutableDefinitionOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Require queries, mutations, subscriptions or fragments to be located in separate files. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```graphql,expect_diagnostic | ||
| /// query Foo { | ||
| /// id | ||
| /// } | ||
| /// | ||
| /// fragment Bar on Baz { | ||
| /// id | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```graphql | ||
| /// query Foo { | ||
| /// id | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ```graphql | ||
| /// fragment Bar on Baz { | ||
| /// id | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| pub UseLoneExecutableDefinition { | ||
| version: "next", | ||
| name: "useLoneExecutableDefinition", | ||
| language: "graphql", | ||
| recommended: false, | ||
| sources: &[RuleSource::EslintGraphql("lone-executable-definition").same()], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseLoneExecutableDefinition { | ||
| type Query = Ast<GraphqlRoot>; | ||
| type State = Vec<TextRange>; | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseLoneExecutableDefinitionOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
|
|
||
| let mut definitions = node | ||
| .definitions() | ||
| .iter() | ||
| .filter_map(|def| match def { | ||
| AnyGraphqlDefinition::GraphqlOperationDefinition(operation_definition) => { | ||
| Some(operation_definition.range()) | ||
| } | ||
| AnyGraphqlDefinition::GraphqlFragmentDefinition(fragment_definition) => { | ||
| Some(fragment_definition.range()) | ||
| } | ||
| AnyGraphqlDefinition::GraphqlSelectionSet(selection_set) => { | ||
| Some(selection_set.range()) | ||
| } | ||
| _ => None, | ||
| }) | ||
| .collect::<Vec<TextRange>>(); | ||
|
|
||
| if definitions.len() > 1 { | ||
| definitions.remove(0); | ||
| return Some(definitions); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let span = ctx.query().range(); | ||
| let mut diagnostic = RuleDiagnostic::new( | ||
| rule_category!(), | ||
| span, | ||
| markup! { | ||
| "Document contains (multiple) definitions." | ||
| }, | ||
| ); | ||
|
|
||
| for range in state { | ||
| diagnostic = diagnostic.detail( | ||
| range, | ||
| markup! { | ||
| "This definition should be in a separate file." | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| Some( | ||
| diagnostic | ||
| .note(markup! { | ||
| "Require queries, mutations, subscriptions or fragments to be located in separate files. Separate definitions into separate files." | ||
| }) | ||
| ) | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
...s/specs/nursery/useLoneExecutableDefinition/invalid/multi-executables-definitions.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # should generate diagnostics | ||
| query Valid { | ||
| id | ||
| } | ||
| { | ||
| id | ||
| } | ||
| fragment Bar on Bar { | ||
| id | ||
| } | ||
| mutation ($name: String!) { | ||
| createFoo { | ||
| name | ||
| } | ||
| } | ||
| mutation Baz($name: String!) { | ||
| createFoo { | ||
| name | ||
| } | ||
| } | ||
| subscription { | ||
| id | ||
| } | ||
| subscription Sub { | ||
| id | ||
| } |
134 changes: 134 additions & 0 deletions
134
...cs/nursery/useLoneExecutableDefinition/invalid/multi-executables-definitions.graphql.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| --- | ||
| source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
| expression: multi-executables-definitions.graphql | ||
| --- | ||
| # Input | ||
| ```graphql | ||
| # should generate diagnostics | ||
| query Valid { | ||
| id | ||
| } | ||
| { | ||
| id | ||
| } | ||
| fragment Bar on Bar { | ||
| id | ||
| } | ||
| mutation ($name: String!) { | ||
| createFoo { | ||
| name | ||
| } | ||
| } | ||
| mutation Baz($name: String!) { | ||
| createFoo { | ||
| name | ||
| } | ||
| } | ||
| subscription { | ||
| id | ||
| } | ||
| subscription Sub { | ||
| id | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| multi-executables-definitions.graphql:2:1 lint/nursery/useLoneExecutableDefinition ━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Document contains (multiple) definitions. | ||
|
|
||
| 1 │ # should generate diagnostics | ||
| > 2 │ query Valid { | ||
| │ ^^^^^^^^^^^^^ | ||
| > 3 │ id | ||
| > 4 │ } | ||
| ... | ||
| > 24 │ subscription Sub { | ||
| > 25 │ id | ||
| > 26 │ } | ||
| │ ^ | ||
| 27 │ | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 3 │ id | ||
| 4 │ } | ||
| > 5 │ { | ||
| │ ^ | ||
| > 6 │ id | ||
| > 7 │ } | ||
| │ ^ | ||
| 8 │ fragment Bar on Bar { | ||
| 9 │ id | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 6 │ id | ||
| 7 │ } | ||
| > 8 │ fragment Bar on Bar { | ||
| │ ^^^^^^^^^^^^^^^^^^^^^ | ||
| > 9 │ id | ||
| > 10 │ } | ||
| │ ^ | ||
| 11 │ mutation ($name: String!) { | ||
| 12 │ createFoo { | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 9 │ id | ||
| 10 │ } | ||
| > 11 │ mutation ($name: String!) { | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| > 12 │ createFoo { | ||
| > 13 │ name | ||
| > 14 │ } | ||
| > 15 │ } | ||
| │ ^ | ||
| 16 │ mutation Baz($name: String!) { | ||
| 17 │ createFoo { | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 14 │ } | ||
| 15 │ } | ||
| > 16 │ mutation Baz($name: String!) { | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| > 17 │ createFoo { | ||
| > 18 │ name | ||
| > 19 │ } | ||
| > 20 │ } | ||
| │ ^ | ||
| 21 │ subscription { | ||
| 22 │ id | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 19 │ } | ||
| 20 │ } | ||
| > 21 │ subscription { | ||
| │ ^^^^^^^^^^^^^^ | ||
| > 22 │ id | ||
| > 23 │ } | ||
| │ ^ | ||
| 24 │ subscription Sub { | ||
| 25 │ id | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 22 │ id | ||
| 23 │ } | ||
| > 24 │ subscription Sub { | ||
| │ ^^^^^^^^^^^^^^^^^^ | ||
| > 25 │ id | ||
| > 26 │ } | ||
| │ ^ | ||
| 27 │ | ||
|
|
||
| i Require queries, mutations, subscriptions or fragments to be located in separate files. Separate definitions into separate files. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
8 changes: 8 additions & 0 deletions
8
...analyze/tests/specs/nursery/useLoneExecutableDefinition/invalid/multi-named-query.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # should generate diagnostics | ||
| query A { | ||
| id | ||
| } | ||
|
|
||
| query B { | ||
| id | ||
| } |
52 changes: 52 additions & 0 deletions
52
...ze/tests/specs/nursery/useLoneExecutableDefinition/invalid/multi-named-query.graphql.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| --- | ||
| source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
| expression: multi-named-query.graphql | ||
| --- | ||
| # Input | ||
| ```graphql | ||
| # should generate diagnostics | ||
| query A { | ||
| id | ||
| } | ||
|
|
||
| query B { | ||
| id | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| multi-named-query.graphql:2:1 lint/nursery/useLoneExecutableDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Document contains (multiple) definitions. | ||
|
|
||
| 1 │ # should generate diagnostics | ||
| > 2 │ query A { | ||
| │ ^^^^^^^^^ | ||
| > 3 │ id | ||
| > 4 │ } | ||
| > 5 │ | ||
| > 6 │ query B { | ||
| > 7 │ id | ||
| > 8 │ } | ||
| │ ^ | ||
| 9 │ | ||
|
|
||
| i This definition should be in a separate file. | ||
|
|
||
| 4 │ } | ||
| 5 │ | ||
| > 6 │ query B { | ||
| │ ^^^^^^^^^ | ||
| > 7 │ id | ||
| > 8 │ } | ||
| │ ^ | ||
| 9 │ | ||
|
|
||
| i Require queries, mutations, subscriptions or fragments to be located in separate files. Separate definitions into separate files. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
7 changes: 7 additions & 0 deletions
7
...e/tests/specs/nursery/useLoneExecutableDefinition/invalid/single-query-definition.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # should generate diagnostics | ||
| { | ||
| id | ||
| } | ||
| fragment Bar on Bar { | ||
| id | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.