|
1 |
| -import { GraphQLESLintRule } from '../types'; |
2 | 1 | import { TokenKind } from 'graphql';
|
3 |
| -import { checkForEslint } from '../utils'; |
| 2 | +import { GraphQLESLintRule } from '../types'; |
4 | 3 |
|
5 | 4 | const HASHTAG_COMMENT = 'HASHTAG_COMMENT';
|
6 | 5 |
|
7 | 6 | const rule: GraphQLESLintRule = {
|
8 | 7 | meta: {
|
9 | 8 | messages: {
|
10 |
| - [HASHTAG_COMMENT]: `Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.`, |
| 9 | + [HASHTAG_COMMENT]: |
| 10 | + 'Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.', |
11 | 11 | },
|
12 | 12 | docs: {
|
13 | 13 | description:
|
14 | 14 | 'Requires to use `"""` or `"` for adding a GraphQL description instead of `#`.\nAllows to use hashtag for comments, as long as it\'s not attached to an AST definition.',
|
15 | 15 | category: 'Best Practices',
|
16 |
| - url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-hashtag-description.md`, |
| 16 | + url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-hashtag-description.md', |
17 | 17 | examples: [
|
18 | 18 | {
|
19 | 19 | title: 'Incorrect',
|
@@ -55,60 +55,27 @@ const rule: GraphQLESLintRule = {
|
55 | 55 | create(context) {
|
56 | 56 | return {
|
57 | 57 | Document(node) {
|
58 |
| - if (node) { |
59 |
| - const rawNode = node.rawNode(); |
| 58 | + const rawNode = node.rawNode(); |
| 59 | + let token = rawNode.loc.startToken; |
60 | 60 |
|
61 |
| - if (rawNode && rawNode.loc && rawNode.loc.startToken) { |
62 |
| - let token = rawNode.loc.startToken; |
| 61 | + while (token !== null) { |
| 62 | + const { kind, prev, next, value, line, column } = token; |
63 | 63 |
|
64 |
| - while (token !== null) { |
65 |
| - if (token.kind === TokenKind.COMMENT && token.next && token.prev) { |
66 |
| - if ( |
67 |
| - token.prev.kind !== TokenKind.SOF && |
68 |
| - token.prev.kind !== TokenKind.COMMENT && |
69 |
| - token.next.kind !== TokenKind.COMMENT && |
70 |
| - token.next.line - token.line > 1 && |
71 |
| - token.prev.line !== token.line |
72 |
| - ) { |
73 |
| - context.report({ |
74 |
| - messageId: HASHTAG_COMMENT, |
75 |
| - loc: { |
76 |
| - start: { |
77 |
| - line: token.line, |
78 |
| - column: token.column, |
79 |
| - }, |
80 |
| - end: { |
81 |
| - line: token.line, |
82 |
| - column: token.column, |
83 |
| - }, |
84 |
| - }, |
85 |
| - }); |
86 |
| - } else if ( |
87 |
| - token.next.kind !== TokenKind.COMMENT && |
88 |
| - !checkForEslint(token, rawNode) && |
89 |
| - token.next.kind !== TokenKind.EOF && |
90 |
| - token.next.line - token.line < 2 && |
91 |
| - token.prev.line !== token.line |
92 |
| - ) { |
93 |
| - context.report({ |
94 |
| - messageId: HASHTAG_COMMENT, |
95 |
| - loc: { |
96 |
| - start: { |
97 |
| - line: token.line, |
98 |
| - column: token.column, |
99 |
| - }, |
100 |
| - end: { |
101 |
| - line: token.line, |
102 |
| - column: token.column, |
103 |
| - }, |
104 |
| - }, |
105 |
| - }); |
106 |
| - } |
107 |
| - } |
| 64 | + if (kind === TokenKind.COMMENT && prev && next) { |
| 65 | + const isEslintComment = value.trimLeft().startsWith('eslint'); |
| 66 | + const linesAfter = next.line - line; |
108 | 67 |
|
109 |
| - token = token.next; |
| 68 | + if (!isEslintComment && line !== prev.line && next.kind === TokenKind.NAME && linesAfter < 2) { |
| 69 | + context.report({ |
| 70 | + messageId: HASHTAG_COMMENT, |
| 71 | + loc: { |
| 72 | + start: { line, column }, |
| 73 | + end: { line, column }, |
| 74 | + }, |
| 75 | + }); |
110 | 76 | }
|
111 | 77 | }
|
| 78 | + token = next; |
112 | 79 | }
|
113 | 80 | },
|
114 | 81 | };
|
|
0 commit comments