-
-
Notifications
You must be signed in to change notification settings - Fork 930
feat(graphql_analyze): implement useUniqueInputFieldNames #8592
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
Netail
merged 2 commits into
biomejs:main
from
Netail:feat/use-unique-input-field-names
Dec 28, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,13 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`useUniqueInputFieldNames`](https://biomejs.dev/linter/rules/use-unique-input-field-names/). Require fields within an input object to be unique. | ||
|
|
||
| **Invalid:** | ||
|
|
||
| ```graphql | ||
| query A($x: Int, $x: Int) { | ||
| field | ||
| } | ||
| ``` | ||
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
84 changes: 84 additions & 0 deletions
84
crates/biome_graphql_analyze/src/lint/nursery/use_unique_input_field_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,84 @@ | ||
| use std::collections::HashSet; | ||
|
|
||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_graphql_syntax::GraphqlObjectValue; | ||
| use biome_rowan::{AstNode, TokenText}; | ||
| use biome_rule_options::use_unique_input_field_names::UseUniqueInputFieldNamesOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Require fields within an input object to be unique. | ||
| /// | ||
| /// A GraphQL input object value is only valid if all supplied fields are uniquely named. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```graphql,expect_diagnostic | ||
| /// query { | ||
| /// field(arg: { f1: "value", f1: "value" }) | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```graphql | ||
| /// query { | ||
| /// field(arg: { f1: "value", f2: "value" }) | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| pub UseUniqueInputFieldNames { | ||
| version: "next", | ||
| name: "useUniqueInputFieldNames", | ||
| language: "graphql", | ||
| recommended: false, | ||
| sources: &[RuleSource::EslintGraphql("unique-input-field-names").same()], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseUniqueInputFieldNames { | ||
| type Query = Ast<GraphqlObjectValue>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseUniqueInputFieldNamesOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
| let mut found: HashSet<TokenText> = HashSet::new(); | ||
|
|
||
| for element in node.members() { | ||
| if let Some(name) = element.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 input field name." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "A GraphQL input object value is only valid if all supplied fields are uniquely named. Make sure to name every input field differently." | ||
| }), | ||
| ) | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueInputFieldNames/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,10 @@ | ||
| # should generate diagnostics | ||
| query A { | ||
| field(arg: { f1: "value", f1: "value" }) | ||
| } | ||
| query B { | ||
| field(arg: { f1: "value", f1: "value", f1: "value" }) | ||
| } | ||
| query C { | ||
| field(arg: { f1: {f2: "value", f2: "value" }}) | ||
| } |
76 changes: 76 additions & 0 deletions
76
...s/biome_graphql_analyze/tests/specs/nursery/useUniqueInputFieldNames/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,76 @@ | ||
| --- | ||
| source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
| expression: invalid.graphql | ||
| --- | ||
| # Input | ||
| ```graphql | ||
| # should generate diagnostics | ||
| query A { | ||
| field(arg: { f1: "value", f1: "value" }) | ||
| } | ||
| query B { | ||
| field(arg: { f1: "value", f1: "value", f1: "value" }) | ||
| } | ||
| query C { | ||
| field(arg: { f1: {f2: "value", f2: "value" }}) | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.graphql:3:13 lint/nursery/useUniqueInputFieldNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Duplicate input field name. | ||
|
|
||
| 1 │ # should generate diagnostics | ||
| 2 │ query A { | ||
| > 3 │ field(arg: { f1: "value", f1: "value" }) | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 4 │ } | ||
| 5 │ query B { | ||
|
|
||
| i A GraphQL input object value is only valid if all supplied fields are uniquely named. Make sure to name every input field 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:6:13 lint/nursery/useUniqueInputFieldNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Duplicate input field name. | ||
|
|
||
| 4 │ } | ||
| 5 │ query B { | ||
| > 6 │ field(arg: { f1: "value", f1: "value", f1: "value" }) | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 7 │ } | ||
| 8 │ query C { | ||
|
|
||
| i A GraphQL input object value is only valid if all supplied fields are uniquely named. Make sure to name every input field 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:9:19 lint/nursery/useUniqueInputFieldNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Duplicate input field name. | ||
|
|
||
| 7 │ } | ||
| 8 │ query C { | ||
| > 9 │ field(arg: { f1: {f2: "value", f2: "value" }}) | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 10 │ } | ||
| 11 │ | ||
|
|
||
| i A GraphQL input object value is only valid if all supplied fields are uniquely named. Make sure to name every input field 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. | ||
|
|
||
|
|
||
| ``` |
21 changes: 21 additions & 0 deletions
21
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueInputFieldNames/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,21 @@ | ||
| # should not generate diagnostics | ||
| query A { | ||
| field(arg: { f: true }) | ||
| } | ||
| query B { | ||
| field(arg1: { f: true }, arg2: { f: true }) | ||
| } | ||
| query C { | ||
| field(arg: { f1: "value", f2: "value", f3: "value" }) | ||
| } | ||
| query D { | ||
| field(arg: { | ||
| deep: { | ||
| deep: { | ||
| id: 1 | ||
| } | ||
| id: 1 | ||
| } | ||
| id: 1 | ||
| }) | ||
| } |
29 changes: 29 additions & 0 deletions
29
crates/biome_graphql_analyze/tests/specs/nursery/useUniqueInputFieldNames/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,29 @@ | ||
| --- | ||
| source: crates/biome_graphql_analyze/tests/spec_tests.rs | ||
| expression: valid.graphql | ||
| --- | ||
| # Input | ||
| ```graphql | ||
| # should not generate diagnostics | ||
| query A { | ||
| field(arg: { f: true }) | ||
| } | ||
| query B { | ||
| field(arg1: { f: true }, arg2: { f: true }) | ||
| } | ||
| query C { | ||
| field(arg: { f1: "value", f2: "value", f3: "value" }) | ||
| } | ||
| query D { | ||
| field(arg: { | ||
| deep: { | ||
| deep: { | ||
| id: 1 | ||
| } | ||
| id: 1 | ||
| } | ||
| id: 1 | ||
| }) | ||
| } | ||
|
|
||
| ``` |
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
6 changes: 6 additions & 0 deletions
6
crates/biome_rule_options/src/use_unique_input_field_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,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 UseUniqueInputFieldNamesOptions {} |
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.