Skip to content

Commit

Permalink
fix: processor should always return code string in preprocess method (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Apr 6, 2022
1 parent f2209a3 commit 2efc772
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-dogs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix: processor should always return code string in preprocess method
2 changes: 1 addition & 1 deletion docs/custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const rule = {
}
```

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.
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.

## Testing your rules

Expand Down
4 changes: 2 additions & 2 deletions docs/rules/relay-page-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Set of rules to follow Relay specification for `PageInfo` object.
type PageInfo {
hasPreviousPage: Boolean!
hasNextPage: Boolean!
startCursor: String!
endCursor: String!
startCursor: String
endCursor: String
}
```

Expand Down
3 changes: 3 additions & 0 deletions examples/code-file/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
node: true,
es6: true,
},
rules: {
'no-console': 'error',
},
},
{
files: ['*.graphql'],
Expand Down
1 change: 1 addition & 0 deletions examples/code-file/not-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('should report `no-console` error')
2 changes: 1 addition & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"chalk": "4.1.2",
"debug": "4.3.4",
"fast-glob": "3.2.11",
"graphql-config": "^4.2.0",
"graphql-config": "^4.3.0",
"graphql-depth-limit": "1.1.0",
"lodash.lowercase": "4.3.0"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const processor: Linter.Processor<Block | string> = {
supportsAutofix: true,
preprocess(code, filePath) {
if (RELEVANT_KEYWORDS.every(keyword => !code.includes(keyword))) {
return [];
return [code];
}
const extractedDocuments = parseCode({
code,
Expand All @@ -32,11 +32,11 @@ export const processor: Linter.Processor<Block | string> = {
}));
blocksMap.set(filePath, blocks);

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

for (const message of messages[i]) {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/relay-page-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const rule: GraphQLESLintRule = {
type PageInfo {
hasPreviousPage: Boolean!
hasNextPage: Boolean!
startCursor: String!
endCursor: String!
startCursor: String
endCursor: String
}
`,
},
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin/tests/__snapshots__/examples.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ Array [

exports[`Examples should work on \`.js\` files 1`] = `
Array [
Object {
filePath: examples/code-file/not-query.js,
messages: Array [
Object {
message: Unexpected console statement.,
ruleId: no-console,
},
],
},
Object {
filePath: examples/code-file/query.js,
messages: Array [
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/tests/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Examples', () => {
it('should work on `.js` files', () => {
const cwd = join(process.cwd(), 'examples/code-file');
const results = getEslintOutput(cwd);
expect(countErrors(results)).toBe(2);
expect(countErrors(results)).toBe(3);
testSnapshot(results);
});

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3796,10 +3796,10 @@ grapheme-splitter@^1.0.4:
resolved "https://registry.yarnpkg.com/grapheme-splitter/-/grapheme-splitter-1.0.4.tgz#9cf3a665c6247479896834af35cf1dbb4400767e"
integrity sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==

graphql-config@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/graphql-config/-/graphql-config-4.2.0.tgz#031008ccde56eaa2861651d11cf825b78ca0ff0f"
integrity sha512-Qyf02bOfz2jvKc15VQllDS1MQVuywPPYkZ4ChR9ffzNBQk0JX+7ZmfuPwnCkJQQMms56yywU5w1fu9BZVcuUkA==
graphql-config@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/graphql-config/-/graphql-config-4.3.0.tgz#b9bb7bf9c892a90e66ea937e8d7ed170eb1fd5e2"
integrity sha512-Uiu3X7+s5c056WyrvdZVz2vG1fhAipMlYmtiCU/4Z2mX79OXDr1SqIon2MprC/pExIWJfAQZCcjYDY76fPBUQg==
dependencies:
"@endemolshinegroup/cosmiconfig-typescript-loader" "3.0.2"
"@graphql-tools/graphql-file-loader" "^7.3.7"
Expand Down

0 comments on commit 2efc772

Please sign in to comment.