From d482c049e5862000afeddf3021b6ebb664915d4e Mon Sep 17 00:00:00 2001 From: Guo Yunhe Date: Thu, 2 Jan 2025 22:52:07 +0800 Subject: [PATCH] test(eslint-config-ali): fix test --- eslint.config.mjs | 2 +- packages/eslint-config-ali/CHANGELOG.md | 2 +- packages/eslint-config-ali/eslint.config.mjs | 2 +- .../src/configs/typescript.ts | 4 +- packages/eslint-config-ali/src/configs/vue.ts | 94 ------------------- packages/eslint-config-ali/src/index.ts | 1 - .../eslint-config-ali/src/presets/react.ts | 2 - packages/eslint-config-ali/src/presets/vue.ts | 6 -- .../test/fixtures/ts-vue.vue | 36 ------- .../eslint-config-ali/test/fixtures/vue.vue | 16 ---- .../eslint-config-ali/test/self-lint.test.ts | 16 ---- .../test/use-babel-eslint.test.xxx.ts | 44 --------- .../tests/bad/.prettierignore | 4 + .../{test/fixtures => tests/bad}/es5.js | 0 .../{test/fixtures => tests/bad}/index.js | 0 .../{test/fixtures => tests/bad}/node.js | 0 .../{test/fixtures => tests/bad}/rax.jsx | 0 .../bad}/react-display-name.js | 0 .../fixtures => tests/bad}/react-hooks.jsx | 0 .../{test/fixtures => tests/bad}/react.jsx | 0 .../fixtures => tests/bad}/ts-import-a.ts | 0 .../fixtures => tests/bad}/ts-import-b.ts | 0 .../{test/fixtures => tests/bad}/ts-node.ts | 0 .../{test/fixtures => tests/bad}/ts-rax.tsx | 0 .../{test/fixtures => tests/bad}/ts-react.tsx | 0 .../{test/fixtures => tests/bad}/ts.ts | 0 .../fixtures => tests/bad}/tsconfig.json | 0 .../bad}/use-babel-eslint.jsx | 12 +-- .../tests/good/react-hooks.jsx | 15 +++ .../tests/good/react-hooks.tsx | 15 +++ .../eslint-config-ali/tests/react.test.ts | 47 ++++++++++ packages/eslint-config-ali/tests/self.test.ts | 15 +++ .../validate-js-configs.test.xxx.ts | 0 .../validate-ts-configs.test.xxx.ts | 0 packages/eslint-config-ali/tsconfig.json | 6 +- 35 files changed, 111 insertions(+), 228 deletions(-) delete mode 100644 packages/eslint-config-ali/src/configs/vue.ts delete mode 100644 packages/eslint-config-ali/src/presets/vue.ts delete mode 100644 packages/eslint-config-ali/test/fixtures/ts-vue.vue delete mode 100644 packages/eslint-config-ali/test/fixtures/vue.vue delete mode 100644 packages/eslint-config-ali/test/self-lint.test.ts delete mode 100644 packages/eslint-config-ali/test/use-babel-eslint.test.xxx.ts create mode 100644 packages/eslint-config-ali/tests/bad/.prettierignore rename packages/eslint-config-ali/{test/fixtures => tests/bad}/es5.js (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/index.js (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/node.js (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/rax.jsx (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/react-display-name.js (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/react-hooks.jsx (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/react.jsx (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/ts-import-a.ts (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/ts-import-b.ts (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/ts-node.ts (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/ts-rax.tsx (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/ts-react.tsx (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/ts.ts (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/tsconfig.json (100%) rename packages/eslint-config-ali/{test/fixtures => tests/bad}/use-babel-eslint.jsx (99%) create mode 100644 packages/eslint-config-ali/tests/good/react-hooks.jsx create mode 100644 packages/eslint-config-ali/tests/good/react-hooks.tsx create mode 100644 packages/eslint-config-ali/tests/react.test.ts create mode 100644 packages/eslint-config-ali/tests/self.test.ts rename packages/eslint-config-ali/{test => tests}/validate-js-configs.test.xxx.ts (100%) rename packages/eslint-config-ali/{test => tests}/validate-ts-configs.test.xxx.ts (100%) diff --git a/eslint.config.mjs b/eslint.config.mjs index f4ed87a..f0c3365 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,3 +1,3 @@ import { base } from 'eslint-config-ali'; -export default [...base, { ignores: ['fixtures'] }]; +export default [...base, { ignores: ['**/bad/'] }]; diff --git a/packages/eslint-config-ali/CHANGELOG.md b/packages/eslint-config-ali/CHANGELOG.md index c5393ee..fec1739 100644 --- a/packages/eslint-config-ali/CHANGELOG.md +++ b/packages/eslint-config-ali/CHANGELOG.md @@ -4,7 +4,7 @@ - **破坏性变更**: 移除了 `eslint` 8.x 支持,仅支持 `eslint` 9.x - **破坏性变更**: 产物改为 ESM,移除了原有文件导出,只导出单一默认对象 -- **破坏性变更**: 移除 `rax`, `jsx-a11y`, `es5`, `node`, `egg` 配置 +- **破坏性变更**: 移除 `rax`, `jsx-a11y`, `es5`, `node`, `egg`, `vue` 配置 - 支持 `.gitignore` (仅限项目根目录),不再需要 `.eslintignore` 文件 - 引入 `@stylistic/eslint-plugin` 统一 JS/TS/JSX 代码风格类规则 diff --git a/packages/eslint-config-ali/eslint.config.mjs b/packages/eslint-config-ali/eslint.config.mjs index 4445bd9..d047a49 100644 --- a/packages/eslint-config-ali/eslint.config.mjs +++ b/packages/eslint-config-ali/eslint.config.mjs @@ -1,3 +1,3 @@ import { base } from './dist'; -export default [...base, { ignores: ['fixtures'] }]; +export default [...base, { ignores: ['bad/'] }]; diff --git a/packages/eslint-config-ali/src/configs/typescript.ts b/packages/eslint-config-ali/src/configs/typescript.ts index b11ce95..136191c 100644 --- a/packages/eslint-config-ali/src/configs/typescript.ts +++ b/packages/eslint-config-ali/src/configs/typescript.ts @@ -5,7 +5,7 @@ import ts from 'typescript-eslint'; const typescript = ts.config({ name: 'ali/typescripts', files: ['**/*.{cts,mts,ts,tsx,vue}'], - extends: [ts.configs.recommended], + extends: [ts.configs.recommendedTypeChecked], languageOptions: { parserOptions: { project: true, @@ -103,7 +103,7 @@ const typescript = ts.config({ * @extend */ 'dot-notation': 'off', - '@typescript-eslint/dot-notation': ['error', { allowKeywords: true }], + '@typescript-eslint/dot-notation': 'error', /** * 【关闭】函数返回值必须与声明的类型一致 diff --git a/packages/eslint-config-ali/src/configs/vue.ts b/packages/eslint-config-ali/src/configs/vue.ts deleted file mode 100644 index f084d17..0000000 --- a/packages/eslint-config-ali/src/configs/vue.ts +++ /dev/null @@ -1,94 +0,0 @@ -import globals from 'globals'; -import defineConfig from '../utils/defineConfig'; - -export default defineConfig({ - name: 'ali/vue', - rules: { - // 给 template 提供 eslint-disable 的能力,支持如下注释: - // eslint-disable,eslint-enable,eslint-disable-line,eslint-disable-next-line - 'vue/comment-directive': 'error', - - // 本条是对JS规约 no-unused-vars 的补充,防止变量被错误地标记为未使用 - 'vue/jsx-uses-vars': 'error', - - // 组件的 data 必须是一个函数 - 'vue/no-shared-component-data': 'error', - - // Prop 定义类型应该是构造函数 - 'vue/require-prop-type-constructor': 'error', - - // Prop 的默认值必须匹配它的类型 - 'vue/require-valid-default-prop': 'error', - - // 为 v-for 设置键值 - 'vue/require-v-for-key': 'error', - - // 避免 v-if 和 v-for 用在一起 - 'vue/no-use-v-if-with-v-for': 'warn', - - // 计算属性禁止包含异步方法 - 'vue/no-async-in-computed-properties': 'error', - - // 禁止在对象字面量中出现重复的键 - 'vue/no-dupe-keys': 'error', - - // 禁止出现重复的属性 - 'vue/no-duplicate-attributes': 'error', - - // 禁止出现语法错误 - // @link https://html.spec.whatwg.org/multipage/parsing.html#parse-errors - 'vue/no-parsing-error': [ - 'error', - { - 'x-invalid-end-tag': false, - 'invalid-first-character-of-tag-name': false, - }, - ], - - // 禁止使用 vue 中的关键字 - 'vue/no-reserved-keys': 'error', - - // 禁止在计算属性中对属性修改 - 'vue/no-side-effects-in-computed-properties': 'error', - - // 禁止 - - - - diff --git a/packages/eslint-config-ali/test/fixtures/vue.vue b/packages/eslint-config-ali/test/fixtures/vue.vue deleted file mode 100644 index d846996..0000000 --- a/packages/eslint-config-ali/test/fixtures/vue.vue +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/packages/eslint-config-ali/test/self-lint.test.ts b/packages/eslint-config-ali/test/self-lint.test.ts deleted file mode 100644 index 36fe55f..0000000 --- a/packages/eslint-config-ali/test/self-lint.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { ESLint } from 'eslint'; -import path from 'path'; -import { base } from '../src'; - -describe('test/self-lint.test.js', () => { - it('Repo self should pass lint', async () => { - const cli = new ESLint({ - baseConfig: base, - overrideConfigFile: true, - ignore: true, - }); - - const reports = await cli.lintFiles([path.join(import.meta.dirname, '../src')]); - expect(reports.reduce((count, { errorCount }) => errorCount + count, 0)).toBe(0); - }); -}); diff --git a/packages/eslint-config-ali/test/use-babel-eslint.test.xxx.ts b/packages/eslint-config-ali/test/use-babel-eslint.test.xxx.ts deleted file mode 100644 index 729673b..0000000 --- a/packages/eslint-config-ali/test/use-babel-eslint.test.xxx.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { ESLint } from 'eslint'; -import path from 'node:path'; - -describe('test/use-babel-eslint.test.js', () => { - it('babel-eslint parser run well for react', async () => { - const configPath = './react.js'; - const filePath = path.join(import.meta.dirname, './fixtures/use-babel-eslint.jsx'); - - const cli = new ESLint({ - overrideConfigFile: configPath, - ignore: false, - }); - - const results = await cli.lintFiles([filePath]); - const { messages, errorCount, fatalErrorCount, warningCount } = results[0]; - - assert.equal(fatalErrorCount, 0); - assert.equal(errorCount, 26); - assert.equal(warningCount, 7); - - const errorReportedByReactPlugin = messages.filter((result) => { - return result.ruleId && result.ruleId.indexOf('react/') !== -1; - }); - - assert.notEqual(errorReportedByReactPlugin.length, 0); - }); - - it('babel-eslint parser run well for vue', async () => { - const configPath = './vue.js'; - const filePath = path.join(import.meta.dirname, './fixtures/vue.vue'); - - const cli = new ESLint({ - overrideConfigFile: configPath, - ignore: false, - }); - - const results = await cli.lintFiles([filePath]); - const { errorCount, fatalErrorCount, warningCount } = results[0]; - - assert.equal(fatalErrorCount, 0); - assert.equal(errorCount, 4); - assert.equal(warningCount, 0); - }); -}); diff --git a/packages/eslint-config-ali/tests/bad/.prettierignore b/packages/eslint-config-ali/tests/bad/.prettierignore new file mode 100644 index 0000000..56caaa0 --- /dev/null +++ b/packages/eslint-config-ali/tests/bad/.prettierignore @@ -0,0 +1,4 @@ +*.js +*.jsx +*.ts +*.tsx diff --git a/packages/eslint-config-ali/test/fixtures/es5.js b/packages/eslint-config-ali/tests/bad/es5.js similarity index 100% rename from packages/eslint-config-ali/test/fixtures/es5.js rename to packages/eslint-config-ali/tests/bad/es5.js diff --git a/packages/eslint-config-ali/test/fixtures/index.js b/packages/eslint-config-ali/tests/bad/index.js similarity index 100% rename from packages/eslint-config-ali/test/fixtures/index.js rename to packages/eslint-config-ali/tests/bad/index.js diff --git a/packages/eslint-config-ali/test/fixtures/node.js b/packages/eslint-config-ali/tests/bad/node.js similarity index 100% rename from packages/eslint-config-ali/test/fixtures/node.js rename to packages/eslint-config-ali/tests/bad/node.js diff --git a/packages/eslint-config-ali/test/fixtures/rax.jsx b/packages/eslint-config-ali/tests/bad/rax.jsx similarity index 100% rename from packages/eslint-config-ali/test/fixtures/rax.jsx rename to packages/eslint-config-ali/tests/bad/rax.jsx diff --git a/packages/eslint-config-ali/test/fixtures/react-display-name.js b/packages/eslint-config-ali/tests/bad/react-display-name.js similarity index 100% rename from packages/eslint-config-ali/test/fixtures/react-display-name.js rename to packages/eslint-config-ali/tests/bad/react-display-name.js diff --git a/packages/eslint-config-ali/test/fixtures/react-hooks.jsx b/packages/eslint-config-ali/tests/bad/react-hooks.jsx similarity index 100% rename from packages/eslint-config-ali/test/fixtures/react-hooks.jsx rename to packages/eslint-config-ali/tests/bad/react-hooks.jsx diff --git a/packages/eslint-config-ali/test/fixtures/react.jsx b/packages/eslint-config-ali/tests/bad/react.jsx similarity index 100% rename from packages/eslint-config-ali/test/fixtures/react.jsx rename to packages/eslint-config-ali/tests/bad/react.jsx diff --git a/packages/eslint-config-ali/test/fixtures/ts-import-a.ts b/packages/eslint-config-ali/tests/bad/ts-import-a.ts similarity index 100% rename from packages/eslint-config-ali/test/fixtures/ts-import-a.ts rename to packages/eslint-config-ali/tests/bad/ts-import-a.ts diff --git a/packages/eslint-config-ali/test/fixtures/ts-import-b.ts b/packages/eslint-config-ali/tests/bad/ts-import-b.ts similarity index 100% rename from packages/eslint-config-ali/test/fixtures/ts-import-b.ts rename to packages/eslint-config-ali/tests/bad/ts-import-b.ts diff --git a/packages/eslint-config-ali/test/fixtures/ts-node.ts b/packages/eslint-config-ali/tests/bad/ts-node.ts similarity index 100% rename from packages/eslint-config-ali/test/fixtures/ts-node.ts rename to packages/eslint-config-ali/tests/bad/ts-node.ts diff --git a/packages/eslint-config-ali/test/fixtures/ts-rax.tsx b/packages/eslint-config-ali/tests/bad/ts-rax.tsx similarity index 100% rename from packages/eslint-config-ali/test/fixtures/ts-rax.tsx rename to packages/eslint-config-ali/tests/bad/ts-rax.tsx diff --git a/packages/eslint-config-ali/test/fixtures/ts-react.tsx b/packages/eslint-config-ali/tests/bad/ts-react.tsx similarity index 100% rename from packages/eslint-config-ali/test/fixtures/ts-react.tsx rename to packages/eslint-config-ali/tests/bad/ts-react.tsx diff --git a/packages/eslint-config-ali/test/fixtures/ts.ts b/packages/eslint-config-ali/tests/bad/ts.ts similarity index 100% rename from packages/eslint-config-ali/test/fixtures/ts.ts rename to packages/eslint-config-ali/tests/bad/ts.ts diff --git a/packages/eslint-config-ali/test/fixtures/tsconfig.json b/packages/eslint-config-ali/tests/bad/tsconfig.json similarity index 100% rename from packages/eslint-config-ali/test/fixtures/tsconfig.json rename to packages/eslint-config-ali/tests/bad/tsconfig.json diff --git a/packages/eslint-config-ali/test/fixtures/use-babel-eslint.jsx b/packages/eslint-config-ali/tests/bad/use-babel-eslint.jsx similarity index 99% rename from packages/eslint-config-ali/test/fixtures/use-babel-eslint.jsx rename to packages/eslint-config-ali/tests/bad/use-babel-eslint.jsx index 91dbc5e..4a2115b 100644 --- a/packages/eslint-config-ali/test/fixtures/use-babel-eslint.jsx +++ b/packages/eslint-config-ali/tests/bad/use-babel-eslint.jsx @@ -1,17 +1,17 @@ -import React, { Component } from "react"; -import { connect } from "react-redux"; -import { bindActionCreators } from "redux"; import { Breadcrumb, Button, - Search, - Select, Dropdown, Menu, Notice, + Search, + Select, } from "antd"; -import Layout from "layout"; import { Translate } from "antd-i18n"; +import Layout from "layout"; +import { Component } from "react"; +import { connect } from "react-redux"; +import { bindActionCreators } from "redux"; import * as actions from "../actions/index"; function scoreAudioCoverFile(imgFile) { diff --git a/packages/eslint-config-ali/tests/good/react-hooks.jsx b/packages/eslint-config-ali/tests/good/react-hooks.jsx new file mode 100644 index 0000000..d437ef7 --- /dev/null +++ b/packages/eslint-config-ali/tests/good/react-hooks.jsx @@ -0,0 +1,15 @@ +import { useEffect, useMemo, useState } from 'react'; + +export function GoodReactHooks() { + const [foo, setFoo] = useState('foo'); + const [bar, setBar] = useState('bar'); + + const foobar = useMemo(() => foo + bar, [foo, bar]); + + useEffect(() => { + setFoo('notfoo'); + setBar('notbar'); + }, []); + + return
{foobar}
; +} diff --git a/packages/eslint-config-ali/tests/good/react-hooks.tsx b/packages/eslint-config-ali/tests/good/react-hooks.tsx new file mode 100644 index 0000000..d437ef7 --- /dev/null +++ b/packages/eslint-config-ali/tests/good/react-hooks.tsx @@ -0,0 +1,15 @@ +import { useEffect, useMemo, useState } from 'react'; + +export function GoodReactHooks() { + const [foo, setFoo] = useState('foo'); + const [bar, setBar] = useState('bar'); + + const foobar = useMemo(() => foo + bar, [foo, bar]); + + useEffect(() => { + setFoo('notfoo'); + setBar('notbar'); + }, []); + + return
{foobar}
; +} diff --git a/packages/eslint-config-ali/tests/react.test.ts b/packages/eslint-config-ali/tests/react.test.ts new file mode 100644 index 0000000..c95259d --- /dev/null +++ b/packages/eslint-config-ali/tests/react.test.ts @@ -0,0 +1,47 @@ +import { ESLint } from 'eslint'; +import path from 'node:path'; +import { react } from '../src'; + +it('should lint jsx files', async () => { + const filePath = path.join(import.meta.dirname, './bad/use-babel-eslint.jsx'); + + const cli = new ESLint({ + baseConfig: react, + overrideConfigFile: true, + ignore: true, + }); + + const results = await cli.lintFiles([filePath]); + const { messages, errorCount, fatalErrorCount, warningCount } = results[0]; + + expect(fatalErrorCount).toBe(0); + expect(errorCount).toBe(20); + expect(warningCount).toBe(7); + + const errorReportedByReactPlugin = messages.filter((result) => { + return ( + (result.ruleId && result.ruleId.startsWith('react/')) || + result.ruleId.startsWith('react-hooks/') + ); + }); + + expect(errorReportedByReactPlugin.length).toBe(3); +}); + +describe('good', () => { + ['react-hooks.jsx', 'react-hooks.tsx'].forEach((file) => { + it(`should pass lint for ${file}`, async () => { + const cli = new ESLint({ + baseConfig: react, + overrideConfigFile: true, + ignore: true, + }); + + const results = await cli.lintFiles(path.join(import.meta.dirname, 'good', file)); + + expect(results[0].fatalErrorCount).toBe(0); + expect(results[0].errorCount).toBe(0); + expect(results[0].warningCount).toBe(0); + }); + }); +}); diff --git a/packages/eslint-config-ali/tests/self.test.ts b/packages/eslint-config-ali/tests/self.test.ts new file mode 100644 index 0000000..5ebde87 --- /dev/null +++ b/packages/eslint-config-ali/tests/self.test.ts @@ -0,0 +1,15 @@ +import { ESLint } from 'eslint'; +import path from 'path'; +import { base } from '../src'; + +// Lint the package source code +it('should pass self lint', async () => { + const cli = new ESLint({ + baseConfig: base, + overrideConfigFile: true, + ignore: true, + }); + + const reports = await cli.lintFiles([path.join(import.meta.dirname, '../src')]); + expect(reports.reduce((count, { errorCount }) => errorCount + count, 0)).toBe(0); +}); diff --git a/packages/eslint-config-ali/test/validate-js-configs.test.xxx.ts b/packages/eslint-config-ali/tests/validate-js-configs.test.xxx.ts similarity index 100% rename from packages/eslint-config-ali/test/validate-js-configs.test.xxx.ts rename to packages/eslint-config-ali/tests/validate-js-configs.test.xxx.ts diff --git a/packages/eslint-config-ali/test/validate-ts-configs.test.xxx.ts b/packages/eslint-config-ali/tests/validate-ts-configs.test.xxx.ts similarity index 100% rename from packages/eslint-config-ali/test/validate-ts-configs.test.xxx.ts rename to packages/eslint-config-ali/tests/validate-ts-configs.test.xxx.ts diff --git a/packages/eslint-config-ali/tsconfig.json b/packages/eslint-config-ali/tsconfig.json index 48cb945..b715da9 100644 --- a/packages/eslint-config-ali/tsconfig.json +++ b/packages/eslint-config-ali/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es2017", "lib": ["esnext"], - "allowJs": true, + "allowJs": false, "checkJs": false, "skipLibCheck": true, "esModuleInterop": true, @@ -10,7 +10,9 @@ "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, - "types": ["node", "rive/globals"] + "types": ["node", "rive/globals"], + "jsx": "react-jsx", + "noEmit": true }, "exclude": [".rive", "build", "coverage", "dist", "node_modules"] }