@graphql-eslint/eslint-plugin 3.14.0-alpha-20221221133443-5be7fc6 → 3.14.0-alpha-20221222012949-5caade4

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,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const schema = {
6
+ type: 'array',
7
+ maxItems: 1,
8
+ items: {
9
+ type: 'object',
10
+ additionalProperties: false,
11
+ properties: {
12
+ checkInputType: {
13
+ type: 'boolean',
14
+ default: false,
15
+ description: 'Check that the input type name follows the convention <mutationName>Input',
16
+ },
17
+ caseSensitiveInputType: {
18
+ type: 'boolean',
19
+ default: true,
20
+ description: 'Allow for case discrepancies in the input type name',
21
+ },
22
+ checkQueries: {
23
+ type: 'boolean',
24
+ default: false,
25
+ description: 'Apply the rule to Queries',
26
+ },
27
+ checkMutations: {
28
+ type: 'boolean',
29
+ default: true,
30
+ description: 'Apply the rule to Mutations',
31
+ },
32
+ },
33
+ },
34
+ };
35
+ const isObjectType = (node) => [graphql_1.Kind.OBJECT_TYPE_DEFINITION, graphql_1.Kind.OBJECT_TYPE_EXTENSION].includes(node.type);
36
+ const isQueryType = (node) => isObjectType(node) && node.name.value === 'Query';
37
+ const isMutationType = (node) => isObjectType(node) && node.name.value === 'Mutation';
38
+ exports.rule = {
39
+ meta: {
40
+ type: 'suggestion',
41
+ hasSuggestions: true,
42
+ docs: {
43
+ description: 'Require mutation argument to be always called "input" and input type to be called Mutation name + "Input".\nUsing the same name for all input parameters will make your schemas easier to consume and more predictable. Using the same name as mutation for InputType will make it easier to find mutations that InputType belongs to.',
44
+ category: 'Schema',
45
+ url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/input-name.md',
46
+ examples: [
47
+ {
48
+ title: 'Incorrect',
49
+ usage: [{ checkInputType: true }],
50
+ code: /* GraphQL */ `
51
+ type Mutation {
52
+ SetMessage(message: InputMessage): String
53
+ }
54
+ `,
55
+ },
56
+ {
57
+ title: 'Correct (with checkInputType)',
58
+ usage: [{ checkInputType: true }],
59
+ code: /* GraphQL */ `
60
+ type Mutation {
61
+ SetMessage(input: SetMessageInput): String
62
+ }
63
+ `,
64
+ },
65
+ {
66
+ title: 'Correct (without checkInputType)',
67
+ usage: [{ checkInputType: false }],
68
+ code: /* GraphQL */ `
69
+ type Mutation {
70
+ SetMessage(input: AnyInputTypeName): String
71
+ }
72
+ `,
73
+ },
74
+ ],
75
+ },
76
+ schema,
77
+ },
78
+ create(context) {
79
+ const options = {
80
+ checkInputType: false,
81
+ caseSensitiveInputType: true,
82
+ checkQueries: false,
83
+ checkMutations: true,
84
+ ...context.options[0],
85
+ };
86
+ const shouldCheckType = node => (options.checkMutations && isMutationType(node)) ||
87
+ (options.checkQueries && isQueryType(node));
88
+ const listeners = {
89
+ 'FieldDefinition > InputValueDefinition[name.value!=input] > Name'(node) {
90
+ if (shouldCheckType(node.parent.parent.parent)) {
91
+ const inputName = node.value;
92
+ context.report({
93
+ node,
94
+ message: `Input \`${inputName}\` should be called \`input\`.`,
95
+ suggest: [
96
+ {
97
+ desc: 'Rename to `input`',
98
+ fix: fixer => fixer.replaceText(node, 'input'),
99
+ },
100
+ ],
101
+ });
102
+ }
103
+ },
104
+ };
105
+ if (options.checkInputType) {
106
+ listeners['FieldDefinition > InputValueDefinition NamedType'] = (node) => {
107
+ const findInputType = item => {
108
+ let currentNode = item;
109
+ while (currentNode.type !== graphql_1.Kind.INPUT_VALUE_DEFINITION) {
110
+ currentNode = currentNode.parent;
111
+ }
112
+ return currentNode;
113
+ };
114
+ const inputValueNode = findInputType(node);
115
+ if (shouldCheckType(inputValueNode.parent.parent)) {
116
+ const mutationName = `${inputValueNode.parent.name.value}Input`;
117
+ const name = node.name.value;
118
+ if ((options.caseSensitiveInputType && node.name.value !== mutationName) ||
119
+ name.toLowerCase() !== mutationName.toLowerCase()) {
120
+ context.report({
121
+ node: node.name,
122
+ message: `Input type \`${name}\` name should be \`${mutationName}\`.`,
123
+ suggest: [
124
+ {
125
+ desc: `Rename to \`${mutationName}\``,
126
+ fix: fixer => fixer.replaceText(node, mutationName),
127
+ },
128
+ ],
129
+ });
130
+ }
131
+ }
132
+ };
133
+ }
134
+ return listeners;
135
+ },
136
+ };
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const utils_1 = require("../utils");
5
+ const RULE_ID = 'lone-executable-definition';
6
+ const definitionTypes = ['fragment', 'query', 'mutation', 'subscription'];
7
+ const schema = {
8
+ type: 'array',
9
+ maxItems: 1,
10
+ items: {
11
+ type: 'object',
12
+ minProperties: 1,
13
+ additionalProperties: false,
14
+ properties: {
15
+ ignore: {
16
+ ...utils_1.ARRAY_DEFAULT_OPTIONS,
17
+ maxItems: 3,
18
+ items: {
19
+ enum: definitionTypes,
20
+ },
21
+ description: 'Allow certain definitions to be placed alongside others.',
22
+ },
23
+ },
24
+ },
25
+ };
26
+ exports.rule = {
27
+ meta: {
28
+ type: 'suggestion',
29
+ docs: {
30
+ category: 'Operations',
31
+ description: 'Require all queries, mutations, subscriptions and fragments to be located in separate files.',
32
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
33
+ examples: [
34
+ {
35
+ title: 'Incorrect',
36
+ code: /* GraphQL */ `
37
+ query Foo {
38
+ id
39
+ }
40
+ fragment Bar on Baz {
41
+ id
42
+ }
43
+ `,
44
+ },
45
+ {
46
+ title: 'Correct',
47
+ code: /* GraphQL */ `
48
+ query Foo {
49
+ id
50
+ }
51
+ `,
52
+ },
53
+ ],
54
+ },
55
+ messages: {
56
+ [RULE_ID]: '{{name}} should be in a separate file.',
57
+ },
58
+ schema,
59
+ },
60
+ create(context) {
61
+ var _a;
62
+ const ignore = new Set(((_a = context.options[0]) === null || _a === void 0 ? void 0 : _a.ignore) || []);
63
+ const definitions = [];
64
+ return {
65
+ ':matches(OperationDefinition, FragmentDefinition)'(node) {
66
+ const type = 'operation' in node ? node.operation : 'fragment';
67
+ if (!ignore.has(type)) {
68
+ definitions.push({ type, node });
69
+ }
70
+ },
71
+ 'Program:exit'() {
72
+ var _a, _b;
73
+ for (const { node, type } of definitions.slice(1) /* ignore first definition */) {
74
+ let name = (0, utils_1.pascalCase)(type);
75
+ const definitionName = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value;
76
+ if (definitionName) {
77
+ name += ` "${definitionName}"`;
78
+ }
79
+ context.report({
80
+ loc: ((_b = node.name) === null || _b === void 0 ? void 0 : _b.loc) || (0, utils_1.getLocation)(node.loc.start, type),
81
+ messageId: RULE_ID,
82
+ data: { name },
83
+ });
84
+ }
85
+ },
86
+ };
87
+ },
88
+ };
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const path_1 = require("path");
5
+ const fs_1 = require("fs");
6
+ const graphql_1 = require("graphql");
7
+ const utils_1 = require("../utils");
8
+ const MATCH_EXTENSION = 'MATCH_EXTENSION';
9
+ const MATCH_STYLE = 'MATCH_STYLE';
10
+ const CASE_STYLES = [
11
+ 'camelCase',
12
+ 'PascalCase',
13
+ 'snake_case',
14
+ 'UPPER_CASE',
15
+ 'kebab-case',
16
+ 'matchDocumentStyle',
17
+ ];
18
+ const schemaOption = {
19
+ oneOf: [{ $ref: '#/definitions/asString' }, { $ref: '#/definitions/asObject' }],
20
+ };
21
+ const schema = {
22
+ definitions: {
23
+ asString: {
24
+ enum: CASE_STYLES,
25
+ description: `One of: ${CASE_STYLES.map(t => `\`${t}\``).join(', ')}`,
26
+ },
27
+ asObject: {
28
+ type: 'object',
29
+ additionalProperties: false,
30
+ minProperties: 1,
31
+ properties: {
32
+ style: { enum: CASE_STYLES },
33
+ suffix: { type: 'string' },
34
+ prefix: { type: 'string' },
35
+ },
36
+ },
37
+ },
38
+ type: 'array',
39
+ minItems: 1,
40
+ maxItems: 1,
41
+ items: {
42
+ type: 'object',
43
+ additionalProperties: false,
44
+ minProperties: 1,
45
+ properties: {
46
+ fileExtension: { enum: ['.gql', '.graphql'] },
47
+ query: schemaOption,
48
+ mutation: schemaOption,
49
+ subscription: schemaOption,
50
+ fragment: schemaOption,
51
+ },
52
+ },
53
+ };
54
+ exports.rule = {
55
+ meta: {
56
+ type: 'suggestion',
57
+ docs: {
58
+ category: 'Operations',
59
+ description: 'This rule allows you to enforce that the file name should match the operation name.',
60
+ url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/match-document-filename.md',
61
+ examples: [
62
+ {
63
+ title: 'Correct',
64
+ usage: [{ fileExtension: '.gql' }],
65
+ code: /* GraphQL */ `
66
+ # user.gql
67
+ type User {
68
+ id: ID!
69
+ }
70
+ `,
71
+ },
72
+ {
73
+ title: 'Correct',
74
+ usage: [{ query: 'snake_case' }],
75
+ code: /* GraphQL */ `
76
+ # user_by_id.gql
77
+ query UserById {
78
+ userById(id: 5) {
79
+ id
80
+ name
81
+ fullName
82
+ }
83
+ }
84
+ `,
85
+ },
86
+ {
87
+ title: 'Correct',
88
+ usage: [{ fragment: { style: 'kebab-case', suffix: '.fragment' } }],
89
+ code: /* GraphQL */ `
90
+ # user-fields.fragment.gql
91
+ fragment user_fields on User {
92
+ id
93
+ email
94
+ }
95
+ `,
96
+ },
97
+ {
98
+ title: 'Correct',
99
+ usage: [{ mutation: { style: 'PascalCase', suffix: 'Mutation' } }],
100
+ code: /* GraphQL */ `
101
+ # DeleteUserMutation.gql
102
+ mutation DELETE_USER {
103
+ deleteUser(id: 5)
104
+ }
105
+ `,
106
+ },
107
+ {
108
+ title: 'Incorrect',
109
+ usage: [{ fileExtension: '.graphql' }],
110
+ code: /* GraphQL */ `
111
+ # post.gql
112
+ type Post {
113
+ id: ID!
114
+ }
115
+ `,
116
+ },
117
+ {
118
+ title: 'Incorrect',
119
+ usage: [{ query: 'PascalCase' }],
120
+ code: /* GraphQL */ `
121
+ # user-by-id.gql
122
+ query UserById {
123
+ userById(id: 5) {
124
+ id
125
+ name
126
+ fullName
127
+ }
128
+ }
129
+ `,
130
+ },
131
+ {
132
+ title: 'Correct',
133
+ usage: [{ fragment: { style: 'kebab-case', prefix: 'mutation.' } }],
134
+ code: /* GraphQL */ `
135
+ # mutation.add-alert.graphql
136
+ mutation addAlert {
137
+ foo
138
+ }
139
+ `,
140
+ },
141
+ {
142
+ title: 'Correct',
143
+ usage: [{ fragment: { prefix: 'query.' } }],
144
+ code: /* GraphQL */ `
145
+ # query.me.graphql
146
+ query me {
147
+ foo
148
+ }
149
+ `,
150
+ },
151
+ ],
152
+ configOptions: [
153
+ {
154
+ query: 'kebab-case',
155
+ mutation: 'kebab-case',
156
+ subscription: 'kebab-case',
157
+ fragment: 'kebab-case',
158
+ },
159
+ ],
160
+ },
161
+ messages: {
162
+ [MATCH_EXTENSION]: 'File extension "{{ fileExtension }}" don\'t match extension "{{ expectedFileExtension }}"',
163
+ [MATCH_STYLE]: 'Unexpected filename "{{ filename }}". Rename it to "{{ expectedFilename }}"',
164
+ },
165
+ schema,
166
+ },
167
+ create(context) {
168
+ const options = context.options[0] || {
169
+ fileExtension: null,
170
+ };
171
+ const filePath = context.getFilename();
172
+ const isVirtualFile = !(0, fs_1.existsSync)(filePath);
173
+ if (process.env.NODE_ENV !== 'test' && isVirtualFile) {
174
+ // Skip validation for code files
175
+ return {};
176
+ }
177
+ const fileExtension = (0, path_1.extname)(filePath);
178
+ const filename = (0, path_1.basename)(filePath, fileExtension);
179
+ return {
180
+ Document(documentNode) {
181
+ var _a;
182
+ if (options.fileExtension && options.fileExtension !== fileExtension) {
183
+ context.report({
184
+ loc: utils_1.REPORT_ON_FIRST_CHARACTER,
185
+ messageId: MATCH_EXTENSION,
186
+ data: {
187
+ fileExtension,
188
+ expectedFileExtension: options.fileExtension,
189
+ },
190
+ });
191
+ }
192
+ const firstOperation = documentNode.definitions.find(n => n.kind === graphql_1.Kind.OPERATION_DEFINITION);
193
+ const firstFragment = documentNode.definitions.find(n => n.kind === graphql_1.Kind.FRAGMENT_DEFINITION);
194
+ const node = firstOperation || firstFragment;
195
+ if (!node) {
196
+ return;
197
+ }
198
+ const docName = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value;
199
+ if (!docName) {
200
+ return;
201
+ }
202
+ const docType = 'operation' in node ? node.operation : 'fragment';
203
+ let option = options[docType];
204
+ if (!option) {
205
+ // Config not provided
206
+ return;
207
+ }
208
+ if (typeof option === 'string') {
209
+ option = { style: option };
210
+ }
211
+ const expectedExtension = options.fileExtension || fileExtension;
212
+ let expectedFilename = option.prefix || '';
213
+ if (option.style) {
214
+ expectedFilename +=
215
+ option.style === 'matchDocumentStyle' ? docName : (0, utils_1.convertCase)(option.style, docName);
216
+ }
217
+ else {
218
+ expectedFilename += filename;
219
+ }
220
+ expectedFilename += (option.suffix || '') + expectedExtension;
221
+ const filenameWithExtension = filename + expectedExtension;
222
+ if (expectedFilename !== filenameWithExtension) {
223
+ context.report({
224
+ loc: utils_1.REPORT_ON_FIRST_CHARACTER,
225
+ messageId: MATCH_STYLE,
226
+ data: {
227
+ expectedFilename,
228
+ filename: filenameWithExtension,
229
+ },
230
+ });
231
+ }
232
+ },
233
+ };
234
+ },
235
+ };