Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/cold-ravens-show.md
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
}
```
71 changes: 46 additions & 25 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ define_categories! {
"lint/nursery/useSortedClasses": "https://biomejs.dev/linter/rules/use-sorted-classes",
"lint/nursery/useSpread": "https://biomejs.dev/linter/rules/no-spread",
"lint/nursery/useUniqueGraphqlOperationName": "https://biomejs.dev/linter/rules/use-unique-graphql-operation-name",
"lint/nursery/useUniqueInputFieldNames": "https://biomejs.dev/linter/rules/use-unique-input-field-names",
"lint/nursery/useUniqueVariableNames": "https://biomejs.dev/linter/rules/use-unique-variable-names",
"lint/nursery/useVueConsistentDefinePropsDeclaration": "https://biomejs.dev/linter/rules/use-vue-consistent-define-props-declaration",
"lint/nursery/useVueDefineMacrosOrder": "https://biomejs.dev/linter/rules/use-vue-define-macros-order",
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_graphql_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pub mod no_empty_source;
pub mod use_consistent_graphql_descriptions;
pub mod use_deprecated_date;
pub mod use_unique_graphql_operation_name;
pub mod use_unique_input_field_names;
pub mod use_unique_variable_names;
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: no_empty_source :: NoEmptySource , self :: use_consistent_graphql_descriptions :: UseConsistentGraphqlDescriptions , self :: use_deprecated_date :: UseDeprecatedDate , self :: use_unique_graphql_operation_name :: UseUniqueGraphqlOperationName , self :: use_unique_variable_names :: UseUniqueVariableNames ,] } }
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: no_empty_source :: NoEmptySource , self :: use_consistent_graphql_descriptions :: UseConsistentGraphqlDescriptions , self :: use_deprecated_date :: UseDeprecatedDate , self :: use_unique_graphql_operation_name :: UseUniqueGraphqlOperationName , self :: use_unique_input_field_names :: UseUniqueInputFieldNames , self :: use_unique_variable_names :: UseUniqueVariableNames ,] } }
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."
}),
)
}
}
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" }})
}
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.


```
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
})
}
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
})
}

```
1 change: 1 addition & 0 deletions crates/biome_rule_options/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ pub mod use_trim_start_end;
pub mod use_unified_type_signatures;
pub mod use_unique_element_ids;
pub mod use_unique_graphql_operation_name;
pub mod use_unique_input_field_names;
pub mod use_unique_variable_names;
pub mod use_valid_anchor;
pub mod use_valid_aria_props;
Expand Down
6 changes: 6 additions & 0 deletions crates/biome_rule_options/src/use_unique_input_field_names.rs
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 {}
14 changes: 14 additions & 0 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.