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 () => ( + <> +

App

+ + ) + } +}) diff --git a/packages/vant-eslint-config/test/vue-tsx/index.tsx b/packages/vant-eslint-config/test/vue-tsx/index.tsx new file mode 100644 index 000000000..c83ae9d68 --- /dev/null +++ b/packages/vant-eslint-config/test/vue-tsx/index.tsx @@ -0,0 +1,23 @@ +import { defineComponent, ref } from 'vue'; +import App from './app'; + +const h2 = 1; + +export default defineComponent({ + name: 'Index', + setup() { + const count = ref(0); + + count++; + count + 1; + 1 + count; + + return () => ( + <> +

About

+ + + + ); + }, +}); diff --git a/packages/vant-eslint-config/test/vue-tsx/main.ts b/packages/vant-eslint-config/test/vue-tsx/main.ts new file mode 100644 index 000000000..fa0ebf3c5 --- /dev/null +++ b/packages/vant-eslint-config/test/vue-tsx/main.ts @@ -0,0 +1 @@ +interface Foo {} diff --git a/packages/vant-eslint-config/test/vue-tsx/tsconfig.json b/packages/vant-eslint-config/test/vue-tsx/tsconfig.json new file mode 100644 index 000000000..c7a2083e1 --- /dev/null +++ b/packages/vant-eslint-config/test/vue-tsx/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "strict": true, + "jsx": "preserve", + "importHelpers": true, + "moduleResolution": "node", + "experimentalDecorators": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "baseUrl": ".", + "lib": ["esnext", "dom", "dom.iterable", "scripthost"] + }, + "include": ["**/*.ts", "**/*.tsx", "**/*.vue"], + "exclude": ["node_modules"] +} diff --git a/packages/vant-eslint-config/test/vue/index.vue b/packages/vant-eslint-config/test/vue/index.vue new file mode 100644 index 000000000..9d0ad8445 --- /dev/null +++ b/packages/vant-eslint-config/test/vue/index.vue @@ -0,0 +1,19 @@ + + + + + diff --git a/packages/vant-eslint-config/test/vue/main.js b/packages/vant-eslint-config/test/vue/main.js new file mode 100644 index 000000000..710a1acf4 --- /dev/null +++ b/packages/vant-eslint-config/test/vue/main.js @@ -0,0 +1,2 @@ +const a = b + 1 +export default a