-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql_analyze): useDeprecatedReason (#3289)
- Loading branch information
1 parent
436d44b
commit 7935235
Showing
12 changed files
with
184 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
crates/biome_graphql_analyze/src/lint/nursery/use_deprecated_reason.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
crates/biome_graphql_analyze/src/lint/nursery/use_dummy_rule.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
crates/biome_graphql_analyze/tests/specs/nursery/useDeprecatedReason/invalid.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
70 changes: 70 additions & 0 deletions
70
crates/biome_graphql_analyze/tests/specs/nursery/useDeprecatedReason/invalid.graphql.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
> 2 │ member @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. | ||
7 │ query { | ||
> 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. | ||
11 │ query { | ||
> 12 │ member @deprecated(abc: 123) | ||
│ ^^^^^^^^^^^^^^^^^^^^^ | ||
13 │ } | ||
14 │ | ||
i Add a `reason` argument to the directive. | ||
``` |
6 changes: 6 additions & 0 deletions
6
crates/biome_graphql_analyze/tests/specs/nursery/useDeprecatedReason/valid.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
crates/biome_graphql_analyze/tests/specs/nursery/useDummyRule/valid.graphql
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.