diff --git a/bin/index.js b/bin/index.js index 8c0ca58..b177d91 100644 --- a/bin/index.js +++ b/bin/index.js @@ -6,6 +6,7 @@ const { ESLint } = require("eslint"); const LibRulesAndConfigs = require("../lib/index"); // lib定义的规则名称集 const supportFileExtNames = require("../lib/execConfigs/supportFileExtNames"); // 支持的文件类型名后缀集合 const BlackFilesList = require("../lib/execConfigs/BlackFilesList"); // 排除的路径集合 +const FileIgnoredList = require("../lib/execConfigs/FileIgnoredList"); // 排除的文件集合 let targetPath = ""; let type = ""; @@ -22,7 +23,7 @@ function generateEslintInstances() { const pathToConfigFile = require.resolve(`@afuteam/eslint-plugin-fe/lib/configs/${name}.js`); // 禁止行内配置 allowInlineConfig - eslintInstances[name] = new ESLint({ overrideConfigFile: pathToConfigFile, useEslintrc: false, allowInlineConfig: allowInlineConfig }); + eslintInstances[name] = new ESLint({ overrideConfigFile: pathToConfigFile, useEslintrc: false, allowInlineConfig: allowInlineConfig, }); } } @@ -39,7 +40,13 @@ function handleSelectWhichEslintInstances(type, eslintInstances) { return eslintInstances[type] || ""; } -// TODO lintType 可能是多个,需要支持的,暂时不支持 +// 判断文件名后缀是否在黑名单 +function isFileIgnored(filePath) { + const fileName = path.basename(filePath); + return FileIgnoredList.some(blackName => fileName.endsWith(blackName)); +} + +// TODO lintType 暂不支持 多个,比如 Astro 类型的项目 async function lintFiles(filePaths) { @@ -47,8 +54,10 @@ async function lintFiles(filePaths) { let totalWarnings = 0; for (const filePath of filePaths) { + const curFileIsIgnored = isFileIgnored(filePath); + const extName = path.extname(filePath).slice(1); - if (fileGroups[extName]) { + if (!curFileIsIgnored && fileGroups[extName]) { fileGroups[extName].push(filePath); } } @@ -87,6 +96,7 @@ async function lintFiles(filePaths) { } console.log('Total 排除目录列表:', BlackFilesList); + console.log('Total 排除文件名规:', FileIgnoredList); console.log('Total 支持文件类型:', supportFileExtNames); console.log('Total errors:', totalErrors); diff --git a/lib/configs/base.js b/lib/configs/base.js index 0211d26..fc3f7f2 100644 --- a/lib/configs/base.js +++ b/lib/configs/base.js @@ -18,5 +18,5 @@ module.exports = { Atomics: 'readonly', SharedArrayBuffer: 'readonly', }, - ignorePatterns: ['*.min.*'], + ignorePatterns: ['*.min.js', 'iconfont.js'], } diff --git a/lib/configs/js.js b/lib/configs/js.js index 9906a3a..f077e16 100644 --- a/lib/configs/js.js +++ b/lib/configs/js.js @@ -1,3 +1,5 @@ +const BaseOptions = require('./base.js'); + module.exports = { rules: { 'no-unused-vars': 1, // 非TS项目,此项需设为1 @@ -100,5 +102,6 @@ module.exports = { "wrap-iife": 0, "wrap-regex": 0, "yield-star-spacing": 0 - } + }, + ignorePatterns: BaseOptions.ignorePatterns, } diff --git a/lib/configs/react-ts/index.js b/lib/configs/react-ts/index.js index a315d25..a6c6f7f 100644 --- a/lib/configs/react-ts/index.js +++ b/lib/configs/react-ts/index.js @@ -1,3 +1,4 @@ +const BaseOptions = require("../base.js"); const JSRules = require("../js.js"); @@ -65,4 +66,5 @@ module.exports = { '@typescript-eslint/prefer-namespace-keyword': 'off', // error -> off '@typescript-eslint/triple-slash-reference': 'off', // error -> off }, + ignorePatterns: BaseOptions.ignorePatterns, }; diff --git a/lib/configs/ts/index.js b/lib/configs/ts/index.js index c5c1e55..34e0961 100644 --- a/lib/configs/ts/index.js +++ b/lib/configs/ts/index.js @@ -1,3 +1,4 @@ +const BaseOptions = require("../base.js"); const JSRules = require("../js.js"); module.exports = { @@ -46,4 +47,6 @@ module.exports = { '@typescript-eslint/prefer-namespace-keyword': 'off', // error -> off '@typescript-eslint/triple-slash-reference': 'off', // error -> off }, + ignorePatterns: BaseOptions.ignorePatterns, + }; diff --git a/lib/configs/vue2-ts/index.js b/lib/configs/vue2-ts/index.js index 663dc9b..59da488 100644 --- a/lib/configs/vue2-ts/index.js +++ b/lib/configs/vue2-ts/index.js @@ -1,3 +1,4 @@ +const BaseOptions = require("../base.js"); const JSRules = require("../js.js"); module.exports = { @@ -47,4 +48,5 @@ module.exports = { '@typescript-eslint/prefer-namespace-keyword': 'off', // error -> off '@typescript-eslint/triple-slash-reference': 'off', // error -> off }, + ignorePatterns: BaseOptions.ignorePatterns, }; diff --git a/lib/configs/vue3-ts/index.js b/lib/configs/vue3-ts/index.js index e34264b..ac09b3f 100644 --- a/lib/configs/vue3-ts/index.js +++ b/lib/configs/vue3-ts/index.js @@ -1,3 +1,4 @@ +const BaseOptions = require("../base.js"); const JSRules = require("../js.js"); module.exports = { @@ -51,4 +52,5 @@ module.exports = { '@typescript-eslint/prefer-namespace-keyword': 'off', // error -> off '@typescript-eslint/triple-slash-reference': 'off', // error -> off }, + ignorePatterns: BaseOptions.ignorePatterns, }; diff --git a/lib/execConfigs/FileIgnoredList.js b/lib/execConfigs/FileIgnoredList.js new file mode 100644 index 0000000..9f5ed03 --- /dev/null +++ b/lib/execConfigs/FileIgnoredList.js @@ -0,0 +1,5 @@ +const FileIgnoredList = [ + 'min.js', + 'iconfont.js' +] +module.exports = FileIgnoredList diff --git a/tests/blackfile/a.min.js b/tests/blackfile/a.min.js new file mode 100644 index 0000000..acd98ae --- /dev/null +++ b/tests/blackfile/a.min.js @@ -0,0 +1 @@ +console.log(kkkk) \ No newline at end of file diff --git a/tests/blackfile/b.js b/tests/blackfile/b.js new file mode 100644 index 0000000..212771e --- /dev/null +++ b/tests/blackfile/b.js @@ -0,0 +1,3 @@ +console.log(qdii) + +var unused_var = 1; diff --git a/tests/blackfile/iconfont.js b/tests/blackfile/iconfont.js new file mode 100644 index 0000000..1108b96 --- /dev/null +++ b/tests/blackfile/iconfont.js @@ -0,0 +1 @@ +console.log(yyy); \ No newline at end of file