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
22 changes: 22 additions & 0 deletions .changeset/cool-plums-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@biomejs/biome": patch
---

Added the nursery rule [`noRootType`](https://biomejs.dev/linter/rules/no-root-type).
Disallow the usage of specified root types. (e.g. `mutation` and/or `subscription`)

**Invalid:**

```json
{
"options": {
"disallow": ["mutation"]
}
}
```

```graphql
type Mutation {
SetMessage(message: String): String
}
```
4 changes: 4 additions & 0 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.

5 changes: 3 additions & 2 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 @@ -4,6 +4,7 @@

use biome_analyze::declare_lint_group;
pub mod no_empty_source;
pub mod no_root_type;
pub mod use_consistent_graphql_descriptions;
pub mod use_deprecated_date;
pub mod use_lone_executable_definition;
Expand All @@ -13,4 +14,4 @@ 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_lone_executable_definition :: UseLoneExecutableDefinition , self :: use_unique_argument_names :: UseUniqueArgumentNames , self :: use_unique_enum_value_names :: UseUniqueEnumValueNames , 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 :: no_root_type :: NoRootType , 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_enum_value_names :: UseUniqueEnumValueNames , 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 ,] } }
136 changes: 136 additions & 0 deletions crates/biome_graphql_analyze/src/lint/nursery/no_root_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use biome_analyze::{
Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule,
};
use biome_console::markup;
use biome_graphql_syntax::{
GraphqlLanguage, GraphqlObjectTypeDefinition, GraphqlObjectTypeExtension,
};
use biome_rowan::{SyntaxToken, TextRange, TokenText, declare_node_union};
use biome_rule_options::no_root_type::NoRootTypeOptions;
use biome_string_case::StrOnlyExtension;

declare_lint_rule! {
/// Disallow the usage of specified root types
///
/// Prevent the usage of certain root types (e.g. `mutation` and/or `subscription`)
///
/// ## Examples
///
/// ### Invalid
///
/// ```json,options
/// {
/// "options": {
/// "disallow": ["mutation"]
/// }
/// }
/// ```
///
/// ```graphql,expect_diagnostic,use_options
/// type Mutation {
/// createUser(input: CreateUserInput!): User!
/// }
/// ```
///
/// ### Valid
///
/// ```graphql,use_options
/// type Query {
/// users: [User!]!
/// }
/// ```
///
/// ## Options
///
/// ### `disallow`
///
/// This required option lists all disallowed root types (e.g. `mutation` and/or `subscription`).
/// The values of the list are case-insensitive.
///
/// Default `[]`
///
/// ```json,options
/// {
/// "options": {
/// "disallow": ["subscription"]
/// }
/// }
/// ```
///
/// ```graphql,expect_diagnostic,use_options
/// type Subscription {
/// user: User
/// }
/// ```
///
pub NoRootType {
version: "next",
name: "noRootType",
language: "graphql",
recommended: false,
sources: &[RuleSource::EslintGraphql("no-root-type").same()],
}
}

impl Rule for NoRootType {
type Query = Ast<NoRootTypeQuery>;
type State = (TokenText, TextRange);
type Signals = Option<Self::State>;
type Options = NoRootTypeOptions;

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let root_types = &ctx.options().disallow;

if root_types.is_empty() {
return None;
}

match node {
NoRootTypeQuery::GraphqlObjectTypeDefinition(type_def) => {
let name = type_def.name().ok()?;
let value_token = name.value_token().ok()?;
check_name(root_types, value_token)
}
NoRootTypeQuery::GraphqlObjectTypeExtension(type_ext) => {
let name = type_ext.name().ok()?;
let value_token = name.value_token().ok()?;
check_name(root_types, value_token)
}
}
}

fn diagnostic(_ctx: &RuleContext<Self>, (name, range): &Self::State) -> Option<RuleDiagnostic> {
Some(
RuleDiagnostic::new(
rule_category!(),
range,
markup! {
"The root type "{{name.to_string()}}" is forbidden."
},
)
.note(markup! {
"It's forbidden to use this root type within this project. Rework to use a different root type."
}),
)
}
}

declare_node_union! {
pub NoRootTypeQuery = GraphqlObjectTypeDefinition | GraphqlObjectTypeExtension
}

fn check_name(
root_types: &Vec<String>,
name: SyntaxToken<GraphqlLanguage>,
) -> Option<(TokenText, TextRange)> {
let trimmed = name.token_text_trimmed();

for root_type in root_types {
if root_type.to_lowercase_cow() == trimmed.to_lowercase_cow() {
return Some((trimmed, name.text_range()));
}
}

None
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# should generate diagnostics
type Mutation {
createUser(input: CreateUserInput!): User!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/biome_graphql_analyze/tests/spec_tests.rs
expression: invalid.graphql
---
# Input
```graphql
# should generate diagnostics
type Mutation {
createUser(input: CreateUserInput!): User!
}

```

# Diagnostics
```
invalid.graphql:2:6 lint/nursery/noRootType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i The root type Mutation is forbidden.

1 │ # should generate diagnostics
> 2 │ type Mutation {
│ ^^^^^^^^^
3 │ createUser(input: CreateUserInput!): User!
4 │ }

i It's forbidden to use this root type within this project. Rework to use a different root type.

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,17 @@
{
"$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json",
"linter": {
"rules": {
"nursery": {
"noRootType": {
"level": "error",
"options": {
"disallow": [
"mutation"
]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# should not generate diagnostics
type Query {
users: [User!]!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/biome_graphql_analyze/tests/spec_tests.rs
expression: valid.graphql
---
# Input
```graphql
# should not generate diagnostics
type Query {
users: [User!]!
}

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json",
"linter": {
"rules": {
"nursery": {
"noRootType": {
"level": "error",
"options": {
"disallow": []
}
}
}
}
}
}
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 @@ -176,6 +176,7 @@ pub mod no_restricted_globals;
pub mod no_restricted_imports;
pub mod no_restricted_types;
pub mod no_return_assign;
pub mod no_root_type;
pub mod no_script_url;
pub mod no_secrets;
pub mod no_self_assign;
Expand Down
11 changes: 11 additions & 0 deletions crates/biome_rule_options/src/no_root_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 NoRootTypeOptions {
/// A list of disallowed root types (e.g. "mutation" and/or "subscription").
/// The values of the list are case-insensitive.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub disallow: Vec<String>,
}
Loading