Skip to content

Commit b53d409

Browse files
committed
Adapt for proper node16 moduleResolution projects
1 parent 6761b90 commit b53d409

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,
@@ -7062,7 +7063,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
70627063
if (!type) {
70637064
return false
70647065
}
7065-
7066+
70667067
// Class companion object
70677068
if (getObjectFlags(type) & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & SymbolFlags.Class) {
70687069
return true
@@ -8410,6 +8411,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84108411
return top;
84118412
}
84128413

8414+
function getSpecifierForModuleSymbolSpecial(symbol: Symbol, context: NodeBuilderContext, overrideImportMode?: ResolutionMode) {
8415+
let specifier = getSpecifierForModuleSymbol(symbol, context, overrideImportMode)
8416+
// ts plus import workaround
8417+
if (specifier && specifier.indexOf("/node_modules/") > 0) {
8418+
const r = tryGetImportLocation(fileMap.map, specifier)
8419+
if (r) { specifier = r; }
8420+
}
8421+
return specifier
8422+
}
8423+
84138424
function getSpecifierForModuleSymbol(symbol: Symbol, context: NodeBuilderContext, overrideImportMode?: ResolutionMode) {
84148425
let file = getDeclarationOfKind<SourceFile>(symbol, SyntaxKind.SourceFile);
84158426
if (!file) {
@@ -8495,7 +8506,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84958506
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeNext) {
84968507
// An `import` type directed at an esm format file is only going to resolve in esm mode - set the esm mode assertion
84978508
if (targetFile?.impliedNodeFormat === ModuleKind.ESNext && targetFile.impliedNodeFormat !== contextFile?.impliedNodeFormat) {
8498-
specifier = getSpecifierForModuleSymbol(chain[0], context, ModuleKind.ESNext);
8509+
specifier = getSpecifierForModuleSymbolSpecial(chain[0], context, ModuleKind.ESNext);
84998510
assertion = factory.createImportTypeAssertionContainer(factory.createAssertClause(factory.createNodeArray([
85008511
factory.createAssertEntry(
85018512
factory.createStringLiteral("resolution-mode"),
@@ -8506,7 +8517,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85068517
}
85078518
}
85088519
if (!specifier) {
8509-
specifier = getSpecifierForModuleSymbol(chain[0], context);
8520+
specifier = getSpecifierForModuleSymbolSpecial(chain[0], context);
85108521
}
85118522
if (!(context.flags & NodeBuilderFlags.AllowNodeModulesRelativePaths) && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Classic && specifier.indexOf("/node_modules/") >= 0) {
85128523
const oldSpecifier = specifier;
@@ -32006,7 +32017,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3200632017
if (forExtension) {
3200732018
return forExtension;
3200832019
}
32009-
// TSPLUS EXTENSION END
32020+
// TSPLUS EXTENSION END
3201032021
const parentSymbol = getNodeLinks(left).resolvedSymbol;
3201132022
const assignmentKind = getAssignmentTargetKind(node);
3201232023
const apparentType = getApparentType(assignmentKind !== AssignmentKind.None || isMethodAccessForCall(node) ? getWidenedType(leftType) : leftType);
@@ -35012,8 +35023,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3501235023
Diagnostics.Deriving_type_0_1,
3501335024
typeToString(derivation.type),
3501435025
`using implicit ${
35015-
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.implicit).fileName ?
35016-
`${getImportPath(derivation.implicit)}#${derivation.implicit.symbol.escapedName}` :
35026+
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.implicit).fileName ?
35027+
`${getImportPath(derivation.implicit)}#${derivation.implicit.symbol.escapedName}` :
3501735028
derivation.implicit.symbol.escapedName
3501835029
}`
3501935030
)
@@ -35075,8 +35086,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3507535086
Diagnostics.Deriving_type_0_1,
3507635087
typeToString(derivation.type),
3507735088
`using${(derivation.usedBy.length > 0 ? " (recursive)" : "")} rule ${
35078-
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.rule).fileName ?
35079-
`${getImportPath(derivation.rule)}#${derivation.rule.symbol.escapedName}` :
35089+
getSourceFileOfNode(location).fileName !== getSourceFileOfNode(derivation.rule).fileName ?
35090+
`${getImportPath(derivation.rule)}#${derivation.rule.symbol.escapedName}` :
3508035091
derivation.rule.symbol.escapedName
3508135092
}`
3508235093
)
@@ -35308,7 +35319,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3530835319
if (local.valueDeclaration &&
3530935320
(isParameterDeclaration(local.valueDeclaration as VariableLikeDeclaration) || isLocalImplicit(local.valueDeclaration)) &&
3531035321
isNamedDeclaration(local.valueDeclaration) &&
35311-
isIdentifier(local.valueDeclaration.name) &&
35322+
isIdentifier(local.valueDeclaration.name) &&
3531235323
isBlockScopedNameDeclaredBeforeUse(local.valueDeclaration.name, location)
3531335324
) {
3531435325
const { tags: implicitTags, type: implicitType } = getTypeAndImplicitTags(local);
@@ -35864,7 +35875,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3586435875
}
3586535876
return checked;
3586635877
}
35867-
// TSPLUS EXTENSION END
35878+
// TSPLUS EXTENSION END
3586835879

3586935880
/**
3587035881
* Syntactically and semantically checks a call or new expression.
@@ -41491,7 +41502,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4149141502
const links = getNodeLinks(node);
4149241503
if (links.tsPlusPipeableExtension) {
4149341504
checkFluentPipeableAgreement(links.tsPlusPipeableExtension);
41494-
}
41505+
}
4149541506
}
4149641507
}
4149741508

@@ -48614,7 +48625,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4861448625
signature.parameters[0] &&
4861548626
signature.parameters[0].declarations &&
4861648627
signature.parameters[0].declarations.find((decl) => isVariableLike(decl) && isParameterDeclaration(decl) && isRestParameter(decl as ParameterDeclaration))
48617-
) {
48628+
) {
4861848629
return true
4861948630
}
4862048631
return false;
@@ -48806,7 +48817,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4880648817
priority: tag.priority,
4880748818
});
4880848819
}
48809-
}
48820+
}
4881048821
}
4881148822
function cacheTsPlusGetterVariable(file: SourceFile, declaration: VariableDeclarationWithIdentifier) {
4881248823
for (const { target, name } of collectTsPlusGetterTags(declaration)) {
@@ -49322,15 +49333,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4932249333
cacheTsPlusIndexFunction(declaration);
4932349334
}
4932449335
else {
49325-
cacheTsPlusIndexVariable(declaration);
49336+
cacheTsPlusIndexVariable(declaration);
4932649337
}
4932749338
}
4932849339
for (const declaration of file.tsPlusContext.pipeableIndex) {
4932949340
if (isFunctionDeclaration(declaration)) {
4933049341
cacheTsPlusPipeableIndexFunction(declaration);
4933149342
}
4933249343
else {
49333-
cacheTsPlusPipeableIndexVariable(declaration);
49344+
cacheTsPlusPipeableIndexVariable(declaration);
4933449345
}
4933549346
}
4933649347
}

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)