eslint-plugin: add support for detecting Node.js builtins

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-02-03 14:19:19 +01:00
parent 4d05d5bf83
commit f459b168f0
+18 -2
View File
@@ -16,6 +16,7 @@
// @ts-check
const { builtinModules } = require('module');
const getPackages = require('./getPackages');
/**
@@ -41,10 +42,18 @@ const getPackages = require('./getPackages');
* @property {string} packageName
*/
/**
* @typedef BuiltinImport
* @type {object}
* @property {'builtin'} type
* @property {string} path
* @property {string} packageName
*/
/**
* @callback ImportVisitor
* @param {import('eslint').Rule.Node} node
* @param {LocalImport | InternalImport | ExternalImport} import
* @param {LocalImport | InternalImport | ExternalImport | BuiltinImport} import
*/
/**
@@ -81,10 +90,17 @@ module.exports = function visitImports(context, visitor) {
}
const pkg = packages?.map.get(packageName);
if (!pkg) {
if (
packageName.startsWith('node:') ||
builtinModules.includes(packageName)
) {
return visitor(node, { type: 'builtin', path: subPath, packageName });
}
return visitor(node, {
type: 'external',
path: subPath,
packageName: packageName,
packageName,
});
}