Skip to content

Commit

Permalink
feat(graphql_analyze): useDeprecatedReason (#3289)
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 committed Jun 26, 2024
1 parent 436d44b commit 7935235
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

#### New features

- Add `nursery/useDeprecatedReason` rule. Contributed by @vohoanglong0107.
- Add [nursery/noExportedImports](https://biomejs.dev/linter/rules/no-exported-imports/). Contributed by @Conaclos

#### Bug fixes
Expand Down
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 @@ -154,6 +154,7 @@ define_categories! {
"lint/nursery/useConsistentGridAreas": "https://biomejs.dev/linter/rules/use-consistent-grid-areas",
"lint/nursery/useDateNow": "https://biomejs.dev/linter/rules/use-date-now",
"lint/nursery/useDefaultSwitchClause": "https://biomejs.dev/linter/rules/use-default-switch-clause",
"lint/nursery/useDeprecatedReason": "https://biomejs.dev/linter/rules/use-deprecated-reason",
"lint/nursery/useErrorMessage": "https://biomejs.dev/linter/rules/use-error-message",
"lint/nursery/useExplicitLengthCheck": "https://biomejs.dev/linter/rules/use-explicit-length-check",
"lint/nursery/useFocusableInteractive": "https://biomejs.dev/linter/rules/use-focusable-interactive",
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_graphql_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use biome_analyze::declare_lint_group;

pub mod use_dummy_rule;
pub mod use_deprecated_reason;

declare_lint_group! {
pub Nursery {
name : "nursery" ,
rules : [
self :: use_dummy_rule :: UseDummyRule ,
self :: use_deprecated_reason :: UseDeprecatedReason ,
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_console::markup;
use biome_graphql_syntax::GraphqlDirective;
use biome_rowan::AstNode;

declare_rule! {
/// Require specifying the reason argument when using @deprecated directive
///
/// This rule checks the parameter of @deprecated directive for the use of reason argument,
/// suggesting user to add it in case the argument is missing.
///
/// ## Examples
///
/// ### Invalid
///
/// ```graphql,expect_diagnostic
/// query {
/// member @deprecated
/// }
/// ```
///
/// ### Valid
///
/// ```graphql
/// query {
/// member @deprecated(reason: "Why?")
/// }
/// ```
pub UseDeprecatedReason {
version: "next",
name: "useDeprecatedReason",
language: "graphql",
recommended: false,
}
}

impl Rule for UseDeprecatedReason {
type Query = Ast<GraphqlDirective>;
type State = GraphqlDirective;
type Signals = Option<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let name = node.name().ok()?;
if name.text() != "deprecated" {
return None;
}
// Fire diagnostic if the directive does not have any arguments
let Some(arguments) = node.arguments() else {
return Some(node.clone());
};
let arguments = arguments.arguments();
let has_reason = arguments
.into_iter()
.any(|argument| argument.name().is_ok_and(|name| name.text() == "reason"));
if has_reason {
None
} else {
Some(node.clone())
}
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
//
// Read our guidelines to write great diagnostics:
// https://docs.rs/biome_analyze/latest/biome_analyze/#what-a-rule-should-say-to-the-user
//
let span = ctx.query().range();
Some(
RuleDiagnostic::new(
rule_category!(),
span,
markup! {
"The directive `@deprecated` should have a `reason` argument."
},
)
.note(markup! {
"Add a `reason` argument to the directive."
}),
)
}
}
42 changes: 0 additions & 42 deletions crates/biome_graphql_analyze/src/lint/nursery/use_dummy_rule.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/biome_graphql_analyze/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

use crate::lint;

pub type UseDummyRule =
<lint::nursery::use_dummy_rule::UseDummyRule as biome_analyze::Rule>::Options;
pub type UseDeprecatedReason =
<lint::nursery::use_deprecated_reason::UseDeprecatedReason as biome_analyze::Rule>::Options;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query {
member @deprecated {
id
}
}

query {
member @deprecated()
}

query {
member @deprecated(abc: 123)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
source: crates/biome_graphql_analyze/tests/spec_tests.rs
expression: invalid.graphql
---
# Input
```graphql
query {
member @deprecated {
id
}
}
query {
member @deprecated()
}
query {
member @deprecated(abc: 123)
}
```

# Diagnostics
```
invalid.graphql:2:10 lint/nursery/useDeprecatedReason ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The directive `@deprecated` should have a `reason` argument.
1 │ query {
> 2member @deprecated {
│ ^^^^^^^^^^^
3 │ id
4 │ }
i Add a `reason` argument to the directive.
```
```
invalid.graphql:8:10 lint/nursery/useDeprecatedReason ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The directive `@deprecated` should have a `reason` argument.
7query {
> 8 │ member @deprecated()
│ ^^^^^^^^^^^^^
9 │ }
10
i Add a `reason` argument to the directive.
```
```
invalid.graphql:12:10 lint/nursery/useDeprecatedReason ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! The directive `@deprecated` should have a `reason` argument.
11query {
> 12 │ member @deprecated(abc: 123)
│ ^^^^^^^^^^^^^^^^^^^^^
13 │ }
14
i Add a `reason` argument to the directive.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* should not generate diagnostics */
query {
member @deprecated(reason: "Use `members` instead") {
id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ expression: valid.graphql
---
# Input
```graphql
type User {
name String
/* should not generate diagnostics */
query {
member @deprecated(reason: "Use `members` instead") {
id
}
}
```

This file was deleted.

1 change: 1 addition & 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.

0 comments on commit 7935235

Please sign in to comment.