Skip to content

Commit 2c914d4

Browse files
authored
prefer-node-protocol: Always check require() (#1827)
1 parent 989fe9d commit 2c914d4

12 files changed

+38
-91
lines changed

docs/rules/prefer-node-protocol.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export {strict as default} from 'assert';
2525
import fs from 'fs/promises';
2626
```
2727

28+
```js
29+
const fs = require('fs/promises');
30+
```
31+
2832
## Pass
2933

3034
```js
@@ -51,20 +55,6 @@ import _ from 'lodash';
5155
import fs from './fs.js';
5256
```
5357

54-
## Options
55-
56-
Type: `object`
57-
58-
### `checkRequire`
59-
60-
Type: `boolean`\
61-
Default: `false`
62-
63-
Currently, `require(…)` with the `node:` protocol is only available on Node.js 16. If you don't care about old versions, you can set this to `true`.
64-
65-
We'll remove this option and check `require(…)` by default once this feature get backported to v12.
66-
6758
```js
68-
// eslint unicorn/prefer-node-protocol: ["error", {"checkRequire": true}]
69-
const fs = require('fs'); // Fails
59+
const fs = require('node:fs/promises');
7060
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"engines": {
14-
"node": ">=14"
14+
"node": ">=14.18"
1515
},
1616
"scripts": {
1717
"create-rule": "node ./scripts/create-rule.mjs && npm run generate-rule-notices && npm run generate-rules-table",

rules/filename-case.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const path = require('path');
2+
const path = require('node:path');
33
const {camelCase, kebabCase, snakeCase, upperFirst} = require('lodash');
44
const cartesianProductSamples = require('./utils/cartesian-product-samples.js');
55

rules/prefer-node-protocol.js

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,31 @@ const importExportSourceSelector = [
1414
'Literal.source',
1515
].join('');
1616

17-
/** @param {import('eslint').Rule.RuleContext} context */
18-
const create = context => {
19-
const {checkRequire} = {
20-
checkRequire: false,
21-
...context.options[0],
22-
};
23-
const selectors = [importExportSourceSelector];
24-
if (checkRequire) {
25-
selectors.push(STATIC_REQUIRE_SOURCE_SELECTOR);
26-
}
17+
const selector = matches([
18+
importExportSourceSelector,
19+
STATIC_REQUIRE_SOURCE_SELECTOR,
20+
]);
2721

28-
return {
29-
[matches(selectors)](node) {
30-
const {value} = node;
31-
if (
32-
typeof value !== 'string'
33-
|| value.startsWith('node:')
34-
|| !isBuiltinModule(value)
35-
) {
36-
return;
37-
}
22+
const create = () => ({
23+
[selector](node) {
24+
const {value} = node;
25+
if (
26+
typeof value !== 'string'
27+
|| value.startsWith('node:')
28+
|| !isBuiltinModule(value)
29+
) {
30+
return;
31+
}
3832

39-
return {
40-
node,
41-
messageId: MESSAGE_ID,
42-
data: {moduleName: value},
43-
/** @param {import('eslint').Rule.RuleFixer} fixer */
44-
fix: fixer => replaceStringLiteral(fixer, node, 'node:', 0, 0),
45-
};
46-
},
47-
};
48-
};
49-
50-
const schema = [
51-
{
52-
type: 'object',
53-
additionalProperties: false,
54-
properties: {
55-
checkRequire: {
56-
type: 'boolean',
57-
default: false,
58-
},
59-
},
33+
return {
34+
node,
35+
messageId: MESSAGE_ID,
36+
data: {moduleName: value},
37+
/** @param {import('eslint').Rule.RuleFixer} fixer */
38+
fix: fixer => replaceStringLiteral(fixer, node, 'node:', 0, 0),
39+
};
6040
},
61-
];
41+
});
6242

6343
/** @type {import('eslint').Rule.RuleModule} */
6444
module.exports = {
@@ -69,7 +49,6 @@ module.exports = {
6949
description: 'Prefer using the `node:` protocol when importing Node.js builtin modules.',
7050
},
7151
fixable: 'code',
72-
schema,
7352
messages,
7453
},
7554
};

rules/prevent-abbreviations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const path = require('path');
2+
const path = require('node:path');
33
const {defaultsDeep, upperFirst, lowerFirst} = require('lodash');
44

55
const avoidCapture = require('./utils/avoid-capture.js');

rules/utils/get-documentation-url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const path = require('path');
2+
const path = require('node:path');
33
const packageJson = require('../../package.json');
44

55
const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn';

rules/utils/rule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
const path = require('path');
3-
const fs = require('fs');
2+
const path = require('node:path');
3+
const fs = require('node:fs');
44
const getDocumentationUrl = require('./get-documentation-url.js');
55

66
const isIterable = object => typeof object[Symbol.iterator] === 'function';

scripts/internal-rules/prefer-disallow-over-forbid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const path = require('path');
2+
const path = require('node:path');
33
const {matches} = require('../../rules/selectors/index.js');
44
const toLocation = require('../../rules/utils/to-location.js');
55

scripts/internal-rules/prefer-negative-boolean-attribute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const path = require('path');
2+
const path = require('node:path');
33

44
const messageId = path.basename(__filename, '.js');
55

test/prefer-node-protocol.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ test.snapshot({
88
'import unicorn from "unicorn";',
99
'import fs from "./fs";',
1010
'import fs from "unknown-builtin-module";',
11-
'const fs = require("fs");',
1211
'import fs from "node:fs";',
1312
outdent`
1413
async function foo() {
@@ -60,8 +59,7 @@ test.snapshot({
6059
],
6160
});
6261

63-
// `options`
64-
const checkRequireOptions = [{checkRequire: true}];
62+
// `require`
6563
test.snapshot({
6664
valid: [
6765
'const fs = require("node:fs");',
@@ -76,11 +74,11 @@ test.snapshot({
7674
'const fs = require();',
7775
'const fs = require(...["fs"]);',
7876
'const fs = require("unicorn");',
79-
].map(code => ({code, options: checkRequireOptions})),
77+
],
8078
invalid: [
8179
'const {promises} = require("fs")',
8280
'const fs = require(\'fs/promises\')',
83-
].map(code => ({code, options: checkRequireOptions})),
81+
],
8482
});
8583

8684
test.babel({

0 commit comments

Comments
 (0)