-
-
Notifications
You must be signed in to change notification settings - Fork 964
feat(graphql_analyze): implement useUniqueVariableNames #8588
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
Merged
Merged
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
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,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`useUniqueVariableNames`](https://biomejs.dev/linter/rules/use-unique-variable-names/). Enforce unique variable names for GraphQL operations. |
Large diffs are not rendered by default.
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
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
85 changes: 85 additions & 0 deletions
85
crates/biome_graphql_analyze/src/lint/nursery/use_unique_variable_names.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,85 @@ | ||
| use std::collections::HashSet; | ||
|
|
||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_graphql_syntax::GraphqlVariableDefinitions; | ||
| use biome_rowan::{AstNode, TokenText}; | ||
| use biome_rule_options::use_unique_variable_names::UseUniqueVariableNamesOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Require all variable definitions to be unique. | ||
| /// | ||
| /// A GraphQL operation is only valid if all its variables are uniquely named. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```graphql,expect_diagnostic | ||
| /// query C($x: Int, $x: Int) { | ||
| /// __typename | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```graphql | ||
| /// query C($x: Int, $y: Int) { | ||
| /// __typename | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| pub UseUniqueVariableNames { | ||
| version: "next", | ||
| name: "useUniqueVariableNames", | ||
| language: "graphql", | ||
| recommended: false, | ||
| sources: &[RuleSource::EslintGraphql("unique-variable-names").same()], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseUniqueVariableNames { | ||
| type Query = Ast<GraphqlVariableDefinitions>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseUniqueVariableNamesOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
| let node = ctx.query(); | ||
| let mut found: HashSet<TokenText> = HashSet::new(); | ||
|
|
||
| for element in node.elements() { | ||
| if let Some(variable) = element.variable().ok() | ||
| && let Some(name) = variable.name().ok() | ||
| && let Some(value_token) = name.value_token().ok() | ||
| { | ||
| let string = value_token.token_text(); | ||
| if found.contains(&string) { | ||
| return Some(()); | ||
| } else { | ||
| found.insert(string); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let span = ctx.query().range(); | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| span, | ||
| markup! { | ||
| "Duplicate variable name." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "A GraphQL operation is only valid if all its variables are uniquely named. Make sure to name every variable differently." | ||
| }), | ||
| ) | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueVariableNames/invalid.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,4 @@ | ||
| # should generate diagnostics | ||
| query A($x: Int, $x: Int, $x: String) { __typename } | ||
| query B($x: String, $x: Int) { __typename } | ||
| query C($x: Int, $x: Int) { __typename } |
68 changes: 68 additions & 0 deletions
68
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueVariableNames/invalid.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,68 @@ | ||
| --- | ||
| source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
| expression: invalid.graphql | ||
| --- | ||
| # Input | ||
| ```graphql | ||
| # should generate diagnostics | ||
| query A($x: Int, $x: Int, $x: String) { __typename } | ||
| query B($x: String, $x: Int) { __typename } | ||
| query C($x: Int, $x: Int) { __typename } | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.graphql:2:8 lint/nursery/useUniqueVariableNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Duplicate variable name. | ||
|
|
||
| 1 │ # should generate diagnostics | ||
| > 2 │ query A($x: Int, $x: Int, $x: String) { __typename } | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 3 │ query B($x: String, $x: Int) { __typename } | ||
| 4 │ query C($x: Int, $x: Int) { __typename } | ||
|
|
||
| i A GraphQL operation is only valid if all its variables are uniquely named. Make sure to name every variable differently. | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.graphql:3:8 lint/nursery/useUniqueVariableNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Duplicate variable name. | ||
|
|
||
| 1 │ # should generate diagnostics | ||
| 2 │ query A($x: Int, $x: Int, $x: String) { __typename } | ||
| > 3 │ query B($x: String, $x: Int) { __typename } | ||
| │ ^^^^^^^^^^^^^^^^^^^^^ | ||
| 4 │ query C($x: Int, $x: Int) { __typename } | ||
| 5 │ | ||
|
|
||
| i A GraphQL operation is only valid if all its variables are uniquely named. Make sure to name every variable differently. | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.graphql:4:8 lint/nursery/useUniqueVariableNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Duplicate variable name. | ||
|
|
||
| 2 │ query A($x: Int, $x: Int, $x: String) { __typename } | ||
| 3 │ query B($x: String, $x: Int) { __typename } | ||
| > 4 │ query C($x: Int, $x: Int) { __typename } | ||
| │ ^^^^^^^^^^^^^^^^^^ | ||
| 5 │ | ||
|
|
||
| i A GraphQL operation is only valid if all its variables are uniquely named. Make sure to name every variable differently. | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
| ``` |
3 changes: 3 additions & 0 deletions
3
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueVariableNames/valid.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,3 @@ | ||
| # should not generate diagnostics | ||
| query A($x: Int, $y: String) { __typename } | ||
| query B($x: String, $y: Int) { __typename } |
11 changes: 11 additions & 0 deletions
11
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueVariableNames/valid.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,11 @@ | ||
| --- | ||
| source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
| expression: valid.graphql | ||
| --- | ||
| # Input | ||
| ```graphql | ||
| # should not generate diagnostics | ||
| query A($x: Int, $y: String) { __typename } | ||
| query B($x: String, $y: Int) { __typename } | ||
|
|
||
| ``` |
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
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,6 @@ | ||
| use biome_deserialize_macros::{Deserializable, Merge}; | ||
| use serde::{Deserialize, Serialize}; | ||
| #[derive(Default, Clone, Debug, Deserialize, Deserializable, Merge, Eq, PartialEq, Serialize)] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
| pub struct UseUniqueVariableNamesOptions {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
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.