Skip to content

Commit 5999a52

Browse files
committed
Support for an external exportStar helper
1 parent 88c6825 commit 5999a52

19 files changed

+178
-46
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19160,6 +19160,10 @@ namespace ts {
1916019160
if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) {
1916119161
error(node.moduleSpecifier, Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol));
1916219162
}
19163+
19164+
if (modulekind !== ModuleKind.System && modulekind !== ModuleKind.ES2015) {
19165+
checkExternalEmitHelpers(node, ExternalEmitHelpers.ExportStar);
19166+
}
1916319167
}
1916419168
}
1916519169
}
@@ -20788,7 +20792,7 @@ namespace ts {
2078820792
function checkExternalEmitHelpers(location: Node, helpers: ExternalEmitHelpers) {
2078920793
if ((requestedExternalEmitHelpers & helpers) !== helpers && compilerOptions.importHelpers) {
2079020794
const sourceFile = getSourceFileOfNode(location);
20791-
if (isEffectiveExternalModule(sourceFile, compilerOptions)) {
20795+
if (!isDeclarationFile(sourceFile) && isEffectiveExternalModule(sourceFile, compilerOptions)) {
2079220796
const helpersModule = resolveHelpersModule(sourceFile, location);
2079320797
if (helpersModule !== unknownSymbol) {
2079420798
const uncheckedHelpers = helpers & ~requestedExternalEmitHelpers;
@@ -20817,6 +20821,8 @@ namespace ts {
2081720821
case ExternalEmitHelpers.Param: return "__param";
2081820822
case ExternalEmitHelpers.Awaiter: return "__awaiter";
2081920823
case ExternalEmitHelpers.Generator: return "__generator";
20824+
case ExternalEmitHelpers.ExportStar: return "__exportStar";
20825+
default: Debug.fail("Unrecognized helper");
2082020826
}
2082120827
}
2082220828

src/compiler/factory.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,25 +2808,37 @@ namespace ts {
28082808
return emitNode && emitNode.externalHelpersModuleName;
28092809
}
28102810

2811-
export function getOrCreateExternalHelpersModuleNameIfNeeded(node: SourceFile, compilerOptions: CompilerOptions) {
2812-
if (compilerOptions.importHelpers && (isExternalModule(node) || compilerOptions.isolatedModules)) {
2811+
export function getOrCreateExternalHelpersModuleNameIfNeeded(node: SourceFile, compilerOptions: CompilerOptions, hasExportStarsToExportValues?: boolean) {
2812+
if (compilerOptions.importHelpers && isEffectiveExternalModule(node, compilerOptions)) {
28132813
const externalHelpersModuleName = getExternalHelpersModuleName(node);
28142814
if (externalHelpersModuleName) {
28152815
return externalHelpersModuleName;
28162816
}
28172817

2818-
const helpers = getEmitHelpers(node);
2819-
if (helpers) {
2820-
for (const helper of helpers) {
2821-
if (!helper.scoped) {
2822-
const parseNode = getOriginalNode(node, isSourceFile);
2823-
const emitNode = getOrCreateEmitNode(parseNode);
2824-
return emitNode.externalHelpersModuleName || (emitNode.externalHelpersModuleName = createUniqueName(externalHelpersModuleNameText));
2818+
const moduleKind = getEmitModuleKind(compilerOptions);
2819+
let create = hasExportStarsToExportValues
2820+
&& moduleKind !== ModuleKind.System
2821+
&& moduleKind !== ModuleKind.ES2015;
2822+
if (!create) {
2823+
const helpers = getEmitHelpers(node);
2824+
if (helpers) {
2825+
for (const helper of helpers) {
2826+
if (!helper.scoped) {
2827+
create = true;
2828+
break;
2829+
}
28252830
}
28262831
}
28272832
}
2833+
2834+
if (create) {
2835+
const parseNode = getOriginalNode(node, isSourceFile);
2836+
const emitNode = getOrCreateEmitNode(parseNode);
2837+
return emitNode.externalHelpersModuleName || (emitNode.externalHelpersModuleName = createUniqueName(externalHelpersModuleNameText));
2838+
}
28282839
}
28292840
}
2841+
28302842
/**
28312843
* Adds an EmitHelper to a node.
28322844
*/
@@ -3293,17 +3305,6 @@ namespace ts {
32933305
let exportEquals: ExportAssignment = undefined;
32943306
let hasExportStarsToExportValues = false;
32953307

3296-
const externalHelpersModuleName = getOrCreateExternalHelpersModuleNameIfNeeded(sourceFile, compilerOptions);
3297-
const externalHelpersImportDeclaration = externalHelpersModuleName && createImportDeclaration(
3298-
/*decorators*/ undefined,
3299-
/*modifiers*/ undefined,
3300-
createImportClause(/*name*/ undefined, createNamespaceImport(externalHelpersModuleName)),
3301-
createLiteral(externalHelpersModuleNameText));
3302-
3303-
if (externalHelpersImportDeclaration) {
3304-
externalImports.push(externalHelpersImportDeclaration);
3305-
}
3306-
33073308
for (const node of sourceFile.statements) {
33083309
switch (node.kind) {
33093310
case SyntaxKind.ImportDeclaration:
@@ -3414,6 +3415,17 @@ namespace ts {
34143415
}
34153416
}
34163417

3418+
const externalHelpersModuleName = getOrCreateExternalHelpersModuleNameIfNeeded(sourceFile, compilerOptions, hasExportStarsToExportValues);
3419+
const externalHelpersImportDeclaration = externalHelpersModuleName && createImportDeclaration(
3420+
/*decorators*/ undefined,
3421+
/*modifiers*/ undefined,
3422+
createImportClause(/*name*/ undefined, createNamespaceImport(externalHelpersModuleName)),
3423+
createLiteral(externalHelpersModuleNameText));
3424+
3425+
if (externalHelpersImportDeclaration) {
3426+
externalImports.unshift(externalHelpersImportDeclaration);
3427+
}
3428+
34173429
return { externalImports, exportSpecifiers, exportEquals, hasExportStarsToExportValues, exportedBindings, exportedNames, externalHelpersImportDeclaration };
34183430
}
34193431

src/compiler/transformers/module/module.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace ts {
8787
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false);
8888

8989
const updated = updateSourceFileNode(node, createNodeArray(statements, node.statements));
90-
if (currentModuleInfo.hasExportStarsToExportValues) {
90+
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
9191
addEmitHelper(updated, exportStarHelper);
9292
}
9393

@@ -377,7 +377,7 @@ namespace ts {
377377
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ true);
378378

379379
const body = createBlock(statements, /*location*/ undefined, /*multiLine*/ true);
380-
if (currentModuleInfo.hasExportStarsToExportValues) {
380+
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
381381
// If we have any `export * from ...` declarations
382382
// we need to inform the emitter to add the __export helper.
383383
addEmitHelper(body, exportStarHelper);
@@ -691,15 +691,7 @@ namespace ts {
691691
else {
692692
// export * from "mod";
693693
return createStatement(
694-
createCall(
695-
createIdentifier("__export"),
696-
/*typeArguments*/ undefined,
697-
[
698-
moduleKind !== ModuleKind.AMD
699-
? createRequireCall(node)
700-
: generatedName
701-
]
702-
),
694+
createExportStarHelper(context, moduleKind !== ModuleKind.AMD ? createRequireCall(node) : generatedName),
703695
/*location*/ node
704696
);
705697
}
@@ -1441,6 +1433,14 @@ namespace ts {
14411433
text: `
14421434
function __export(m) {
14431435
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
1444-
}`
1436+
}
1437+
`
14451438
};
1439+
1440+
function createExportStarHelper(context: TransformationContext, module: Expression) {
1441+
const compilerOptions = context.getCompilerOptions();
1442+
return compilerOptions.importHelpers
1443+
? createCall(getHelperName("__exportStar"), /*typeArguments*/ undefined, [module, createIdentifier("exports")])
1444+
: createCall(createIdentifier("__export"), /*typeArguments*/ undefined, [module]);
1445+
}
14461446
}

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3733,9 +3733,10 @@ namespace ts {
37333733
Param = 1 << 5, // __param (used by TypeScript decorators transformation)
37343734
Awaiter = 1 << 6, // __awaiter (used by ES2017 async functions transformation)
37353735
Generator = 1 << 7, // __generator (used by ES2015 generator transformation)
3736+
ExportStar = 1 << 8, // __exportStar (used by CommonJS/AMD/UMD module transformation)
37363737

37373738
FirstEmitHelper = Extends,
3738-
LastEmitHelper = Generator
3739+
LastEmitHelper = ExportStar
37393740
}
37403741

37413742
/* @internal */

tests/baselines/reference/importHelpersAmd.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ export class A { }
55

66
//// [b.ts]
77
import { A } from "./a";
8+
export * from "./a";
89
export class B extends A { }
910

1011
//// [tslib.d.ts]
1112
export declare function __extends(d: Function, b: Function): void;
1213
export declare function __assign(t: any, ...sources: any[]): any;
14+
export declare function __rest(t: any, propertyNames: string[]): any;
1315
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
1416
export declare function __param(paramIndex: number, decorator: Function): Function;
1517
export declare function __metadata(metadataKey: any, metadataValue: any): Function;
1618
export declare function __awaiter(thisArg: any, _arguments: any, P: Function, generator: Function): any;
17-
19+
export declare function __generator(thisArg: any, body: Function): any;
20+
export declare function __exportStar(m: any, exports: any): void;
1821

1922
//// [a.js]
2023
define(["require", "exports"], function (require, exports) {
@@ -27,8 +30,9 @@ define(["require", "exports"], function (require, exports) {
2730
exports.A = A;
2831
});
2932
//// [b.js]
30-
define(["require", "exports", "tslib", "./a"], function (require, exports, tslib_1, a_1) {
33+
define(["require", "exports", "tslib", "./a", "./a"], function (require, exports, tslib_1, a_1, a_2) {
3134
"use strict";
35+
tslib_1.__exportStar(a_2, exports);
3236
var B = (function (_super) {
3337
tslib_1.__extends(B, _super);
3438
function B() {

tests/baselines/reference/importHelpersAmd.symbols

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export class A { }
66
import { A } from "./a";
77
>A : Symbol(A, Decl(b.ts, 0, 8))
88

9+
export * from "./a";
910
export class B extends A { }
10-
>B : Symbol(B, Decl(b.ts, 0, 24))
11+
>B : Symbol(B, Decl(b.ts, 1, 20))
1112
>A : Symbol(A, Decl(b.ts, 0, 8))
1213

1314
=== tests/cases/compiler/tslib.d.ts ===
@@ -23,6 +24,11 @@ export declare function __assign(t: any, ...sources: any[]): any;
2324
>t : Symbol(t, Decl(tslib.d.ts, --, --))
2425
>sources : Symbol(sources, Decl(tslib.d.ts, --, --))
2526

27+
export declare function __rest(t: any, propertyNames: string[]): any;
28+
>__rest : Symbol(__rest, Decl(tslib.d.ts, --, --))
29+
>t : Symbol(t, Decl(tslib.d.ts, --, --))
30+
>propertyNames : Symbol(propertyNames, Decl(tslib.d.ts, --, --))
31+
2632
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
2733
>__decorate : Symbol(__decorate, Decl(tslib.d.ts, --, --))
2834
>decorators : Symbol(decorators, Decl(tslib.d.ts, --, --))
@@ -53,3 +59,14 @@ export declare function __awaiter(thisArg: any, _arguments: any, P: Function, ge
5359
>generator : Symbol(generator, Decl(tslib.d.ts, --, --))
5460
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
5561

62+
export declare function __generator(thisArg: any, body: Function): any;
63+
>__generator : Symbol(__generator, Decl(tslib.d.ts, --, --))
64+
>thisArg : Symbol(thisArg, Decl(tslib.d.ts, --, --))
65+
>body : Symbol(body, Decl(tslib.d.ts, --, --))
66+
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
67+
68+
export declare function __exportStar(m: any, exports: any): void;
69+
>__exportStar : Symbol(__exportStar, Decl(tslib.d.ts, --, --))
70+
>m : Symbol(m, Decl(tslib.d.ts, --, --))
71+
>exports : Symbol(exports, Decl(tslib.d.ts, --, --))
72+

tests/baselines/reference/importHelpersAmd.types

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class A { }
66
import { A } from "./a";
77
>A : typeof A
88

9+
export * from "./a";
910
export class B extends A { }
1011
>B : B
1112
>A : A
@@ -23,6 +24,11 @@ export declare function __assign(t: any, ...sources: any[]): any;
2324
>t : any
2425
>sources : any[]
2526

27+
export declare function __rest(t: any, propertyNames: string[]): any;
28+
>__rest : (t: any, propertyNames: string[]) => any
29+
>t : any
30+
>propertyNames : string[]
31+
2632
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
2733
>__decorate : (decorators: Function[], target: any, key?: string | symbol, desc?: any) => any
2834
>decorators : Function[]
@@ -53,3 +59,14 @@ export declare function __awaiter(thisArg: any, _arguments: any, P: Function, ge
5359
>generator : Function
5460
>Function : Function
5561

62+
export declare function __generator(thisArg: any, body: Function): any;
63+
>__generator : (thisArg: any, body: Function) => any
64+
>thisArg : any
65+
>body : Function
66+
>Function : Function
67+
68+
export declare function __exportStar(m: any, exports: any): void;
69+
>__exportStar : (m: any, exports: any) => void
70+
>m : any
71+
>exports : any
72+

tests/baselines/reference/importHelpersInAmbientContext.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ declare namespace N {
4949
//// [tslib.d.ts]
5050
export declare function __extends(d: Function, b: Function): void;
5151
export declare function __assign(t: any, ...sources: any[]): any;
52+
export declare function __rest(t: any, propertyNames: string[]): any;
5253
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
5354
export declare function __param(paramIndex: number, decorator: Function): Function;
5455
export declare function __metadata(metadataKey: any, metadataValue: any): Function;
5556
export declare function __awaiter(thisArg: any, _arguments: any, P: Function, generator: Function): any;
56-
57+
export declare function __generator(thisArg: any, body: Function): any;
58+
export declare function __exportStar(m: any, exports: any): void;
5759

5860
//// [b.js]
5961
"use strict";

tests/baselines/reference/importHelpersInAmbientContext.symbols

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ export declare function __assign(t: any, ...sources: any[]): any;
9999
>t : Symbol(t, Decl(tslib.d.ts, --, --))
100100
>sources : Symbol(sources, Decl(tslib.d.ts, --, --))
101101

102+
export declare function __rest(t: any, propertyNames: string[]): any;
103+
>__rest : Symbol(__rest, Decl(tslib.d.ts, --, --))
104+
>t : Symbol(t, Decl(tslib.d.ts, --, --))
105+
>propertyNames : Symbol(propertyNames, Decl(tslib.d.ts, --, --))
106+
102107
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
103108
>__decorate : Symbol(__decorate, Decl(tslib.d.ts, --, --))
104109
>decorators : Symbol(decorators, Decl(tslib.d.ts, --, --))
@@ -129,3 +134,14 @@ export declare function __awaiter(thisArg: any, _arguments: any, P: Function, ge
129134
>generator : Symbol(generator, Decl(tslib.d.ts, --, --))
130135
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
131136

137+
export declare function __generator(thisArg: any, body: Function): any;
138+
>__generator : Symbol(__generator, Decl(tslib.d.ts, --, --))
139+
>thisArg : Symbol(thisArg, Decl(tslib.d.ts, --, --))
140+
>body : Symbol(body, Decl(tslib.d.ts, --, --))
141+
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
142+
143+
export declare function __exportStar(m: any, exports: any): void;
144+
>__exportStar : Symbol(__exportStar, Decl(tslib.d.ts, --, --))
145+
>m : Symbol(m, Decl(tslib.d.ts, --, --))
146+
>exports : Symbol(exports, Decl(tslib.d.ts, --, --))
147+

tests/baselines/reference/importHelpersInAmbientContext.types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ export declare function __assign(t: any, ...sources: any[]): any;
9999
>t : any
100100
>sources : any[]
101101

102+
export declare function __rest(t: any, propertyNames: string[]): any;
103+
>__rest : (t: any, propertyNames: string[]) => any
104+
>t : any
105+
>propertyNames : string[]
106+
102107
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
103108
>__decorate : (decorators: Function[], target: any, key?: string | symbol, desc?: any) => any
104109
>decorators : Function[]
@@ -129,3 +134,14 @@ export declare function __awaiter(thisArg: any, _arguments: any, P: Function, ge
129134
>generator : Function
130135
>Function : Function
131136

137+
export declare function __generator(thisArg: any, body: Function): any;
138+
>__generator : (thisArg: any, body: Function) => any
139+
>thisArg : any
140+
>body : Function
141+
>Function : Function
142+
143+
export declare function __exportStar(m: any, exports: any): void;
144+
>__exportStar : (m: any, exports: any) => void
145+
>m : any
146+
>exports : any
147+

0 commit comments

Comments
 (0)