|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use biome_analyze::{ |
| 4 | + Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, |
| 5 | +}; |
| 6 | +use biome_console::markup; |
| 7 | +use biome_graphql_syntax::{ |
| 8 | + GraphqlFieldsDefinition, GraphqlInputFieldsDefinition, GraphqlInputObjectTypeDefinition, |
| 9 | + GraphqlInterfaceTypeDefinition, GraphqlObjectTypeDefinition, |
| 10 | +}; |
| 11 | +use biome_rowan::{AstNode, TokenText, declare_node_union}; |
| 12 | +use biome_rule_options::use_unique_field_definition_names::UseUniqueFieldDefinitionNamesOptions; |
| 13 | + |
| 14 | +declare_lint_rule! { |
| 15 | + /// Require all fields of a type to be unique. |
| 16 | + /// |
| 17 | + /// A GraphQL complex type is only valid if all its fields are uniquely named. |
| 18 | + /// |
| 19 | + /// ## Examples |
| 20 | + /// |
| 21 | + /// ### Invalid |
| 22 | + /// |
| 23 | + /// ```graphql,expect_diagnostic |
| 24 | + /// type SomeObject { |
| 25 | + /// foo: String |
| 26 | + /// foo: String |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// ```graphql,expect_diagnostic |
| 31 | + /// interface SomeObject { |
| 32 | + /// foo: String |
| 33 | + /// foo: String |
| 34 | + /// } |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + /// ```graphql,expect_diagnostic |
| 38 | + /// input SomeObject { |
| 39 | + /// foo: String |
| 40 | + /// foo: String |
| 41 | + /// } |
| 42 | + /// ``` |
| 43 | + /// |
| 44 | + /// ### Valid |
| 45 | + /// |
| 46 | + /// ```graphql |
| 47 | + /// type SomeObject { |
| 48 | + /// foo: String |
| 49 | + /// bar: String |
| 50 | + /// } |
| 51 | + /// ``` |
| 52 | + /// |
| 53 | + /// ```graphql |
| 54 | + /// interface SomeObject { |
| 55 | + /// foo: String |
| 56 | + /// bar: String |
| 57 | + /// } |
| 58 | + /// ``` |
| 59 | + /// |
| 60 | + /// ```graphql |
| 61 | + /// input SomeObject { |
| 62 | + /// foo: String |
| 63 | + /// bar: String |
| 64 | + /// } |
| 65 | + /// ``` |
| 66 | + /// |
| 67 | + pub UseUniqueFieldDefinitionNames { |
| 68 | + version: "next", |
| 69 | + name: "useUniqueFieldDefinitionNames", |
| 70 | + language: "graphql", |
| 71 | + recommended: false, |
| 72 | + sources: &[RuleSource::EslintGraphql("unique-field-definition-names").same()], |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl Rule for UseUniqueFieldDefinitionNames { |
| 77 | + type Query = Ast<UseUniqueFieldDefinitionNamesQuery>; |
| 78 | + type State = (); |
| 79 | + type Signals = Option<Self::State>; |
| 80 | + type Options = UseUniqueFieldDefinitionNamesOptions; |
| 81 | + |
| 82 | + fn run(ctx: &RuleContext<Self>) -> Self::Signals { |
| 83 | + let node = ctx.query(); |
| 84 | + |
| 85 | + match node { |
| 86 | + UseUniqueFieldDefinitionNamesQuery::GraphqlObjectTypeDefinition(object_def) => { |
| 87 | + let fields = object_def.fields()?; |
| 88 | + check_list(fields) |
| 89 | + } |
| 90 | + UseUniqueFieldDefinitionNamesQuery::GraphqlInterfaceTypeDefinition(interface_def) => { |
| 91 | + let fields = interface_def.fields()?; |
| 92 | + check_list(fields) |
| 93 | + } |
| 94 | + UseUniqueFieldDefinitionNamesQuery::GraphqlInputObjectTypeDefinition(input_def) => { |
| 95 | + let fields = input_def.input_fields()?; |
| 96 | + check_input_list(fields) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { |
| 102 | + let span = ctx.query().range(); |
| 103 | + Some( |
| 104 | + RuleDiagnostic::new( |
| 105 | + rule_category!(), |
| 106 | + span, |
| 107 | + markup! { |
| 108 | + "Duplicate field name." |
| 109 | + }, |
| 110 | + ) |
| 111 | + .note(markup! { |
| 112 | + "A GraphQL complex type is only valid if all its fields are uniquely named. Make sure to name every field differently." |
| 113 | + }), |
| 114 | + ) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +fn check_list(fields: GraphqlFieldsDefinition) -> Option<()> { |
| 119 | + let mut found: HashSet<TokenText> = HashSet::new(); |
| 120 | + |
| 121 | + for element in fields.fields() { |
| 122 | + if let Some(name) = element.name().ok() |
| 123 | + && let Some(value_token) = name.value_token().ok() |
| 124 | + { |
| 125 | + let string = value_token.token_text(); |
| 126 | + if found.contains(&string) { |
| 127 | + return Some(()); |
| 128 | + } else { |
| 129 | + found.insert(string); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + None |
| 135 | +} |
| 136 | + |
| 137 | +fn check_input_list(fields: GraphqlInputFieldsDefinition) -> Option<()> { |
| 138 | + let mut found: HashSet<TokenText> = HashSet::new(); |
| 139 | + |
| 140 | + for element in fields.fields() { |
| 141 | + if let Some(name) = element.name().ok() |
| 142 | + && let Some(value_token) = name.value_token().ok() |
| 143 | + { |
| 144 | + let string = value_token.token_text(); |
| 145 | + if found.contains(&string) { |
| 146 | + return Some(()); |
| 147 | + } else { |
| 148 | + found.insert(string); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + None |
| 154 | +} |
| 155 | + |
| 156 | +declare_node_union! { |
| 157 | + pub UseUniqueFieldDefinitionNamesQuery = GraphqlObjectTypeDefinition | GraphqlInterfaceTypeDefinition | GraphqlInputObjectTypeDefinition |
| 158 | +} |
0 commit comments