Skip to content

Commit f4bcafe

Browse files
zhaoxingyue年翼
and
年翼
authoredNov 11, 2020
feat: 修改 rules/node 中已被 eslint 废弃的 node rules,引入 eslint-plugin-node 及自定规则 (#57)
* fix(node): 修改已被 eslint 废弃的 node rules,增加 eslint-plugin-node 及自定规则 * style(node): 修改代码格式 * fix: 关闭 prefer-global 对不常用变量的限制 Co-authored-by: 年翼 <nianyi.zxy@alibaba-inc.com>
1 parent 1235d9a commit f4bcafe

10 files changed

+326
-31
lines changed
 

‎lib/rules/node.js

+195-31
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,230 @@ module.exports = {
44
env: {
55
node: true,
66
},
7-
7+
plugins: [
8+
'node',
9+
],
810
rules: {
911
/**
10-
* not enforce return after a callback
11-
* @see http://eslint.org/docs/rules/callback-return
12+
* disallow new operators with calls to require
13+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-new-require.md
14+
*/
15+
'node/no-new-require': 'error',
16+
17+
/**
18+
* enforce either Buffer or require("buffer").Buffer
19+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/buffer.md
20+
*/
21+
'node/prefer-global/buffer': [ 'error', 'always' ],
22+
23+
/**
24+
* enforce either console or require("console")
25+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/console.md
26+
*/
27+
'node/prefer-global/console': [ 'error', 'always' ],
28+
29+
/**
30+
* enforce either process or require("process")
31+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/process.md
32+
*/
33+
'node/prefer-global/process': [ 'error', 'always' ],
34+
35+
/**
36+
* enforce either TextDecoder or require("util").TextDecoder
37+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/text-decoder.md
38+
*/
39+
'node/prefer-global/text-decoder': 'off',
40+
41+
/**
42+
* enforce either TextEncoder or require("util").TextEncoder
43+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/text-encoder.md
44+
*/
45+
'node/prefer-global/text-encoder': 'off',
46+
47+
/**
48+
* enforce either URLSearchParams or require("url").URLSearchParams
49+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/url-search-params.md
50+
*/
51+
'node/prefer-global/url-search-params': 'off',
52+
53+
/**
54+
* enforce either URL or require("url").URL
55+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-global/url.md
56+
*/
57+
'node/prefer-global/url': 'off',
58+
59+
/**
60+
* enforce require("dns").promises
61+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-promises/dns.md
62+
*/
63+
'node/prefer-promises/dns': 'warn',
64+
65+
/**
66+
* enforce require("fs").promises
67+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/prefer-promises/fs.md
68+
*/
69+
'node/prefer-promises/fs': 'warn',
70+
71+
/**
72+
* disallow import declarations which import private modules
73+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unpublished-import.md
74+
*/
75+
'node/no-unpublished-import': 'off',
76+
77+
/**
78+
* disallow unsupported ECMAScript built-ins on the specified version
79+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unsupported-features/es-builtins.md
80+
*/
81+
'node/no-unsupported-features/es-builtins': 'off',
82+
83+
/**
84+
* suggest correct usage of shebang
85+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/shebang.md
86+
*/
87+
'node/shebang': 'off',
88+
89+
/**
90+
* disallow unsupported ECMAScript syntax on the specified version
91+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unsupported-features/es-syntax.md
92+
*/
93+
'node/no-unsupported-features/es-syntax': 'off',
94+
95+
/**
96+
* disallow unsupported Node.js built-in APIs on the specified version
97+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unsupported-features/node-builtins.md
98+
*/
99+
'node/no-unsupported-features/node-builtins': 'off',
100+
101+
/**
102+
* make process.exit() expressions the same code path as throw
103+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/process-exit-as-throw.md
104+
*/
105+
'node/process-exit-as-throw': 'off',
106+
107+
/**
108+
* disallow deprecated APIs
109+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-deprecated-api.md
110+
*/
111+
'node/no-deprecated-api': 'off',
112+
113+
/**
114+
* require error handling in callbacks
115+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/handle-callback-err.md
116+
*/
117+
'node/handle-callback-err': 'off',
118+
119+
/**
120+
* ensure Node.js-style error-first callback pattern is followed
121+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-callback-literal.md
122+
*/
123+
'node/no-callback-literal': 'off',
124+
125+
/**
126+
* disallow the assignment to exports
127+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-exports-assign.md
128+
*/
129+
'node/no-exports-assign': 'off',
130+
131+
/**
132+
* disallow import declarations which import extraneous modules
133+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-extraneous-import.md
134+
*/
135+
'node/no-extraneous-import': 'off',
136+
137+
/**
138+
* disallow require() expressions which import extraneous modules
139+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-extraneous-require.md
140+
*/
141+
'node/no-extraneous-require': 'off',
142+
143+
/**
144+
* disallow import declarations which import non-existence modules
145+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-missing-import.md
146+
*/
147+
'node/no-missing-import': 'off',
148+
149+
/**
150+
* disallow require() expressions which import non-existence modules
151+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-missing-require.md
152+
*/
153+
'node/no-missing-require': 'off',
154+
155+
/**
156+
* disallow string concatenation with __dirname and __filename
157+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-path-concat.md
158+
*/
159+
'node/no-path-concat': 'off',
160+
161+
/**
162+
* disallow the use of process.exit()
163+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-process-exit.md
164+
*/
165+
'node/no-process-exit': 'off',
166+
167+
/**
168+
* disallow bin files that npm ignores
169+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unpublished-bin.md
170+
*/
171+
'node/no-unpublished-bin': 'off',
172+
173+
/**
174+
* disallow require() expressions which import private modules
175+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-unpublished-require.md
12176
*/
13-
'callback-return': 'off',
177+
'node/no-unpublished-require': 'off',
14178

15179
/**
16-
* not require all requires be top-level
17-
* @see http://eslint.org/docs/rules/global-require
180+
* require return statements after callbacks
181+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/callback-return.md
18182
*/
19-
'global-require': 'off',
183+
'node/callback-return': 'off',
20184

21185
/**
22-
* not enforces error handling in callbacks (node environment)
23-
* @see http://eslint.org/docs/rules/handle-callback-err
186+
* enforce the style of file extensions in import declarations
187+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/file-extension-in-import.md
24188
*/
25-
'handle-callback-err': 'off',
189+
'node/file-extension-in-import': 'off',
26190

27191
/**
28-
* allow mixing regular variable and require declarations
29-
* @see http://eslint.org/docs/rules/no-mixed-requires
192+
* equire require() calls to be placed at top-level module scope
193+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/global-require.md
30194
*/
31-
'no-mixed-requires': 'off',
195+
'node/global-require': 'off',
32196

33197
/**
34-
* allow use of new operator with the require function
35-
* @see http://eslint.org/docs/rules/no-new-require
198+
* disallow require calls to be mixed with regular variable declarations
199+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-mixed-requires.md
36200
*/
37-
'no-new-require': 'off',
201+
'node/no-mixed-requires': 'off',
38202

39203
/**
40-
* allow string concatenation with __dirname and __filename
41-
* @see http://eslint.org/docs/rules/no-path-concat
204+
* disallow the use of process.env
205+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-process-env.md
42206
*/
43-
'no-path-concat': 'off',
207+
'node/no-process-env': 'off',
44208

45209
/**
46-
* allow use of process.env
47-
* @see http://eslint.org/docs/rules/no-process-env
210+
* disallow specified modules when loaded by import declarations
211+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-restricted-import.md
48212
*/
49-
'no-process-env': 'off',
213+
'node/no-restricted-import': 'off',
50214

51215
/**
52-
* allow process.exit()
53-
* @see http://eslint.org/docs/rules/no-process-exit
216+
* disallow specified modules when loaded by require
217+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-restricted-require.md
54218
*/
55-
'no-process-exit': 'off',
219+
'node/no-restricted-require': 'off',
56220

57221
/**
58-
* not restrict usage of specified node modules
59-
* @see http://eslint.org/docs/rules/no-restricted-modules
222+
* disallow synchronous methods
223+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-sync.md
60224
*/
61-
'no-restricted-modules': 'off',
225+
'node/no-sync': 'off',
62226

63227
/**
64-
* allow use of synchronous methods
65-
* @see http://eslint.org/docs/rules/no-sync
228+
* enforce either module.exports or exports
229+
* @see https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/exports-style.md
66230
*/
67-
'no-sync': 'off',
231+
'node/exports-style': 'off',
68232
},
69233
};

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"eslint-plugin-import": "^2.14.0",
2020
"eslint-plugin-jsdoc": "^4.1.1",
2121
"eslint-plugin-jsx-a11y": "^6.1.1",
22+
"eslint-plugin-node": "^11.1.0",
2223
"eslint-plugin-react": "^7.11.1"
2324
},
2425
"devDependencies": {

‎test/fixtures/node-app/.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../index.js"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
const fs = new require('fs');
4+
console.log(fs);
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
console.log(fs);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const { Buffer } = require('buffer');
4+
Buffer.alloc(16);
5+
6+
const console = require('console');
7+
console.log('hello');
8+
9+
const process = require('process');
10+
process.exit(0);
11+
12+
const { TextDecoder } = require('util');
13+
new TextDecoder();
14+
15+
const { TextEncoder } = require('util');
16+
new TextEncoder()
17+
18+
const { URLSearchParams } = require('url');
19+
new URLSearchParams();
20+
21+
const { URL } = require('url');
22+
new URL();
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
Buffer.alloc(16);
4+
console.log('hello');
5+
new TextDecoder();
6+
new TextEncoder();
7+
new URLSearchParams();
8+
new URL();
9+
process.exit(0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const dns = require('dns');
4+
const fs = require('fs');
5+
6+
() => {
7+
fs.readFile('./.eslintrc', 'utf8', (error, content) => {
8+
console.log(error, content);
9+
});
10+
};
11+
12+
hostname => {
13+
dns.lookup(hostname, (error, address, family) => {
14+
console.log(error, address, family);
15+
});
16+
};
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const { promises: dns } = require('dns');
4+
const { promises: fs } = require('fs');
5+
6+
const readFile = async filePath => {
7+
const content = await fs.readFile(filePath, 'utf8');
8+
console.log(content);
9+
};
10+
readFile();
11+
12+
const lookup = async hostname => {
13+
const { address, family } = await dns.lookup(hostname);
14+
console.log(address, family);
15+
};
16+
lookup();

‎test/node.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const coffee = require('coffee');
5+
6+
describe('test/node.test.js', () => {
7+
const cwd = path.join(__dirname, 'fixtures/node-app');
8+
9+
describe('no-new-require', () => {
10+
it('should success', () => {
11+
return coffee.spawn('eslint', [ 'no-new-require.js' ], { cwd })
12+
.expect('code', 0)
13+
.end();
14+
});
15+
16+
it('should fail with new require', () => {
17+
return coffee.spawn('eslint', [ 'no-new-require-fail.js' ], { cwd })
18+
.expect('stdout', /no-new-require/)
19+
.expect('code', 1)
20+
.end();
21+
});
22+
});
23+
24+
describe('prefer-promises', () => {
25+
it('should success', () => {
26+
return coffee.spawn('eslint', [ 'prefer-promises.js' ], { cwd })
27+
.expect('code', 0)
28+
.end();
29+
});
30+
31+
it('should fail with unused promises', () => {
32+
return coffee.spawn('eslint', [ 'prefer-promises-fail.js' ], { cwd })
33+
.expect('stdout', /node\/prefer-promises\/fs/)
34+
.expect('stdout', /node\/prefer-promises\/dns/)
35+
.expect('code', 0)
36+
.end();
37+
});
38+
});
39+
40+
describe('prefer-global', () => {
41+
it('should success', () => {
42+
return coffee.spawn('eslint', [ 'prefer-global.js' ], { cwd })
43+
.expect('code', 0)
44+
.end();
45+
});
46+
47+
it('should fail with unused the global variable', () => {
48+
return coffee.spawn('eslint', [ 'prefer-global-fail.js' ], { cwd })
49+
.expect('stdout', /prefer-global\/buffer/)
50+
.expect('stdout', /prefer-global\/console/)
51+
.expect('stdout', /prefer-global\/process/)
52+
.expect('code', 1)
53+
.end();
54+
});
55+
});
56+
});

0 commit comments

Comments
 (0)
Please sign in to comment.