forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 61
feat: add prefer-process-get-builtin-module rule #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aladdin-add
merged 1 commit into
eslint-community:master
from
ColumbusLabs:codex/prefer-process-get-builtin-module
Aug 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # n/prefer-process-get-builtin-module | ||
|
|
||
| 📝 Enforce using `process.getBuiltinModule()` to load Node.js built-in modules. | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Node.js exposes built-in modules synchronously through `process.getBuiltinModule()`. | ||
| In ES modules, this avoids creating a `require` function solely to access a | ||
| built-in module. It also communicates that the requested module is built into | ||
| Node.js. | ||
|
|
||
| This API is available starting in Node.js 20.16.0 on the 20.x release line and | ||
| in Node.js 22.3.0 or later. | ||
|
|
||
| ## 📖 Rule Details | ||
|
|
||
| This rule reports calls to `require()` for built-in modules and awaited dynamic | ||
| imports of built-in modules. It ignores non-built-in modules, dynamic imports | ||
| that are not awaited directly, arbitrary functions named `require`, and | ||
| references where `process` is shadowed. A local `require` created with | ||
| `createRequire()` from `node:module` is recognized. | ||
|
|
||
| This rule is not automatically fixable. An awaited dynamic import returns an | ||
| ES module namespace object, while `process.getBuiltinModule()` returns the | ||
| underlying built-in exports object. Review how the loaded module is used when | ||
| applying the suggested replacement. | ||
|
|
||
| 👍 Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| /*eslint n/prefer-process-get-builtin-module: error */ | ||
|
|
||
| const fs = process.getBuiltinModule("node:fs") | ||
| const eslint = require("eslint") | ||
| const lazyFs = import("node:fs") | ||
| ``` | ||
|
|
||
| 👎 Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| /*eslint n/prefer-process-get-builtin-module: error */ | ||
|
|
||
| const fs = require("node:fs") | ||
| const promises = await import("node:fs/promises") | ||
| ``` | ||
|
|
||
| ### Configured Node.js version range | ||
|
|
||
| [Configured Node.js version range](../../README.md#configured-nodejs-version-range) | ||
|
|
||
| ### Options | ||
|
|
||
| ```json | ||
| { | ||
| "n/prefer-process-get-builtin-module": [ | ||
| "error", | ||
| { | ||
| "version": ">=22.3.0" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| #### version | ||
|
|
||
| This rule reads the [`engines`] field of `package.json`. You can override that | ||
| range with the `version` option, which accepts any valid | ||
| [`node-semver` range](https://github.com/npm/node-semver#range-grammar). | ||
|
|
||
| The rule does not report when the configured range includes Node.js versions | ||
| without `process.getBuiltinModule()`. | ||
|
|
||
| ## 🔎 Implementation | ||
|
|
||
| - [Rule source](../../lib/rules/prefer-process-get-builtin-module.js) | ||
| - [Test source](../../tests/lib/rules/prefer-process-get-builtin-module.js) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /** | ||
| * @author ColumbusLabs | ||
| * See LICENSE file in root directory for full license. | ||
| */ | ||
|
|
||
| import { isBuiltin } from "node:module" | ||
| import { | ||
| findVariable, | ||
| getStringIfConstant, | ||
| } from "@eslint-community/eslint-utils" | ||
| import { Range, subset } from "semver" | ||
| import { getConfiguredNodeVersion } from "../util/get-configured-node-version.js" | ||
| import { schema as configuredNodeVersionSchema } from "../util/get-configured-node-version.js" | ||
|
|
||
| const supportedRange = new Range("^20.16.0 || >=22.3.0") | ||
|
|
||
| /** | ||
| * @param {import("eslint").Rule.RuleContext} context | ||
| * @param {import("estree").Node} node | ||
| * @returns {boolean} | ||
| */ | ||
| function isProcessShadowed(context, node) { | ||
| const scope = context.sourceCode.getScope(node) | ||
| const variable = findVariable(scope, "process") | ||
| return Boolean(variable?.defs.length) | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("eslint").Rule.RuleContext} context | ||
| * @param {import("estree").Identifier} node | ||
| * @returns {boolean} | ||
| */ | ||
| function isCreateRequireImport(context, node) { | ||
| const variable = findVariable(context.sourceCode.getScope(node), node) | ||
| return Boolean( | ||
| variable?.defs.some( | ||
| definition => | ||
| definition.type === "ImportBinding" && | ||
| definition.node.type === "ImportSpecifier" && | ||
| definition.node.imported.type === "Identifier" && | ||
| definition.node.imported.name === "createRequire" && | ||
| (definition.parent.source.value === "node:module" || | ||
| definition.parent.source.value === "module") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("eslint").Rule.RuleContext} context | ||
| * @param {import("estree").Identifier} node | ||
| * @returns {boolean} | ||
| */ | ||
| function isNodeRequire(context, node) { | ||
| const variable = findVariable(context.sourceCode.getScope(node), node) | ||
| if (variable == null || variable.defs.length === 0) { | ||
| return true | ||
| } | ||
|
|
||
| return variable.defs.some(definition => { | ||
| if ( | ||
| definition.type !== "Variable" || | ||
| definition.node.type !== "VariableDeclarator" || | ||
| definition.parent.type !== "VariableDeclaration" || | ||
| definition.parent.kind !== "const" || | ||
| definition.node.init?.type !== "CallExpression" || | ||
| definition.node.init.callee.type !== "Identifier" | ||
| ) { | ||
| return false | ||
| } | ||
| return isCreateRequireImport(context, definition.node.init.callee) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("estree").Node | null | undefined} node | ||
| * @returns {boolean} | ||
| */ | ||
| function isBuiltinModuleName(node) { | ||
| if (node == null || node.type === "SpreadElement") { | ||
| return false | ||
| } | ||
| const name = getStringIfConstant(node) | ||
| return typeof name === "string" && isBuiltin(name) | ||
| } | ||
|
|
||
| /** @type {import("./rule-module.js").RuleModule} */ | ||
| export default { | ||
| meta: { | ||
| docs: { | ||
| description: | ||
| "enforce using `process.getBuiltinModule()` to load Node.js built-in modules", | ||
| recommended: false, | ||
| url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/prefer-process-get-builtin-module.md", | ||
| }, | ||
| messages: { | ||
| preferProcessGetBuiltinModule: | ||
| "Prefer `process.getBuiltinModule()` over `{{method}}()` for Node.js built-in modules.", | ||
| }, | ||
| schema: [ | ||
| { | ||
| type: "object", | ||
| properties: { | ||
| version: configuredNodeVersionSchema, | ||
| }, | ||
| additionalProperties: false, | ||
| }, | ||
| ], | ||
| type: "suggestion", | ||
| }, | ||
| create(context) { | ||
| if (!subset(getConfiguredNodeVersion(context), supportedRange)) { | ||
| return {} | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("estree").CallExpression} node | ||
| */ | ||
| function reportRequire(node) { | ||
| if ( | ||
| node.callee.type !== "Identifier" || | ||
| node.callee.name !== "require" || | ||
| !isNodeRequire(context, node.callee) || | ||
| ("optional" in node && node.optional) || | ||
| node.arguments.length !== 1 || | ||
| !isBuiltinModuleName(node.arguments[0]) || | ||
| isProcessShadowed(context, node) | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| context.report({ | ||
| node, | ||
| messageId: "preferProcessGetBuiltinModule", | ||
| data: { method: "require" }, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("estree").AwaitExpression} node | ||
| */ | ||
| function reportImport(node) { | ||
| const importExpression = node.argument | ||
| if ( | ||
| importExpression.type !== "ImportExpression" || | ||
| importExpression.options != null || | ||
| !isBuiltinModuleName(importExpression.source) || | ||
| isProcessShadowed(context, node) | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| context.report({ | ||
| node, | ||
| messageId: "preferProcessGetBuiltinModule", | ||
| data: { method: "import" }, | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| AwaitExpression: reportImport, | ||
| CallExpression: reportRequire, | ||
| } | ||
| }, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.