Initial commit
This commit is contained in:
36
src/rules/require-node-prefix.ts
Normal file
36
src/rules/require-node-prefix.ts
Normal file
@ -0,0 +1,36 @@
|
||||
// @ts-check
|
||||
import { builtinModules } from "node:module";
|
||||
import type { Rule } from "eslint";
|
||||
|
||||
const rule: Rule.RuleModule = {
|
||||
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}"`),
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
export default rule;
|
Reference in New Issue
Block a user