Skip to content

Commit 988e445

Browse files
authored
fix: ignore operations and fragments in no-hashtag-description rule (#818)
1 parent 37c1579 commit 988e445

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

.changeset/young-roses-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
fix: ignore operations and fragments in `no-hashtag-description` rule

packages/plugin/src/rules/no-hashtag-description.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TokenKind } from 'graphql';
1+
import { Kind, TokenKind } from 'graphql';
22
import { GraphQLESLintRule } from '../types';
33
import { getLocation } from '../utils';
44

@@ -7,8 +7,7 @@ const HASHTAG_COMMENT = 'HASHTAG_COMMENT';
77
const rule: GraphQLESLintRule = {
88
meta: {
99
messages: {
10-
[HASHTAG_COMMENT]:
11-
'Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.',
10+
[HASHTAG_COMMENT]: `Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.`,
1211
},
1312
docs: {
1413
description:
@@ -56,16 +55,17 @@ const rule: GraphQLESLintRule = {
5655
schema: [],
5756
},
5857
create(context) {
58+
const selector = `${Kind.DOCUMENT}[definitions.0.kind!=/^(${Kind.OPERATION_DEFINITION}|${Kind.FRAGMENT_DEFINITION})$/]`;
5959
return {
60-
Document(node) {
60+
[selector](node) {
6161
const rawNode = node.rawNode();
6262
let token = rawNode.loc.startToken;
6363

6464
while (token !== null) {
6565
const { kind, prev, next, value, line, column } = token;
6666

6767
if (kind === TokenKind.COMMENT && prev && next) {
68-
const isEslintComment = value.trimLeft().startsWith('eslint');
68+
const isEslintComment = value.trimStart().startsWith('eslint');
6969
const linesAfter = next.line - line;
7070

7171
if (!isEslintComment && line !== prev.line && next.kind === TokenKind.NAME && linesAfter < 2) {

packages/plugin/tests/no-hashtag-description.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ ruleTester.runGraphQLTests('no-hashtag-description', rule, {
7070
): User
7171
}
7272
`,
73+
/* GraphQL */ `
74+
# ok
75+
query {
76+
test
77+
}
78+
`,
79+
/* GraphQL */ `
80+
# ok
81+
mutation {
82+
test
83+
}
84+
`,
85+
/* GraphQL */ `
86+
# ok
87+
subscription {
88+
test
89+
}
90+
`,
91+
/* GraphQL */ `
92+
# ok
93+
fragment UserFields on User {
94+
id
95+
}
96+
`,
7397
],
7498
invalid: [
7599
{

0 commit comments

Comments
 (0)