Skip to content

Commit 0206266

Browse files
committed
added support for real eslint comments, based on graphql comments (# sign)
1 parent 36150de commit 0206266

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

example/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
type Query {
2+
// eslint-disable-next-line @graphql-eslint/require-description
23
hello: String
34
}

packages/graphql-estree/src/converter.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
import { convertDescription, GraphQLESTreeNode, SafeGraphQLType, convertLocation, convertRange } from "@graphql-eslint/types";
1+
import { extractCommentsFromAst, convertDescription, GraphQLESTreeNode, SafeGraphQLType, convertLocation, convertRange } from "@graphql-eslint/types";
22
import {
33
ASTNode,
44
TypeNode,
55
TypeInfo,
66
visit,
77
visitWithTypeInfo,
88
} from "graphql";
9+
import { Comment } from "estree";
910

1011
export function convertToESTree<T extends ASTNode>(
1112
node: T,
1213
typeInfo?: TypeInfo
13-
): GraphQLESTreeNode<T> {
14+
): { rootTree: GraphQLESTreeNode<T>, comments: Comment[] } {
15+
const comments: Comment[] = extractCommentsFromAst(node);
16+
console.log(comments);
1417
const visitor = { leave: convertNode(typeInfo) };
15-
return visit(node, typeInfo ? visitWithTypeInfo(typeInfo, visitor) : visitor);
18+
19+
return {
20+
rootTree: visit(node, typeInfo ? visitWithTypeInfo(typeInfo, visitor) : visitor),
21+
comments,
22+
};
1623
}
1724

1825
function hasTypeField<T extends ASTNode>(

packages/parser/src/parser.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,20 @@ export function parseForESLint(
7373
const graphqlAst = parseGraphQLSDL(config.filePath || '', code, {
7474
...config,
7575
noLocation: false,
76-
commentDescriptions: true,
7776
});
7877

79-
const estreeAst = convertToESTree(graphqlAst.document, schema ? new TypeInfo(schema) : null);
78+
const { rootTree, comments } = convertToESTree(graphqlAst.document, schema ? new TypeInfo(schema) : null);
8079

8180
return {
8281
services: parserServices,
8382
parserServices,
8483
ast: {
8584
type: "Program",
86-
body: [estreeAst as any],
85+
body: [rootTree as any],
8786
sourceType: "script",
88-
comments: [],
89-
loc: estreeAst.loc,
90-
range: estreeAst.range,
87+
comments,
88+
loc: rootTree.loc,
89+
range: rootTree.range,
9190
tokens: [],
9291
},
9392
};

packages/plugin/src/rules/require-description.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ type RequireDescriptionRuleConfig = { on: typeof DESCRIBABLE_NODES };
2222

2323
function verifyRule(
2424
context: GraphQLESlintRuleContext<RequireDescriptionRuleConfig>,
25-
node: {
26-
kind: string;
25+
node: GraphQLESTreeNode<ASTNode> & {
2726
readonly description?: GraphQLESTreeNode<StringValueNode>;
2827
}
2928
) {
@@ -34,7 +33,16 @@ function verifyRule(
3433
node.description.value.trim().length === 0
3534
) {
3635
context.report({
37-
node: node as GraphQLESTreeNode<ASTNode>,
36+
loc: {
37+
start: {
38+
line: node.loc.start.line,
39+
column: node.loc.start.column - 1,
40+
},
41+
end: {
42+
line: node.loc.end.line,
43+
column: node.loc.end.column - 1,
44+
}
45+
},
3846
messageId: REQUIRE_DESCRIPTION_ERROR,
3947
data: {
4048
nodeType: node.kind,

packages/types/src/utils.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { GraphQLESlintRuleContext } from "./rule";
2-
import { Kind, GraphQLSchema, Location, ValueNode, StringValueNode, ASTNode, IntValueNode, FloatValueNode, BooleanValueNode, ListValueNode, ObjectValueNode, VariableNode } from 'graphql';
2+
import { Kind, GraphQLSchema, Location, ValueNode, StringValueNode, ASTNode, IntValueNode, FloatValueNode, BooleanValueNode, ListValueNode, ObjectValueNode, VariableNode, TokenKind, Token } from 'graphql';
33
import { SourceLocation, Comment } from "estree";
44
import { GraphQLESTreeNode } from './estree-ast';
5+
import { dedentBlockStringValue } from 'graphql/language/blockString';
56

67
export function requireGraphQLSchemaFromContext(
78
context: GraphQLESlintRuleContext
@@ -62,6 +63,42 @@ export function convertRange(gqlLocation: Location): [number, number] {
6263
return [gqlLocation.start, gqlLocation.end];
6364
}
6465

66+
export function extractCommentsFromAst(node: ASTNode): Comment[] {
67+
const loc = node.loc;
68+
69+
if (!loc) {
70+
return [];
71+
}
72+
73+
const comments: Comment[] = [];
74+
let token = loc.startToken;
75+
76+
while (token !== null) {
77+
if (token.kind === TokenKind.COMMENT) {
78+
const value = String(token.value);
79+
comments.push({
80+
type: "Block",
81+
value: ' ' + value + ' ',
82+
loc: {
83+
start: {
84+
line: token.line,
85+
column: token.column,
86+
},
87+
end: {
88+
line: token.line,
89+
column: token.column,
90+
}
91+
},
92+
range: [token.start, token.end],
93+
});
94+
}
95+
96+
token = token.next;
97+
}
98+
99+
return comments;
100+
}
101+
65102
export function convertLocation(gqlLocation: Location): SourceLocation {
66103
return {
67104
start: {

0 commit comments

Comments
 (0)