diff --git a/src/service/resolve-module-name-literals.ts b/src/service/resolve-module-name-literals.ts index cc933b0..da179eb 100644 --- a/src/service/resolve-module-name-literals.ts +++ b/src/service/resolve-module-name-literals.ts @@ -1,8 +1,8 @@ import ts from 'typescript' import { equivalents } from '../equivalents.js' +import { getCurrentDirectory } from '../ts-sys-cached.js' import { addRootFile } from './file-versions.js' import { getCanonicalFileName } from './get-canonical-filename.js' -import { getCurrentDirectory } from '../ts-sys-cached.js' import { tsconfig } from './tsconfig.js' // reset cache on config change @@ -46,10 +46,12 @@ const fixupResolvedModule = ( | ts.ResolvedTypeReferenceDirective ) => { const { resolvedFileName } = resolvedModule + /* c8 ignore next */ if (resolvedFileName === undefined) return // [MUST_UPDATE_FOR_NEW_FILE_EXTENSIONS] // .ts,.mts,.cts is always switched to internal // .js is switched on-demand + /* c8 ignore start */ if ( resolvedModule.isExternalLibraryImport && ((resolvedFileName.endsWith('.ts') && @@ -63,6 +65,7 @@ const fixupResolvedModule = ( ) { resolvedModule.isExternalLibraryImport = false } + /* c8 ignore stop */ if (!resolvedModule.isExternalLibraryImport) { knownInternalFilenames.add(resolvedFileName) } @@ -89,7 +92,6 @@ export const getResolveModuleNameLiterals = ( containingSourceFile, _reusedNames ) => { - // moduleLiterals[n].text is the equivalent to moduleName string return moduleLiterals.map((moduleLiteral, i) => { const moduleName = moduleLiteral.text const mode = containingSourceFile @@ -104,7 +106,9 @@ export const getResolveModuleNameLiterals = ( | undefined } ).getModeForResolutionAtIndex?.(containingSourceFile, i) - : undefined + : /* c8 ignore start */ + undefined + /* c8 ignore stop */ let { resolvedModule } = ts.resolveModuleName( moduleName, containingFile, @@ -119,7 +123,10 @@ export const getResolveModuleNameLiterals = ( const ext = lastDotIndex >= 0 ? moduleName.slice(lastDotIndex) : '' if (ext) { - const replacements = equivalents(moduleName) + const replacements = equivalents( + moduleName, + mode !== ts.ModuleKind.ESNext + ) for (const rep of replacements) { ;({ resolvedModule } = ts.resolveModuleName( rep, diff --git a/src/service/resolve-type-reference-directive-references.ts b/src/service/resolve-type-reference-directive-references.ts index d864efc..f379e1a 100644 --- a/src/service/resolve-type-reference-directive-references.ts +++ b/src/service/resolve-type-reference-directive-references.ts @@ -4,9 +4,8 @@ import { getCanonicalFileName } from './get-canonical-filename.js' import { getCurrentDirectory } from '../ts-sys-cached.js' import { tsconfig } from './tsconfig.js' -const config = tsconfig() - -const resolveTypeReferenceDirectiveReferencesInternalCache = new Map< +// ResolveTypeReferenceDirectiveReferences internal cache +const rtrdrInternalCache = new Map< string, ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations >() @@ -15,6 +14,7 @@ export const getResolveTypeReferenceDirectiveReferences = ( host: ts.LanguageServiceHost, moduleResolutionCache: ts.ModuleResolutionCache ) => { + const config = tsconfig() const typeReferenceDirectiveResolutionCache = ts.createTypeReferenceDirectiveResolutionCache( getCurrentDirectory(), @@ -22,12 +22,14 @@ export const getResolveTypeReferenceDirectiveReferences = ( config.options, moduleResolutionCache.getPackageJsonInfoCache() ) + const resolveTypeReferenceDirectiveReferences = ( typeDirectiveReferences: readonly (ts.FileReference | string)[], containingFile: string, redirectedReference: ts.ResolvedProjectReference | undefined, options: ts.CompilerOptions, - containingSourceFile: ts.SourceFile | string | undefined + containingSourceFile: ts.SourceFile | string | undefined, + _reusedNames?: (ts.FileReference | string)[] ): readonly ts.ResolvedTypeReferenceDirectiveWithFailedLookupLocations[] => { const entries = typeDirectiveReferences const resolutionCache = typeReferenceDirectiveResolutionCache @@ -40,11 +42,11 @@ export const getResolveTypeReferenceDirectiveReferences = ( resolutionCache: ts.TypeReferenceDirectiveResolutionCache ) => any + /* c8 ignore start */ if (typeDirectiveReferences.length === 0) return [] + /* c8 ignore stop */ const resolutions: any[] = [] - const cache = resolveTypeReferenceDirectiveReferencesInternalCache - const loader = createLoader( containingFile, redirectedReference, @@ -59,13 +61,14 @@ export const getResolveTypeReferenceDirectiveReferences = ( containingSourceFile ) const key = createModeAwareCacheKey(name, mode) - let result = cache.get(key) + let result = rtrdrInternalCache.get(key) if (!result) { - cache.set(key, (result = loader.resolve(name, mode))) + rtrdrInternalCache.set(key, (result = loader.resolve(name, mode))) } resolutions.push(result) } return resolutions } + return resolveTypeReferenceDirectiveReferences } diff --git a/test/service/resolve-module-name-literals.ts b/test/service/resolve-module-name-literals.ts index e69de29..a3bd08c 100644 --- a/test/service/resolve-module-name-literals.ts +++ b/test/service/resolve-module-name-literals.ts @@ -0,0 +1,107 @@ +import { resolve } from 'path' +import t from 'tap' +// import { fileURLToPath } from 'url' +// import { getLanguageService } from '../../src/service/language-service.js' +// import { markFileNameInternal } from '../../src/service/resolve-module-name-literals.js' +// +// const svc = getLanguageService() +// svc.getProgram() +// +// markFileNameInternal(fileURLToPath(import.meta.url)) + +t.test('commonjs program', async t => { + const dir = t.testdir({ + 'tsconfig.json': JSON.stringify({ + include: ['index.ts'], + compilerOptions: { + esModuleInterop: true, + forceConsistentCasingInFileNames: true, + module: 'commonjs', + moduleResolution: 'node10', + skipLibCheck: true, + strict: true, + target: 'es2022', + }, + }), + 'package.json': JSON.stringify({ type: 'commonjs' }), + 'index.ts': ` +import assert from 'node:assert' +import './def' +import { bar } from './bar' +import { baz } from './baz' +import t from 'tap' +assert.equal(1, 1) +export type Foo = { + foo: string +} +export const foo = (f: Foo) => f.foo +`, + 'bar.cts': ` +export const bar = 'bar' +`, + 'baz.ts': ` +export const baz = 'baz' +`, + }) + const cwd = process.cwd() + process.chdir(dir) + t.teardown(() => process.chdir(cwd)) + const { getLanguageService } = (await t.mockImport( + '../../src/service/language-service.js' + )) as typeof import('../../src/service/language-service.js') + const { markFileNameInternal } = (await t.mockImport( + '../../src/service/resolve-module-name-literals.js' + )) as typeof import('../../src/service/resolve-module-name-literals.js') + const svc = getLanguageService() + svc.getHost().resolveModuleNameLiterals + svc.getProgram() + markFileNameInternal(resolve(dir, 'index.ts')) +}) + +t.test('esm program', async t => { + const dir = t.testdir({ + 'tsconfig.json': JSON.stringify({ + include: ['index.ts'], + compilerOptions: { + esModuleInterop: true, + forceConsistentCasingInFileNames: true, + module: 'nodenext', + moduleResolution: 'nodenext', + skipLibCheck: true, + strict: true, + target: 'es2022', + }, + }), + 'package.json': JSON.stringify({ type: 'module' }), + 'index.ts': ` +import assert from 'node:assert' +import { bar } from './bar.cjs' +import { baz } from './baz.js' +import t from 'tap' +assert.equal(1, 1) +export type Foo = { + foo: string +} +export const foo = (f: Foo) => f.foo +`, + 'bar.cts': ` +export const bar = 'bar' +`, + 'baz.ts': ` +export const baz = 'baz' +`, + }) + const cwd = process.cwd() + process.chdir(dir) + t.teardown(() => process.chdir(cwd)) + const { getLanguageService } = (await t.mockImport( + '../../src/service/language-service.js' + )) as typeof import('../../src/service/language-service.js') + const { markFileNameInternal } = (await t.mockImport( + '../../src/service/resolve-module-name-literals.js' + )) as typeof import('../../src/service/resolve-module-name-literals.js') + const svc = getLanguageService() + svc.getHost().resolveModuleNameLiterals + svc.getProgram() + markFileNameInternal(resolve(dir, 'index.ts')) +}) diff --git a/test/service/resolve-type-reference-directive-references.ts b/test/service/resolve-type-reference-directive-references.ts index e69de29..b0ca19a 100644 --- a/test/service/resolve-type-reference-directive-references.ts +++ b/test/service/resolve-type-reference-directive-references.ts @@ -0,0 +1,5 @@ +import t from 'tap' +import { getLanguageService } from '../../src/service/language-service.js' + +getLanguageService().getProgram() +t.pass('just a test file for coverage purposes, nothing to do') diff --git a/tsconfig.json b/tsconfig.json index d8a5105..7f39495 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,4 @@ { - // settings for tsimp importer - "tsimp": { - "compilerOptions": { - "resolveJsonModule": false - } - }, "compilerOptions": { "declaration": true, "declarationMap": true,