Skip to content

Commit 425b77f

Browse files
committed
release: 0.99 breaking, switch to flat, package renamed back to eslint-config-coremail
ref https://eslint.org/docs/latest/use/configure/configuration-files-new - breaking: switch to flat ESLint configuration - breaking: package renamed back to eslint-config-coremail
1 parent 1072157 commit 425b77f

File tree

10 files changed

+169
-83
lines changed

10 files changed

+169
-83
lines changed

README.md

+31-37
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,45 @@
22
# ESLint config files for Coremail (c)
33
[![npm][npm-image]][npm-url]
44

5-
[npm-image]: https://img.shields.io/npm/v/eslint-plugin-coremail.svg
6-
[npm-url]: https://npmjs.org/package/eslint-plugin-coremail
7-
8-
## Important Note
9-
10-
Since eslint 6 changes its config resolving mechanism, config module can not share outside project root.
11-
12-
<ins>**This project has been changed to a plugin module and renamed (only package name).**</ins>
13-
14-
For more information, check this issue: https://github.com/eslint/eslint/issues/12654
5+
[npm-image]: https://img.shields.io/npm/v/eslint-config-coremail.svg
6+
[npm-url]: https://npmjs.org/package/eslint-config-coremail
157

168
## Usage
179

18-
To use this config, add this to your `package.json`
19-
20-
```json
21-
{
22-
"devDependencies" : {
23-
"eslint" : "8.46.0",
24-
"eslint-plugin-coremail" : "0.5.0"
25-
}
26-
}
27-
```
28-
29-
or install manually
10+
This package exports [a flat ESLint configuration](https://eslint.org/blog/2022/08/new-config-system-part-2/)
11+
which is supported since `eslint 8.21.0`
3012

3113
```bash
32-
npm install eslint eslint-plugin-coremail
14+
npm install --save-dev eslint eslint-config-coremail
3315
```
3416

17+
Example `eslint.config.js` in esm:
18+
```js
19+
import {configs} from 'eslint-config-coremail';
20+
21+
export default [
22+
// default using browser & node env
23+
configs.standard,
24+
// for old IE compatible
25+
{
26+
files : ['path/to/legacy/**'],
27+
...configs.legacy,
28+
},
29+
]
30+
```
3531

36-
Then, add this to your `.eslintrc.yaml` file:
37-
38-
- Using standard rules
39-
40-
```yaml
41-
extends: plugin:coremail/standard
42-
```
43-
44-
- Applying compliant rules (old IE compatible)
45-
46-
```yaml
47-
extends: plugin:coremail/compliant
48-
```
49-
32+
Example `eslint.config.js` in commonjs:
33+
```js
34+
module.export = import('eslint-config-coremail').then(({configs}) => [
35+
// default using browser & node env
36+
configs.standard,
37+
// for old IE compatible
38+
{
39+
files : ['path/to/legacy/**'],
40+
...configs.legacy,
41+
},
42+
]);
43+
```
5044

5145
## Learn more
5246

config/index.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
/**
2-
* Copyright (c) 2020 Coremail.cn, Ltd. All Rights Reserved.
2+
* Copyright (c) 2023 Coremail.cn, Ltd. All Rights Reserved.
33
*/
44

5-
module.exports = {
6-
configs : {
7-
standard : require('./standard'),
8-
compliant : require('./compliant'),
9-
},
5+
import {use} from './util.js';
6+
import es from './standard.js';
7+
import es3 from './legacy.js';
8+
9+
export {globals, use, mergeRules} from './util.js';
10+
export const configs = {
11+
es,
12+
node : use(es, 'node'),
13+
browser : use(es, 'browser'),
14+
standard : use(es, 'node', 'browser'),
15+
legacy : use(es3, 'builtin', 'browser'),
1016
};

config/compliant.js renamed to config/legacy.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
* Copyright (c) 2023 Coremail.cn, Ltd. All Rights Reserved.
33
*/
44

5-
const error = 'error';
5+
import {error} from './util.js';
66

7-
module.exports = {
7+
export default {
88

9-
parserOptions : {ecmaVersion : 3, allowReserved : true},
9+
languageOptions : {
10+
ecmaVersion : 3,
11+
sourceType : 'script',
12+
parserOptions : {allowReserved : true},
13+
},
1014

1115
rules : {
1216
/* eslint-disable indent *//* @formatter:off */

config/standard.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
* Copyright (c) 2023 Coremail.cn, Ltd. All Rights Reserved.
33
*/
44

5-
const error = 'error', off = 'off', first = 'first', never = 'never';
5+
import standard from 'eslint-config-standard';
6+
import {error, first, mergeRules, never, off} from './util.js';
67

7-
module.exports = {
8+
export default {
89

9-
extends : ['standard'],
10-
parserOptions : { ecmaVersion : 2024 },
10+
languageOptions : {
11+
ecmaVersion : 'latest',
12+
parserOptions : {ecmaFeatures : {jsx : true}},
13+
},
14+
15+
plugins : Object.fromEntries(await Promise.all(standard.plugins.map(async key => [
16+
key, (await import(`eslint-plugin-${key}`)).default,
17+
]))),
1118

1219
/* eslint-disable indent */// @formatter:off
13-
rules : {
20+
rules : mergeRules(standard.rules, {
1421
'no-var' : [error],
1522
'no-tabs' : [error],
1623
'indent' : [error, 4, {
@@ -71,5 +78,8 @@ module.exports = {
7178
'no-empty' : [off],
7279
'n/no-path-concat' : [off],
7380
'n/no-exports-assign' : [off],
74-
},
81+
82+
// waiting https://github.com/import-js/eslint-plugin-import/issues/2556
83+
'import/export' : [off],
84+
}),
7585
};

config/util.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (c) 2023 Coremail.cn, Ltd. All Rights Reserved.
3+
*/
4+
5+
import {createRequire} from 'module';
6+
7+
export const error = 'error', off = 'off', first = 'first', never = 'never';
8+
9+
// try using 'globals' defined in the project first, it can be different between one required by eslint itself
10+
export const globals = (require => {
11+
// projectDir (../../..)
12+
// +- node_modules (../..)
13+
// +- eslint
14+
// +- globals
15+
// +- eslint-config-email (..)
16+
// +- config (this location)
17+
try {
18+
// noinspection JSFileReferences
19+
return require('../../../node_modules/globals');
20+
} catch {
21+
return require('globals');
22+
}
23+
})(createRequire(import.meta.url));
24+
25+
// merge configuration objects or env into one
26+
export function use(...envs) {
27+
const result = {};
28+
envs.forEach(x => Object.entries((typeof x === 'string') ? {globals : globals[x]} : x).forEach(([key, val]) => {
29+
switch (key) {
30+
case 'languageOptions':
31+
const {globals, ...remains} = val;
32+
result[key] = {...result[key], ...remains};
33+
return addGlobals(result, globals);
34+
35+
case 'globals':
36+
return addGlobals(result, val);
37+
38+
case 'rules':
39+
return (result[key] = mergeRules(result[key], val));
40+
41+
case 'linterOptions':
42+
case 'plugins':
43+
case 'settings':
44+
return (result[key] = {...result[key], ...val});
45+
}
46+
result[key] = val;
47+
}));
48+
return result;
49+
}
50+
51+
function addGlobals(x, globals) {
52+
const languageOptions = x.languageOptions = (x.languageOptions || {});
53+
languageOptions.globals = {...x.languageOptions.globals, ...globals};
54+
}
55+
56+
57+
// ref https://eslint.org/docs/latest/use/configure/configuration-files-new#rule-configuration-cascade
58+
// cascaded rules only override severity if passing in a simple value
59+
export function mergeRules(...rules) {
60+
const result = {};
61+
rules.forEach(rules => Object.entries(rules || {}).forEach(([ruleId, def]) => {
62+
if (Array.isArray(result[ruleId])) {
63+
if (typeof def === 'string') {
64+
return (result[ruleId][0] = def);
65+
} else if (def?.length === 1 && typeof def?.[0] === 'string') {
66+
return (result[ruleId][0] = def[0]);
67+
}
68+
}
69+
result[ruleId] = def;
70+
}));
71+
return result;
72+
}

eslint.config.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// noinspection JSUnusedGlobalSymbols
2+
3+
import {configs} from './config/index.js';
4+
5+
export default [configs.es, {
6+
files : ['test/samples/**'],
7+
rules : {
8+
// disable debatable rules
9+
'semi' : 'off',
10+
'quotes' : 'off',
11+
// sample test codes have many unused / undeclared symbols
12+
'no-undef' : 'off',
13+
'no-unused-vars' : 'off',
14+
'no-unreachable' : 'off',
15+
'no-constant-condition' : 'off',
16+
'no-use-before-define' : 'off',
17+
},
18+
}, {
19+
files : ['test/samples/es3/**'],
20+
...configs.legacy,
21+
}];

package.json

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2-
"name" : "eslint-plugin-coremail",
2+
"name" : "eslint-config-coremail",
33
"description" : "Javascript Standard Style - ESLint Shareable Config for coremail.cn",
4-
"version" : "0.5.0",
4+
"version" : "0.99.1",
55
"author" : {
66
"name" : "William Leung",
77
"email" : "[email protected]"
88
},
99

10+
"type" : "module",
11+
1012
"keywords" : [
1113
"code linter",
1214
"code sytle",
@@ -22,28 +24,24 @@
2224

2325
"license" : "MIT",
2426
"main" : "config/index.js",
27+
"files" : [
28+
"config/*"
29+
],
2530

2631
"scripts" : {
2732
"test" : "eslint ."
2833
},
2934

3035
"peerDependencies" : {
31-
"eslint" : "^8.46.0"
36+
"eslint" : "^8.52.0",
37+
"globals" : "*"
3238
},
3339

3440
"devDependencies" : {
35-
"eslint" : "~8.46.0"
41+
"eslint" : "~8.52.0"
3642
},
3743

3844
"dependencies" : {
39-
"eslint-plugin-promise" : "~6.1.1",
40-
"eslint-plugin-import" : "~2.28.0",
41-
"eslint-plugin-node" : "~11.1.0",
4245
"eslint-config-standard" : "~17.1.0"
43-
},
44-
45-
"eslintConfig" : {
46-
"root" : true,
47-
"extends" : "./config/standard.js"
4846
}
4947
}

test/eslint.sh

-3
This file was deleted.

test/samples/.eslintrc.yaml

-12
This file was deleted.

test/samples/es3/.eslintrc

-4
This file was deleted.

0 commit comments

Comments
 (0)