Skip to content

Commit

Permalink
fix error report for alphabetize rule (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV committed Oct 31, 2021
1 parent 46f03f7 commit ebab010
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 120 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-rockets-cheat.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix error report for `alphabetize` rule
12 changes: 3 additions & 9 deletions packages/plugin/src/rules/alphabetize.ts
Expand Up @@ -23,6 +23,7 @@ import {
import { GraphQLESLintRule } from '../types';
import { GraphQLESTreeNode } from '../estree-parser';
import { GraphQLESLintRuleListener } from '../testkit';
import { getLocation } from '../utils';

const ALPHABETIZE = 'ALPHABETIZE';

Expand Down Expand Up @@ -137,7 +138,7 @@ const rule: GraphQLESLintRule<AlphabetizeConfig> = {
],
},
messages: {
[ALPHABETIZE]: '"{{ currName }}" should be before "{{ prevName }}".',
[ALPHABETIZE]: '"{{ currName }}" should be before "{{ prevName }}"',
},
schema: {
type: 'array',
Expand Down Expand Up @@ -203,17 +204,10 @@ const rule: GraphQLESLintRule<AlphabetizeConfig> = {
for (const node of nodes) {
const currName = node.name.value;
if (prevName && prevName > currName) {
const { start, end } = node.name.loc;
const isVariableNode = node.kind === Kind.VARIABLE;

context.report({
loc: {
start: {
line: start.line,
column: start.column - (isVariableNode ? 2 : 1),
},
end,
},
loc: getLocation(node.loc, node.name.value, { offsetEnd: isVariableNode ? 0 : 1 }),
messageId: ALPHABETIZE,
data: isVariableNode
? {
Expand Down
22 changes: 21 additions & 1 deletion packages/plugin/src/testkit.ts
Expand Up @@ -11,6 +11,7 @@ export type GraphQLESLintRuleListener<WithTypeInfo extends boolean = false> = {
} & Record<string, any>;

export type GraphQLValidTestCase<Options> = Omit<RuleTester.ValidTestCase, 'options' | 'parserOptions'> & {
name: string;
options?: Options;
parserOptions?: ParserOptions;
};
Expand Down Expand Up @@ -50,7 +51,26 @@ export class GraphQLRuleTester extends RuleTester {
invalid: GraphQLInvalidTestCase<Config>[];
}
): void {
super.run(name, rule as Rule.RuleModule, tests);
const ruleTests = Linter.version.startsWith('8')
? tests
: {
valid: tests.valid.map(test => {
if (typeof test === 'string') {
return test;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { name, ...testCaseOptions } = test;
return testCaseOptions;
}),
invalid: tests.invalid.map(test => {
// ESLint 7 throws an error on CI - Unexpected top-level property "name"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { name, ...testCaseOptions } = test;
return testCaseOptions;
}),
};

super.run(name, rule as Rule.RuleModule, ruleTests);

// Skip snapshot testing if `expect` variable is not defined
if (typeof expect === 'undefined') {
Expand Down
146 changes: 73 additions & 73 deletions packages/plugin/tests/__snapshots__/alphabetize.spec.ts.snap
Expand Up @@ -5,7 +5,7 @@ exports[` 1`] = `
2 | type User {
3 | password: String
> 4 | firstName: String!
| ^ "firstName" should be before "password".
| ^^^^^^^^^ "firstName" should be before "password"
5 | age: Int
6 | lastName: String!
7 | }
Expand All @@ -18,7 +18,7 @@ exports[` 2`] = `
3 | password: String
4 | firstName: String!
> 5 | age: Int
| ^ "age" should be before "firstName".
| ^^^ "age" should be before "firstName"
6 | lastName: String!
7 | }
8 |
Expand All @@ -31,29 +31,29 @@ exports[` 3`] = `
4 | firstName: String!
5 | password: String
> 6 | lastName: String!
| ^ "lastName" should be before "password".
| ^^^^^^^^ "lastName" should be before "password"
7 | }
8 |
`;

exports[` 4`] = `
1 |
2 | interface Test {
3 | c: Int
> 4 | b: Int
| ^ "b" should be before "c".
5 | a: Int
3 | cc: Int
> 4 | bb: Int
| ^^ "bb" should be before "cc"
5 | aa: Int
6 | }
7 |
`;

exports[` 5`] = `
1 |
2 | interface Test {
3 | c: Int
4 | b: Int
> 5 | a: Int
| ^ "a" should be before "b".
3 | cc: Int
4 | bb: Int
> 5 | aa: Int
| ^^ "aa" should be before "bb"
6 | }
7 |
`;
Expand All @@ -63,7 +63,7 @@ exports[` 6`] = `
2 | input UserInput {
3 | password: String
> 4 | firstName: String!
| ^ "firstName" should be before "password".
| ^^^^^^^^^ "firstName" should be before "password"
5 | age: Int
6 | lastName: String!
7 | }
Expand All @@ -76,7 +76,7 @@ exports[` 7`] = `
3 | password: String
4 | firstName: String!
> 5 | age: Int
| ^ "age" should be before "firstName".
| ^^^ "age" should be before "firstName"
6 | lastName: String!
7 | }
8 |
Expand All @@ -89,7 +89,7 @@ exports[` 8`] = `
4 | firstName: String!
5 | password: String
> 6 | lastName: String!
| ^ "lastName" should be before "password".
| ^^^^^^^^ "lastName" should be before "password"
7 | }
8 |
`;
Expand All @@ -99,7 +99,7 @@ exports[` 9`] = `
2 | enum Role {
3 | SUPER_ADMIN
> 4 | ADMIN
| ^ "ADMIN" should be before "SUPER_ADMIN".
| ^^^^^ "ADMIN" should be before "SUPER_ADMIN"
5 | USER
6 | GOD
7 | }
Expand All @@ -113,7 +113,7 @@ exports[` 10`] = `
4 | ADMIN
5 | USER
> 6 | GOD
| ^ "GOD" should be before "USER".
| ^^^ "GOD" should be before "USER"
7 | }
8 |
`;
Expand All @@ -124,62 +124,62 @@ exports[` 11`] = `
3 | ADMIN
4 | SUPER_ADMIN
> 5 | GOD
| ^ "GOD" should be before "SUPER_ADMIN".
| ^^^ "GOD" should be before "SUPER_ADMIN"
6 | USER
7 | }
8 |
`;

exports[` 12`] = `
1 |
> 2 | directive @test(c: Int, b: Int, a: Int) on FIELD_DEFINITION
| ^ "b" should be before "c".
> 2 | directive @test(cc: Int, bb: Int, aa: Int) on FIELD_DEFINITION
| ^^ "bb" should be before "cc"
3 |
`;

exports[` 13`] = `
1 |
> 2 | directive @test(c: Int, b: Int, a: Int) on FIELD_DEFINITION
| ^ "a" should be before "b".
> 2 | directive @test(cc: Int, bb: Int, aa: Int) on FIELD_DEFINITION
| ^^ "aa" should be before "bb"
3 |
`;

exports[` 14`] = `
1 |
2 | type Query {
> 3 | test(c: Int, b: Int, a: Int): Int
| ^ "b" should be before "c".
> 3 | test(cc: Int, bb: Int, aa: Int): Int
| ^^ "bb" should be before "cc"
4 | }
5 |
`;

exports[` 15`] = `
1 |
2 | type Query {
> 3 | test(c: Int, b: Int, a: Int): Int
| ^ "a" should be before "b".
> 3 | test(cc: Int, bb: Int, aa: Int): Int
| ^^ "aa" should be before "bb"
4 | }
5 |
`;

exports[` 16`] = `
1 |
2 | fragment TestFields on Test {
3 | c
> 4 | b
| ^ "b" should be before "c".
5 | a
3 | cc
> 4 | bb
| ^^ "bb" should be before "cc"
5 | aa
6 | }
7 |
`;

exports[` 17`] = `
1 |
2 | fragment TestFields on Test {
3 | c
4 | b
> 5 | a
| ^ "a" should be before "b".
3 | cc
4 | bb
> 5 | aa
| ^^ "aa" should be before "bb"
6 | }
7 |
`;
Expand All @@ -188,14 +188,14 @@ exports[` 18`] = `
1 |
2 | query {
3 | test {
4 | c
> 5 | b
| ^ "b" should be before "c".
6 | a
4 | cc
> 5 | bb
| ^^ "bb" should be before "cc"
6 | aa
7 | ... on Test {
8 | cc
9 | bb
10 | aa
8 | ccc
9 | bbb
10 | aaa
11 | }
12 | }
13 | }
Expand All @@ -206,14 +206,14 @@ exports[` 19`] = `
1 |
2 | query {
3 | test {
4 | c
5 | b
> 6 | a
| ^ "a" should be before "b".
4 | cc
5 | bb
> 6 | aa
| ^^ "aa" should be before "bb"
7 | ... on Test {
8 | cc
9 | bb
10 | aa
8 | ccc
9 | bbb
10 | aaa
11 | }
12 | }
13 | }
Expand All @@ -224,14 +224,14 @@ exports[` 20`] = `
1 |
2 | query {
3 | test {
4 | c
5 | b
6 | a
4 | cc
5 | bb
6 | aa
7 | ... on Test {
8 | cc
> 9 | bb
| ^ "bb" should be before "cc".
10 | aa
8 | ccc
> 9 | bbb
| ^^^ "bbb" should be before "ccc"
10 | aaa
11 | }
12 | }
13 | }
Expand All @@ -242,14 +242,14 @@ exports[` 21`] = `
1 |
2 | query {
3 | test {
4 | c
5 | b
6 | a
4 | cc
5 | bb
6 | aa
7 | ... on Test {
8 | cc
9 | bb
> 10 | aa
| ^ "aa" should be before "bb".
8 | ccc
9 | bbb
> 10 | aaa
| ^^^ "aaa" should be before "bbb"
11 | }
12 | }
13 | }
Expand All @@ -258,9 +258,9 @@ exports[` 21`] = `

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

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

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

exports[` 25`] = `
1 |
2 | mutation ($c: Int, $b: Int, $a: Int) {
> 3 | test(cc: $c, bb: $b, aa: $a) {
| ^ "aa" should be before "bb".
2 | mutation ($cc: Int, $bb: Int, $aa: Int) {
> 3 | test(ccc: $cc, bbb: $bb, aaa: $aa) {
| ^^^ "aaa" should be before "bbb"
4 | something
5 | }
6 | }
Expand Down

0 comments on commit ebab010

Please sign in to comment.