Created
May 22, 2023 06:05
-
-
Save alex-kinokon/f8f373e1a6bb01aa654d9085f2cff834 to your computer and use it in GitHub Desktop.
eslint-import-require-node-prefix
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
// License: https://unlicense.org/ | |
// @ts-check | |
const { builtinModules } = require("module"); | |
/** | |
* @type {import("eslint").Rule.RuleModule} | |
*/ | |
module.exports = { | |
meta: { | |
type: "problem", | |
docs: { | |
description: | |
"Disallow imports of built-in Node.js modules without the `node:` prefix", | |
category: "Best Practices", | |
recommended: true, | |
}, | |
fixable: "code", | |
schema: [], | |
}, | |
create: context => ({ | |
ImportDeclaration(node) { | |
const { source } = node; | |
if (source?.type === "Literal" && typeof source.value === "string") { | |
const moduleName = source.value; | |
if (builtinModules.includes(moduleName) && !moduleName.startsWith("node:")) { | |
context.report({ | |
node: source, | |
message: `Import of built-in Node.js module "${moduleName}" must use the "node:" prefix.`, | |
fix: fixer => fixer.replaceText(source, `"node:${moduleName}"`), | |
}); | |
} | |
} | |
}, | |
}), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment