@graphql-eslint/eslint-plugin 3.14.0-alpha-20221221142641-4e1a924 → 3.14.0-alpha-20221222124206-b82954b

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. package/README.md +309 -0
  2. package/cjs/cache.js +30 -0
  3. package/cjs/configs/base.js +7 -0
  4. package/cjs/configs/index.js +16 -0
  5. package/cjs/configs/operations-all.js +31 -0
  6. package/cjs/configs/operations-recommended.js +56 -0
  7. package/cjs/configs/relay.js +12 -0
  8. package/cjs/configs/schema-all.js +23 -0
  9. package/cjs/configs/schema-recommended.js +52 -0
  10. package/cjs/documents.js +149 -0
  11. package/cjs/estree-converter/converter.js +62 -0
  12. package/cjs/estree-converter/index.js +6 -0
  13. package/cjs/estree-converter/types.js +2 -0
  14. package/cjs/estree-converter/utils.js +109 -0
  15. package/cjs/graphql-config.js +55 -0
  16. package/cjs/index.js +17 -0
  17. package/cjs/parser.js +61 -0
  18. package/cjs/processor.js +78 -0
  19. package/cjs/rules/alphabetize.js +348 -0
  20. package/cjs/rules/description-style.js +78 -0
  21. package/cjs/rules/graphql-js-validation.js +499 -0
  22. package/cjs/rules/index.js +68 -0
  23. package/cjs/rules/input-name.js +136 -0
  24. package/cjs/rules/lone-executable-definition.js +88 -0
  25. package/cjs/rules/match-document-filename.js +235 -0
  26. package/cjs/rules/naming-convention.js +310 -0
  27. package/cjs/rules/no-anonymous-operations.js +67 -0
  28. package/cjs/rules/no-case-insensitive-enum-values-duplicates.js +61 -0
  29. package/cjs/rules/no-deprecated.js +124 -0
  30. package/cjs/rules/no-duplicate-fields.js +112 -0
  31. package/cjs/rules/no-hashtag-description.js +89 -0
  32. package/cjs/rules/no-root-type.js +86 -0
  33. package/cjs/rules/no-scalar-result-type-on-mutation.js +66 -0
  34. package/cjs/rules/no-typename-prefix.js +65 -0
  35. package/cjs/rules/no-unreachable-types.js +158 -0
  36. package/cjs/rules/no-unused-fields.js +130 -0
  37. package/cjs/rules/relay-arguments.js +121 -0
  38. package/cjs/rules/relay-connection-types.js +107 -0
  39. package/cjs/rules/relay-edge-types.js +189 -0
  40. package/cjs/rules/relay-page-info.js +100 -0
  41. package/cjs/rules/require-deprecation-date.js +123 -0
  42. package/cjs/rules/require-deprecation-reason.js +56 -0
  43. package/cjs/rules/require-description.js +193 -0
  44. package/cjs/rules/require-field-of-type-query-in-mutation-result.js +72 -0
  45. package/cjs/rules/require-id-when-available.js +199 -0
  46. package/cjs/rules/selection-set-depth.js +135 -0
  47. package/cjs/rules/strict-id-in-types.js +162 -0
  48. package/cjs/rules/unique-fragment-name.js +90 -0
  49. package/cjs/rules/unique-operation-name.js +65 -0
  50. package/cjs/schema.js +42 -0
  51. package/cjs/testkit.js +183 -0
  52. package/cjs/types.js +2 -0
  53. package/cjs/utils.js +96 -0
  54. package/docs/README.md +82 -0
  55. package/docs/custom-rules.md +184 -0
  56. package/docs/deprecated-rules.md +24 -0
  57. package/docs/parser-options.md +95 -0
  58. package/docs/parser.md +67 -0
  59. package/docs/rules/alphabetize.md +194 -0
  60. package/docs/rules/description-style.md +57 -0
  61. package/docs/rules/executable-definitions.md +20 -0
  62. package/docs/rules/fields-on-correct-type.md +23 -0
  63. package/docs/rules/fragments-on-composite-type.md +20 -0
  64. package/docs/rules/input-name.md +80 -0
  65. package/docs/rules/known-argument-names.md +23 -0
  66. package/docs/rules/known-directives.md +48 -0
  67. package/docs/rules/known-fragment-names.md +72 -0
  68. package/docs/rules/known-type-names.md +24 -0
  69. package/docs/rules/lone-anonymous-operation.md +20 -0
  70. package/docs/rules/lone-executable-definition.md +59 -0
  71. package/docs/rules/lone-schema-definition.md +19 -0
  72. package/docs/rules/match-document-filename.md +181 -0
  73. package/docs/rules/naming-convention.md +320 -0
  74. package/docs/rules/no-anonymous-operations.md +43 -0
  75. package/docs/rules/no-case-insensitive-enum-values-duplicates.md +46 -0
  76. package/docs/rules/no-deprecated.md +88 -0
  77. package/docs/rules/no-duplicate-fields.md +69 -0
  78. package/docs/rules/no-fragment-cycles.md +19 -0
  79. package/docs/rules/no-hashtag-description.md +62 -0
  80. package/docs/rules/no-root-type.md +55 -0
  81. package/docs/rules/no-scalar-result-type-on-mutation.md +39 -0
  82. package/docs/rules/no-typename-prefix.md +42 -0
  83. package/docs/rules/no-undefined-variables.md +20 -0
  84. package/docs/rules/no-unreachable-types.md +52 -0
  85. package/docs/rules/no-unused-fields.md +64 -0
  86. package/docs/rules/no-unused-fragments.md +20 -0
  87. package/docs/rules/no-unused-variables.md +20 -0
  88. package/docs/rules/one-field-subscriptions.md +19 -0
  89. package/docs/rules/overlapping-fields-can-be-merged.md +20 -0
  90. package/docs/rules/possible-fragment-spread.md +21 -0
  91. package/docs/rules/possible-type-extension.md +19 -0
  92. package/docs/rules/provided-required-arguments.md +21 -0
  93. package/docs/rules/relay-arguments.md +59 -0
  94. package/docs/rules/relay-connection-types.md +43 -0
  95. package/docs/rules/relay-edge-types.md +60 -0
  96. package/docs/rules/relay-page-info.md +34 -0
  97. package/docs/rules/require-deprecation-date.md +59 -0
  98. package/docs/rules/require-deprecation-reason.md +49 -0
  99. package/docs/rules/require-description.md +147 -0
  100. package/docs/rules/require-field-of-type-query-in-mutation-result.md +50 -0
  101. package/docs/rules/require-id-when-available.md +91 -0
  102. package/docs/rules/scalar-leafs.md +23 -0
  103. package/docs/rules/selection-set-depth.md +86 -0
  104. package/docs/rules/strict-id-in-types.md +129 -0
  105. package/docs/rules/unique-argument-names.md +19 -0
  106. package/docs/rules/unique-directive-names-per-location.md +21 -0
  107. package/docs/rules/unique-directive-names.md +19 -0
  108. package/docs/rules/unique-enum-value-names.md +16 -0
  109. package/docs/rules/unique-field-definition-names.md +19 -0
  110. package/docs/rules/unique-fragment-name.md +52 -0
  111. package/docs/rules/unique-input-field-names.md +19 -0
  112. package/docs/rules/unique-operation-name.md +56 -0
  113. package/docs/rules/unique-operation-types.md +19 -0
  114. package/docs/rules/unique-type-names.md +19 -0
  115. package/docs/rules/unique-variable-names.md +19 -0
  116. package/docs/rules/value-literals-of-correct-type.md +22 -0
  117. package/docs/rules/variables-are-input-types.md +20 -0
  118. package/docs/rules/variables-in-allowed-position.md +19 -0
  119. package/package.json +8 -11
  120. package/{cache.d.ts → typings/cache.d.cts} +0 -0
  121. package/typings/cache.d.ts +10 -0
  122. package/typings/configs/base.d.cts +5 -0
  123. package/typings/configs/base.d.ts +5 -0
  124. package/typings/configs/index.d.cts +139 -0
  125. package/typings/configs/index.d.ts +139 -0
  126. package/typings/configs/operations-all.d.cts +20 -0
  127. package/typings/configs/operations-all.d.ts +20 -0
  128. package/typings/configs/operations-recommended.d.cts +50 -0
  129. package/typings/configs/operations-recommended.d.ts +50 -0
  130. package/typings/configs/relay.d.cts +10 -0
  131. package/typings/configs/relay.d.ts +10 -0
  132. package/typings/configs/schema-all.d.cts +15 -0
  133. package/typings/configs/schema-all.d.ts +15 -0
  134. package/typings/configs/schema-recommended.d.cts +47 -0
  135. package/typings/configs/schema-recommended.d.ts +47 -0
  136. package/{documents.d.ts → typings/documents.d.cts} +0 -0
  137. package/typings/documents.d.ts +21 -0
  138. package/{estree-converter/converter.d.ts → typings/estree-converter/converter.d.cts} +0 -0
  139. package/typings/estree-converter/converter.d.ts +3 -0
  140. package/{estree-converter/index.d.ts → typings/estree-converter/index.d.cts} +0 -0
  141. package/typings/estree-converter/index.d.ts +3 -0
  142. package/{estree-converter/types.d.ts → typings/estree-converter/types.d.cts} +0 -0
  143. package/typings/estree-converter/types.d.ts +40 -0
  144. package/{estree-converter/utils.d.ts → typings/estree-converter/utils.d.cts} +0 -0
  145. package/typings/estree-converter/utils.d.ts +13 -0
  146. package/{graphql-config.d.ts → typings/graphql-config.d.cts} +0 -0
  147. package/typings/graphql-config.d.ts +4 -0
  148. package/typings/index.d.cts +9 -0
  149. package/{index.d.ts → typings/index.d.ts} +1 -5
  150. package/{parser.d.ts → typings/parser.d.cts} +0 -0
  151. package/typings/parser.d.ts +2 -0
  152. package/{processor.d.ts → typings/processor.d.cts} +0 -0
  153. package/typings/processor.d.ts +6 -0
  154. package/{rules/alphabetize.d.ts → typings/rules/alphabetize.d.cts} +0 -0
  155. package/typings/rules/alphabetize.d.ts +76 -0
  156. package/{rules/description-style.d.ts → typings/rules/description-style.d.cts} +0 -0
  157. package/typings/rules/description-style.d.ts +20 -0
  158. package/{rules/graphql-js-validation.d.ts → typings/rules/graphql-js-validation.d.cts} +0 -0
  159. package/typings/rules/graphql-js-validation.d.ts +2 -0
  160. package/{rules/index.d.ts → typings/rules/index.d.cts} +0 -0
  161. package/typings/rules/index.d.ts +104 -0
  162. package/{rules/input-name.d.ts → typings/rules/input-name.d.cts} +0 -0
  163. package/typings/rules/input-name.d.ts +35 -0
  164. package/{rules/lone-executable-definition.d.ts → typings/rules/lone-executable-definition.d.cts} +0 -0
  165. package/typings/rules/lone-executable-definition.d.ts +26 -0
  166. package/{rules/match-document-filename.d.ts → typings/rules/match-document-filename.d.cts} +0 -0
  167. package/typings/rules/match-document-filename.d.ts +72 -0
  168. package/{rules/naming-convention.d.ts → typings/rules/naming-convention.d.cts} +0 -0
  169. package/typings/rules/naming-convention.d.ts +83 -0
  170. package/{rules/no-anonymous-operations.d.ts → typings/rules/no-anonymous-operations.d.cts} +0 -0
  171. package/{rules/no-case-insensitive-enum-values-duplicates.d.ts → typings/rules/no-anonymous-operations.d.ts} +0 -0
  172. package/{rules/no-hashtag-description.d.ts → typings/rules/no-case-insensitive-enum-values-duplicates.d.cts} +0 -0
  173. package/{rules/no-scalar-result-type-on-mutation.d.ts → typings/rules/no-case-insensitive-enum-values-duplicates.d.ts} +0 -0
  174. package/{rules/no-deprecated.d.ts → typings/rules/no-deprecated.d.cts} +0 -0
  175. package/typings/rules/no-deprecated.d.ts +2 -0
  176. package/{rules/no-duplicate-fields.d.ts → typings/rules/no-duplicate-fields.d.cts} +0 -0
  177. package/{rules/relay-page-info.d.ts → typings/rules/no-duplicate-fields.d.ts} +0 -0
  178. package/{rules/no-typename-prefix.d.ts → typings/rules/no-hashtag-description.d.cts} +0 -0
  179. package/{rules/no-unreachable-types.d.ts → typings/rules/no-hashtag-description.d.ts} +0 -0
  180. package/{rules/no-root-type.d.ts → typings/rules/no-root-type.d.cts} +0 -0
  181. package/typings/rules/no-root-type.d.ts +25 -0
  182. package/{rules/no-unused-fields.d.ts → typings/rules/no-scalar-result-type-on-mutation.d.cts} +0 -0
  183. package/{rules/unique-operation-name.d.ts → typings/rules/no-scalar-result-type-on-mutation.d.ts} +0 -0
  184. package/typings/rules/no-typename-prefix.d.cts +2 -0
  185. package/typings/rules/no-typename-prefix.d.ts +2 -0
  186. package/typings/rules/no-unreachable-types.d.cts +2 -0
  187. package/typings/rules/no-unreachable-types.d.ts +2 -0
  188. package/typings/rules/no-unused-fields.d.cts +2 -0
  189. package/typings/rules/no-unused-fields.d.ts +2 -0
  190. package/{rules/relay-arguments.d.ts → typings/rules/relay-arguments.d.cts} +0 -0
  191. package/typings/rules/relay-arguments.d.ts +21 -0
  192. package/{rules/relay-connection-types.d.ts → typings/rules/relay-connection-types.d.cts} +0 -0
  193. package/typings/rules/relay-connection-types.d.ts +4 -0
  194. package/{rules/relay-edge-types.d.ts → typings/rules/relay-edge-types.d.cts} +0 -0
  195. package/typings/rules/relay-edge-types.d.ts +31 -0
  196. package/{rules/require-deprecation-reason.d.ts → typings/rules/relay-page-info.d.cts} +0 -0
  197. package/{rules/require-field-of-type-query-in-mutation-result.d.ts → typings/rules/relay-page-info.d.ts} +0 -0
  198. package/{rules/require-deprecation-date.d.ts → typings/rules/require-deprecation-date.d.cts} +0 -0
  199. package/typings/rules/require-deprecation-date.d.ts +18 -0
  200. package/typings/rules/require-deprecation-reason.d.cts +2 -0
  201. package/typings/rules/require-deprecation-reason.d.ts +2 -0
  202. package/{rules/require-description.d.ts → typings/rules/require-description.d.cts} +0 -0
  203. package/typings/rules/require-description.d.ts +14 -0
  204. package/typings/rules/require-field-of-type-query-in-mutation-result.d.cts +2 -0
  205. package/typings/rules/require-field-of-type-query-in-mutation-result.d.ts +2 -0
  206. package/{rules/require-id-when-available.d.ts → typings/rules/require-id-when-available.d.cts} +0 -0
  207. package/typings/rules/require-id-when-available.d.ts +36 -0
  208. package/{rules/selection-set-depth.d.ts → typings/rules/selection-set-depth.d.cts} +0 -0
  209. package/typings/rules/selection-set-depth.d.ts +28 -0
  210. package/{rules/strict-id-in-types.d.ts → typings/rules/strict-id-in-types.d.cts} +0 -0
  211. package/typings/rules/strict-id-in-types.d.ts +57 -0
  212. package/{rules/unique-fragment-name.d.ts → typings/rules/unique-fragment-name.d.cts} +0 -0
  213. package/typings/rules/unique-fragment-name.d.ts +5 -0
  214. package/typings/rules/unique-operation-name.d.cts +2 -0
  215. package/typings/rules/unique-operation-name.d.ts +2 -0
  216. package/{schema.d.ts → typings/schema.d.cts} +0 -0
  217. package/typings/schema.d.ts +3 -0
  218. package/{testkit.d.ts → typings/testkit.d.cts} +0 -0
  219. package/typings/testkit.d.ts +27 -0
  220. package/{types.d.ts → typings/types.d.cts} +0 -0
  221. package/typings/types.d.ts +81 -0
  222. package/{utils.d.ts → typings/utils.d.cts} +0 -0
  223. package/typings/utils.d.ts +34 -0
  224. package/configs/base.json +0 -4
  225. package/configs/operations-all.json +0 -25
  226. package/configs/operations-recommended.json +0 -50
  227. package/configs/relay.json +0 -9
  228. package/configs/schema-all.json +0 -17
  229. package/configs/schema-recommended.json +0 -49
  230. package/index.js +0 -4995
  231. package/index.mjs +0 -4983
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const utils_1 = require("../utils");
6
+ const KindToDisplayName = {
7
+ // types
8
+ [graphql_1.Kind.OBJECT_TYPE_DEFINITION]: 'Type',
9
+ [graphql_1.Kind.INTERFACE_TYPE_DEFINITION]: 'Interface',
10
+ [graphql_1.Kind.ENUM_TYPE_DEFINITION]: 'Enumerator',
11
+ [graphql_1.Kind.SCALAR_TYPE_DEFINITION]: 'Scalar',
12
+ [graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION]: 'Input type',
13
+ [graphql_1.Kind.UNION_TYPE_DEFINITION]: 'Union',
14
+ // fields
15
+ [graphql_1.Kind.FIELD_DEFINITION]: 'Field',
16
+ [graphql_1.Kind.INPUT_VALUE_DEFINITION]: 'Input property',
17
+ [graphql_1.Kind.ARGUMENT]: 'Argument',
18
+ [graphql_1.Kind.DIRECTIVE_DEFINITION]: 'Directive',
19
+ // rest
20
+ [graphql_1.Kind.ENUM_VALUE_DEFINITION]: 'Enumeration value',
21
+ [graphql_1.Kind.OPERATION_DEFINITION]: 'Operation',
22
+ [graphql_1.Kind.FRAGMENT_DEFINITION]: 'Fragment',
23
+ [graphql_1.Kind.VARIABLE_DEFINITION]: 'Variable',
24
+ };
25
+ const StyleToRegex = {
26
+ camelCase: /^[a-z][\dA-Za-z]*$/,
27
+ PascalCase: /^[A-Z][\dA-Za-z]*$/,
28
+ snake_case: /^[a-z][\d_a-z]*[\da-z]*$/,
29
+ UPPER_CASE: /^[A-Z][\dA-Z_]*[\dA-Z]*$/,
30
+ };
31
+ const ALLOWED_KINDS = Object.keys(KindToDisplayName).sort();
32
+ const ALLOWED_STYLES = Object.keys(StyleToRegex);
33
+ const schemaOption = {
34
+ oneOf: [{ $ref: '#/definitions/asString' }, { $ref: '#/definitions/asObject' }],
35
+ };
36
+ const schema = {
37
+ definitions: {
38
+ asString: {
39
+ enum: ALLOWED_STYLES,
40
+ description: `One of: ${ALLOWED_STYLES.map(t => `\`${t}\``).join(', ')}`,
41
+ },
42
+ asObject: {
43
+ type: 'object',
44
+ additionalProperties: false,
45
+ properties: {
46
+ style: { enum: ALLOWED_STYLES },
47
+ prefix: { type: 'string' },
48
+ suffix: { type: 'string' },
49
+ forbiddenPrefixes: utils_1.ARRAY_DEFAULT_OPTIONS,
50
+ forbiddenSuffixes: utils_1.ARRAY_DEFAULT_OPTIONS,
51
+ ignorePattern: {
52
+ type: 'string',
53
+ description: 'Option to skip validation of some words, e.g. acronyms',
54
+ },
55
+ },
56
+ },
57
+ },
58
+ type: 'array',
59
+ maxItems: 1,
60
+ items: {
61
+ type: 'object',
62
+ additionalProperties: false,
63
+ properties: {
64
+ types: {
65
+ ...schemaOption,
66
+ description: `Includes:\n${utils_1.TYPES_KINDS.map(kind => `- \`${kind}\``).join('\n')}`,
67
+ },
68
+ ...Object.fromEntries(ALLOWED_KINDS.map(kind => [
69
+ kind,
70
+ {
71
+ ...schemaOption,
72
+ description: `Read more about this kind on [spec.graphql.org](https://spec.graphql.org/October2021/#${kind}).`,
73
+ },
74
+ ])),
75
+ allowLeadingUnderscore: {
76
+ type: 'boolean',
77
+ default: false,
78
+ },
79
+ allowTrailingUnderscore: {
80
+ type: 'boolean',
81
+ default: false,
82
+ },
83
+ },
84
+ patternProperties: {
85
+ [`^(${ALLOWED_KINDS.join('|')})(.+)?$`]: schemaOption,
86
+ },
87
+ description: [
88
+ "> It's possible to use a [`selector`](https://eslint.org/docs/developer-guide/selectors) that starts with allowed `ASTNode` names which are described below.",
89
+ '>',
90
+ '> Paste or drop code into the editor in [ASTExplorer](https://astexplorer.net) and inspect the generated AST to compose your selector.',
91
+ '>',
92
+ '> Example: pattern property `FieldDefinition[parent.name.value=Query]` will match only fields for type `Query`.',
93
+ ].join('\n'),
94
+ },
95
+ };
96
+ exports.rule = {
97
+ meta: {
98
+ type: 'suggestion',
99
+ docs: {
100
+ description: 'Require names to follow specified conventions.',
101
+ category: ['Schema', 'Operations'],
102
+ recommended: true,
103
+ url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/naming-convention.md',
104
+ examples: [
105
+ {
106
+ title: 'Incorrect',
107
+ usage: [{ types: 'PascalCase', FieldDefinition: 'camelCase' }],
108
+ code: /* GraphQL */ `
109
+ type user {
110
+ first_name: String!
111
+ }
112
+ `,
113
+ },
114
+ {
115
+ title: 'Incorrect',
116
+ usage: [{ FragmentDefinition: { style: 'PascalCase', forbiddenSuffixes: ['Fragment'] } }],
117
+ code: /* GraphQL */ `
118
+ fragment UserFragment on User {
119
+ # ...
120
+ }
121
+ `,
122
+ },
123
+ {
124
+ title: 'Incorrect',
125
+ usage: [{ 'FieldDefinition[parent.name.value=Query]': { forbiddenPrefixes: ['get'] } }],
126
+ code: /* GraphQL */ `
127
+ type Query {
128
+ getUsers: [User!]!
129
+ }
130
+ `,
131
+ },
132
+ {
133
+ title: 'Correct',
134
+ usage: [{ types: 'PascalCase', FieldDefinition: 'camelCase' }],
135
+ code: /* GraphQL */ `
136
+ type User {
137
+ firstName: String
138
+ }
139
+ `,
140
+ },
141
+ {
142
+ title: 'Correct',
143
+ usage: [{ FragmentDefinition: { style: 'PascalCase', forbiddenSuffixes: ['Fragment'] } }],
144
+ code: /* GraphQL */ `
145
+ fragment UserFields on User {
146
+ # ...
147
+ }
148
+ `,
149
+ },
150
+ {
151
+ title: 'Correct',
152
+ usage: [{ 'FieldDefinition[parent.name.value=Query]': { forbiddenPrefixes: ['get'] } }],
153
+ code: /* GraphQL */ `
154
+ type Query {
155
+ users: [User!]!
156
+ }
157
+ `,
158
+ },
159
+ {
160
+ title: 'Correct',
161
+ usage: [{ FieldDefinition: { style: 'camelCase', ignorePattern: '^(EAN13|UPC|UK)' } }],
162
+ code: /* GraphQL */ `
163
+ type Product {
164
+ EAN13: String
165
+ UPC: String
166
+ UKFlag: String
167
+ }
168
+ `,
169
+ },
170
+ ],
171
+ configOptions: {
172
+ schema: [
173
+ {
174
+ types: 'PascalCase',
175
+ FieldDefinition: 'camelCase',
176
+ InputValueDefinition: 'camelCase',
177
+ Argument: 'camelCase',
178
+ DirectiveDefinition: 'camelCase',
179
+ EnumValueDefinition: 'UPPER_CASE',
180
+ 'FieldDefinition[parent.name.value=Query]': {
181
+ forbiddenPrefixes: ['query', 'get'],
182
+ forbiddenSuffixes: ['Query'],
183
+ },
184
+ 'FieldDefinition[parent.name.value=Mutation]': {
185
+ forbiddenPrefixes: ['mutation'],
186
+ forbiddenSuffixes: ['Mutation'],
187
+ },
188
+ 'FieldDefinition[parent.name.value=Subscription]': {
189
+ forbiddenPrefixes: ['subscription'],
190
+ forbiddenSuffixes: ['Subscription'],
191
+ },
192
+ },
193
+ ],
194
+ operations: [
195
+ {
196
+ VariableDefinition: 'camelCase',
197
+ OperationDefinition: {
198
+ style: 'PascalCase',
199
+ forbiddenPrefixes: ['Query', 'Mutation', 'Subscription', 'Get'],
200
+ forbiddenSuffixes: ['Query', 'Mutation', 'Subscription'],
201
+ },
202
+ FragmentDefinition: {
203
+ style: 'PascalCase',
204
+ forbiddenPrefixes: ['Fragment'],
205
+ forbiddenSuffixes: ['Fragment'],
206
+ },
207
+ },
208
+ ],
209
+ },
210
+ },
211
+ hasSuggestions: true,
212
+ schema,
213
+ },
214
+ create(context) {
215
+ const options = context.options[0] || {};
216
+ const { allowLeadingUnderscore, allowTrailingUnderscore, types, ...restOptions } = options;
217
+ function normalisePropertyOption(kind) {
218
+ const style = restOptions[kind] || types;
219
+ return typeof style === 'object' ? style : { style };
220
+ }
221
+ function report(node, message, suggestedName) {
222
+ context.report({
223
+ node,
224
+ message,
225
+ suggest: [
226
+ {
227
+ desc: `Rename to \`${suggestedName}\``,
228
+ fix: fixer => fixer.replaceText(node, suggestedName),
229
+ },
230
+ ],
231
+ });
232
+ }
233
+ const checkNode = (selector) => (n) => {
234
+ const { name: node } = n.kind === graphql_1.Kind.VARIABLE_DEFINITION ? n.variable : n;
235
+ if (!node) {
236
+ return;
237
+ }
238
+ const { prefix, suffix, forbiddenPrefixes, forbiddenSuffixes, style, ignorePattern } = normalisePropertyOption(selector);
239
+ const nodeType = KindToDisplayName[n.kind] || n.kind;
240
+ const nodeName = node.value;
241
+ const error = getError();
242
+ if (error) {
243
+ const { errorMessage, renameToName } = error;
244
+ const [leadingUnderscores] = nodeName.match(/^_*/);
245
+ const [trailingUnderscores] = nodeName.match(/_*$/);
246
+ const suggestedName = leadingUnderscores + renameToName + trailingUnderscores;
247
+ report(node, `${nodeType} "${nodeName}" should ${errorMessage}`, suggestedName);
248
+ }
249
+ function getError() {
250
+ const name = nodeName.replace(/(^_+)|(_+$)/g, '');
251
+ if (ignorePattern && new RegExp(ignorePattern, 'u').test(name)) {
252
+ return;
253
+ }
254
+ if (prefix && !name.startsWith(prefix)) {
255
+ return {
256
+ errorMessage: `have "${prefix}" prefix`,
257
+ renameToName: prefix + name,
258
+ };
259
+ }
260
+ if (suffix && !name.endsWith(suffix)) {
261
+ return {
262
+ errorMessage: `have "${suffix}" suffix`,
263
+ renameToName: name + suffix,
264
+ };
265
+ }
266
+ const forbiddenPrefix = forbiddenPrefixes === null || forbiddenPrefixes === void 0 ? void 0 : forbiddenPrefixes.find(prefix => name.startsWith(prefix));
267
+ if (forbiddenPrefix) {
268
+ return {
269
+ errorMessage: `not have "${forbiddenPrefix}" prefix`,
270
+ renameToName: name.replace(new RegExp(`^${forbiddenPrefix}`), ''),
271
+ };
272
+ }
273
+ const forbiddenSuffix = forbiddenSuffixes === null || forbiddenSuffixes === void 0 ? void 0 : forbiddenSuffixes.find(suffix => name.endsWith(suffix));
274
+ if (forbiddenSuffix) {
275
+ return {
276
+ errorMessage: `not have "${forbiddenSuffix}" suffix`,
277
+ renameToName: name.replace(new RegExp(`${forbiddenSuffix}$`), ''),
278
+ };
279
+ }
280
+ // Style is optional
281
+ if (!style) {
282
+ return;
283
+ }
284
+ const caseRegex = StyleToRegex[style];
285
+ if (!caseRegex.test(name)) {
286
+ return {
287
+ errorMessage: `be in ${style} format`,
288
+ renameToName: (0, utils_1.convertCase)(style, name),
289
+ };
290
+ }
291
+ }
292
+ };
293
+ const checkUnderscore = (isLeading) => (node) => {
294
+ const suggestedName = node.value.replace(isLeading ? /^_+/ : /_+$/, '');
295
+ report(node, `${isLeading ? 'Leading' : 'Trailing'} underscores are not allowed`, suggestedName);
296
+ };
297
+ const listeners = {};
298
+ if (!allowLeadingUnderscore) {
299
+ listeners['Name[value=/^_/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'] = checkUnderscore(true);
300
+ }
301
+ if (!allowTrailingUnderscore) {
302
+ listeners['Name[value=/_$/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'] = checkUnderscore(false);
303
+ }
304
+ const selectors = new Set([types && utils_1.TYPES_KINDS, Object.keys(restOptions)].flat().filter(Boolean));
305
+ for (const selector of selectors) {
306
+ listeners[selector] = checkNode(selector);
307
+ }
308
+ return listeners;
309
+ },
310
+ };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const utils_1 = require("../utils");
6
+ const RULE_ID = 'no-anonymous-operations';
7
+ exports.rule = {
8
+ meta: {
9
+ type: 'suggestion',
10
+ hasSuggestions: true,
11
+ docs: {
12
+ category: 'Operations',
13
+ description: 'Require name for your GraphQL operations. This is useful since most GraphQL client libraries are using the operation name for caching purposes.',
14
+ recommended: true,
15
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
16
+ examples: [
17
+ {
18
+ title: 'Incorrect',
19
+ code: /* GraphQL */ `
20
+ query {
21
+ # ...
22
+ }
23
+ `,
24
+ },
25
+ {
26
+ title: 'Correct',
27
+ code: /* GraphQL */ `
28
+ query user {
29
+ # ...
30
+ }
31
+ `,
32
+ },
33
+ ],
34
+ },
35
+ messages: {
36
+ [RULE_ID]: 'Anonymous GraphQL operations are forbidden. Make sure to name your {{ operation }}!',
37
+ },
38
+ schema: [],
39
+ },
40
+ create(context) {
41
+ return {
42
+ 'OperationDefinition[name=undefined]'(node) {
43
+ const [firstSelection] = node.selectionSet.selections;
44
+ const suggestedName = firstSelection.kind === graphql_1.Kind.FIELD
45
+ ? (firstSelection.alias || firstSelection.name).value
46
+ : node.operation;
47
+ context.report({
48
+ loc: (0, utils_1.getLocation)(node.loc.start, node.operation),
49
+ messageId: RULE_ID,
50
+ data: {
51
+ operation: node.operation,
52
+ },
53
+ suggest: [
54
+ {
55
+ desc: `Rename to \`${suggestedName}\``,
56
+ fix(fixer) {
57
+ const sourceCode = context.getSourceCode();
58
+ const hasQueryKeyword = sourceCode.getText({ range: [node.range[0], node.range[0] + 1] }) !== '{';
59
+ return fixer.insertTextAfterRange([node.range[0], node.range[0] + (hasQueryKeyword ? node.operation.length : 0)], `${hasQueryKeyword ? '' : 'query'} ${suggestedName}${hasQueryKeyword ? '' : ' '}`);
60
+ },
61
+ },
62
+ ],
63
+ });
64
+ },
65
+ };
66
+ },
67
+ };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ exports.rule = {
6
+ meta: {
7
+ type: 'suggestion',
8
+ hasSuggestions: true,
9
+ docs: {
10
+ url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/no-case-insensitive-enum-values-duplicates.md',
11
+ category: 'Schema',
12
+ recommended: true,
13
+ description: 'Disallow case-insensitive enum values duplicates.',
14
+ examples: [
15
+ {
16
+ title: 'Incorrect',
17
+ code: /* GraphQL */ `
18
+ enum MyEnum {
19
+ Value
20
+ VALUE
21
+ ValuE
22
+ }
23
+ `,
24
+ },
25
+ {
26
+ title: 'Correct',
27
+ code: /* GraphQL */ `
28
+ enum MyEnum {
29
+ Value1
30
+ Value2
31
+ Value3
32
+ }
33
+ `,
34
+ },
35
+ ],
36
+ },
37
+ schema: [],
38
+ },
39
+ create(context) {
40
+ const selector = [graphql_1.Kind.ENUM_TYPE_DEFINITION, graphql_1.Kind.ENUM_TYPE_EXTENSION].join(',');
41
+ return {
42
+ [selector](node) {
43
+ const duplicates = node.values.filter((item, index, array) => array.findIndex(v => v.name.value.toLowerCase() === item.name.value.toLowerCase()) !==
44
+ index);
45
+ for (const duplicate of duplicates) {
46
+ const enumName = duplicate.name.value;
47
+ context.report({
48
+ node: duplicate.name,
49
+ message: `Case-insensitive enum values duplicates are not allowed! Found: \`${enumName}\`.`,
50
+ suggest: [
51
+ {
52
+ desc: `Remove \`${enumName}\` enum value`,
53
+ fix: fixer => fixer.remove(duplicate),
54
+ },
55
+ ],
56
+ });
57
+ }
58
+ },
59
+ };
60
+ },
61
+ };
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const utils_1 = require("../utils");
6
+ const RULE_ID = 'no-deprecated';
7
+ exports.rule = {
8
+ meta: {
9
+ type: 'suggestion',
10
+ hasSuggestions: true,
11
+ docs: {
12
+ category: 'Operations',
13
+ description: 'Enforce that deprecated fields or enum values are not in use by operations.',
14
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
15
+ requiresSchema: true,
16
+ examples: [
17
+ {
18
+ title: 'Incorrect (field)',
19
+ code: /* GraphQL */ `
20
+ # In your schema
21
+ type User {
22
+ id: ID!
23
+ name: String! @deprecated(reason: "old field, please use fullName instead")
24
+ fullName: String!
25
+ }
26
+
27
+ # Query
28
+ query user {
29
+ user {
30
+ name # This is deprecated, so you'll get an error
31
+ }
32
+ }
33
+ `,
34
+ },
35
+ {
36
+ title: 'Incorrect (enum value)',
37
+ code: /* GraphQL */ `
38
+ # In your schema
39
+ type Mutation {
40
+ changeSomething(type: SomeType): Boolean!
41
+ }
42
+
43
+ enum SomeType {
44
+ NEW
45
+ OLD @deprecated(reason: "old field, please use NEW instead")
46
+ }
47
+
48
+ # Mutation
49
+ mutation {
50
+ changeSomething(
51
+ type: OLD # This is deprecated, so you'll get an error
52
+ ) {
53
+ ...
54
+ }
55
+ }
56
+ `,
57
+ },
58
+ {
59
+ title: 'Correct',
60
+ code: /* GraphQL */ `
61
+ # In your schema
62
+ type User {
63
+ id: ID!
64
+ name: String! @deprecated(reason: "old field, please use fullName instead")
65
+ fullName: String!
66
+ }
67
+
68
+ # Query
69
+ query user {
70
+ user {
71
+ id
72
+ fullName
73
+ }
74
+ }
75
+ `,
76
+ },
77
+ ],
78
+ recommended: true,
79
+ },
80
+ messages: {
81
+ [RULE_ID]: 'This {{ type }} is marked as deprecated in your GraphQL schema (reason: {{ reason }})',
82
+ },
83
+ schema: [],
84
+ },
85
+ create(context) {
86
+ (0, utils_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
87
+ function report(node, reason) {
88
+ const nodeName = node.kind === graphql_1.Kind.ENUM ? node.value : node.name.value;
89
+ const nodeType = node.kind === graphql_1.Kind.ENUM ? 'enum value' : 'field';
90
+ context.report({
91
+ node,
92
+ messageId: RULE_ID,
93
+ data: {
94
+ type: nodeType,
95
+ reason,
96
+ },
97
+ suggest: [
98
+ {
99
+ desc: `Remove \`${nodeName}\` ${nodeType}`,
100
+ fix: fixer => fixer.remove(node),
101
+ },
102
+ ],
103
+ });
104
+ }
105
+ return {
106
+ EnumValue(node) {
107
+ var _a;
108
+ const typeInfo = node.typeInfo();
109
+ const reason = (_a = typeInfo.enumValue) === null || _a === void 0 ? void 0 : _a.deprecationReason;
110
+ if (reason) {
111
+ report(node, reason);
112
+ }
113
+ },
114
+ Field(node) {
115
+ var _a;
116
+ const typeInfo = node.typeInfo();
117
+ const reason = (_a = typeInfo.fieldDef) === null || _a === void 0 ? void 0 : _a.deprecationReason;
118
+ if (reason) {
119
+ report(node, reason);
120
+ }
121
+ },
122
+ };
123
+ },
124
+ };
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const RULE_ID = 'no-duplicate-fields';
6
+ exports.rule = {
7
+ meta: {
8
+ type: 'suggestion',
9
+ hasSuggestions: true,
10
+ docs: {
11
+ description: 'Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.',
12
+ category: 'Operations',
13
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
14
+ recommended: true,
15
+ examples: [
16
+ {
17
+ title: 'Incorrect',
18
+ code: /* GraphQL */ `
19
+ query {
20
+ user {
21
+ name
22
+ email
23
+ name # duplicate field
24
+ }
25
+ }
26
+ `,
27
+ },
28
+ {
29
+ title: 'Incorrect',
30
+ code: /* GraphQL */ `
31
+ query {
32
+ users(
33
+ first: 100
34
+ skip: 50
35
+ after: "cji629tngfgou0b73kt7vi5jo"
36
+ first: 100 # duplicate argument
37
+ ) {
38
+ id
39
+ }
40
+ }
41
+ `,
42
+ },
43
+ {
44
+ title: 'Incorrect',
45
+ code: /* GraphQL */ `
46
+ query (
47
+ $first: Int!
48
+ $first: Int! # duplicate variable
49
+ ) {
50
+ users(first: $first, skip: 50) {
51
+ id
52
+ }
53
+ }
54
+ `,
55
+ },
56
+ ],
57
+ },
58
+ messages: {
59
+ [RULE_ID]: '{{ type }} `{{ fieldName }}` defined multiple times.',
60
+ },
61
+ schema: [],
62
+ },
63
+ create(context) {
64
+ function checkNode(usedFields, node) {
65
+ const fieldName = node.value;
66
+ if (usedFields.has(fieldName)) {
67
+ const { parent } = node;
68
+ context.report({
69
+ node,
70
+ messageId: RULE_ID,
71
+ data: {
72
+ type: parent.type,
73
+ fieldName,
74
+ },
75
+ suggest: [
76
+ {
77
+ desc: `Remove \`${fieldName}\` ${parent.type.toLowerCase()}`,
78
+ fix(fixer) {
79
+ return fixer.remove((parent.type === graphql_1.Kind.VARIABLE ? parent.parent : parent));
80
+ },
81
+ },
82
+ ],
83
+ });
84
+ }
85
+ else {
86
+ usedFields.add(fieldName);
87
+ }
88
+ }
89
+ return {
90
+ OperationDefinition(node) {
91
+ const set = new Set();
92
+ for (const varDef of node.variableDefinitions) {
93
+ checkNode(set, varDef.variable.name);
94
+ }
95
+ },
96
+ Field(node) {
97
+ const set = new Set();
98
+ for (const arg of node.arguments) {
99
+ checkNode(set, arg.name);
100
+ }
101
+ },
102
+ SelectionSet(node) {
103
+ const set = new Set();
104
+ for (const selection of node.selections) {
105
+ if (selection.kind === graphql_1.Kind.FIELD) {
106
+ checkNode(set, selection.alias || selection.name);
107
+ }
108
+ }
109
+ },
110
+ };
111
+ },
112
+ };