Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support node: prefix for built-in modules [sc-22633] #1009

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {} from 'node:path'
import {} from 'node:url'
import {} from 'crypto'
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ describe('dependency-parser - parser()', () => {
])
})

it('should handle node: prefix for built-ins', () => {
const toAbsolutePath = (...filepath: string[]) => path.join(__dirname, 'check-parser-fixtures', 'builtin-with-node-prefix', ...filepath)
const parser = new Parser({
supportedNpmModules: defaultNpmModules,
})
parser.parse(toAbsolutePath('entrypoint.ts'))
})

/*
* There is an unhandled edge-case when require() is reassigned.
* Even though the check might execute fine, we throw an error for a missing dependency.
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/services/check-parser/package-files/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,11 @@ export function isLocalPath (importPath: string) {

return false
}

export function isBuiltinPath (importPath: string) {
if (importPath.startsWith('node:')) {
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SourceFile } from './source-file'
import { PackageJsonFile } from './package-json-file'
import { TSConfigFile } from './tsconfig-json-file'
import { JSConfigFile } from './jsconfig-json-file'
import { isLocalPath, PathResult } from './paths'
import { isBuiltinPath, isLocalPath, PathResult } from './paths'
import { FileLoader, LoadFile } from './loader'
import { JsonSourceFile } from './json-source-file'
import { LookupContext } from './lookup'
Expand Down Expand Up @@ -248,6 +248,13 @@ export class PackageFilesResolver {

resolve:
for (const importPath of dependencies) {
if (isBuiltinPath(importPath)) {
resolved.external.push({
importPath,
})
continue resolve
}

if (isLocalPath(importPath)) {
const relativeDepPath = path.resolve(dirname, importPath)
const sourceFile = this.cache.sourceFile(relativeDepPath, context)
Expand Down
31 changes: 28 additions & 3 deletions packages/cli/src/services/check-parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ type SupportedFileExtension = '.js' | '.mjs' | '.ts'
const PACKAGE_EXTENSION = `${path.sep}package.json`

const supportedBuiltinModules = [
'assert', 'buffer', 'crypto', 'dns', 'fs', 'path', 'querystring', 'readline ', 'stream', 'string_decoder',
'timers', 'tls', 'url', 'util', 'zlib',
'node:assert',
'node:buffer',
'node:crypto',
'node:dns',
'node:fs',
'node:path',
'node:querystring',
'node:readline',
'node:stream',
'node:string_decoder',
'node:timers',
'node:tls',
'node:url',
'node:util',
'node:zlib',
]

function validateEntrypoint (entrypoint: string): {extension: SupportedFileExtension, content: string} {
Expand Down Expand Up @@ -87,6 +100,18 @@ export class Parser {
this.checkUnsupportedModules = options.checkUnsupportedModules ?? true
}

supportsModule (importPath: string) {
if (this.supportedModules.has(importPath)) {
return true
}

if (this.supportedModules.has('node:' + importPath)) {
return true
}

return false
}

parse (entrypoint: string) {
const { content } = validateEntrypoint(entrypoint)

Expand Down Expand Up @@ -130,7 +155,7 @@ export class Parser {

if (this.checkUnsupportedModules) {
const unsupportedDependencies = resolvedDependencies.external.flatMap(dep => {
if (!this.supportedModules.has(dep.importPath)) {
if (!this.supportsModule(dep.importPath)) {
return [dep.importPath]
} else {
return []
Expand Down
Loading