From f459b168f03033ca6c4025ce16941d7bc0480835 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 3 Feb 2023 14:19:19 +0100 Subject: [PATCH] eslint-plugin: add support for detecting Node.js builtins Signed-off-by: Patrik Oldsberg --- packages/eslint-plugin/lib/visitImports.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/lib/visitImports.js b/packages/eslint-plugin/lib/visitImports.js index d73c3203a8..c89d65fb49 100644 --- a/packages/eslint-plugin/lib/visitImports.js +++ b/packages/eslint-plugin/lib/visitImports.js @@ -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, }); }