|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const isCoreModule = require('is-core-module'); |
| 4 | +const { default: docsUrl } = require('../docsUrl'); |
| 5 | + |
| 6 | +const DO_PREFER_MESSAGE_ID = 'requireNodeProtocol'; |
| 7 | +const NEVER_PREFER_MESSAGE_ID = 'forbidNodeProtocol'; |
| 8 | +const messages = { |
| 9 | + [DO_PREFER_MESSAGE_ID]: 'Prefer `node:{{moduleName}}` over `{{moduleName}}`.', |
| 10 | + [NEVER_PREFER_MESSAGE_ID]: 'Prefer `{{moduleName}}` over `node:{{moduleName}}`.', |
| 11 | +}; |
| 12 | + |
| 13 | +function replaceStringLiteral( |
| 14 | + fixer, |
| 15 | + node, |
| 16 | + text, |
| 17 | + relativeRangeStart, |
| 18 | + relativeRangeEnd, |
| 19 | +) { |
| 20 | + const firstCharacterIndex = node.range[0] + 1; |
| 21 | + const start = Number.isInteger(relativeRangeEnd) |
| 22 | + ? relativeRangeStart + firstCharacterIndex |
| 23 | + : firstCharacterIndex; |
| 24 | + const end = Number.isInteger(relativeRangeEnd) |
| 25 | + ? relativeRangeEnd + firstCharacterIndex |
| 26 | + : node.range[1] - 1; |
| 27 | + |
| 28 | + return fixer.replaceTextRange([start, end], text); |
| 29 | +} |
| 30 | + |
| 31 | +function isStringLiteral(node) { |
| 32 | + return node.type === 'Literal' && typeof node.value === 'string'; |
| 33 | +} |
| 34 | + |
| 35 | +function isStaticRequireWith1Param(node) { |
| 36 | + return !node.optional |
| 37 | + && node.callee.type === 'Identifier' |
| 38 | + && node.callee.name === 'require' |
| 39 | + // check for only 1 argument |
| 40 | + && node.arguments.length === 1 |
| 41 | + && node.arguments[0] |
| 42 | + && isStringLiteral(node.arguments[0]); |
| 43 | +} |
| 44 | + |
| 45 | +function checkAndReport(src, context) { |
| 46 | + // TODO use src.quasis[0].value.raw |
| 47 | + if (src.type === 'TemplateLiteral') { return; } |
| 48 | + const moduleName = 'value' in src ? src.value : src.name; |
| 49 | + if (typeof moduleName !== 'string') { console.log(src, moduleName); } |
| 50 | + const { settings } = context; |
| 51 | + const nodeVersion = settings && settings['import/node-version']; |
| 52 | + if ( |
| 53 | + typeof nodeVersion !== 'undefined' |
| 54 | + && ( |
| 55 | + typeof nodeVersion !== 'string' |
| 56 | + || !(/^[0-9]+\.[0-9]+\.[0-9]+$/).test(nodeVersion) |
| 57 | + ) |
| 58 | + ) { |
| 59 | + throw new TypeError('`import/node-version` setting must be a string in the format "10.23.45" (a semver version, with no leading zero)'); |
| 60 | + } |
| 61 | + |
| 62 | + if (context.options[0] === 'never') { |
| 63 | + if (!moduleName.startsWith('node:')) { return; } |
| 64 | + |
| 65 | + const actualModuleName = moduleName.slice(5); |
| 66 | + if (!isCoreModule(actualModuleName, nodeVersion || undefined)) { return; } |
| 67 | + |
| 68 | + context.report({ |
| 69 | + node: src, |
| 70 | + message: messages[NEVER_PREFER_MESSAGE_ID], |
| 71 | + data: { moduleName: actualModuleName }, |
| 72 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 73 | + fix(fixer) { |
| 74 | + return replaceStringLiteral(fixer, src, '', 0, 5); |
| 75 | + }, |
| 76 | + }); |
| 77 | + } else if (context.options[0] === 'always') { |
| 78 | + if ( |
| 79 | + moduleName.startsWith('node:') |
| 80 | + || !isCoreModule(moduleName, nodeVersion || undefined) |
| 81 | + || !isCoreModule(`node:${moduleName}`, nodeVersion || undefined) |
| 82 | + ) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + context.report({ |
| 87 | + node: src, |
| 88 | + message: messages[DO_PREFER_MESSAGE_ID], |
| 89 | + data: { moduleName }, |
| 90 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 91 | + fix(fixer) { |
| 92 | + return replaceStringLiteral(fixer, src, 'node:', 0, 0); |
| 93 | + }, |
| 94 | + }); |
| 95 | + } else if (typeof context.options[0] === 'undefined') { |
| 96 | + throw new Error('Missing option'); |
| 97 | + } else { |
| 98 | + throw new Error(`Unexpected option: ${context.options[0]}`); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 103 | +module.exports = { |
| 104 | + meta: { |
| 105 | + type: 'suggestion', |
| 106 | + docs: { |
| 107 | + description: 'Enforce either using, or omitting, the `node:` protocol when importing Node.js builtin modules.', |
| 108 | + recommended: true, |
| 109 | + category: 'Static analysis', |
| 110 | + url: docsUrl('enforce-node-protocol-usage'), |
| 111 | + }, |
| 112 | + fixable: 'code', |
| 113 | + schema: { |
| 114 | + type: 'array', |
| 115 | + minItems: 1, |
| 116 | + maxItems: 1, |
| 117 | + items: [ |
| 118 | + { |
| 119 | + enum: ['always', 'never'], |
| 120 | + }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + messages, |
| 124 | + }, |
| 125 | + create(context) { |
| 126 | + return { |
| 127 | + CallExpression(node) { |
| 128 | + if (!isStaticRequireWith1Param(node)) { return; } |
| 129 | + |
| 130 | + const arg = node.arguments[0]; |
| 131 | + |
| 132 | + return checkAndReport(arg, context); |
| 133 | + }, |
| 134 | + ExportNamedDeclaration(node) { |
| 135 | + return checkAndReport(node.source, context); |
| 136 | + }, |
| 137 | + ImportDeclaration(node) { |
| 138 | + return checkAndReport(node.source, context); |
| 139 | + }, |
| 140 | + ImportExpression(node) { |
| 141 | + if (!isStringLiteral(node.source)) { return; } |
| 142 | + |
| 143 | + return checkAndReport(node.source, context); |
| 144 | + }, |
| 145 | + }; |
| 146 | + }, |
| 147 | +}; |
0 commit comments