Skip to content

Commit 14b4529

Browse files
Fix tslib resolutions (#58451)
Co-authored-by: Sheetal Nandi <[email protected]>
1 parent 320764c commit 14b4529

17 files changed

+615
-14
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32194,12 +32194,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3219432194
const errorMessage = isClassic
3219532195
? Diagnostics.Cannot_find_module_0_Did_you_mean_to_set_the_moduleResolution_option_to_nodenext_or_to_add_aliases_to_the_paths_option
3219632196
: Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations;
32197-
// Synthesized JSX import is either first or after tslib
32198-
const jsxImportIndex = compilerOptions.importHelpers ? 1 : 0;
32199-
const specifier = file?.imports[jsxImportIndex];
32200-
if (specifier) {
32201-
Debug.assert(nodeIsSynthesized(specifier) && specifier.text === runtimeImportSpecifier, `Expected sourceFile.imports[${jsxImportIndex}] to be the synthesized JSX runtime import`);
32202-
}
32197+
const specifier = getJSXRuntimeImportSpecifier(file, runtimeImportSpecifier);
3220332198
const mod = resolveExternalModule(specifier || location!, runtimeImportSpecifier, errorMessage, location!);
3220432199
const result = mod && mod !== unknownSymbol ? getMergedSymbol(resolveSymbol(mod)) : undefined;
3220532200
if (links) {
@@ -49193,9 +49188,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4919349188
}
4919449189
}
4919549190

49196-
function resolveHelpersModule(node: SourceFile, errorNode: Node) {
49191+
function resolveHelpersModule(file: SourceFile, errorNode: Node) {
4919749192
if (!externalHelpersModule) {
49198-
externalHelpersModule = resolveExternalModule(node, externalHelpersModuleNameText, Diagnostics.This_syntax_requires_an_imported_helper_but_module_0_cannot_be_found, errorNode) || unknownSymbol;
49193+
externalHelpersModule = resolveExternalModule(getImportHelpersImportSpecifier(file), externalHelpersModuleNameText, Diagnostics.This_syntax_requires_an_imported_helper_but_module_0_cannot_be_found, errorNode) || unknownSymbol;
4919949194
}
4920049195
return externalHelpersModule;
4920149196
}
@@ -51145,6 +51140,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5114551140
blockScopeKind === NodeFlags.Using ||
5114651141
blockScopeKind === NodeFlags.AwaitUsing;
5114751142
}
51143+
51144+
function getJSXRuntimeImportSpecifier(file: SourceFile | undefined, specifierText: string) {
51145+
// Synthesized JSX import is either first or after tslib
51146+
const jsxImportIndex = compilerOptions.importHelpers ? 1 : 0;
51147+
const specifier = file?.imports[jsxImportIndex];
51148+
if (specifier) {
51149+
Debug.assert(nodeIsSynthesized(specifier) && specifier.text === specifierText, `Expected sourceFile.imports[${jsxImportIndex}] to be the synthesized JSX runtime import`);
51150+
}
51151+
return specifier;
51152+
}
51153+
51154+
function getImportHelpersImportSpecifier(file: SourceFile) {
51155+
Debug.assert(compilerOptions.importHelpers, "Expected importHelpers to be enabled");
51156+
const specifier = file.imports[0];
51157+
Debug.assert(specifier && nodeIsSynthesized(specifier) && specifier.text === "tslib", `Expected sourceFile.imports[0] to be the synthesized tslib import`);
51158+
return specifier;
51159+
}
5114851160
}
5114951161

