Skip to content

Commit 056364d

Browse files
authored
prefer-node-protocol: Support process.getBuiltinModule() (#2611)
1 parent eb08fc5 commit 056364d

File tree

5 files changed

+79
-17
lines changed

5 files changed

+79
-17
lines changed

docs/rules/prefer-node-protocol.md

+21-15
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,46 @@
77
<!-- end auto-generated rule header -->
88
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->
99

10-
When importing builtin modules, it's better to use the [`node:` protocol](https://nodejs.org/api/esm.html#node-imports) as it makes it perfectly clear that the package is a Node.js builtin module.
10+
When getting builtin modules, it's better to use the [`node:` protocol](https://nodejs.org/api/esm.html#node-imports) as it makes it perfectly clear that the package is a Node.js builtin module.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
import dgram from 'dgram';
17+
18+
//
19+
import dgram from 'node:dgram';
1620
```
1721

1822
```js
23+
//
1924
export {strict as default} from 'assert';
25+
26+
//
27+
export {strict as default} from 'node:assert';
2028
```
2129

2230
```js
31+
//
2332
import fs from 'fs/promises';
24-
```
25-
26-
## Pass
2733

28-
```js
29-
import dgram from 'node:dgram';
34+
//
35+
import fs from 'node:fs/promises';
3036
```
3137

3238
```js
33-
export {strict as default} from 'node:assert';
34-
```
39+
//
40+
const fs = require('fs/promises');
3541

36-
```js
37-
import fs from 'node:fs/promises';
42+
//
43+
const fs = require('node:fs/promises');
3844
```
3945

4046
```js
41-
import _ from 'lodash';
42-
```
47+
//
48+
const fs = process.getBuiltinModule('fs/promises');
4349

44-
```js
45-
import fs from './fs.js';
50+
//
51+
const fs = process.getBuiltinModule('node:fs/promises');
4652
```

rules/prefer-node-protocol.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import isBuiltinModule from 'is-builtin-module';
2-
import isStaticRequire from './ast/is-static-require.js';
2+
import {
3+
isStaticRequire,
4+
isMethodCall,
5+
} from './ast/index.js';
36

47
const MESSAGE_ID = 'prefer-node-protocol';
58
const messages = {
@@ -19,7 +22,16 @@ const create = context => ({
1922
&& node.parent.source === node
2023
)
2124
|| (
22-
isStaticRequire(node.parent)
25+
(
26+
isMethodCall(node.parent, {
27+
object: 'process',
28+
method: 'getBuiltinModule',
29+
argumentsLength: 1,
30+
optionalCall: false,
31+
optionalMember: false,
32+
})
33+
|| isStaticRequire(node.parent)
34+
)
2335
&& node.parent.arguments[0] === node
2436
)
2537
)) {

test/prefer-node-protocol.js

+23
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ test.snapshot({
9292
],
9393
});
9494

95+
// `process.getBuiltinModule`
96+
test.snapshot({
97+
valid: [
98+
'const fs = process.getBuiltinModule("node:fs")',
99+
'const fs = process.getBuiltinModule?.("fs")',
100+
'const fs = process?.getBuiltinModule("fs")',
101+
'const fs = process.notGetBuiltinModule("fs")',
102+
'const fs = notProcess.getBuiltinModule("fs")',
103+
'const fs = process.getBuiltinModule("fs", extra)',
104+
'const fs = process.getBuiltinModule(...["fs"])',
105+
'const fs = process.getBuiltinModule()',
106+
'const fs = process.getBuiltinModule("unicorn")',
107+
// Not checking this to avoid false positive
108+
outdent`
109+
import {getBuiltinModule} from 'node:process';
110+
const fs = getBuiltinModule("fs");
111+
`,
112+
],
113+
invalid: [
114+
'const fs = process.getBuiltinModule("fs")',
115+
],
116+
});
117+
95118
test.babel({
96119
valid: [
97120
'export fs from "node:fs";',

test/snapshots/prefer-node-protocol.js.md

+21
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,24 @@ Generated by [AVA](https://avajs.dev).
363363
> 1 | const fs = require('fs/promises')␊
364364
| ^^^^^^^^^^^^^ Prefer \`node:fs/promises\` over \`fs/promises\`.␊
365365
`
366+
367+
## invalid(1): const fs = process.getBuiltinModule("fs")
368+
369+
> Input
370+
371+
`␊
372+
1 | const fs = process.getBuiltinModule("fs")␊
373+
`
374+
375+
> Output
376+
377+
`␊
378+
1 | const fs = process.getBuiltinModule("node:fs")␊
379+
`
380+
381+
> Error 1/1
382+
383+
`␊
384+
> 1 | const fs = process.getBuiltinModule("fs")␊
385+
| ^^^^ Prefer \`node:fs\` over \`fs\`.␊
386+
`
54 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)