diff --git a/docs/rules/prefer-node-protocol.md b/docs/rules/prefer-node-protocol.md index 6cfd07107a..ab4a507d77 100644 --- a/docs/rules/prefer-node-protocol.md +++ b/docs/rules/prefer-node-protocol.md @@ -25,6 +25,10 @@ export {strict as default} from 'assert'; import fs from 'fs/promises'; ``` +```js +const fs = require('fs/promises'); +``` + ## Pass ```js @@ -51,20 +55,6 @@ import _ from 'lodash'; import fs from './fs.js'; ``` -## Options - -Type: `object` - -### `checkRequire` - -Type: `boolean`\ -Default: `false` - -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`. - -We'll remove this option and check `require(…)` by default once this feature get backported to v12. - ```js -// eslint unicorn/prefer-node-protocol: ["error", {"checkRequire": true}] -const fs = require('fs'); // Fails +const fs = require('node:fs/promises'); ``` diff --git a/package.json b/package.json index a97a1ca8bc..30004828cd 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "url": "https://sindresorhus.com" }, "engines": { - "node": ">=14" + "node": ">=14.18" }, "scripts": { "create-rule": "node ./scripts/create-rule.mjs && npm run generate-rule-notices && npm run generate-rules-table", diff --git a/rules/filename-case.js b/rules/filename-case.js index cb921d36a0..5a00467807 100644 --- a/rules/filename-case.js +++ b/rules/filename-case.js @@ -1,5 +1,5 @@ 'use strict'; -const path = require('path'); +const path = require('node:path'); const {camelCase, kebabCase, snakeCase, upperFirst} = require('lodash'); const cartesianProductSamples = require('./utils/cartesian-product-samples.js'); diff --git a/rules/prefer-node-protocol.js b/rules/prefer-node-protocol.js index 5533c41bee..55a44a35cd 100644 --- a/rules/prefer-node-protocol.js +++ b/rules/prefer-node-protocol.js @@ -14,51 +14,31 @@ const importExportSourceSelector = [ 'Literal.source', ].join(''); -/** @param {import('eslint').Rule.RuleContext} context */ -const create = context => { - const {checkRequire} = { - checkRequire: false, - ...context.options[0], - }; - const selectors = [importExportSourceSelector]; - if (checkRequire) { - selectors.push(STATIC_REQUIRE_SOURCE_SELECTOR); - } +const selector = matches([ + importExportSourceSelector, + STATIC_REQUIRE_SOURCE_SELECTOR, +]); - return { - [matches(selectors)](node) { - const {value} = node; - if ( - typeof value !== 'string' - || value.startsWith('node:') - || !isBuiltinModule(value) - ) { - return; - } +const create = () => ({ + [selector](node) { + const {value} = node; + if ( + typeof value !== 'string' + || value.startsWith('node:') + || !isBuiltinModule(value) + ) { + return; + } - return { - node, - messageId: MESSAGE_ID, - data: {moduleName: value}, - /** @param {import('eslint').Rule.RuleFixer} fixer */ - fix: fixer => replaceStringLiteral(fixer, node, 'node:', 0, 0), - }; - }, - }; -}; - -const schema = [ - { - type: 'object', - additionalProperties: false, - properties: { - checkRequire: { - type: 'boolean', - default: false, - }, - }, + return { + node, + messageId: MESSAGE_ID, + data: {moduleName: value}, + /** @param {import('eslint').Rule.RuleFixer} fixer */ + fix: fixer => replaceStringLiteral(fixer, node, 'node:', 0, 0), + }; }, -]; +}); /** @type {import('eslint').Rule.RuleModule} */ module.exports = { @@ -69,7 +49,6 @@ module.exports = { description: 'Prefer using the `node:` protocol when importing Node.js builtin modules.', }, fixable: 'code', - schema, messages, }, }; diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index f51c5741a7..e4bf84315d 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -1,5 +1,5 @@ 'use strict'; -const path = require('path'); +const path = require('node:path'); const {defaultsDeep, upperFirst, lowerFirst} = require('lodash'); const avoidCapture = require('./utils/avoid-capture.js'); diff --git a/rules/utils/get-documentation-url.js b/rules/utils/get-documentation-url.js index 92d8d73787..3128369df3 100644 --- a/rules/utils/get-documentation-url.js +++ b/rules/utils/get-documentation-url.js @@ -1,5 +1,5 @@ 'use strict'; -const path = require('path'); +const path = require('node:path'); const packageJson = require('../../package.json'); const repoUrl = 'https://github.com/sindresorhus/eslint-plugin-unicorn'; diff --git a/rules/utils/rule.js b/rules/utils/rule.js index d4dcab1624..d23e010178 100644 --- a/rules/utils/rule.js +++ b/rules/utils/rule.js @@ -1,6 +1,6 @@ 'use strict'; -const path = require('path'); -const fs = require('fs'); +const path = require('node:path'); +const fs = require('node:fs'); const getDocumentationUrl = require('./get-documentation-url.js'); const isIterable = object => typeof object[Symbol.iterator] === 'function'; diff --git a/scripts/internal-rules/prefer-disallow-over-forbid.js b/scripts/internal-rules/prefer-disallow-over-forbid.js index 1c20abec73..605ba047cc 100644 --- a/scripts/internal-rules/prefer-disallow-over-forbid.js +++ b/scripts/internal-rules/prefer-disallow-over-forbid.js @@ -1,5 +1,5 @@ 'use strict'; -const path = require('path'); +const path = require('node:path'); const {matches} = require('../../rules/selectors/index.js'); const toLocation = require('../../rules/utils/to-location.js'); diff --git a/scripts/internal-rules/prefer-negative-boolean-attribute.js b/scripts/internal-rules/prefer-negative-boolean-attribute.js index a448a3d648..59e77629ec 100644 --- a/scripts/internal-rules/prefer-negative-boolean-attribute.js +++ b/scripts/internal-rules/prefer-negative-boolean-attribute.js @@ -1,5 +1,5 @@ 'use strict'; -const path = require('path'); +const path = require('node:path'); const messageId = path.basename(__filename, '.js'); diff --git a/test/prefer-node-protocol.mjs b/test/prefer-node-protocol.mjs index 5db6d91c88..23ade4b65f 100644 --- a/test/prefer-node-protocol.mjs +++ b/test/prefer-node-protocol.mjs @@ -8,7 +8,6 @@ test.snapshot({ 'import unicorn from "unicorn";', 'import fs from "./fs";', 'import fs from "unknown-builtin-module";', - 'const fs = require("fs");', 'import fs from "node:fs";', outdent` async function foo() { @@ -60,8 +59,7 @@ test.snapshot({ ], }); -// `options` -const checkRequireOptions = [{checkRequire: true}]; +// `require` test.snapshot({ valid: [ 'const fs = require("node:fs");', @@ -76,11 +74,11 @@ test.snapshot({ 'const fs = require();', 'const fs = require(...["fs"]);', 'const fs = require("unicorn");', - ].map(code => ({code, options: checkRequireOptions})), + ], invalid: [ 'const {promises} = require("fs")', 'const fs = require(\'fs/promises\')', - ].map(code => ({code, options: checkRequireOptions})), + ], }); test.babel({ diff --git a/test/snapshots/prefer-node-protocol.mjs.md b/test/snapshots/prefer-node-protocol.mjs.md index 02e7970087..fba55b82e2 100644 --- a/test/snapshots/prefer-node-protocol.mjs.md +++ b/test/snapshots/prefer-node-protocol.mjs.md @@ -255,16 +255,6 @@ Generated by [AVA](https://avajs.dev). ## Invalid #1 1 | const {promises} = require("fs") -> Options - - `␊ - [␊ - {␊ - "checkRequire": true␊ - }␊ - ]␊ - ` - > Output `␊ @@ -281,16 +271,6 @@ Generated by [AVA](https://avajs.dev). ## Invalid #2 1 | const fs = require('fs/promises') -> Options - - `␊ - [␊ - {␊ - "checkRequire": true␊ - }␊ - ]␊ - ` - > Output `␊ diff --git a/test/snapshots/prefer-node-protocol.mjs.snap b/test/snapshots/prefer-node-protocol.mjs.snap index e1b10a97b6..e69b401f80 100644 Binary files a/test/snapshots/prefer-node-protocol.mjs.snap and b/test/snapshots/prefer-node-protocol.mjs.snap differ