5115051162
function isNotAccessor(declaration: Declaration): boolean {

src/compiler/program.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,7 +3419,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
34193419

34203420
function createSyntheticImport(text: string, file: SourceFile) {
34213421
const externalHelpersModuleReference = factory.createStringLiteral(text);
3422-
const importDecl = factory.createImportDeclaration(/*modifiers*/ undefined, /*importClause*/ undefined, externalHelpersModuleReference, /*attributes*/ undefined);
3422+
const importDecl = factory.createImportDeclaration(/*modifiers*/ undefined, /*importClause*/ undefined, externalHelpersModuleReference);
34233423
addInternalEmitFlags(importDecl, InternalEmitFlags.NeverApplyImportHelper);
34243424
setParent(externalHelpersModuleReference, importDecl);
34253425
setParent(importDecl, file);
@@ -3444,11 +3444,11 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
34443444
let ambientModules: string[] | undefined;
34453445

34463446
// If we are importing helpers, we need to add a synthetic reference to resolve the
3447-
// helpers library.
3448-
if (
3449-
(getIsolatedModules(options) || isExternalModuleFile)
3450-
&& !file.isDeclarationFile
3451-
) {
3447+
// helpers library. (A JavaScript file without `externalModuleIndicator` set might be
3448+
// a CommonJS module; `commonJsModuleIndicator` doesn't get set until the binder has
3449+
// run. We synthesize a helpers import for it just in case; it will never be used if
3450+
// the binder doesn't find and set a `commonJsModuleIndicator`.)
3451+
if (isJavaScriptFile || (!file.isDeclarationFile && (getIsolatedModules(options) || isExternalModule(file)))) {
34523452
if (options.importHelpers) {
34533453
// synthesize 'import "tslib"' declaration
34543454
imports = [createSyntheticImport(externalHelpersModuleNameText, file)];
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [tests/cases/compiler/importHelpersBundler.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "tslib",
6+
"main": "tslib.js",
7+
"module": "tslib.es6.js",
8+
"jsnext:main": "tslib.es6.js",
9+
"typings": "tslib.d.ts",
10+
"exports": {
11+
".": {
12+
"module": {
13+
"types": "./modules/index.d.ts",
14+
"default": "./tslib.es6.mjs"
15+
},
16+
"import": {
17+
"node": "./modules/index.js",
18+
"default": {
19+
"types": "./modules/index.d.ts",
20+
"default": "./tslib.es6.mjs"
21+
}
22+
},
23+
"default": "./tslib.js"
24+
},
25+
"./*": "./*",
26+
"./": "./"
27+
}
28+
}
29+
30+
//// [tslib.d.ts]
31+
export {};
32+
33+
//// [package.json]
34+
{ "type": "module" }
35+
36+
//// [index.d.ts]
37+
export declare var __rest: any;
38+
39+
//// [main.ts]
40+
export function foo(args: any) {
41+
const { bar, ...extraArgs } = args;
42+
return extraArgs;
43+
}
44+
45+
46+
//// [main.js]
47+
import { __rest } from "tslib";
48+
export function foo(args) {
49+
const { bar } = args, extraArgs = __rest(args, ["bar"]);
50+
return extraArgs;
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/importHelpersBundler.ts] ////
2+
3+
=== /node_modules/tslib/tslib.d.ts ===
4+
5+
export {};
6+
7+
=== /node_modules/tslib/modules/index.d.ts ===
8+
export declare var __rest: any;
9+
>__rest : Symbol(__rest, Decl(index.d.ts, 0, 18))
10+
11+
=== /main.ts ===
12+
export function foo(args: any) {
13+
>foo : Symbol(foo, Decl(main.ts, 0, 0))
14+
>args : Symbol(args, Decl(main.ts, 0, 20))
15+
16+
const { bar, ...extraArgs } = args;
17+
>bar : Symbol(bar, Decl(main.ts, 1, 9))
18+
>extraArgs : Symbol(extraArgs, Decl(main.ts, 1, 14))
19+
>args : Symbol(args, Decl(main.ts, 0, 20))
20+
21+
return extraArgs;
22+
>extraArgs : Symbol(extraArgs, Decl(main.ts, 1, 14))
23+
}
24+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/importHelpersBundler.ts] ////
2+
3+
=== /node_modules/tslib/tslib.d.ts ===
4+
5+
export {};
6+
7+
=== /node_modules/tslib/modules/index.d.ts ===
8+
export declare var __rest: any;
9+
>__rest : any
10+
11+
=== /main.ts ===
12+
export function foo(args: any) {
13+
>foo : (args: any) => any
14+
> : ^ ^^ ^^^^^^^^
15+
>args : any
16+
17+
const { bar, ...extraArgs } = args;
18+
>bar : any
19+
> : ^^^
20+
>extraArgs : any
21+
> : ^^^
22+
>args : any
23+
24+
return extraArgs;
25+
>extraArgs : any
26+
}
27+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//// [tests/cases/compiler/importHelpersCommonJSJavaScript.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "tslib",
6+
"main": "tslib.js",
7+
"module": "tslib.es6.js",
8+
"jsnext:main": "tslib.es6.js",
9+
"typings": "tslib.d.ts",
10+
"exports": {
11+
".": {
12+
"module": {
13+
"types": "./modules/index.d.ts",
14+
"default": "./tslib.es6.mjs"
15+
},
16+
"import": {
17+
"node": "./modules/index.js",
18+
"default": {
19+
"types": "./modules/index.d.ts",
20+
"default": "./tslib.es6.mjs"
21+
}
22+
},
23+
"default": "./tslib.js"
24+
},
25+
"./*": "./*",
26+
"./": "./"
27+
}
28+
}
29+
30+
//// [tslib.d.ts]
31+
export declare var __extends: any;
32+
33+
//// [package.json]
34+
{ "type": "module" }
35+
36+
//// [index.d.ts]
37+
export {};
38+
39+
//// [index.js]
40+
class Foo {}
41+
42+
class Bar extends Foo {}
43+
44+
module.exports = Bar;
45+
46+
47+
//// [index.js]
48+
var tslib_1 = require("tslib");
49+
var Foo = /** @class */ (function () {
50+
function Foo() {
51+
}
52+
return Foo;
53+
}());
54+
var Bar = /** @class */ (function (_super) {
55+
tslib_1.__extends(Bar, _super);
56+
function Bar() {
57+
return _super !== null && _super.apply(this, arguments) || this;
58+
}
59+
return Bar;
60+
}(Foo));
61+
module.exports = Bar;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/importHelpersCommonJSJavaScript.ts] ////
2+
3+
=== /node_modules/tslib/tslib.d.ts ===
4+
export declare var __extends: any;
5+
>__extends : Symbol(__extends, Decl(tslib.d.ts, --, --))
6+
7+
=== /node_modules/tslib/modules/index.d.ts ===
8+
9+
export {};
10+
11+
=== /index.js ===
12+
class Foo {}
13+
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
14+
15+
class Bar extends Foo {}
16+
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
17+
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
18+
19+
module.exports = Bar;
20+
>module.exports : Symbol(module.exports, Decl(index.js, 0, 0))
21+
>module : Symbol(export=, Decl(index.js, 2, 24))
22+
>exports : Symbol(export=, Decl(index.js, 2, 24))
23+
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
24+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/importHelpersCommonJSJavaScript.ts] ////
2+
3+
=== /node_modules/tslib/tslib.d.ts ===
4+
export declare var __extends: any;
5+
>__extends : any
6+
7+
=== /node_modules/tslib/modules/index.d.ts ===
8+
9+
export {};
10+
11+
=== /index.js ===
12+
class Foo {}
13+
>Foo : Foo
14+
> : ^^^
15+
16+
class Bar extends Foo {}
17+
>Bar : Bar
18+
> : ^^^
19+
>Foo : Foo
20+
> : ^^^
21+
22+
module.exports = Bar;
23+
>module.exports = Bar : typeof Bar
24+
> : ^^^^^^^^^^
25+
>module.exports : typeof Bar
26+
> : ^^^^^^^^^^
27+
>module : { exports: typeof Bar; }
28+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
29+
>exports : typeof Bar
30+
> : ^^^^^^^^^^
31+
>Bar : typeof Bar
32+
> : ^^^^^^^^^^
33+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//// [tests/cases/compiler/importHelpersCommonJSJavaScript.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "tslib",
6+
"main": "tslib.js",
7+
"module": "tslib.es6.js",
8+
"jsnext:main": "tslib.es6.js",
9+
"typings": "tslib.d.ts",
10+
"exports": {
11+
".": {
12+
"module": {
13+
"types": "./modules/index.d.ts",
14+
"default": "./tslib.es6.mjs"
15+
},
16+
"import": {
17+
"node": "./modules/index.js",
18+
"default": {
19+
"types": "./modules/index.d.ts",
20+
"default": "./tslib.es6.mjs"
21+
}
22+
},
23+
"default": "./tslib.js"
24+
},
25+
"./*": "./*",
26+
"./": "./"
27+
}
28+
}
29+
30+
//// [tslib.d.ts]
31+
export declare var __extends: any;
32+
33+
//// [package.json]
34+
{ "type": "module" }
35+
36+
//// [index.d.ts]
37+
export {};
38+
39+
//// [index.js]
40+
class Foo {}
41+
42+
class Bar extends Foo {}
43+
44+
module.exports = Bar;
45+
46+
47+
//// [index.js]
48+
var tslib_1 = require("tslib");
49+
var Foo = /** @class */ (function () {
50+
function Foo() {
51+
}
52+
return Foo;
53+
}());
54+
var Bar = /** @class */ (function (_super) {
55+
tslib_1.__extends(Bar, _super);
56+
function Bar() {
57+
return _super !== null && _super.apply(this, arguments) || this;
58+
}
59+
return Bar;
60+
}(Foo));
61+
module.exports = Bar;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/importHelpersCommonJSJavaScript.ts] ////
2+
3+
=== /node_modules/tslib/tslib.d.ts ===
4+
export declare var __extends: any;
5+
>__extends : Symbol(__extends, Decl(tslib.d.ts, --, --))
6+
7+
=== /node_modules/tslib/modules/index.d.ts ===
8+
9+
export {};
10+
11+
=== /index.js ===
12+
class Foo {}
13+
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
14+
15+
class Bar extends Foo {}
16+
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
17+
>Foo : Symbol(Foo, Decl(index.js, 0, 0))
18+
19+
module.exports = Bar;
20+
>module.exports : Symbol(module.exports, Decl(index.js, 0, 0))
21+
>module : Symbol(export=, Decl(index.js, 2, 24))
22+
>exports : Symbol(export=, Decl(index.js, 2, 24))
23+
>Bar : Symbol(Bar, Decl(index.js, 0, 12))
24+

0 commit comments

Comments
 (0)