Skip to content
Open
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
17 changes: 17 additions & 0 deletions .changeset/lazy-doodles-attack.md
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 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

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

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 @@ -6,9 +6,10 @@ use biome_analyze::declare_lint_group;
pub mod no_empty_source;
pub mod use_consistent_graphql_descriptions;
pub mod use_deprecated_date;
pub mod use_lone_executable_definition;
pub mod use_unique_argument_names;
pub mod use_unique_field_definition_names;
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_argument_names :: UseUniqueArgumentNames , self :: use_unique_field_definition_names :: UseUniqueFieldDefinitionNames , self :: use_unique_graphql_operation_name :: UseUniqueGraphqlOperationName , self :: use_unique_input_field_names :: UseUniqueInputFieldNames , 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_lone_executable_definition :: UseLoneExecutableDefinition , self :: use_unique_argument_names :: UseUniqueArgumentNames , self :: use_unique_field_definition_names :: UseUniqueFieldDefinitionNames , 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,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."
})
)
}
}
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
}
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.


```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# should generate diagnostics
query A {
id
}

query B {
id
}
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.


```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# should generate diagnostics
{
id
}
fragment Bar on Bar {
id
}
Loading