From a3106e07643f53a9b450d8a8d5121e56953fd194 Mon Sep 17 00:00:00 2001 From: Simo Kinnunen Date: Mon, 20 Jan 2025 05:26:01 +0900 Subject: [PATCH] feat: support subpaths of supported dependencies (e.g. `node:fs/promises`) --- .../builtin-with-node-prefix/entrypoint.ts | 2 ++ .../cli/src/services/check-parser/parser.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/services/check-parser/__tests__/check-parser-fixtures/builtin-with-node-prefix/entrypoint.ts b/packages/cli/src/services/check-parser/__tests__/check-parser-fixtures/builtin-with-node-prefix/entrypoint.ts index 10577416..8b552816 100644 --- a/packages/cli/src/services/check-parser/__tests__/check-parser-fixtures/builtin-with-node-prefix/entrypoint.ts +++ b/packages/cli/src/services/check-parser/__tests__/check-parser-fixtures/builtin-with-node-prefix/entrypoint.ts @@ -1,3 +1,5 @@ import {} from 'node:path' import {} from 'node:url' +import {} from 'node:fs/promises' import {} from 'crypto' +import {} from 'timers/promises' diff --git a/packages/cli/src/services/check-parser/parser.ts b/packages/cli/src/services/check-parser/parser.ts index 7a4539a1..0432b014 100644 --- a/packages/cli/src/services/check-parser/parser.ts +++ b/packages/cli/src/services/check-parser/parser.ts @@ -100,7 +100,7 @@ export class Parser { this.checkUnsupportedModules = options.checkUnsupportedModules ?? true } - supportsModule (importPath: string) { + supportsModule (importPath: string): boolean { if (this.supportedModules.has(importPath)) { return true } @@ -109,6 +109,21 @@ export class Parser { return true } + // Check namespaced modules and module subpaths. + if (importPath.indexOf('/') !== -1) { + if (importPath.startsWith('@')) { + const [namespace, moduleName] = importPath.split('/', 3) + if (this.supportedModules.has(namespace + '/' + moduleName)) { + return true + } + } else { + // Recurse to cover values with and without node: prefix. + // This will not endlessly recurse because we remove the slash. + const [moduleName] = importPath.split('/', 2) + return this.supportsModule(moduleName) + } + } + return false }