Skip to content

Commit 0001378

Browse files
committed
feat(vue): add a mixed vue/graphql processor
1 parent 0001377 commit 0001378

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

examples/vue-code-file/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
{
99
files: ['*.js', '*.vue'],
1010
parser: 'vue-eslint-parser',
11-
processor: '@graphql-eslint/graphql',
11+
processor: '@graphql-eslint/graphql-vue',
1212
extends: ['eslint:recommended','plugin:vue/base'],
1313
env: {
1414
es6: true,

packages/plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"graphql"
3333
],
3434
"peerDependencies": {
35+
"eslint-plugin-vue": "9.14.1",
3536
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
3637
},
3738
"dependencies": {

packages/plugin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { processor } from './processor.js';
1+
import { processor, processorWithVue } from './processor.js';
22

33
export { parseForESLint } from './parser.js';
44
export { rules } from './rules/index.js';
55
export * from './testkit.js';
66
export * from './types.js';
77
export { requireGraphQLSchemaFromContext, requireSiblingsOperations } from './utils.js';
88

9-
export const processors = { graphql: processor };
9+
export const processors = { graphql: processor, 'graphql-vue': processorWithVue };
1010

1111
export { configs } from './configs/index.js';
1212
export { flatConfigs } from './flat-configs.js';

packages/plugin/src/processor.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,37 @@ export const processor: Linter.Processor<Block | string> = {
124124
return result.sort((a, b) => a.line - b.line || a.column - b.column);
125125
},
126126
};
127+
128+
let vueProcessors: Record<string, Linter.Processor<Block | string>>;
129+
130+
export const processorWithVue: Linter.Processor<Block | string> = {
131+
// Get supportsAutofix and preprocess from the normal processor
132+
...processor,
133+
134+
postprocess(messages, filePath) {
135+
if (messages.length > 0) {
136+
// try to load the vue plugin
137+
if (vueProcessors === undefined) {
138+
try {
139+
vueProcessors = require('eslint-plugin-vue').processors;
140+
} catch (error) {
141+
// eslint-disable-next-line no-console
142+
console.error(error);
143+
vueProcessors = {};
144+
}
145+
}
146+
147+
// If we have a vue processor, pass it the last element that contains the
148+
// full SFC code for processing
149+
if (vueProcessors['.vue']?.postprocess !== undefined) {
150+
const last = messages.length - 1;
151+
messages[last] = vueProcessors['.vue'].postprocess([messages[last]], filePath);
152+
}
153+
154+
// And pass everything to the graphql processor
155+
return processor.postprocess!(messages, filePath);
156+
} else {
157+
return messages.flat();
158+
}
159+
},
160+
};

0 commit comments

Comments
 (0)