Skip to content

Commit 2efc772

Browse files
authored
fix: processor should always return code string in preprocess method (#1023)
1 parent f2209a3 commit 2efc772

File tree

11 files changed

+33
-15
lines changed

11 files changed

+33
-15
lines changed

.changeset/seven-dogs-press.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: processor should always return code string in preprocess method

docs/custom-rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const rule = {
118118
}
119119
```
120120

121-
The structure of the return value of `.typeInfo()` is [defined here](https://github.com/B2o5T/graphql-eslint/blob/master/packages/plugin/src/estree-parser/converter.ts#L32-L40). So based on the `node` you are using, you'll get a different values on `.typeInfo()` result.
121+
The structure of the return value of `.typeInfo()` is [defined here](https://github.com/B2o5T/graphql-eslint/blob/master/packages/plugin/src/estree-converter/converter.ts#L32-L40). So based on the `node` you are using, you'll get a different values on `.typeInfo()` result.
122122

123123
## Testing your rules
124124

docs/rules/relay-page-info.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Set of rules to follow Relay specification for `PageInfo` object.
2121
type PageInfo {
2222
hasPreviousPage: Boolean!
2323
hasNextPage: Boolean!
24-
startCursor: String!
25-
endCursor: String!
24+
startCursor: String
25+
endCursor: String
2626
}
2727
```
2828

examples/code-file/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module.exports = {
99
node: true,
1010
es6: true,
1111
},
12+
rules: {
13+
'no-console': 'error',
14+
},
1215
},
1316
{
1417
files: ['*.graphql'],

examples/code-file/not-query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('should report `no-console` error')

packages/plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"chalk": "4.1.2",
4242
"debug": "4.3.4",
4343
"fast-glob": "3.2.11",
44-
"graphql-config": "^4.2.0",
44+
"graphql-config": "^4.3.0",
4545
"graphql-depth-limit": "1.1.0",
4646
"lodash.lowercase": "4.3.0"
4747
},

packages/plugin/src/processor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const processor: Linter.Processor<Block | string> = {
1313
supportsAutofix: true,
1414
preprocess(code, filePath) {
1515
if (RELEVANT_KEYWORDS.every(keyword => !code.includes(keyword))) {
16-
return [];
16+
return [code];
1717
}
1818
const extractedDocuments = parseCode({
1919
code,
@@ -32,11 +32,11 @@ export const processor: Linter.Processor<Block | string> = {
3232
}));
3333
blocksMap.set(filePath, blocks);
3434

35-
return [...blocks, code /* For eslint-plugin-prettier */];
35+
return [code, ...blocks];
3636
},
3737
postprocess(messages, filePath) {
38-
const blocks = blocksMap.get(filePath);
39-
for (let i = 0; i < blocks?.length; i += 1) {
38+
const blocks = blocksMap.get(filePath) || [];
39+
for (let i = 0; i < blocks.length; i += 1) {
4040
const { lineOffset, offset } = blocks[i];
4141

4242
for (const message of messages[i]) {

packages/plugin/src/rules/relay-page-info.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const rule: GraphQLESLintRule = {
3131
type PageInfo {
3232
hasPreviousPage: Boolean!
3333
hasNextPage: Boolean!
34-
startCursor: String!
35-
endCursor: String!
34+
startCursor: String
35+
endCursor: String
3636
}
3737
`,
3838
},

packages/plugin/tests/__snapshots__/examples.spec.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ Array [
5353

5454
exports[`Examples should work on \`.js\` files 1`] = `
5555
Array [
56+
Object {
57+
filePath: examples/code-file/not-query.js,
58+
messages: Array [
59+
Object {
60+
message: Unexpected console statement.,
61+
ruleId: no-console,
62+
},
63+
],
64+
},
5665
Object {
5766
filePath: examples/code-file/query.js,
5867
messages: Array [

packages/plugin/tests/examples.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Examples', () => {
4848
it('should work on `.js` files', () => {
4949
const cwd = join(process.cwd(), 'examples/code-file');
5050
const results = getEslintOutput(cwd);
51-
expect(countErrors(results)).toBe(2);
51+
expect(countErrors(results)).toBe(3);
5252
testSnapshot(results);
5353
});
5454

0 commit comments

Comments
 (0)