Skip to content

Commit 14210d1

Browse files
committed
Adapt for proper node16 moduleResolution projects
1 parent 3c765f3 commit 14210d1

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

src/compiler/checker.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ import {
10391039
getAllJSDocTags,
10401040
getFileMap,
10411041
getImportLocation,
1042+
tryGetImportLocation,
10421043
getJSDocCommentsAndTags,
10431044
isReturnStatement,
10441045
JSDocTag,
@@ -7060,7 +7061,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
70607061
if (!type) {
70617062
return false
70627063
}
7063-
7064+
70647065
// Class companion object
70657066
if (getObjectFlags(type) & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & SymbolFlags.Class) {
70667067
return true
@@ -8408,6 +8409,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84088409
return top;
84098410
}
84108411

8412+
function getSpecifierForModuleSymbolSpecial(symbol: Symbol, context: NodeBuilderContext, overrideImportMode?: ResolutionMode) {
8413+
let specifier = getSpecifierForModuleSymbol(symbol, context, overrideImportMode)
8414+
// ts plus import workaround
8415+
if (specifier && specifier.indexOf("/node_modules/") > 0) {
8416+
const r = tryGetImportLocation(fileMap.map, specifier)
8417+
if (r) { specifier = r; }
8418+
}
8419+
return specifier
8420+
}
8421+
84118422
function getSpecifierForModuleSymbol(symbol: Symbol, context: NodeBuilderContext, overrideImportMode?: ResolutionMode) {
84128423
let file = getDeclarationOfKind<SourceFile>(symbol, SyntaxKind.SourceFile);
84138424
if (!file) {
@@ -8493,7 +8504,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84938504
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeNext) {
84948505
// An `import` type directed at an esm format file is only going to resolve in esm mode - set the esm mode assertion
84958506
if (targetFile?.impliedNodeFormat === ModuleKind.ESNext && targetFile.impliedNodeFormat !== contextFile?.impliedNodeFormat) {
8496-
specifier = getSpecifierForModuleSymbol(chain[0], context, ModuleKind.ESNext);
8507+
specifier = getSpecifierForModuleSymbolSpecial(chain[0], context, ModuleKind.ESNext);
84978508
assertion = factory.createImportTypeAssertionContainer(factory.createAssertClause(factory.createNodeArray([
84988509
factory.createAssertEntry(
84998510
factory.createStringLiteral("resolution-mode"),
@@ -8504,7 +8515,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85048515
}
85058516
}
85068517
if (!specifier) {
8507-
specifier = getSpecifierForModuleSymbol(chain[0], context);
8518+
specifier = getSpecifierForModuleSymbolSpecial(chain[0], context);
85088519
}
85098520
if (!(context.flags & NodeBuilderFlags.AllowNodeModulesRelativePaths) && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Classic && specifier.indexOf("/node_modules/") >= 0) {
85108521
const oldSpecifier = specifier;
@@ -32004,7 +32015,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3200432015
if (forExtension) {
3200532016
return forExtension;
3200632017
}
32007-
// TSPLUS EXTENSION END
32018+
// TSPLUS EXTENSION END
3200832019
const parentSymbol = getNodeLinks(left).resolvedSymbol;
3200932020
const assignmentKind = getAssignmentTargetKind(node);
3201032021
const apparentType = getApparentType(assignmentKind !== AssignmentKind.None || isMethodAccessForCall(node) ? getWidenedType(leftType) : leftType);
@@ -35010,8 +35021,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3501035021
Diagnostics.Deriving_type_0_1,
3501135022
typeToString(derivation.type),
3501235023
`using implicit ${
35013-
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.implicit).fileName ?
35014-
`${getImportPath(derivation.implicit)}#${derivation.implicit.symbol.escapedName}` :
35024+
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.implicit).fileName ?
35025+
`${getImportPath(derivation.implicit)}#${derivation.implicit.symbol.escapedName}` :
3501535026
derivation.implicit.symbol.escapedName
3501635027
}`
3501735028
)
@@ -35073,8 +35084,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3507335084
Diagnostics.Deriving_type_0_1,
3507435085
typeToString(derivation.type),
3507535086
`using${(derivation.usedBy.length > 0 ? " (recursive)" : "")} rule ${
35076-
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.rule).fileName ?
35077-
`${getImportPath(derivation.rule)}#${derivation.rule.symbol.escapedName}` :
35087+
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.rule).fileName ?
35088+
`${getImportPath(derivation.rule)}#${derivation.rule.symbol.escapedName}` :
3507835089
derivation.rule.symbol.escapedName
3507935090
}`
3508035091
)
@@ -35306,7 +35317,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3530635317
if (local.valueDeclaration &&
3530735318
(isParameterDeclaration(local.valueDeclaration as VariableLikeDeclaration) || isLocalImplicit(local.valueDeclaration)) &&
3530835319
isNamedDeclaration(local.valueDeclaration) &&
35309-
isIdentifier(local.valueDeclaration.name) &&
35320+
isIdentifier(local.valueDeclaration.name) &&
3531035321
isBlockScopedNameDeclaredBeforeUse(local.valueDeclaration.name, location)
3531135322
) {
3531235323
const { tags: implicitTags, type: implicitType } = getTypeAndImplicitTags(local);
@@ -35862,7 +35873,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3586235873
}
3586335874
return checked;
3586435875
}
35865-
// TSPLUS EXTENSION END
35876+
// TSPLUS EXTENSION END
3586635877

3586735878
/**
3586835879
* Syntactically and semantically checks a call or new expression.
@@ -41489,7 +41500,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4148941500
const links = getNodeLinks(node);
4149041501
if (links.tsPlusPipeableExtension) {
4149141502
checkFluentPipeableAgreement(links.tsPlusPipeableExtension);
41492-
}
41503+
}
4149341504
}
4149441505
}
4149541506

@@ -48602,7 +48613,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4860248613
signature.parameters[0] &&
4860348614
signature.parameters[0].declarations &&
4860448615
signature.parameters[0].declarations.find((decl) => isVariableLike(decl) && isParameterDeclaration(decl) && isRestParameter(decl as ParameterDeclaration))
48605-
) {
48616+
) {
4860648617
return true
4860748618
}
4860848619
return false;
@@ -48794,7 +48805,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4879448805
priority: tag.priority,
4879548806
});
4879648807
}
48797-
}
48808+
}
4879848809
}
4879948810
function cacheTsPlusGetterVariable(file: SourceFile, declaration: VariableDeclarationWithIdentifier) {
4880048811
for (const { target, name } of collectTsPlusGetterTags(declaration)) {
@@ -49310,15 +49321,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4931049321
cacheTsPlusIndexFunction(declaration);
4931149322
}
4931249323
else {
49313-
cacheTsPlusIndexVariable(declaration);
49324+
cacheTsPlusIndexVariable(declaration);
4931449325
}
4931549326
}
4931649327
for (const declaration of file.tsPlusContext.pipeableIndex) {
4931749328
if (isFunctionDeclaration(declaration)) {
4931849329
cacheTsPlusPipeableIndexFunction(declaration);
4931949330
}
4932049331
else {
49321-
cacheTsPlusPipeableIndexVariable(declaration);
49332+
cacheTsPlusPipeableIndexVariable(declaration);
4932249333
}
4932349334
}
4932449335
}

src/compiler/moduleSpecifiers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ function tryGetModuleNameFromExports(options: CompilerOptions, targetFilePath: s
843843
for (const key of getOwnKeys(exports as MapLike<unknown>)) {
844844
if (key === "default" || conditions.indexOf(key) >= 0 || isApplicableVersionedTypesKey(conditions, key)) {
845845
const subTarget = (exports as MapLike<unknown>)[key];
846-
const result = tryGetModuleNameFromExports(options, targetFilePath, packageDirectory, packageName, subTarget, conditions);
846+
const mode2 = typeof subTarget === "string" ? endsWith(subTarget, "/") ? 1 /* Directory */ : stringContains(subTarget, "*") ? MatchingMode.Pattern : MatchingMode.Exact : MatchingMode.Exact;
847+
const result = tryGetModuleNameFromExports(options, targetFilePath, packageDirectory, packageName, subTarget, conditions, mode2);
847848
if (result) {
848849
return result;
849850
}
@@ -929,7 +930,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
929930
}
930931

931932
// If the module was found in @types, get the actual Node package name
932-
const nodeModulesDirectoryName = moduleSpecifier.substring(parts.topLevelPackageNameIndex + 1);
933+
const nodeModulesDirectoryName = moduleSpecifier.substring(parts.topLevelPackageNameIndex + 1).replace(/\.pnpm\/[^\/]+\/node_modules\//, "");
933934
const packageName = getPackageNameFromTypesPackageName(nodeModulesDirectoryName);
934935
// For classic resolution, only allow importing from node_modules/@types, not other node_modules
935936
return getEmitModuleResolutionKind(options) === ModuleResolutionKind.Classic && packageName === nodeModulesDirectoryName ? undefined : packageName;

src/compiler/transformers/utilities.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ export interface ExternalModuleInfo {
104104
generatedExportSpecifiers?: Map<Identifier, ExportSpecifier[]>;
105105
}
106106

107-
export function getImportLocation(fileMap: [string, RegExp][], source: string) {
107+
const importLocationCache: Record<string, string | undefined> = {}
108+
export function tryGetImportLocation(fileMap: [string, RegExp][], source: string) {
109+
if (source in importLocationCache) { return importLocationCache[source] }
108110
for (const [path, reg] of fileMap) {
109111
if (source.match(reg)) {
110-
return source.replace(reg, path)
112+
const r = source.replace(reg, path)
113+
importLocationCache[source] = r
114+
return r;
111115
}
112116
}
113-
throw new Error(`cannot get import path for file: ${source} (Make sure to add it in your tsplus.config.json)`)
117+
importLocationCache[source] = undefined
118+
return undefined
119+
}
120+
export function getImportLocation(fileMap: [string, RegExp][], source: string) {
121+
const found = tryGetImportLocation(fileMap, source)
122+
if (!found) { throw new Error(`cannot get import path for file: ${source} (Make sure to add it in your tsplus.config.json)`); }
123+
return found
114124
}
115125

116126
export function getTraceLocation(traceMap: [string, RegExp][], source: string) {

0 commit comments

Comments
 (0)