Skip to content

Commit

Permalink
release: 1.0 for eslint 9 - migrate to stylistic rules
Browse files Browse the repository at this point in the history
- breaking: `@stylistic/eslint-plugin` rules migration
  following this guide https://eslint.style/guide/migration
- remove dependency `eslint-config-standard`, all rules imported (with deprecated rules replacing)
- bump `eslint-plugin-import` and restore rule 'import/export'
  ref import-js/eslint-plugin-import#2556

new / changed rules
- disable 'import/no-webpack-loader-syntax' (imported webmail rules)
- add 'no-restricted-globals' to forbid using global event (imported webmail rules)
- change 'generator-star-spacing' and 'yield-star-spacing' to always 'after'
  • Loading branch information
梁伟然 committed Oct 21, 2024
1 parent 425b77f commit 56a1aaf
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 143 deletions.
100 changes: 59 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@

# ESLint config files for Coremail (c)
[![npm][npm-image]][npm-url]
# eslint-config-email [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url]

[npm-image]: https://img.shields.io/npm/v/eslint-config-coremail.svg
[npm-url]: https://npmjs.org/package/eslint-config-coremail
[downloads-image]: https://img.shields.io/npm/dm/eslint-config-coremail.svg
[downloads-url]: https://npmjs.org/package/eslint-config-coremail

## Usage

