Skip to content
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

feat(graphql_analyze): useNamedOperation #4337

Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

40 changes: 31 additions & 9 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

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

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 @@ -201,6 +201,7 @@ define_categories! {
"lint/nursery/useGuardForIn": "https://biomejs.dev/linter/rules/use-guard-for-in",
"lint/nursery/useImportRestrictions": "https://biomejs.dev/linter/rules/use-import-restrictions",
"lint/nursery/useJsxCurlyBraceConvention": "https://biomejs.dev/linter/rules/use-jsx-curly-brace-convention",
"lint/nursery/useNamedOperation": "https://biomejs.dev/linter/rules/use-named-operation",
"lint/nursery/useSortedClasses": "https://biomejs.dev/linter/rules/use-sorted-classes",
"lint/nursery/useStrictMode": "https://biomejs.dev/linter/rules/use-strict-mode",
"lint/nursery/useTrimStartEnd": "https://biomejs.dev/linter/rules/use-trim-start-end",
Expand Down
1 change: 1 addition & 0 deletions crates/biome_graphql_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ biome_console = { workspace = true }
biome_deserialize = { workspace = true }
biome_deserialize_macros = { workspace = true }
biome_diagnostics = { workspace = true }
biome_graphql_factory = { workspace = true }
biome_graphql_syntax = { workspace = true }
biome_rowan = { workspace = true }
biome_string_case = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion crates/biome_graphql_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ pub use crate::registry::visit_registry;
use crate::suppression_action::GraphqlSuppressionAction;
use biome_analyze::{
AnalysisFilter, AnalyzerOptions, AnalyzerSignal, ControlFlow, LanguageRoot, MatchQueryParams,
MetadataRegistry, RuleRegistry, SuppressionKind,
MetadataRegistry, RuleAction, RuleRegistry, SuppressionKind,
};
use biome_diagnostics::{category, Error};
use biome_graphql_syntax::GraphqlLanguage;
use biome_suppression::{parse_suppression_comment, SuppressionDiagnostic};
use std::ops::Deref;
use std::sync::LazyLock;

pub(crate) type GraphqlRuleAction = RuleAction<GraphqlLanguage>;

pub static METADATA: LazyLock<MetadataRegistry> = LazyLock::new(|| {
let mut metadata = MetadataRegistry::default();
visit_registry(&mut metadata);
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_graphql_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use biome_analyze::declare_lint_group;

pub mod no_duplicated_fields;
pub mod use_deprecated_reason;
pub mod use_named_operation;

declare_lint_group! {
pub Nursery {
name : "nursery" ,
rules : [
self :: no_duplicated_fields :: NoDuplicatedFields ,
self :: use_deprecated_reason :: UseDeprecatedReason ,
self :: use_named_operation :: UseNamedOperation ,
]
}
}
117 changes: 117 additions & 0 deletions crates/biome_graphql_analyze/src/lint/nursery/use_named_operation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use biome_analyze::{
context::RuleContext, declare_lint_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic,
RuleSource, RuleSourceKind,
};
use biome_console::markup;
use biome_graphql_factory::make;
use biome_graphql_syntax::{GraphqlOperationDefinition, GraphqlOperationType};
use biome_rowan::{AstNode, BatchMutationExt};
use biome_string_case::Case;

use crate::GraphqlRuleAction;

declare_lint_rule! {
/// Enforce specifying the name of GraphQL operations.
///
/// This is useful because most GraphQL client libraries use the operation name for caching purposes.
///
/// ## Examples
///
/// ### Invalid
///
/// ```graphql,expect_diagnostic
/// query {}
/// ```
///
/// ### Valid
///
/// ```graphql
/// query Human {
/// name
/// }
/// ```
///
pub UseNamedOperation {
version: "next",
name: "useNamedOperation",
language: "graphql",
sources: &[RuleSource::EslintGraphql("no-anonymous-operations")],
source_kind: RuleSourceKind::SameLogic,
recommended: true,
fix_kind: FixKind::Unsafe,
}
}

impl Rule for UseNamedOperation {
type Query = Ast<GraphqlOperationDefinition>;
type State = GraphqlOperationType;
type Signals = Option<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let operation_type = node.ty().ok()?;
if node.name().is_some() {
None
} else {
Some(operation_type)
}
}

fn diagnostic(
_ctx: &RuleContext<Self>,
operation_type: &Self::State,
) -> Option<RuleDiagnostic> {
Some(
RuleDiagnostic::new(
rule_category!(),
operation_type.range(),
markup! {
"Anonymous GraphQL operations are forbidden. Make sure to name your " {operation_type.text()}"."
},
)
.note(markup! {
"Most GraphQL client libraries use the operation name for caching purposes."
}),
)
}

fn action(ctx: &RuleContext<Self>, operation_type: &Self::State) -> Option<GraphqlRuleAction> {
let mut mutation = ctx.root().begin();
let node = ctx.query().clone();
let operation_type = operation_type.text();
let suggested_name = get_suggested_name(&node, operation_type.clone());
let new_name = make::graphql_name_binding(make::ident(&suggested_name));
let new_node = node.clone().with_name(Some(new_name));
mutation.replace_node(node, new_node);

Some(GraphqlRuleAction::new(
ActionCategory::QuickFix,
ctx.metadata().applicability(),
markup! {
"Rename this "{operation_type}" to "{suggested_name}"."
},
mutation,
))
}
}

fn get_suggested_name(operation: &GraphqlOperationDefinition, operation_type: String) -> String {
let suggested_name = operation
.selection_set()
.ok()
.and_then(|selection_set| {
selection_set
.selections()
.into_iter()
.find_map(|selection| selection.as_graphql_field().cloned())
})
.and_then(|first_field| {
first_field
.alias()
.map(|alias| alias.text())
.or(first_field.name().ok().map(|name| name.text()))
})
.unwrap_or(operation_type);
Case::Pascal.convert(&suggested_name)
}
2 changes: 2 additions & 0 deletions crates/biome_graphql_analyze/src/options.rs

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query { human }
mutation { ...Type }
subscription {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
source: crates/biome_graphql_analyze/tests/spec_tests.rs
expression: invalid.graphql
---
# Input
```graphql
query { human }
mutation { ...Type }
subscription {}

```

# Diagnostics
```
invalid.graphql:1:1 lint/nursery/useNamedOperation FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Anonymous GraphQL operations are forbidden. Make sure to name your query.

> 1 │ query { human }
│ ^^^^^
2 │ mutation { ...Type }
3 │ subscription {}

i Most GraphQL client libraries use the operation name for caching purposes.

i Unsafe fix: Rename this query to Human.

1 │ query·Human{·human·}
│ +++++

```

```
invalid.graphql:2:1 lint/nursery/useNamedOperation FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Anonymous GraphQL operations are forbidden. Make sure to name your mutation.

1 │ query { human }
> 2 │ mutation { ...Type }
│ ^^^^^^^^
3 │ subscription {}
4 │

i Most GraphQL client libraries use the operation name for caching purposes.

i Unsafe fix: Rename this mutation to Mutation.

2 │ mutation·Mutation{·...Type·}
│ ++++++++

```

```
invalid.graphql:3:1 lint/nursery/useNamedOperation FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Anonymous GraphQL operations are forbidden. Make sure to name your subscription.

1 │ query { human }
2 │ mutation { ...Type }
> 3 │ subscription {}
│ ^^^^^^^^^^^^
4 │

i Most GraphQL client libraries use the operation name for caching purposes.

i Unsafe fix: Rename this subscription to Subscription.

3 │ subscription·Subscription{}
│ ++++++++++++

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* should not generate diagnostics */
// var a = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: crates/biome_graphql_analyze/tests/spec_tests.rs
expression: valid.graphql
---
# Input
```graphql
/* should not generate diagnostics */
// var a = 1;
```
Loading