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
5 changes: 5 additions & 0 deletions .changeset/public-walls-buy.md
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.
67 changes: 44 additions & 23 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ define_categories! {
"lint/nursery/noParametersOnlyUsedInRecursion": "https://biomejs.dev/linter/rules/no-parameters-only-used-in-recursion",
"lint/nursery/noProto": "https://biomejs.dev/linter/rules/no-proto",
"lint/nursery/noReactForwardRef": "https://biomejs.dev/linter/rules/no-react-forward-ref",
"lint/nursery/noScriptUrl": "https://biomejs.dev/linter/rules/no-script-url",
"lint/nursery/noReturnAssign": "https://biomejs.dev/linter/rules/no-return-assign",
"lint/nursery/noScriptUrl": "https://biomejs.dev/linter/rules/no-script-url",
"lint/nursery/noShadow": "https://biomejs.dev/linter/rules/no-shadow",
"lint/nursery/noSyncScripts": "https://biomejs.dev/linter/rules/no-sync-scripts",
"lint/nursery/noTernary": "https://biomejs.dev/linter/rules/no-ternary",
Expand Down 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/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",
"lint/nursery/useVueHyphenatedAttributes": "https://biomejs.dev/linter/rules/use-vue-hyphenated-attributes",
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,4 +7,5 @@ pub mod no_empty_source;
pub mod use_consistent_graphql_descriptions;
pub mod use_deprecated_date;
pub mod use_unique_graphql_operation_name;
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 ,] } }
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 ,] } }
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."
}),
)
}
}
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 }
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.


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

```
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_variable_names;
pub mod use_valid_anchor;
pub mod use_valid_aria_props;
pub mod use_valid_aria_role;
Expand Down
6 changes: 6 additions & 0 deletions crates/biome_rule_options/src/use_unique_variable_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 UseUniqueVariableNamesOptions {}
16 changes: 15 additions & 1 deletion 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.