Skip to content

Commit

Permalink
fix: ignore operations and fragments in no-hashtag-description rule (
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Dec 8, 2021
1 parent 37c1579 commit 988e445
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-roses-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix: ignore operations and fragments in `no-hashtag-description` rule
10 changes: 5 additions & 5 deletions packages/plugin/src/rules/no-hashtag-description.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TokenKind } from 'graphql';
import { Kind, TokenKind } from 'graphql';
import { GraphQLESLintRule } from '../types';
import { getLocation } from '../utils';

Expand All @@ -7,8 +7,7 @@ const HASHTAG_COMMENT = 'HASHTAG_COMMENT';
const rule: GraphQLESLintRule = {
meta: {
messages: {
[HASHTAG_COMMENT]:
'Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.',
[HASHTAG_COMMENT]: `Using hashtag (#) for adding GraphQL descriptions is not allowed. Prefer using """ for multiline, or " for a single line description.`,
},
docs: {
description:
Expand Down Expand Up @@ -56,16 +55,17 @@ const rule: GraphQLESLintRule = {
schema: [],
},
create(context) {
const selector = `${Kind.DOCUMENT}[definitions.0.kind!=/^(${Kind.OPERATION_DEFINITION}|${Kind.FRAGMENT_DEFINITION})$/]`;
return {
Document(node) {
[selector](node) {
const rawNode = node.rawNode();
let token = rawNode.loc.startToken;

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

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

if (!isEslintComment && line !== prev.line && next.kind === TokenKind.NAME && linesAfter < 2) {
Expand Down
24 changes: 24 additions & 0 deletions packages/plugin/tests/no-hashtag-description.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ ruleTester.runGraphQLTests('no-hashtag-description', rule, {
): User
}
`,
/* GraphQL */ `
# ok
query {
test
}
`,
/* GraphQL */ `
# ok
mutation {
test
}
`,
/* GraphQL */ `
# ok
subscription {
test
}
`,
/* GraphQL */ `
# ok
fragment UserFields on User {
id
}
`,
],
invalid: [
{
Expand Down

0 comments on commit 988e445

Please sign in to comment.