From afbff1ad83a1a811aa65c5f9f69abdd41eb220aa Mon Sep 17 00:00:00 2001 From: chenshuai2144 Date: Sun, 27 Dec 2020 16:00:00 +0800 Subject: [PATCH] fix: Fix the problem of always reporting errors --- package.json | 1 + src/eslint.ts | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 503e897..23d96c3 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "eslint-plugin-react": "^7.21.5", "eslint-plugin-react-hooks": "^4.1.2", "eslint-plugin-unicorn": "^20.0.0", + "fast-glob": "^3.2.4", "stylelint": "^13.7.0", "stylelint-config-css-modules": "^2.2.0", "stylelint-config-prettier": "^8.0.1", diff --git a/src/eslint.ts b/src/eslint.ts index 6e123c6..a782c3d 100644 --- a/src/eslint.ts +++ b/src/eslint.ts @@ -10,11 +10,31 @@ const parserOptions: { project: './tsconfig.json', }; +const isJsMoreTs = async (path = 'src') => { + const fg = require('fast-glob'); + const jsFiles = await fg(`${path}/src/**/*.{js,jsx}`, { deep: 3 }); + const tsFiles = await fg(`${path}/src/**/*.{ts,tsx}`, { deep: 3 }); + return jsFiles.length > tsFiles.length; +}; + +const configPath = path.join(__dirname, 'fabric.rc'); const isTsProject = fs.existsSync(path.join(process.cwd() || '.', './tsconfig.json')); if (isTsProject) { - console.log('这是一个 TypeScript 项目,如果不是请删除 tsconfig.json'); + if (!fs.existsSync(configPath)) { + isJsMoreTs(configPath).then((jsMoreTs) => { + fs.writeFileSync(configPath, new Date().getDate().toString()); + if (!jsMoreTs) return; + console.log('这是一个 TypeScript 项目,如果不是请删除 tsconfig.json'); + }); + } else { + const cacheTime = fs.readFileSync(configPath).toString(); + if (new Date().getDate() !== parseInt(cacheTime, 10)) { + fs.rmSync(configPath); + } + } } + module.exports = { extends: ['eslint-config-airbnb-base', 'prettier', 'prettier/react'].concat( isTsProject ? ['prettier/@typescript-eslint', 'plugin:@typescript-eslint/recommended'] : [], @@ -55,7 +75,7 @@ module.exports = { 'jsx-a11y/anchor-is-valid': 0, 'sort-imports': 0, 'class-methods-use-this': 0, - 'no-confusing-arrow': 1, + 'no-confusing-arrow': 0, 'linebreak-style': 0, // Too restrictive, writing ugly code to defend against a very unlikely scenario: https://eslint.org/docs/rules/no-prototype-builtins 'no-prototype-builtins': 'off',