Skip to content

Commit 7225671

Browse files
committed
Merge branch 'main' into module-resolution/ts-extensions
2 parents c6b963b + 0c60da9 commit 7225671

File tree

53 files changed

+3275
-676
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3275
-676
lines changed

package-lock.json

Lines changed: 305 additions & 301 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35025,7 +35025,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3502535025
}
3502635026

3502735027
function hasEmptyObjectIntersection(type: Type): boolean {
35028-
return someType(type, t => t === unknownEmptyObjectType || !!(t.flags & TypeFlags.Intersection) && some((t as IntersectionType).types, isEmptyAnonymousObjectType));
35028+
return someType(type, t => t === unknownEmptyObjectType || !!(t.flags & TypeFlags.Intersection) && isEmptyAnonymousObjectType(getBaseConstraintOrType(t)));
3502935029
}
3503035030

3503135031
function checkInExpression(left: Expression, right: Expression, leftType: Type, rightType: Type): Type {

src/compiler/transformers/module/module.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -958,32 +958,57 @@ export function transformModule(context: TransformationContext): (x: SourceFile
958958
function createImportCallExpressionCommonJS(arg: Expression | undefined, isInlineable?: boolean): Expression {
959959
// import(x)
960960
// emit as
961-
// var _a;
962-
// (_a = x, Promise.resolve().then(() => require(_a)) /*CommonJs Require*/
961+
// Promise.resolve(`${x}`).then((s) => require(s)) /*CommonJs Require*/
963962
// We have to wrap require in then callback so that require is done in asynchronously
964963
// if we simply do require in resolve callback in Promise constructor. We will execute the loading immediately
965-
// If the arg is not inlineable, we have to evaluate it in the current scope with a temp var
966-
const temp = arg && !isSimpleInlineableExpression(arg) && !isInlineable ? factory.createTempVariable(hoistVariableDeclaration) : undefined;
964+
// If the arg is not inlineable, we have to evaluate and ToString() it in the current scope
965+
// Otherwise, we inline it in require() so that it's statically analyzable
966+
const needSyncEval = arg && !isSimpleInlineableExpression(arg) && !isInlineable;
967+
967968
const promiseResolveCall = factory.createCallExpression(
968969
factory.createPropertyAccessExpression(factory.createIdentifier("Promise"), "resolve"),
969970
/*typeArguments*/ undefined,
970-
/*argumentsArray*/ [],
971+
/*argumentsArray*/ needSyncEval
972+
? languageVersion >= ScriptTarget.ES2015
973+
? [
974+
factory.createTemplateExpression(factory.createTemplateHead(""), [
975+
factory.createTemplateSpan(arg, factory.createTemplateTail("")),
976+
]),
977+
]
978+
: [
979+
factory.createCallExpression(
980+
factory.createPropertyAccessExpression(factory.createStringLiteral(""), "concat"),
981+
/*typeArguments*/ undefined,
982+
[arg]
983+
),
984+
]
985+
: []
971986
);
987+
972988
let requireCall: Expression = factory.createCallExpression(
973989
factory.createIdentifier("require"),
974990
/*typeArguments*/ undefined,
975-
temp ? [temp] : arg ? [arg] : [],
991+
needSyncEval ? [factory.createIdentifier("s")] : arg ? [arg] : [],
976992
);
977993
if (getESModuleInterop(compilerOptions)) {
978994
requireCall = emitHelpers().createImportStarHelper(requireCall);
979995
}
980996

997+
const parameters = needSyncEval
998+
? [
999+
factory.createParameterDeclaration(
1000+
/*modifiers*/ undefined,
1001+
/*dotDotDotToken*/ undefined,
1002+
/*name*/ "s"),
1003+
]
1004+
: [];
1005+
9811006
let func: FunctionExpression | ArrowFunction;
9821007
if (languageVersion >= ScriptTarget.ES2015) {
9831008
func = factory.createArrowFunction(
9841009
/*modifiers*/ undefined,
9851010
/*typeParameters*/ undefined,
986-
/*parameters*/ [],
1011+
/*parameters*/ parameters,
9871012
/*type*/ undefined,
9881013
/*equalsGreaterThanToken*/ undefined,
9891014
requireCall);
@@ -994,14 +1019,14 @@ export function transformModule(context: TransformationContext): (x: SourceFile
9941019
/*asteriskToken*/ undefined,
9951020
/*name*/ undefined,
9961021
/*typeParameters*/ undefined,
997-
/*parameters*/ [],
1022+
/*parameters*/ parameters,
9981023
/*type*/ undefined,
9991024
factory.createBlock([factory.createReturnStatement(requireCall)]));
10001025
}
10011026

10021027
const downleveledImport = factory.createCallExpression(factory.createPropertyAccessExpression(promiseResolveCall, "then"), /*typeArguments*/ undefined, [func]);
10031028

1004-
return temp === undefined ? downleveledImport : factory.createCommaListExpression([factory.createAssignment(temp, arg!), downleveledImport]);
1029+
return downleveledImport;
10051030
}
10061031

10071032
function getHelperExpressionForExport(node: ExportDeclaration, innerExpr: Expression) {

src/jsTyping/jsTyping.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
removeFileExtension,
2727
removeMinAndVersionNumbers,
2828
some,
29+
toFileNameLowerCase,
2930
TypeAcquisition,
3031
Version,
3132
versionMajorMinor,
@@ -313,8 +314,8 @@ export function discoverTypings(
313314
// packages. So that needs this dance here.
314315
const pathComponents = getPathComponents(normalizePath(manifestPath));
315316
const isScoped = pathComponents[pathComponents.length - 3][0] === "@";
316-
return isScoped && pathComponents[pathComponents.length - 4].toLowerCase() === modulesDirName || // `node_modules/@foo/bar`
317-
!isScoped && pathComponents[pathComponents.length - 3].toLowerCase() === modulesDirName; // `node_modules/foo`
317+
return isScoped && toFileNameLowerCase(pathComponents[pathComponents.length - 4]) === modulesDirName || // `node_modules/@foo/bar`
318+
!isScoped && toFileNameLowerCase(pathComponents[pathComponents.length - 3]) === modulesDirName; // `node_modules/foo`
318319
});
319320

320321
if (log) log(`Searching for typing names in ${packagesFolderPath}; all files: ${JSON.stringify(dependencyManifestNames)}`);
@@ -361,7 +362,7 @@ export function discoverTypings(
361362
const fromFileNames = mapDefined(fileNames, j => {
362363
if (!hasJSFileExtension(j)) return undefined;
363364

364-
const inferredTypingName = removeFileExtension(getBaseFileName(j.toLowerCase()));
365+
const inferredTypingName = removeFileExtension(toFileNameLowerCase(getBaseFileName(j)));
365366
const cleanedTypingName = removeMinAndVersionNumbers(inferredTypingName);
366367
return safeList.get(cleanedTypingName);
367368
});

src/services/shims.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export class LanguageServiceShimHostAdapter implements LanguageServiceHost {
459459
if ("getTypeReferenceDirectiveResolutionsForFile" in this.shimHost) {
460460
this.resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile) => {
461461
const typeDirectivesForFile = JSON.parse(this.shimHost.getTypeReferenceDirectiveResolutionsForFile!(containingFile)) as MapLike<ResolvedTypeReferenceDirective>; // TODO: GH#18217
462-
return map(typeDirectiveNames as (string | FileReference)[], name => getProperty(typeDirectivesForFile, isString(name) ? name : name.fileName.toLowerCase()));
462+
return map(typeDirectiveNames as (string | FileReference)[], name => getProperty(typeDirectivesForFile, isString(name) ? name : toFileNameLowerCase(name.fileName)));
463463
};
464464
}
465465
}

src/testRunner/unittests/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface ProgramWithSourceTexts extends ts.Program {
2525

2626
export interface TestCompilerHost extends ts.CompilerHost {
2727
getTrace(): string[];
28+
clearTrace(): void;
2829
}
2930

3031
export class SourceText implements ts.IScriptSnapshot {
@@ -124,6 +125,7 @@ export function createTestCompilerHost(texts: readonly NamedSourceText[], target
124125
const result: TestCompilerHost = {
125126
trace: s => trace.push(s),
126127
getTrace: () => trace,
128+
clearTrace: () => trace.length = 0,
127129
getSourceFile: fileName => files.get(fileName),
128130
getDefaultLibFileName: () => "lib.d.ts",
129131
writeFile: ts.notImplemented,

0 commit comments

Comments
 (0)