Skip to content

Commit

Permalink
feat(vue): add a mixed vue/graphql processor
Browse files Browse the repository at this point in the history
  • Loading branch information
mat813 committed May 26, 2023
1 parent 0001377 commit 0001378
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/vue-code-file/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
{
files: ['*.js', '*.vue'],
parser: 'vue-eslint-parser',
processor: '@graphql-eslint/graphql',
processor: '@graphql-eslint/graphql-vue',
extends: ['eslint:recommended','plugin:vue/base'],
env: {
es6: true,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"graphql"
],
"peerDependencies": {
"eslint-plugin-vue": "9.14.1",
"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"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { processor } from './processor.js';
import { processor, processorWithVue } from './processor.js';

export { parseForESLint } from './parser.js';
export { rules } from './rules/index.js';
export * from './testkit.js';
export * from './types.js';
export { requireGraphQLSchemaFromContext, requireSiblingsOperations } from './utils.js';

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

export { configs } from './configs/index.js';
export { flatConfigs } from './flat-configs.js';
34 changes: 34 additions & 0 deletions packages/plugin/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,37 @@ export const processor: Linter.Processor<Block | string> = {
return result.sort((a, b) => a.line - b.line || a.column - b.column);
},
};

let vueProcessors: Record<string, Linter.Processor<Block | string>>;

export const processorWithVue: Linter.Processor<Block | string> = {
// Get supportsAutofix and preprocess from the normal processor
...processor,

postprocess(messages, filePath) {
if (messages.length > 0) {
// try to load the vue plugin
if (vueProcessors === undefined) {
try {
vueProcessors = require('eslint-plugin-vue').processors;

Check failure on line 139 in packages/plugin/src/processor.ts

View workflow job for this annotation

GitHub Actions / Vitest / node v16 / graphql v16 / eslint v8

Require statement not part of import statement

Check failure on line 139 in packages/plugin/src/processor.ts

View workflow job for this annotation

GitHub Actions / Vitest / node v18 / graphql v15 / eslint v8

Require statement not part of import statement

Check failure on line 139 in packages/plugin/src/processor.ts

View workflow job for this annotation

GitHub Actions / Vitest / node v18 / graphql v16 / eslint v8

Require statement not part of import statement
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
vueProcessors = {};
}
}

// If we have a vue processor, pass it the last element that contains the
// full SFC code for processing
if (vueProcessors['.vue']?.postprocess !== undefined) {
const last = messages.length - 1;
messages[last] = vueProcessors['.vue'].postprocess([messages[last]], filePath);
}

// And pass everything to the graphql processor
return processor.postprocess!(messages, filePath);
} else {

Check failure on line 156 in packages/plugin/src/processor.ts

View workflow job for this annotation

GitHub Actions / Vitest / node v16 / graphql v16 / eslint v8

Unnecessary 'else' after 'return'

Check failure on line 156 in packages/plugin/src/processor.ts

View workflow job for this annotation

GitHub Actions / Vitest / node v18 / graphql v15 / eslint v8

Unnecessary 'else' after 'return'

Check failure on line 156 in packages/plugin/src/processor.ts

View workflow job for this annotation

GitHub Actions / Vitest / node v18 / graphql v16 / eslint v8

Unnecessary 'else' after 'return'
return messages.flat();
}
},
};

0 comments on commit 0001378

Please sign in to comment.