This package exports [a flat ESLint configuration](https://eslint.org/blog/2022/08/new-config-system-part-2/)
which is supported since `eslint 8.21.0`

```bash
npm install --save-dev eslint eslint-config-coremail
```

Example `eslint.config.js` in esm:
```js
import {configs} from 'eslint-config-coremail';

export default [
// default using browser & node env
configs.standard,
// for old IE compatible
{
files : ['path/to/legacy/**'],
...configs.legacy,
},
]
```

Example `eslint.config.js` in commonjs:
```js
module.export = import('eslint-config-coremail').then(({configs}) => [
// default using browser & node env
configs.standard,
// for old IE compatible
{
files : ['path/to/legacy/**'],
...configs.legacy,
},
]);
```
#### The eslint config for [Coremail (c)](https://www.coremail.cn)

This module exports [a flat ESLint configuration](https://eslint.org/blog/2022/08/new-config-system-part-2/) which is
supported since `eslint 8.21.0`, basing [eslint-config-standard].

Notes that [eslint-config-standard] is likely [deprecated and stop maintaining](https://github.com/standard/eslint-config-standard/issues/351).
It's encouraged to switch to [neo-standard] for [standard] users.

This project is only focus on eslint config, and not providing framework tooling like [neo-standard].

[eslint-config-standard]: https://github.com/standard/eslint-config-standard
[neo-standard]: https://github.com/neostandard/neostandard
[standard]: https://standardjs.com

## Quick start

1. `npm install --save-dev eslint eslint-config-coremail`

2. For projects using esm, add `eslint.config.js` as:
```js
import {configs} from 'eslint-config-coremail';

export default [
// default enables browser & node env, you can change to `configs.node` for server side project
configs.standard, {
// your overrides here
}, {
// for legacy codes compliant with old IE
...configs.legacy,
files : ['path/to/legacy/codes/**'],
},
]
```

3. For projects using commonjs, add `eslint.config.js` as:
```js
module.exports = import('eslint-config-coremail').then(({configs}) => [
// default enables browser & node env, you can change to `configs.node` for server side project
configs.standard, {
// your overrides here
}, {
// for legacy codes compliant with old IE
...configs.legacy,
files : ['path/to/legacy/codes/**'],
},
]);
```

## Migrating from eslint-config-coremail 0.x / eslint-config-standard 17.x

1. Read [this article](https://eslint.org/blog/2024/04/eslint-v9.0.0-released/) for ESLint 9 migration
2. Migrate [deprecated ESLint style rules](https://eslint.org/blog/2023/10/deprecating-formatting-rules/)
with [eslint-stylistic](https://eslint.style/) rules following [this guide](https://eslint.style/guide/migration)

## Learn more

For the full listing of rules that eslint supports, and more, visit
For more information on the full listing of rules, editor plugins, FAQs, and more, visit the
[ESLint official site](http://eslint.org/docs/rules/).
For more information on the full listing of rules that eslint supports, and more, visit
- [ESLint official site](http://eslint.org/docs/rules/)
- [ESLint Stylistic](https://eslint.style/packages/default#rules)

## License

Expand Down
242 changes: 242 additions & 0 deletions config/imported.js

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions config/legacy.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
/**
* Copyright (c) 2023 Coremail.cn, Ltd. All Rights Reserved.
* Copyright (c) 2024 Coremail.cn, Ltd. All Rights Reserved.
*/

import {error} from './util.js';

export default {

languageOptions : {
ecmaVersion : 3,
sourceType : 'script',
parserOptions : {allowReserved : true},
},

rules : {
/* eslint-disable indent *//* @formatter:off */
plugins : {'@stylistic' : (await import('@stylistic/eslint-plugin')).default},

// here two rules are only for ES3 compliant / IE6 supports
'quote-props' : [error, 'as-needed', {
keywords : true,
unnecessary : false,
}],
'dot-notation' : [error, {
allowKeywords : false,
allowPattern : '.*',
}],
'comma-dangle' : [error, 'never'],
// ES3 compliant END
'no-tabs' : [error],
// rules only for ES3 compliant / IE6 ~ IE8
rules : {
'dot-notation' : [error, {allowKeywords : false, allowPattern : '.*'}],
'@stylistic/quote-props' : [error, 'as-needed', {keywords : true, unnecessary : false}],
'@stylistic/comma-dangle' : [error, 'never'],
'@stylistic/no-tabs' : [error],
},
};
137 changes: 67 additions & 70 deletions config/standard.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,82 @@
/**
* Copyright (c) 2023 Coremail.cn, Ltd. All Rights Reserved.
* Copyright (c) 2024 Coremail.cn, Ltd. All Rights Reserved.
*/

import standard from 'eslint-config-standard';
import {baseRules, plugins, styleRules} from './imported.js';
import {error, first, mergeRules, never, off} from './util.js';

export default {

languageOptions : {
ecmaVersion : 'latest',
parserOptions : {ecmaFeatures : {jsx : true}},
},

plugins : Object.fromEntries(await Promise.all(standard.plugins.map(async key => [
key, (await import(`eslint-plugin-${key}`)).default,
]))),
plugins,
rules : mergeRules(baseRules, styleRules, { /* eslint-disable @stylistic/indent */// @formatter:off
'no-var' : [error],
'@stylistic/no-tabs' : [error],
'@stylistic/indent' : [error, 4, {
SwitchCase : 0,
VariableDeclarator : {var : 1, let : 1, const : 1},
ImportDeclaration : first,
ArrayExpression : first,
ObjectExpression : first,
FunctionDeclaration : {parameters: first},
FunctionExpression : {parameters: first},
CallExpression : {arguments: 1},
MemberExpression : off,
ignoreComments : true,
ignoredNodes : styleRules['@stylistic/indent'][2].ignoredNodes,
}],
'@stylistic/semi' : [off],
'@stylistic/quotes' : [error, 'single', 'avoid-escape'],
'@stylistic/arrow-parens' : [error, 'as-needed'],
'@stylistic/operator-linebreak' : [error, 'before'], // ternary operator should have nothing special
'@stylistic/multiline-ternary' : [off],
'@stylistic/comma-dangle' : [error, { // more friendly to VCS system / manual code arrangements
arrays : 'always-multiline',
objects : 'always-multiline',
imports : 'always-multiline',
exports : 'always-multiline',
functions : 'only-multiline',
}],
'@stylistic/padded-blocks' : [off], // extra space in blocks should not be a problem
'prefer-const' : [off],
'yoda' : [off],
'no-new' : [off],
'no-void' : [off],
'no-case-declarations' : [off],
'no-inner-declarations' : [off],
'default-case' : [off],
'one-var' : [off],
'dot-notation' : [off],
'@stylistic/quote-props' : [off],
'@stylistic/object-curly-spacing' : [off],
'@stylistic/space-before-function-paren' : [error, {named: 'never', anonymous: 'ignore', asyncArrow: 'always'}],
'@stylistic/generator-star-spacing' : [error, 'after'],
'@stylistic/yield-star-spacing' : [error, 'after'],
'@stylistic/no-multiple-empty-lines' : [off],
'@stylistic/no-multi-spaces' : [error, {
exceptions: {VariableDeclarator: true, ImportDeclaration: true},
ignoreEOLComments: true,
}],
'@stylistic/key-spacing' : [off],
'camelcase' : [error, {
properties : never,
ignoreDestructuring : true,
ignoreGlobals : true,
}],

'array-callback-return' : [off],
'no-empty' : [off],
'no-restricted-globals' : [error, {
name : 'event',
message : 'Use local parameter instead.',
}],

/* eslint-disable indent */// @formatter:off
rules : mergeRules(standard.rules, {
'no-var' : [error],
'no-tabs' : [error],
'indent' : [error, 4, {
SwitchCase : 0,
VariableDeclarator : { var : 1, let : 1, const : 1 },
ImportDeclaration : first,
ArrayExpression : first,
ObjectExpression : first,
FunctionDeclaration : {parameters: first},
FunctionExpression : {parameters: first},
CallExpression : {arguments: 1},
MemberExpression : off,
ignoreComments : true,
}],
'semi' : [off, never],
'quotes' : [error, 'single', 'avoid-escape'],
'arrow-parens' : [error, 'as-needed'],
'operator-linebreak' : [error, 'before'], // corrected now, ternary operator should have nothing special
'comma-dangle' : [error, { // more friendly to VCS system / manual code arrangements
arrays : 'always-multiline',
objects : 'always-multiline',
imports : 'always-multiline',
exports : 'always-multiline',
functions : 'only-multiline',
}],
'padded-blocks' : [off], // extra space in blocks should not be a problem
'prefer-const' : [off],
'yoda' : [off],
'no-new' : [off],
'no-void' : [off],
'no-case-declarations' : [off],
'no-inner-declarations' : [off],
'default-case' : [off],
'one-var' : [off],
'dot-notation' : [off],
'quote-props' : [off],
'object-curly-spacing' : [off],
'space-before-function-paren' : [error, {named: never, anonymous: 'ignore', asyncArrow: 'always'}],
'space-before-blocks' : [error],
'no-multiple-empty-lines' : [off],
'no-multi-spaces' : [error, {
exceptions: {VariableDeclarator: true, ImportDeclaration: true},
ignoreEOLComments: true,
}],
'keyword-spacing' : [error],
'key-spacing' : [off],
'comma-spacing' : [error],
'space-infix-ops' : [error],
'spaced-comment' : [error],
'eol-last' : [error],
'camelcase' : [error, {
properties : never,
ignoreDestructuring : true,
ignoreGlobals : true,
}],
'multiline-ternary' : [off],
'array-callback-return' : [off],
'no-empty' : [off],
'n/no-path-concat' : [off],
'n/no-exports-assign' : [off],
'n/no-path-concat' : [off],
'n/no-exports-assign' : [off],

// waiting https://github.com/import-js/eslint-plugin-import/issues/2556
'import/export' : [off],
'import/no-webpack-loader-syntax' : [off],
}),
};
11 changes: 7 additions & 4 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// noinspection JSUnusedGlobalSymbols
/**
* Copyright (c) 2024 Coremail.cn, Ltd. All Rights Reserved.
*/

import {configs} from './config/index.js';

export default [configs.es, {
export default [configs.node, {
files : ['test/samples/**'],
rules : {
// disable debatable rules
'semi' : 'off',
'quotes' : 'off',
'@stylistic/semi' : 'off',
'@stylistic/quotes' : 'off',

// sample test codes have many unused / undeclared symbols
'no-undef' : 'off',
'no-unused-vars' : 'off',
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "eslint-config-coremail",
"description" : "Javascript Standard Style - ESLint Shareable Config for coremail.cn",
"version" : "0.99.1",
"version" : "1.0.2",
"author" : {
"name" : "William Leung",
"email" : "[email protected]"
Expand All @@ -24,6 +24,10 @@

"license" : "MIT",
"main" : "config/index.js",
"exports" : {
"." : "./config/index.js",
"./package.json" : "./package.json"
},
"files" : [
"config/*"
],
Expand All @@ -33,15 +37,18 @@
},

"peerDependencies" : {
"eslint" : "^8.52.0",
"eslint" : "^9.13.0",
"globals" : "*"
},

"devDependencies" : {
"eslint" : "~8.52.0"
"eslint" : "~9.13.0"
},

"dependencies" : {
"eslint-config-standard" : "~17.1.0"
"eslint-plugin-import" : "^2.31.0",
"eslint-plugin-n" : "^17.11.1",
"eslint-plugin-promise" : "^7.1.0",
"@stylistic/eslint-plugin" : "^2.9.0"
}
}
4 changes: 4 additions & 0 deletions test/samples/esm1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//

export const one = 1;
export const two = 2;
3 changes: 3 additions & 0 deletions test/samples/esm2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//

export * from './esm1.js'
5 changes: 2 additions & 3 deletions test/samples/sample1-indent-const.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// The indent rule do not supports '1.5' as unit (6 space) or other multiline alignment technic
/* eslint indent: "off" */
// The indent rule does not support '1.5' as unit (6 space) or other multiline alignment technic
/* eslint @stylistic/indent: "off" */

// noinspection JSUnusedGlobalSymbols
const A = 'a',
B = 'b',
C = 'c';

Loading

0 comments on commit 56a1aaf

Please sign in to comment.