@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,348 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const graphql_1 = require("graphql");
6
+ const lodash_lowercase_1 = tslib_1.__importDefault(require("lodash.lowercase"));
7
+ const utils_1 = require("../utils");
8
+ const RULE_ID = 'alphabetize';
9
+ const fieldsEnum = [
10
+ graphql_1.Kind.OBJECT_TYPE_DEFINITION,
11
+ graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
12
+ graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
13
+ ];
14
+ const valuesEnum = [graphql_1.Kind.ENUM_TYPE_DEFINITION];
15
+ const selectionsEnum = [
16
+ graphql_1.Kind.OPERATION_DEFINITION,
17
+ graphql_1.Kind.FRAGMENT_DEFINITION,
18
+ ];
19
+ const variablesEnum = [graphql_1.Kind.OPERATION_DEFINITION];
20
+ const argumentsEnum = [
21
+ graphql_1.Kind.FIELD_DEFINITION,
22
+ graphql_1.Kind.FIELD,
23
+ graphql_1.Kind.DIRECTIVE_DEFINITION,
24
+ graphql_1.Kind.DIRECTIVE,
25
+ ];
26
+ const schema = {
27
+ type: 'array',
28
+ minItems: 1,
29
+ maxItems: 1,
30
+ items: {
31
+ type: 'object',
32
+ additionalProperties: false,
33
+ minProperties: 1,
34
+ properties: {
35
+ fields: {
36
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
37
+ items: {
38
+ enum: fieldsEnum,
39
+ },
40
+ description: 'Fields of `type`, `interface`, and `input`.',
41
+ },
42
+ values: {
43
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
44
+ items: {
45
+ enum: valuesEnum,
46
+ },
47
+ description: 'Values of `enum`.',
48
+ },
49
+ selections: {
50
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
51
+ items: {
52
+ enum: selectionsEnum,
53
+ },
54
+ description: 'Selections of `fragment` and operations `query`, `mutation` and `subscription`.',
55
+ },
56
+ variables: {
57
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
58
+ items: {
59
+ enum: variablesEnum,
60
+ },
61
+ description: 'Variables of operations `query`, `mutation` and `subscription`.',
62
+ },
63
+ arguments: {
64
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
65
+ items: {
66
+ enum: argumentsEnum,
67
+ },
68
+ description: 'Arguments of fields and directives.',
69
+ },
70
+ definitions: {
71
+ type: 'boolean',
72
+ description: 'Definitions – `type`, `interface`, `enum`, `scalar`, `input`, `union` and `directive`.',
73
+ default: false,
74
+ },
75
+ groups: {
76
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
77
+ minItems: 2,
78
+ description: "Custom order group. Example: `['id', '*', 'createdAt', 'updatedAt']` where `*` says for everything else.",
79
+ },
80
+ },
81
+ },
82
+ };
83
+ exports.rule = {
84
+ meta: {
85
+ type: 'suggestion',
86
+ fixable: 'code',
87
+ docs: {
88
+ category: ['Schema', 'Operations'],
89
+ description: 'Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.',
90
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
91
+ examples: [
92
+ {
93
+ title: 'Incorrect',
94
+ usage: [{ fields: [graphql_1.Kind.OBJECT_TYPE_DEFINITION] }],
95
+ code: /* GraphQL */ `
96
+ type User {
97
+ password: String
98
+ firstName: String! # should be before "password"
99
+ age: Int # should be before "firstName"
100
+ lastName: String!
101
+ }
102
+ `,
103
+ },
104
+ {
105
+ title: 'Correct',
106
+ usage: [{ fields: [graphql_1.Kind.OBJECT_TYPE_DEFINITION] }],
107
+ code: /* GraphQL */ `
108
+ type User {
109
+ age: Int
110
+ firstName: String!
111
+ lastName: String!
112
+ password: String
113
+ }
114
+ `,
115
+ },
116
+ {
117
+ title: 'Incorrect',
118
+ usage: [{ values: [graphql_1.Kind.ENUM_TYPE_DEFINITION] }],
119
+ code: /* GraphQL */ `
120
+ enum Role {
121
+ SUPER_ADMIN
122
+ ADMIN # should be before "SUPER_ADMIN"
123
+ USER
124
+ GOD # should be before "USER"
125
+ }
126
+ `,
127
+ },
128
+ {
129
+ title: 'Correct',
130
+ usage: [{ values: [graphql_1.Kind.ENUM_TYPE_DEFINITION] }],
131
+ code: /* GraphQL */ `
132
+ enum Role {
133
+ ADMIN
134
+ GOD
135
+ SUPER_ADMIN
136
+ USER
137
+ }
138
+ `,
139
+ },
140
+ {
141
+ title: 'Incorrect',
142
+ usage: [{ selections: [graphql_1.Kind.OPERATION_DEFINITION] }],
143
+ code: /* GraphQL */ `
144
+ query {
145
+ me {
146
+ firstName
147
+ lastName
148
+ email # should be before "lastName"
149
+ }
150
+ }
151
+ `,
152
+ },
153
+ {
154
+ title: 'Correct',
155
+ usage: [{ selections: [graphql_1.Kind.OPERATION_DEFINITION] }],
156
+ code: /* GraphQL */ `
157
+ query {
158
+ me {
159
+ email
160
+ firstName
161
+ lastName
162
+ }
163
+ }
164
+ `,
165
+ },
166
+ ],
167
+ configOptions: {
168
+ schema: [
169
+ {
170
+ fields: fieldsEnum,
171
+ values: valuesEnum,
172
+ arguments: argumentsEnum,
173
+ // TODO: add in graphql-eslint v4
174
+ // definitions: true,
175
+ // groups: ['id', '*', 'createdAt', 'updatedAt']
176
+ },
177
+ ],
178
+ operations: [
179
+ {
180
+ selections: selectionsEnum,
181
+ variables: variablesEnum,
182
+ arguments: [graphql_1.Kind.FIELD, graphql_1.Kind.DIRECTIVE],
183
+ },
184
+ ],
185
+ },
186
+ },
187
+ messages: {
188
+ [RULE_ID]: '`{{ currName }}` should be before {{ prevName }}.',
189
+ },
190
+ schema,
191
+ },
192
+ create(context) {
193
+ var _a, _b, _c, _d, _e;
194
+ const sourceCode = context.getSourceCode();
195
+ function isNodeAndCommentOnSameLine(node, comment) {
196
+ return node.loc.end.line === comment.loc.start.line;
197
+ }
198
+ function getBeforeComments(node) {
199
+ const commentsBefore = sourceCode.getCommentsBefore(node);
200
+ if (commentsBefore.length === 0) {
201
+ return [];
202
+ }
203
+ const tokenBefore = sourceCode.getTokenBefore(node);
204
+ if (tokenBefore) {
205
+ return commentsBefore.filter(comment => !isNodeAndCommentOnSameLine(tokenBefore, comment));
206
+ }
207
+ const filteredComments = [];
208
+ const nodeLine = node.loc.start.line;
209
+ // Break on comment that not attached to node
210
+ for (let i = commentsBefore.length - 1; i >= 0; i -= 1) {
211
+ const comment = commentsBefore[i];
212
+ if (nodeLine - comment.loc.start.line - filteredComments.length > 1) {
213
+ break;
214
+ }
215
+ filteredComments.unshift(comment);
216
+ }
217
+ return filteredComments;
218
+ }
219
+ function getRangeWithComments(node) {
220
+ if (node.kind === graphql_1.Kind.VARIABLE) {
221
+ node = node.parent;
222
+ }
223
+ const [firstBeforeComment] = getBeforeComments(node);
224
+ const [firstAfterComment] = sourceCode.getCommentsAfter(node);
225
+ const from = firstBeforeComment || node;
226
+ const to = firstAfterComment && isNodeAndCommentOnSameLine(node, firstAfterComment)
227
+ ? firstAfterComment
228
+ : node;
229
+ return [from.range[0], to.range[1]];
230
+ }
231
+ function checkNodes(nodes) {
232
+ var _a, _b, _c, _d;
233
+ // Starts from 1, ignore nodes.length <= 1
234
+ for (let i = 1; i < nodes.length; i += 1) {
235
+ const currNode = nodes[i];
236
+ const currName = ('alias' in currNode && ((_a = currNode.alias) === null || _a === void 0 ? void 0 : _a.value)) ||
237
+ ('name' in currNode && ((_b = currNode.name) === null || _b === void 0 ? void 0 : _b.value));
238
+ if (!currName) {
239
+ // we don't move unnamed current nodes
240
+ continue;
241
+ }
242
+ const prevNode = nodes[i - 1];
243
+ const prevName = ('alias' in prevNode && ((_c = prevNode.alias) === null || _c === void 0 ? void 0 : _c.value)) ||
244
+ ('name' in prevNode && ((_d = prevNode.name) === null || _d === void 0 ? void 0 : _d.value));
245
+ if (prevName) {
246
+ // Compare with lexicographic order
247
+ const compareResult = prevName.localeCompare(currName);
248
+ const { groups } = opts;
249
+ let shouldSortByGroup = false;
250
+ if (groups === null || groups === void 0 ? void 0 : groups.length) {
251
+ if (!groups.includes('*')) {
252
+ throw new Error('`groups` option should contain `*` string.');
253
+ }
254
+ let indexForPrev = groups.indexOf(prevName);
255
+ if (indexForPrev === -1)
256
+ indexForPrev = groups.indexOf('*');
257
+ let indexForCurr = groups.indexOf(currName);
258
+ if (indexForCurr === -1)
259
+ indexForCurr = groups.indexOf('*');
260
+ shouldSortByGroup = indexForPrev - indexForCurr > 0;
261
+ if (indexForPrev < indexForCurr) {
262
+ continue;
263
+ }
264
+ }
265
+ const shouldSort = compareResult === 1;
266
+ if (!shouldSortByGroup && !shouldSort) {
267
+ const isSameName = compareResult === 0;
268
+ if (!isSameName ||
269
+ !prevNode.kind.endsWith('Extension') ||
270
+ currNode.kind.endsWith('Extension')) {
271
+ continue;
272
+ }
273
+ }
274
+ }
275
+ context.report({
276
+ node: ('alias' in currNode && currNode.alias) || currNode.name,
277
+ messageId: RULE_ID,
278
+ data: {
279
+ currName,
280
+ prevName: prevName ? `\`${prevName}\`` : (0, lodash_lowercase_1.default)(prevNode.kind),
281
+ },
282
+ *fix(fixer) {
283
+ const prevRange = getRangeWithComments(prevNode);
284
+ const currRange = getRangeWithComments(currNode);
285
+ yield fixer.replaceTextRange(prevRange, sourceCode.getText({ range: currRange }));
286
+ yield fixer.replaceTextRange(currRange, sourceCode.getText({ range: prevRange }));
287
+ },
288
+ });
289
+ }
290
+ }
291
+ const opts = context.options[0];
292
+ const fields = new Set((_a = opts.fields) !== null && _a !== void 0 ? _a : []);
293
+ const listeners = {};
294
+ const kinds = [
295
+ fields.has(graphql_1.Kind.OBJECT_TYPE_DEFINITION) && [
296
+ graphql_1.Kind.OBJECT_TYPE_DEFINITION,
297
+ graphql_1.Kind.OBJECT_TYPE_EXTENSION,
298
+ ],
299
+ fields.has(graphql_1.Kind.INTERFACE_TYPE_DEFINITION) && [
300
+ graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
301
+ graphql_1.Kind.INTERFACE_TYPE_EXTENSION,
302
+ ],
303
+ fields.has(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION) && [
304
+ graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
305
+ graphql_1.Kind.INPUT_OBJECT_TYPE_EXTENSION,
306
+ ],
307
+ ]
308
+ .filter(Boolean)
309
+ .flat();
310
+ const fieldsSelector = kinds.join(',');
311
+ const hasEnumValues = ((_b = opts.values) === null || _b === void 0 ? void 0 : _b[0]) === graphql_1.Kind.ENUM_TYPE_DEFINITION;
312
+ const selectionsSelector = (_c = opts.selections) === null || _c === void 0 ? void 0 : _c.join(',');
313
+ const hasVariables = ((_d = opts.variables) === null || _d === void 0 ? void 0 : _d[0]) === graphql_1.Kind.OPERATION_DEFINITION;
314
+ const argumentsSelector = (_e = opts.arguments) === null || _e === void 0 ? void 0 : _e.join(',');
315
+ if (fieldsSelector) {
316
+ listeners[fieldsSelector] = (node) => {
317
+ checkNodes(node.fields);
318
+ };
319
+ }
320
+ if (hasEnumValues) {
321
+ const enumValuesSelector = [graphql_1.Kind.ENUM_TYPE_DEFINITION, graphql_1.Kind.ENUM_TYPE_EXTENSION].join(',');
322
+ listeners[enumValuesSelector] = (node) => {
323
+ checkNodes(node.values);
324
+ };
325
+ }
326
+ if (selectionsSelector) {
327
+ listeners[`:matches(${selectionsSelector}) SelectionSet`] = (node) => {
328
+ checkNodes(node.selections);
329
+ };
330
+ }
331
+ if (hasVariables) {
332
+ listeners.OperationDefinition = (node) => {
333
+ checkNodes(node.variableDefinitions.map(varDef => varDef.variable));
334
+ };
335
+ }
336
+ if (argumentsSelector) {
337
+ listeners[argumentsSelector] = (node) => {
338
+ checkNodes(node.arguments);
339
+ };
340
+ }
341
+ if (opts.definitions) {
342
+ listeners.Document = node => {
343
+ checkNodes(node.definitions);
344
+ };
345
+ }
346
+ return listeners;
347
+ },
348
+ };
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const schema = {
5
+ type: 'array',
6
+ maxItems: 1,
7
+ items: {
8
+ type: 'object',
9
+ additionalProperties: false,
10
+ minProperties: 1,
11
+ properties: {
12
+ style: {
13
+ enum: ['block', 'inline'],
14
+ default: 'block',
15
+ },
16
+ },
17
+ },
18
+ };
19
+ exports.rule = {
20
+ meta: {
21
+ type: 'suggestion',
22
+ hasSuggestions: true,
23
+ docs: {
24
+ examples: [
25
+ {
26
+ title: 'Incorrect',
27
+ usage: [{ style: 'inline' }],
28
+ code: /* GraphQL */ `
29
+ """ Description """
30
+ type someTypeName {
31
+ # ...
32
+ }
33
+ `,
34
+ },
35
+ {
36
+ title: 'Correct',
37
+ usage: [{ style: 'inline' }],
38
+ code: /* GraphQL */ `
39
+ " Description "
40
+ type someTypeName {
41
+ # ...
42
+ }
43
+ `,
44
+ },
45
+ ],
46
+ description: 'Require all comments to follow the same style (either block or inline).',
47
+ category: 'Schema',
48
+ url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/description-style.md',
49
+ recommended: true,
50
+ },
51
+ schema,
52
+ },
53
+ create(context) {
54
+ const { style = 'block' } = context.options[0] || {};
55
+ const isBlock = style === 'block';
56
+ return {
57
+ [`.description[type=StringValue][block!=${isBlock}]`](node) {
58
+ context.report({
59
+ loc: isBlock ? node.loc : node.loc.start,
60
+ message: `Unexpected ${isBlock ? 'inline' : 'block'} description.`,
61
+ suggest: [
62
+ {
63
+ desc: `Change to ${isBlock ? 'block' : 'inline'} style description`,
64
+ fix(fixer) {
65
+ const sourceCode = context.getSourceCode();
66
+ const originalText = sourceCode.getText(node);
67
+ const newText = isBlock
68
+ ? originalText.replace(/(^")|("$)/g, '"""')
69
+ : originalText.replace(/(^""")|("""$)/g, '"').replace(/\s+/g, ' ');
70
+ return fixer.replaceText(node, newText);
71
+ },
72
+ },
73
+ ],
74
+ });
75
+ },
76
+ };
77
+ },
78
+ };