Skip to content

Commit ebab010

Browse files
author
Dimitri POSTOLOV
authored
fix error report for alphabetize rule (#736)
1 parent 46f03f7 commit ebab010

File tree

5 files changed

+139
-120
lines changed

5 files changed

+139
-120
lines changed

.changeset/long-rockets-cheat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
fix error report for `alphabetize` rule

packages/plugin/src/rules/alphabetize.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { GraphQLESLintRule } from '../types';
2424
import { GraphQLESTreeNode } from '../estree-parser';
2525
import { GraphQLESLintRuleListener } from '../testkit';
26+
import { getLocation } from '../utils';
2627

2728
const ALPHABETIZE = 'ALPHABETIZE';
2829

@@ -137,7 +138,7 @@ const rule: GraphQLESLintRule<AlphabetizeConfig> = {
137138
],
138139
},
139140
messages: {
140-
[ALPHABETIZE]: '"{{ currName }}" should be before "{{ prevName }}".',
141+
[ALPHABETIZE]: '"{{ currName }}" should be before "{{ prevName }}"',
141142
},
142143
schema: {
143144
type: 'array',
@@ -203,17 +204,10 @@ const rule: GraphQLESLintRule<AlphabetizeConfig> = {
203204
for (const node of nodes) {
204205
const currName = node.name.value;
205206
if (prevName && prevName > currName) {
206-
const { start, end } = node.name.loc;
207207
const isVariableNode = node.kind === Kind.VARIABLE;
208208

209209
context.report({
210-
loc: {
211-
start: {
212-
line: start.line,
213-
column: start.column - (isVariableNode ? 2 : 1),
214-
},
215-
end,
216-
},
210+
loc: getLocation(node.loc, node.name.value, { offsetEnd: isVariableNode ? 0 : 1 }),
217211
messageId: ALPHABETIZE,
218212
data: isVariableNode
219213
? {

packages/plugin/src/testkit.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type GraphQLESLintRuleListener<WithTypeInfo extends boolean = false> = {
1111
} & Record<string, any>;
1212

1313
export type GraphQLValidTestCase<Options> = Omit<RuleTester.ValidTestCase, 'options' | 'parserOptions'> & {
14+
name: string;
1415
options?: Options;
1516
parserOptions?: ParserOptions;
1617
};
@@ -50,7 +51,26 @@ export class GraphQLRuleTester extends RuleTester {
5051
invalid: GraphQLInvalidTestCase<Config>[];
5152
}
5253
): void {
53-
super.run(name, rule as Rule.RuleModule, tests);
54+
const ruleTests = Linter.version.startsWith('8')
55+
? tests
56+
: {
57+
valid: tests.valid.map(test => {
58+
if (typeof test === 'string') {
59+
return test;
60+
}
61+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
62+
const { name, ...testCaseOptions } = test;
63+
return testCaseOptions;
64+
}),
65+
invalid: tests.invalid.map(test => {
66+
// ESLint 7 throws an error on CI - Unexpected top-level property "name"
67+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
68+
const { name, ...testCaseOptions } = test;
69+
return testCaseOptions;
70+
}),
71+
};
72+
73+
super.run(name, rule as Rule.RuleModule, ruleTests);
5474

5575
// Skip snapshot testing if `expect` variable is not defined
5676
if (typeof expect === 'undefined') {

packages/plugin/tests/__snapshots__/alphabetize.spec.ts.snap

+73-73
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[` 1`] = `
55
2 | type User {
66
3 | password: String
77
> 4 | firstName: String!
8-
| ^ "firstName" should be before "password".
8+
| ^^^^^^^^^ "firstName" should be before "password"
99
5 | age: Int
1010
6 | lastName: String!
1111
7 | }
@@ -18,7 +18,7 @@ exports[` 2`] = `
1818
3 | password: String
1919
4 | firstName: String!
2020
> 5 | age: Int
21-
| ^ "age" should be before "firstName".
21+
| ^^^ "age" should be before "firstName"
2222
6 | lastName: String!
2323
7 | }
2424
8 |
@@ -31,29 +31,29 @@ exports[` 3`] = `
3131
4 | firstName: String!
3232
5 | password: String
3333
> 6 | lastName: String!
34-
| ^ "lastName" should be before "password".
34+
| ^^^^^^^^ "lastName" should be before "password"
3535
7 | }
3636
8 |
3737
`;
3838

3939
exports[` 4`] = `
4040
1 |
4141
2 | interface Test {
42-
3 | c: Int
43-
> 4 | b: Int
44-
| ^ "b" should be before "c".
45-
5 | a: Int
42+
3 | cc: Int
43+
> 4 | bb: Int
44+
| ^^ "bb" should be before "cc"
45+
5 | aa: Int
4646
6 | }
4747
7 |
4848
`;
4949

5050
exports[` 5`] = `
5151
1 |
5252
2 | interface Test {
53-
3 | c: Int
54-
4 | b: Int
55-
> 5 | a: Int
56-
| ^ "a" should be before "b".
53+
3 | cc: Int
54+
4 | bb: Int
55+
> 5 | aa: Int
56+
| ^^ "aa" should be before "bb"
5757
6 | }
5858
7 |
5959
`;
@@ -63,7 +63,7 @@ exports[` 6`] = `
6363
2 | input UserInput {
6464
3 | password: String
6565
> 4 | firstName: String!
66-
| ^ "firstName" should be before "password".
66+
| ^^^^^^^^^ "firstName" should be before "password"
6767
5 | age: Int
6868
6 | lastName: String!
6969
7 | }
@@ -76,7 +76,7 @@ exports[` 7`] = `
7676
3 | password: String
7777
4 | firstName: String!
7878
> 5 | age: Int
79-
| ^ "age" should be before "firstName".
79+
| ^^^ "age" should be before "firstName"
8080
6 | lastName: String!
8181
7 | }
8282
8 |
@@ -89,7 +89,7 @@ exports[` 8`] = `
8989
4 | firstName: String!
9090
5 | password: String
9191
> 6 | lastName: String!
92-
| ^ "lastName" should be before "password".
92+
| ^^^^^^^^ "lastName" should be before "password"
9393
7 | }
9494
8 |
9595
`;
@@ -99,7 +99,7 @@ exports[` 9`] = `
9999
2 | enum Role {
100100
3 | SUPER_ADMIN
101101
> 4 | ADMIN
102-
| ^ "ADMIN" should be before "SUPER_ADMIN".
102+
| ^^^^^ "ADMIN" should be before "SUPER_ADMIN"
103103
5 | USER
104104
6 | GOD
105105
7 | }
@@ -113,7 +113,7 @@ exports[` 10`] = `
113113
4 | ADMIN
114114
5 | USER
115115
> 6 | GOD
116-
| ^ "GOD" should be before "USER".
116+
| ^^^ "GOD" should be before "USER"
117117
7 | }
118118
8 |
119119
`;
@@ -124,62 +124,62 @@ exports[` 11`] = `
124124
3 | ADMIN
125125
4 | SUPER_ADMIN
126126
> 5 | GOD
127-
| ^ "GOD" should be before "SUPER_ADMIN".
127+
| ^^^ "GOD" should be before "SUPER_ADMIN"
128128
6 | USER
129129
7 | }
130130
8 |
131131
`;
132132

133133
exports[` 12`] = `
134134
1 |
135-
> 2 | directive @test(c: Int, b: Int, a: Int) on FIELD_DEFINITION
136-
| ^ "b" should be before "c".
135+
> 2 | directive @test(cc: Int, bb: Int, aa: Int) on FIELD_DEFINITION
136+
| ^^ "bb" should be before "cc"
137137
3 |
138138
`;
139139

140140
exports[` 13`] = `
141141
1 |
142-
> 2 | directive @test(c: Int, b: Int, a: Int) on FIELD_DEFINITION
143-
| ^ "a" should be before "b".
142+
> 2 | directive @test(cc: Int, bb: Int, aa: Int) on FIELD_DEFINITION
143+
| ^^ "aa" should be before "bb"
144144
3 |
145145
`;
146146

147147
exports[` 14`] = `
148148
1 |
149149
2 | type Query {
150-
> 3 | test(c: Int, b: Int, a: Int): Int
151-
| ^ "b" should be before "c".
150+
> 3 | test(cc: Int, bb: Int, aa: Int): Int
151+
| ^^ "bb" should be before "cc"
152152
4 | }
153153
5 |
154154
`;
155155

156156
exports[` 15`] = `
157157
1 |
158158
2 | type Query {
159-
> 3 | test(c: Int, b: Int, a: Int): Int
160-
| ^ "a" should be before "b".
159+
> 3 | test(cc: Int, bb: Int, aa: Int): Int
160+
| ^^ "aa" should be before "bb"
161161
4 | }
162162
5 |
163163
`;
164164

165165
exports[` 16`] = `
166166
1 |
167167
2 | fragment TestFields on Test {
168-
3 | c
169-
> 4 | b
170-
| ^ "b" should be before "c".
171-
5 | a
168+
3 | cc
169+
> 4 | bb
170+
| ^^ "bb" should be before "cc"
171+
5 | aa
172172
6 | }
173173
7 |
174174
`;
175175

176176
exports[` 17`] = `
177177
1 |
178178
2 | fragment TestFields on Test {
179-
3 | c
180-
4 | b
181-
> 5 | a
182-
| ^ "a" should be before "b".
179+
3 | cc
180+
4 | bb
181+
> 5 | aa
182+
| ^^ "aa" should be before "bb"
183183
6 | }
184184
7 |
185185
`;
@@ -188,14 +188,14 @@ exports[` 18`] = `
188188
1 |
189189
2 | query {
190190
3 | test {
191-
4 | c
192-
> 5 | b
193-
| ^ "b" should be before "c".
194-
6 | a
191+
4 | cc
192+
> 5 | bb
193+
| ^^ "bb" should be before "cc"
194+
6 | aa
195195
7 | ... on Test {
196-
8 | cc
197-
9 | bb
198-
10 | aa
196+
8 | ccc
197+
9 | bbb
198+
10 | aaa
199199
11 | }
200200
12 | }
201201
13 | }
@@ -206,14 +206,14 @@ exports[` 19`] = `
206206
1 |
207207
2 | query {
208208
3 | test {
209-
4 | c
210-
5 | b
211-
> 6 | a
212-
| ^ "a" should be before "b".
209+
4 | cc
210+
5 | bb
211+
> 6 | aa
212+
| ^^ "aa" should be before "bb"
213213
7 | ... on Test {
214-
8 | cc
215-
9 | bb
216-
10 | aa
214+
8 | ccc
215+
9 | bbb
216+
10 | aaa
217217
11 | }
218218
12 | }
219219
13 | }
@@ -224,14 +224,14 @@ exports[` 20`] = `
224224
1 |
225225
2 | query {
226226
3 | test {
227-
4 | c
228-
5 | b
229-
6 | a
227+
4 | cc
228+
5 | bb
229+
6 | aa
230230
7 | ... on Test {
231-
8 | cc
232-
> 9 | bb
233-
| ^ "bb" should be before "cc".
234-
10 | aa
231+
8 | ccc
232+
> 9 | bbb
233+
| ^^^ "bbb" should be before "ccc"
234+
10 | aaa
235235
11 | }
236236
12 | }
237237
13 | }
@@ -242,14 +242,14 @@ exports[` 21`] = `
242242
1 |
243243
2 | query {
244244
3 | test {
245-
4 | c
246-
5 | b
247-
6 | a
245+
4 | cc
246+
5 | bb
247+
6 | aa
248248
7 | ... on Test {
249-
8 | cc
250-
9 | bb
251-
> 10 | aa
252-
| ^ "aa" should be before "bb".
249+
8 | ccc
250+
9 | bbb
251+
> 10 | aaa
252+
| ^^^ "aaa" should be before "bbb"
253253
11 | }
254254
12 | }
255255
13 | }
@@ -258,9 +258,9 @@ exports[` 21`] = `
258258

259259
exports[` 22`] = `
260260
1 |
261-
> 2 | mutation ($c: Int, $b: Int, $a: Int) {
262-
| ^^ "$b" should be before "$c".
263-
3 | test(cc: $c, bb: $b, aa: $a) {
261+
> 2 | mutation ($cc: Int, $bb: Int, $aa: Int) {
262+
| ^^^ "$bb" should be before "$cc"
263+
3 | test(ccc: $cc, bbb: $bb, aaa: $aa) {
264264
4 | something
265265
5 | }
266266
6 | }
@@ -269,9 +269,9 @@ exports[` 22`] = `
269269

270270
exports[` 23`] = `
271271
1 |
272-
> 2 | mutation ($c: Int, $b: Int, $a: Int) {
273-
| ^^ "$a" should be before "$b".
274-
3 | test(cc: $c, bb: $b, aa: $a) {
272+
> 2 | mutation ($cc: Int, $bb: Int, $aa: Int) {
273+
| ^^^ "$aa" should be before "$bb"
274+
3 | test(ccc: $cc, bbb: $bb, aaa: $aa) {
275275
4 | something
276276
5 | }
277277
6 | }
@@ -280,9 +280,9 @@ exports[` 23`] = `
280280

281281
exports[` 24`] = `
282282
1 |
283-
2 | mutation ($c: Int, $b: Int, $a: Int) {
284-
> 3 | test(cc: $c, bb: $b, aa: $a) {
285-
| ^ "bb" should be before "cc".
283+
2 | mutation ($cc: Int, $bb: Int, $aa: Int) {
284+
> 3 | test(ccc: $cc, bbb: $bb, aaa: $aa) {
285+
| ^^^ "bbb" should be before "ccc"
286286
4 | something
287287
5 | }
288288
6 | }
@@ -291,9 +291,9 @@ exports[` 24`] = `
291291

292292
exports[` 25`] = `
293293
1 |
294-
2 | mutation ($c: Int, $b: Int, $a: Int) {
295-
> 3 | test(cc: $c, bb: $b, aa: $a) {
296-
| ^ "aa" should be before "bb".
294+
2 | mutation ($cc: Int, $bb: Int, $aa: Int) {
295+
> 3 | test(ccc: $cc, bbb: $bb, aaa: $aa) {
296+
| ^^^ "aaa" should be before "bbb"
297297
4 | something
298298
5 | }
299299
6 | }

0 commit comments

Comments
 (0)