Skip to content

Commit 8af1d65

Browse files
committed
allow to run on code files
1 parent 3ee2e6a commit 8af1d65

File tree

11 files changed

+200
-32
lines changed

11 files changed

+200
-32
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"eslint.enable": true
3+
}

example/.eslintrc.json

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
{
2-
"parser": "@graphql-eslint/parser",
3-
"parserOptions": {
4-
"schemaPath": "./**/schema.graphql"
5-
},
62
"plugins": ["@graphql-eslint"],
3+
"processor": "@graphql-eslint/graphql",
74
"rules": {
8-
"@graphql-eslint/require-id-when-available": ["error", {
9-
"fieldName": "_id"
10-
}],
11-
"@graphql-eslint/validate-against-schema": "error",
12-
"@graphql-eslint/no-anonymous-operations": "warn",
13-
"@graphql-eslint/no-operation-name-suffix": "error",
14-
"@graphql-eslint/deprecation-must-have-reason": "error",
15-
"@graphql-eslint/avoid-operation-name-prefix": ["error", {
16-
"keywords": ["get"]
17-
}],
18-
"@graphql-eslint/no-case-insensitive-enum-values-duplicates": [
19-
"error"
20-
],
21-
"@graphql-eslint/require-description": ["warn", {
22-
"on": ["SchemaDefinition", "FieldDefinition"]
23-
}]
24-
}
25-
}
5+
"@graphql-eslint/no-anonymous-operations": "warn"
6+
},
7+
"overrides": [
8+
{
9+
"files": ["./**/*.graphql"],
10+
"parser": "@graphql-eslint/parser",
11+
"parserOptions": {
12+
"schemaPath": "./**/schema.graphql"
13+
},
14+
"plugins": ["@graphql-eslint"],
15+
"rules": {
16+
"@graphql-eslint/require-id-when-available": [
17+
"error",
18+
{
19+
"fieldName": "_id"
20+
}
21+
],
22+
"@graphql-eslint/validate-against-schema": "error",
23+
"@graphql-eslint/no-anonymous-operations": "warn",
24+
"@graphql-eslint/no-operation-name-suffix": "error",
25+
"@graphql-eslint/deprecation-must-have-reason": "error",
26+
"@graphql-eslint/avoid-operation-name-prefix": [
27+
"error",
28+
{
29+
"keywords": ["get"]
30+
}
31+
],
32+
"@graphql-eslint/no-case-insensitive-enum-values-duplicates": ["error"],
33+
"@graphql-eslint/require-description": [
34+
"warn",
35+
{
36+
"on": ["SchemaDefinition", "FieldDefinition"]
37+
}
38+
]
39+
}
40+
}
41+
]
42+
}

example/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"author": "Dotan Simha <[email protected]>",
77
"license": "MIT",
88
"scripts": {
9-
"lint": "eslint --ext graphql ."
9+
"lint": "eslint --ext graphql,js ."
1010
},
1111
"dependencies": {
1212
"eslint": "7.8.1",
1313
"@graphql-eslint/parser": "0.0.1",
14-
"@graphql-eslint/eslint-plugin": "0.0.1",
14+
"@graphql-eslint/eslint-plugin": "0.0.1",
1515
"graphql": "15.3.0",
1616
"typescript": "4.0.2"
1717
}
18-
}
18+
}

example/query.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
query test {
1+
query {
22
user {
33
id
44
name

example/query.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const myQuery = gql`
2+
query {
3+
user {
4+
id
5+
name
6+
... on User {
7+
name
8+
}
9+
}
10+
}
11+
`;
12+
13+
const myOtherQuery = gql`
14+
query a {
15+
user {
16+
name
17+
}
18+
}
19+
`;

packages/plugin/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"prepack": "bob prepack"
1717
},
1818
"dependencies": {
19-
"@graphql-eslint/types": "0.0.1"
19+
"@graphql-eslint/types": "0.0.1",
20+
"@graphql-tools/graphql-tag-pluck": "6.2.5"
2021
},
2122
"devDependencies": {
2223
"@types/eslint": "7.2.2",
@@ -33,4 +34,4 @@
3334
"publishConfig": {
3435
"access": "public"
3536
}
36-
}
37+
}

packages/plugin/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { rules } from "./rules";
2+
export { processors } from "./processors";
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { extname } from "path";
2+
import { parseCode } from "@graphql-tools/graphql-tag-pluck";
3+
4+
const EXTRACTABLE_FILES_EXTENSIONS = [".js", ".jsx", ".ts", ".tsx"];
5+
const RELEVANT_KEYWORDS = ["gql", "graphql", "/* GraphQL */"];
6+
7+
type Block = {
8+
text: string;
9+
filename: string;
10+
lineOffset?: number;
11+
};
12+
13+
export const createGraphqlProcessor = () => {
14+
const blocksMap = new Map<string, Block[]>();
15+
16+
return {
17+
preprocess: (
18+
text: string,
19+
filename: string
20+
): Array<{ text: string; filename: string }> => {
21+
const blocks: Block[] = [];
22+
blocksMap.set(filename, blocks);
23+
24+
if (
25+
filename &&
26+
text &&
27+
EXTRACTABLE_FILES_EXTENSIONS.includes(extname(filename)) &&
28+
RELEVANT_KEYWORDS.some((keyword) => text.includes(keyword))
29+
) {
30+
const extractedDocuments = parseCode({
31+
code: text,
32+
filePath: filename,
33+
options: {
34+
globalGqlIdentifierName: ["gql", "graphql"],
35+
skipIndent: true,
36+
},
37+
});
38+
39+
if (extractedDocuments && extractedDocuments.length > 0) {
40+
for (const item of extractedDocuments) {
41+
blocks.push({
42+
filename: `document.graphql`,
43+
text: item.content,
44+
lineOffset: item.loc.start.line - 1,
45+
});
46+
}
47+
48+
return blocks;
49+
}
50+
}
51+
52+
return [{ text, filename }];
53+
},
54+
postprocess: (messageLists: any[], filename: string): any[] => {
55+
const blocks = blocksMap.get(filename);
56+
57+
if (blocks && blocks.length > 0) {
58+
for (let i = 0; i < messageLists.length; ++i) {
59+
const messages = messageLists[i];
60+
const { lineOffset } = blocks[i];
61+
62+
for (const message of messages) {
63+
message.line += lineOffset;
64+
65+
if (message.endLine != null) {
66+
message.endLine += lineOffset;
67+
}
68+
}
69+
}
70+
}
71+
72+
return [].concat(...messageLists);
73+
},
74+
};
75+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createGraphqlProcessor } from "./code-files";
2+
3+
export const processors = {
4+
graphql: createGraphqlProcessor(),
5+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function verifyRule(
4040
},
4141
end: {
4242
line: node.loc.end.line,
43-
column: node.loc.end.column - 1,
43+
column: node.loc.end.column,
4444
},
4545
},
4646
messageId: REQUIRE_DESCRIPTION_ERROR,

0 commit comments

Comments
 (0)