From 2c43e68affa24459ae9dc53f8e3c2336e7fe82fb Mon Sep 17 00:00:00 2001 From: zoy-l <409626581@qq.com> Date: Tue, 16 Mar 2021 09:46:50 +0800 Subject: [PATCH] fix: eslint .tsx file failed (#8358) --- packages/vant-eslint-config/index.js | 6 ++- packages/vant-eslint-config/test/.eslintrc | 4 ++ .../vant-eslint-config/test/index.spec.js | 38 +++++++++++++++++++ .../vant-eslint-config/test/vue-tsx/app.tsx | 12 ++++++ .../vant-eslint-config/test/vue-tsx/index.tsx | 23 +++++++++++ .../vant-eslint-config/test/vue-tsx/main.ts | 1 + .../test/vue-tsx/tsconfig.json | 18 +++++++++ .../vant-eslint-config/test/vue/index.vue | 19 ++++++++++ packages/vant-eslint-config/test/vue/main.js | 2 + 9 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 packages/vant-eslint-config/test/.eslintrc create mode 100644 packages/vant-eslint-config/test/index.spec.js create mode 100644 packages/vant-eslint-config/test/vue-tsx/app.tsx create mode 100644 packages/vant-eslint-config/test/vue-tsx/index.tsx create mode 100644 packages/vant-eslint-config/test/vue-tsx/main.ts create mode 100644 packages/vant-eslint-config/test/vue-tsx/tsconfig.json create mode 100644 packages/vant-eslint-config/test/vue/index.vue create mode 100644 packages/vant-eslint-config/test/vue/main.js diff --git a/packages/vant-eslint-config/index.js b/packages/vant-eslint-config/index.js index 2b0f404be..9cf0701fc 100644 --- a/packages/vant-eslint-config/index.js +++ b/packages/vant-eslint-config/index.js @@ -1,8 +1,8 @@ module.exports = { extends: [ + 'plugin:vue/vue3-recommended', 'airbnb-base', 'plugin:@typescript-eslint/recommended', - 'plugin:vue/vue3-recommended', 'prettier', 'prettier/vue', 'prettier/@typescript-eslint', @@ -67,6 +67,10 @@ module.exports = { }, overrides: [ + { + files: ['*.vue'], + parser: require.resolve('vue-eslint-parser') + }, { files: ['**/*.md/*.js', '**/*.md/*.ts'], rules: { diff --git a/packages/vant-eslint-config/test/.eslintrc b/packages/vant-eslint-config/test/.eslintrc new file mode 100644 index 000000000..8d53de665 --- /dev/null +++ b/packages/vant-eslint-config/test/.eslintrc @@ -0,0 +1,4 @@ +{ + "root": true, + "extends": ["../index"] +} diff --git a/packages/vant-eslint-config/test/index.spec.js b/packages/vant-eslint-config/test/index.spec.js new file mode 100644 index 000000000..ca2082f13 --- /dev/null +++ b/packages/vant-eslint-config/test/index.spec.js @@ -0,0 +1,38 @@ +const { ESLint } = require('eslint'); +const path = require('path'); + +const eslint = new ESLint(); + +async function lintProject(name) { + const projectPath = path.resolve(__dirname, name); + const filesToLint = path.resolve(projectPath, '**'); + const rest = await eslint.lintFiles(filesToLint); + const ruleId = []; + rest.forEach((res) => + res.messages.forEach((msg) => { + if (ruleId.indexOf(msg.ruleId) < 0) { + ruleId.push(msg.ruleId); + } + }) + ); + return ruleId; +} + +test('a vue project should pass lint', async () => { + const rest = await lintProject('vue'); + expect([ + 'no-const-assign', + '@typescript-eslint/no-unused-vars', + 'no-undef', + ]).toEqual(rest); +}); + +test('a vue-tsx project should pass lint', async () => { + const rest = await lintProject('vue-tsx'); + + expect([ + '@typescript-eslint/no-unused-vars', + 'vue/no-ref-as-operand', + '@typescript-eslint/no-empty-interface', + ]).toEqual(rest); +}); diff --git a/packages/vant-eslint-config/test/vue-tsx/app.tsx b/packages/vant-eslint-config/test/vue-tsx/app.tsx new file mode 100644 index 000000000..caae23bed --- /dev/null +++ b/packages/vant-eslint-config/test/vue-tsx/app.tsx @@ -0,0 +1,12 @@ +import { defineComponent } from 'vue' + +export default defineComponent({ + name: 'App', + setup() { + return () => ( + <> +