From ec657636a18c49049bd5b0acc7b5bae3396d2dd4 Mon Sep 17 00:00:00 2001 From: yangfan <18767120422@163.com> Date: Tue, 27 Apr 2021 14:48:42 +0800 Subject: [PATCH] feat: add prettier --- README.md | 14 ++++++++++- .../src/configs/common.js | 25 ++++++++++--------- .../src/rules/no-broad-semantic-versioning.js | 7 +----- .../src/rules/no-secret-info.js | 18 ++++++------- .../src/rules/recommend-polyfill.js | 7 +----- packages/spec/CHANGELOG.md | 4 +++ packages/spec/README.md | 14 ++++++++++- packages/spec/examples/common-ts/index.ts | 5 ++++ packages/spec/examples/common/index.js | 5 ++++ packages/spec/examples/rax-ts/index.tsx | 5 ++++ packages/spec/examples/rax/index.jsx | 5 ++++ packages/spec/examples/react-ts/index.tsx | 13 +++++----- packages/spec/examples/react/index.jsx | 9 ++++--- packages/spec/package.json | 4 ++- packages/spec/src/eslint/common-ts.js | 2 ++ packages/spec/src/eslint/common.js | 3 +++ packages/spec/src/eslint/rax-ts.js | 2 ++ packages/spec/src/eslint/rax.js | 2 ++ packages/spec/src/eslint/react-ts.js | 2 ++ packages/spec/src/eslint/react.js | 2 ++ packages/spec/src/eslint/vue-ts.js | 4 +-- packages/spec/src/eslint/vue.js | 4 +-- 22 files changed, 103 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 93cdc7a..b7ccab0 100644 --- a/README.md +++ b/README.md @@ -136,10 +136,22 @@ After that, you can add to your` package.json` the following code: ### Update from @ice/spec -If you are using [@ice/spce](https://www.npmjs.com/package/@ice/spec) in your project, we recommend use `@iceworks/spec` to get better maintainability and faster response to lint rules support. +If you are using [@ice/spec](https://www.npmjs.com/package/@ice/spec) in your project, we recommend use `@iceworks/spec` to get better maintainability and faster response to lint rules support. Based on `@iceworks/spec`'s simple API you can quickly migrate your project, install and update your lint config file, the mission is completed 😁. +### Usage with Node.js API + +If you are using spec rules in `CLIEngine` or `Linter`, Please set `cwd` to path of spec that can help ESLint find dependent plugins and configs. + +```js +// CLIEngine +new CLIEngine({ cwd: path.dirname(require.resolve('@iceworks/spec')) }); + +// Linter +new Linter({ cwd: path.dirname(require.resolve('@iceworks/spec')) }); +``` + ## Develop ### Run Test diff --git a/packages/eslint-plugin-best-practices/src/configs/common.js b/packages/eslint-plugin-best-practices/src/configs/common.js index e60639b..8419fc4 100644 --- a/packages/eslint-plugin-best-practices/src/configs/common.js +++ b/packages/eslint-plugin-best-practices/src/configs/common.js @@ -18,22 +18,23 @@ module.exports = { }, }, { - files: [ - '**/__tests__/*.{j,t}s?(x)', - '**/tests/*.{j,t}s?(x)', - '**/test/*.{j,t}s?(x)', - ], + files: ['**/__tests__/*.{j,t}s?(x)', '**/tests/*.{j,t}s?(x)', '**/test/*.{j,t}s?(x)'], env: { jest: true }, }, ], rules: { - 'max-len': ['warn', 120, 2, { - ignoreUrls: true, - ignoreComments: false, - ignoreRegExpLiterals: true, - ignoreStrings: true, - ignoreTemplateLiterals: true, - }], + 'max-len': [ + 'warn', + 120, + 2, + { + ignoreUrls: true, + ignoreComments: false, + ignoreRegExpLiterals: true, + ignoreStrings: true, + ignoreTemplateLiterals: true, + }, + ], '@iceworks/best-practices/no-http-url': 'warn', '@iceworks/best-practices/no-js-in-ts-project': 'warn', '@iceworks/best-practices/no-lowercase-component-name': 'warn', diff --git a/packages/eslint-plugin-best-practices/src/rules/no-broad-semantic-versioning.js b/packages/eslint-plugin-best-practices/src/rules/no-broad-semantic-versioning.js index ec43074..a68f87e 100644 --- a/packages/eslint-plugin-best-practices/src/rules/no-broad-semantic-versioning.js +++ b/packages/eslint-plugin-best-practices/src/rules/no-broad-semantic-versioning.js @@ -47,12 +47,7 @@ module.exports = { dependencyVersion.indexOf('>') > -1 ) { let newVersioning = '^1.0.0'; - const dependencyPackageFile = path.join( - cwd, - 'node_modules', - dependencyName, - 'package.json', - ); + const dependencyPackageFile = path.join(cwd, 'node_modules', dependencyName, 'package.json'); if (fs.existsSync(dependencyPackageFile)) { const dependencyPackage = fs.readJSONSync(dependencyPackageFile); newVersioning = `^${dependencyPackage.version}`; diff --git a/packages/eslint-plugin-best-practices/src/rules/no-secret-info.js b/packages/eslint-plugin-best-practices/src/rules/no-secret-info.js index 667c543..6ad26c3 100644 --- a/packages/eslint-plugin-best-practices/src/rules/no-secret-info.js +++ b/packages/eslint-plugin-best-practices/src/rules/no-secret-info.js @@ -23,20 +23,18 @@ module.exports = { return { Literal: function handleRequires(node) { if ( - node.value && node.parent && ( - // var secret = 'test'; - (node.parent.type === 'VariableDeclarator' && - node.parent.id && - node.parent.id.name && - reg.test(node.parent.id.name.toLocaleLowerCase()) - ) || + node.value && + node.parent && + // var secret = 'test'; + ((node.parent.type === 'VariableDeclarator' && + node.parent.id && + node.parent.id.name && + reg.test(node.parent.id.name.toLocaleLowerCase())) || // { secret: 'test' }; (node.parent.type === 'Property' && node.parent.key && node.parent.key.name && - reg.test(node.parent.key.name.toLocaleLowerCase()) - ) - ) + reg.test(node.parent.key.name.toLocaleLowerCase()))) ) { context.report({ node, diff --git a/packages/eslint-plugin-best-practices/src/rules/recommend-polyfill.js b/packages/eslint-plugin-best-practices/src/rules/recommend-polyfill.js index 0232ff0..fdc9408 100644 --- a/packages/eslint-plugin-best-practices/src/rules/recommend-polyfill.js +++ b/packages/eslint-plugin-best-practices/src/rules/recommend-polyfill.js @@ -72,12 +72,7 @@ module.exports = { const supports = target.__compat.support; for (let i = 0, l = Object.keys(targetBrowsers).length; i < l; i++) { const browser = Object.keys(targetBrowsers)[i]; - if ( - semver.satisfies( - `${targetBrowsers[browser]}.0.0`, - `<${supports[browser].version_added}`, - ) - ) { + if (semver.satisfies(`${targetBrowsers[browser]}.0.0`, `<${supports[browser].version_added}`)) { context.report({ node, messageId: 'recommendPolyfill', diff --git a/packages/spec/CHANGELOG.md b/packages/spec/CHANGELOG.md index dca3d57..8042cb9 100644 --- a/packages/spec/CHANGELOG.md +++ b/packages/spec/CHANGELOG.md @@ -1,5 +1,9 @@ # changelog +## 1.4.0 + +- ESLint add prettier config and plugin + ## 1.3.1 - Add stylelint peerDependencies diff --git a/packages/spec/README.md b/packages/spec/README.md index b16a001..c9ca626 100644 --- a/packages/spec/README.md +++ b/packages/spec/README.md @@ -150,10 +150,22 @@ After that, you can add to your` package.json` the following code: ### Update from @ice/spec -If you are using [@ice/spce](https://www.npmjs.com/package/@ice/spec) in your project, we recommend use `@iceworks/spec` to get better maintainability and faster response to lint rules support. +If you are using [@ice/spec](https://www.npmjs.com/package/@ice/spec) in your project, we recommend use `@iceworks/spec` to get better maintainability and faster response to lint rules support. Based on `@iceworks/spec`'s simple API you can quickly migrate your project, install and update your lint config file, the mission is completed 😁. +### Usage with Node.js API + +If you are using spec rules in `CLIEngine` or `Linter`, Please set `cwd` to path of spec that can help ESLint find dependent plugins and configs. + +```js +// CLIEngine +new CLIEngine({ cwd: path.dirname(require.resolve('@iceworks/spec')) }); + +// Linter +new Linter({ cwd: path.dirname(require.resolve('@iceworks/spec')) }); +``` + ### Error: Cannot find module 'eslint-plugin-foo' Eslint is not yet supported having plugins as dependencies in shareable config. [issue](https://github.com/eslint/eslint/issues/3458). As a temporary solution, you need add the plugin to devDependencies in your project, like `npm i --save-dev eslint-plugin-jsx-a11y`. diff --git a/packages/spec/examples/common-ts/index.ts b/packages/spec/examples/common-ts/index.ts index bfb7b52..1b6277c 100644 --- a/packages/spec/examples/common-ts/index.ts +++ b/packages/spec/examples/common-ts/index.ts @@ -1,3 +1,8 @@ +// prettier test +// const a = 1; const b = 2; +const a = 1; +const b = 2; + interface AppDeveloper { name: string; id: number; diff --git a/packages/spec/examples/common/index.js b/packages/spec/examples/common/index.js index b83a580..6e6e99e 100644 --- a/packages/spec/examples/common/index.js +++ b/packages/spec/examples/common/index.js @@ -1,2 +1,7 @@ +// prettier test +// const a = 1; const b = 2; +const a = 1; +const b = 2; + const foo = [1, 2]; console.log(foo); diff --git a/packages/spec/examples/rax-ts/index.tsx b/packages/spec/examples/rax-ts/index.tsx index 66d3653..02dd89b 100644 --- a/packages/spec/examples/rax-ts/index.tsx +++ b/packages/spec/examples/rax-ts/index.tsx @@ -9,6 +9,11 @@ interface LogoProps { const unusedVar = 1; +// prettier test +// const a = 1; const b = 2; +const a = 1; +const b = 2; + export default (props: LogoProps) => { const { uri } = props; const source = { uri }; diff --git a/packages/spec/examples/rax/index.jsx b/packages/spec/examples/rax/index.jsx index f74796e..43b241b 100644 --- a/packages/spec/examples/rax/index.jsx +++ b/packages/spec/examples/rax/index.jsx @@ -6,6 +6,11 @@ import './index.css'; const unusedVar = 1; export default (props) => { + // prettier test + // const a = 1; const b = 2; + const a = 1; + const b = 2; + const { uri } = props; const source = { uri }; return ; diff --git a/packages/spec/examples/react-ts/index.tsx b/packages/spec/examples/react-ts/index.tsx index b599ca2..da6fff7 100644 --- a/packages/spec/examples/react-ts/index.tsx +++ b/packages/spec/examples/react-ts/index.tsx @@ -10,17 +10,16 @@ import styles from './index.module.scss'; const unusedVar = 1; +// prettier test +// const a = 1; const b = 2; +const a = 1; +const b = 2; + const TableActionIcon = Icon.createFromIconfontCN({ scriptUrl: '//at.alicdn.com/t/font_1899388_oxn3zhg34oj.js', }); -const getTableData = ({ - current, - pageSize, -}: { - current: number; - pageSize: number; -}): Promise => { +const getTableData = ({ current, pageSize }: { current: number; pageSize: number }): Promise => { const query = `page=${current}&size=${pageSize}`; return fetch(`https://randomuser.me/api?results=${pageSize}&${query}`) .then((res) => res.json()) diff --git a/packages/spec/examples/react/index.jsx b/packages/spec/examples/react/index.jsx index a7058ef..62e502c 100644 --- a/packages/spec/examples/react/index.jsx +++ b/packages/spec/examples/react/index.jsx @@ -4,6 +4,11 @@ import styles from './index.module.scss'; const unusedVar = 1; +// prettier test +// const a = 1; const b = 2; +const a = 1; +const b = 2; + const PageHeader = (props) => { const { breadcrumbs, title, description, ...others } = props; return ( @@ -18,9 +23,7 @@ const PageHeader = (props) => { {title && {title}} - {description && ( - {description} - )} + {description && {description}} ); }; diff --git a/packages/spec/package.json b/packages/spec/package.json index 5c0ee89..cd10939 100644 --- a/packages/spec/package.json +++ b/packages/spec/package.json @@ -1,6 +1,6 @@ { "name": "@iceworks/spec", - "version": "1.3.1", + "version": "1.4.0", "description": "Easy to use eslint/stylelint/prettier/commitlint in rax, ice and react project.", "main": "src/index.js", "files": [ @@ -42,7 +42,9 @@ "babel-eslint": "^10.1.0", "commitlint-config-ali": "^0.1.0", "eslint-config-ali": "^12.0.1", + "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.22.1", + "eslint-plugin-prettier": "^3.4.0", "eslint-plugin-react": "^7.21.5", "eslint-plugin-react-hooks": "^4.2.0", "eslint-plugin-vue": "^7.3.0", diff --git a/packages/spec/src/eslint/common-ts.js b/packages/spec/src/eslint/common-ts.js index f23e8c6..3c22576 100644 --- a/packages/spec/src/eslint/common-ts.js +++ b/packages/spec/src/eslint/common-ts.js @@ -2,6 +2,8 @@ // ESlint config for common ts project module.exports = { extends: [ + 'plugin:prettier/recommended', + require.resolve('eslint-config-prettier'), require.resolve('eslint-config-ali/typescript'), ], rules: { diff --git a/packages/spec/src/eslint/common.js b/packages/spec/src/eslint/common.js index dbf7761..1770c2a 100644 --- a/packages/spec/src/eslint/common.js +++ b/packages/spec/src/eslint/common.js @@ -1,7 +1,10 @@ // https://www.npmjs.com/package/eslint-config-ali // ESlint config for common js project + module.exports = { extends: [ + 'plugin:prettier/recommended', + require.resolve('eslint-config-prettier'), require.resolve('eslint-config-ali'), ], rules: { diff --git a/packages/spec/src/eslint/rax-ts.js b/packages/spec/src/eslint/rax-ts.js index 23070e8..1ae2fc7 100644 --- a/packages/spec/src/eslint/rax-ts.js +++ b/packages/spec/src/eslint/rax-ts.js @@ -2,6 +2,8 @@ // ESlint config for Rax TypeScript project module.exports = { extends: [ + 'plugin:prettier/recommended', + require.resolve('eslint-config-prettier'), require.resolve('eslint-config-ali/typescript/rax'), // For some ci and jest test env, we chose require.resolve instead 'plugin:@iceworks/best-practices/rax-ts' require.resolve('@iceworks/eslint-plugin-best-practices/src/configs/rax-ts'), diff --git a/packages/spec/src/eslint/rax.js b/packages/spec/src/eslint/rax.js index 00d1e9e..99d179c 100644 --- a/packages/spec/src/eslint/rax.js +++ b/packages/spec/src/eslint/rax.js @@ -2,6 +2,8 @@ // ESlint config for Rax project module.exports = { extends: [ + 'plugin:prettier/recommended', + require.resolve('eslint-config-prettier'), require.resolve('eslint-config-ali/rax'), // For some ci and jest test env, we chose require.resolve instead 'plugin:@iceworks/best-practices/rax' require.resolve('@iceworks/eslint-plugin-best-practices/src/configs/rax'), diff --git a/packages/spec/src/eslint/react-ts.js b/packages/spec/src/eslint/react-ts.js index c613637..ac94192 100644 --- a/packages/spec/src/eslint/react-ts.js +++ b/packages/spec/src/eslint/react-ts.js @@ -2,6 +2,8 @@ // ESlint config for ice TypeScript and react TypeScript project module.exports = { extends: [ + 'plugin:prettier/recommended', + require.resolve('eslint-config-prettier'), require.resolve('eslint-config-ali/typescript/react'), // For some ci and jest test env, we chose require.resolve instead 'plugin:@iceworks/best-practices/react-ts' require.resolve('@iceworks/eslint-plugin-best-practices/src/configs/react-ts'), diff --git a/packages/spec/src/eslint/react.js b/packages/spec/src/eslint/react.js index a7b235b..c4cad54 100644 --- a/packages/spec/src/eslint/react.js +++ b/packages/spec/src/eslint/react.js @@ -2,6 +2,8 @@ // ESlint config for ice and react project module.exports = { extends: [ + 'plugin:prettier/recommended', + require.resolve('eslint-config-prettier'), require.resolve('eslint-config-ali/react'), // For some ci and jest test env, we chose require.resolve instead 'plugin:@iceworks/best-practices/react' require.resolve('@iceworks/eslint-plugin-best-practices/src/configs/react'), diff --git a/packages/spec/src/eslint/vue-ts.js b/packages/spec/src/eslint/vue-ts.js index 5c87590..6a6495d 100644 --- a/packages/spec/src/eslint/vue-ts.js +++ b/packages/spec/src/eslint/vue-ts.js @@ -1,9 +1,7 @@ // https://www.npmjs.com/package/eslint-config-ali // ESlint config for Vue TypeScript project module.exports = { - extends: [ - require.resolve('eslint-config-ali/typescript/vue'), - ], + extends: [require.resolve('eslint-config-ali/typescript/vue')], rules: { // Change error to warn '@typescript-eslint/no-unused-vars': 'warn', diff --git a/packages/spec/src/eslint/vue.js b/packages/spec/src/eslint/vue.js index a5a19f8..8f2cd83 100644 --- a/packages/spec/src/eslint/vue.js +++ b/packages/spec/src/eslint/vue.js @@ -1,9 +1,7 @@ // https://www.npmjs.com/package/eslint-config-ali // ESlint config for Vue project module.exports = { - extends: [ - require.resolve('eslint-config-ali/vue'), - ], + extends: [require.resolve('eslint-config-ali/vue')], rules: { // Change error to warn 'no-unused-vars': 'warn',