|
| 1 | +/** |
| 2 | + * @author Yusuke Iinuma |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +"use strict" |
| 6 | + |
| 7 | +const isBuiltinModule = require("is-builtin-module") |
| 8 | +const getConfiguredNodeVersion = require("../util/get-configured-node-version") |
| 9 | +const getSemverRange = require("../util/get-semver-range") |
| 10 | +const visitImport = require("../util/visit-import") |
| 11 | +const visitRequire = require("../util/visit-require") |
| 12 | +const mergeVisitorsInPlace = require("../util/merge-visitors-in-place") |
| 13 | + |
| 14 | +const messageId = "preferNodeProtocol" |
| 15 | + |
| 16 | +module.exports = { |
| 17 | + meta: { |
| 18 | + docs: { |
| 19 | + description: |
| 20 | + "enforce using the `node:` protocol when importing Node.js builtin modules.", |
| 21 | + recommended: false, |
| 22 | + url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/prefer-node-protocol.md", |
| 23 | + }, |
| 24 | + fixable: "code", |
| 25 | + messages: { |
| 26 | + [messageId]: "Prefer `node:{{moduleName}}` over `{{moduleName}}`.", |
| 27 | + }, |
| 28 | + schema: [ |
| 29 | + { |
| 30 | + type: "object", |
| 31 | + properties: { |
| 32 | + version: getConfiguredNodeVersion.schema, |
| 33 | + }, |
| 34 | + additionalProperties: false, |
| 35 | + }, |
| 36 | + ], |
| 37 | + type: "suggestion", |
| 38 | + }, |
| 39 | + create(context) { |
| 40 | + function isCallExpression(node, { name, argumentsLength }) { |
| 41 | + if (node?.type !== "CallExpression") { |
| 42 | + return false |
| 43 | + } |
| 44 | + |
| 45 | + if (node.optional) { |
| 46 | + return false |
| 47 | + } |
| 48 | + |
| 49 | + if (node.arguments.length !== argumentsLength) { |
| 50 | + return false |
| 51 | + } |
| 52 | + |
| 53 | + if ( |
| 54 | + node.callee.type !== "Identifier" || |
| 55 | + node.callee.name !== name |
| 56 | + ) { |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + return true |
| 61 | + } |
| 62 | + |
| 63 | + function isStringLiteral(node) { |
| 64 | + return node?.type === "Literal" && typeof node.type === "string" |
| 65 | + } |
| 66 | + |
| 67 | + function isStaticRequire(node) { |
| 68 | + return ( |
| 69 | + isCallExpression(node, { |
| 70 | + name: "require", |
| 71 | + argumentsLength: 1, |
| 72 | + }) && isStringLiteral(node.arguments[0]) |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + function isEnablingThisRule(context, moduleStyle) { |
| 77 | + const version = getConfiguredNodeVersion(context) |
| 78 | + |
| 79 | + const supportedVersionForEsm = "^12.20.0 || >= 14.13.1" |
| 80 | + // Only check Node.js version because this rule is meaningless if configured Node.js version doesn't match semver range. |
| 81 | + if (!version.intersects(getSemverRange(supportedVersionForEsm))) { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + const supportedVersionForCjs = "^14.18.0 || >= 16.0.0" |
| 86 | + // Only check when using `require` |
| 87 | + if ( |
| 88 | + moduleStyle === "require" && |
| 89 | + !version.intersects(getSemverRange(supportedVersionForCjs)) |
| 90 | + ) { |
| 91 | + return false |
| 92 | + } |
| 93 | + |
| 94 | + return true |
| 95 | + } |
| 96 | + |
| 97 | + const targets = [] |
| 98 | + return [ |
| 99 | + visitImport(context, { includeCore: true }, importTargets => { |
| 100 | + targets.push(...importTargets) |
| 101 | + }), |
| 102 | + visitRequire(context, { includeCore: true }, requireTargets => { |
| 103 | + targets.push( |
| 104 | + ...requireTargets.filter(target => |
| 105 | + isStaticRequire(target.node.parent) |
| 106 | + ) |
| 107 | + ) |
| 108 | + }), |
| 109 | + { |
| 110 | + "Program:exit"() { |
| 111 | + for (const { node, moduleStyle } of targets) { |
| 112 | + if (!isEnablingThisRule(context, moduleStyle)) { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + if (node.type === "TemplateLiteral") { |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + const { value } = node |
| 121 | + if ( |
| 122 | + typeof value !== "string" || |
| 123 | + value.startsWith("node:") || |
| 124 | + !isBuiltinModule(value) || |
| 125 | + !isBuiltinModule(`node:${value}`) |
| 126 | + ) { |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + context.report({ |
| 131 | + node, |
| 132 | + messageId, |
| 133 | + fix(fixer) { |
| 134 | + const firstCharacterIndex = node.range[0] + 1 |
| 135 | + return fixer.replaceTextRange( |
| 136 | + [firstCharacterIndex, firstCharacterIndex], |
| 137 | + "node:" |
| 138 | + ) |
| 139 | + }, |
| 140 | + }) |
| 141 | + } |
| 142 | + }, |
| 143 | + }, |
| 144 | + ].reduce( |
| 145 | + (mergedVisitor, thisVisitor) => |
| 146 | + mergeVisitorsInPlace(mergedVisitor, thisVisitor), |
| 147 | + {} |
| 148 | + ) |
| 149 | + }, |
| 150 | +} |
0 commit comments