Skip to content

Commit bc1058e

Browse files
authored
Merge pull request #13871 from Microsoft/master-fix13709
[Fix 13709] - Emit __esmodule
2 parents de40000 + 1243e11 commit bc1058e

File tree

1,643 files changed

+3302
-1444
lines changed

Some content is hidden

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

1,643 files changed

+3302
-1444
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="moduleNameResolver.ts"/>
1+
/// <reference path="moduleNameResolver.ts"/>
22
/// <reference path="binder.ts"/>
33

44
/* @internal */
@@ -22084,6 +22084,11 @@ namespace ts {
2208422084
}
2208522085
}
2208622086

22087+
if (compilerOptions.module !== ModuleKind.ES2015 && compilerOptions.module !== ModuleKind.System && !compilerOptions.noEmit &&
22088+
!isInAmbientContext(node.parent.parent) && hasModifier(node.parent.parent, ModifierFlags.Export)) {
22089+
checkESModuleMarker(node.name);
22090+
}
22091+
2208722092
const checkLetConstNames = (isLet(node) || isConst(node));
2208822093

2208922094
// 1. LexicalDeclaration : LetOrConst BindingList ;
@@ -22096,6 +22101,22 @@ namespace ts {
2209622101
return checkLetConstNames && checkGrammarNameInLetOrConstDeclarations(node.name);
2209722102
}
2209822103

22104+
function checkESModuleMarker(name: Identifier | BindingPattern): boolean {
22105+
if (name.kind === SyntaxKind.Identifier) {
22106+
if (unescapeIdentifier(name.text) === "__esModule") {
22107+
return grammarErrorOnNode(name, Diagnostics.Identifier_expected_esModule_is_reserved_as_an_exported_marker_when_transforming_ECMAScript_modules);
22108+
}
22109+
}
22110+
else {
22111+
const elements = (<BindingPattern>name).elements;
22112+
for (const element of elements) {
22113+
if (!isOmittedExpression(element)) {
22114+
return checkESModuleMarker(element.name);
22115+
}
22116+
}
22117+
}
22118+
}
22119+
2209922120
function checkGrammarNameInLetOrConstDeclarations(name: Identifier | BindingPattern): boolean {
2210022121
if (name.kind === SyntaxKind.Identifier) {
2210122122
if ((<Identifier>name).originalKeywordKind === SyntaxKind.LetKeyword) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,10 @@
671671
"category": "Error",
672672
"code": 1215
673673
},
674+
"Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.": {
675+
"category": "Error",
676+
"code": 1216
677+
},
674678
"Export assignment is not supported when '--module' flag is 'system'.": {
675679
"category": "Error",
676680
"code": 1218

src/compiler/transformers/module/module.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ namespace ts {
8383

8484
const statements: Statement[] = [];
8585
const statementOffset = addPrologueDirectives(statements, node.statements, /*ensureUseStrict*/ !compilerOptions.noImplicitUseStrict, sourceElementVisitor);
86+
87+
if (!currentModuleInfo.exportEquals) {
88+
append(statements, createUnderscoreUnderscoreESModule());
89+
}
90+
8691
append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, sourceElementVisitor, isStatement, /*optional*/ true));
8792
addRange(statements, visitNodes(node.statements, sourceElementVisitor, isStatement, statementOffset));
8893
addRange(statements, endLexicalEnvironment());
@@ -92,7 +97,6 @@ namespace ts {
9297
if (currentModuleInfo.hasExportStarsToExportValues) {
9398
addEmitHelper(updated, exportStarHelper);
9499
}
95-
96100
return updated;
97101
}
98102

@@ -371,6 +375,10 @@ namespace ts {
371375
const statements: Statement[] = [];
372376
const statementOffset = addPrologueDirectives(statements, node.statements, /*ensureUseStrict*/ !compilerOptions.noImplicitUseStrict, sourceElementVisitor);
373377

378+
if (!currentModuleInfo.exportEquals) {
379+
append(statements, createUnderscoreUnderscoreESModule());
380+
}
381+
374382
// Visit each statement of the module body.
375383
append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, sourceElementVisitor, isStatement, /*optional*/ true));
376384
addRange(statements, visitNodes(node.statements, sourceElementVisitor, isStatement, statementOffset));
@@ -665,6 +673,7 @@ namespace ts {
665673
}
666674

667675
const generatedName = getGeneratedNameForNode(node);
676+
668677
if (node.exportClause) {
669678
const statements: Statement[] = [];
670679
// export { x, y } from "mod";
@@ -838,6 +847,7 @@ namespace ts {
838847
let statements: Statement[];
839848
let variables: VariableDeclaration[];
840849
let expressions: Expression[];
850+
841851
if (hasModifier(node, ModifierFlags.Export)) {
842852
let modifiers: NodeArray<Modifier>;
843853

@@ -1127,43 +1137,39 @@ namespace ts {
11271137
* @param allowComments Whether to allow comments on the export.
11281138
*/
11291139
function appendExportStatement(statements: Statement[] | undefined, exportName: Identifier, expression: Expression, location?: TextRange, allowComments?: boolean): Statement[] | undefined {
1130-
if (exportName.text === "default") {
1131-
const sourceFile = getOriginalNode(currentSourceFile, isSourceFile);
1132-
if (sourceFile && !sourceFile.symbol.exports.get("___esModule")) {
1133-
if (languageVersion === ScriptTarget.ES3) {
1134-
statements = append(statements,
1135-
createStatement(
1136-
createExportExpression(
1137-
createIdentifier("__esModule"),
1138-
createTrue()
1139-
)
1140-
)
1141-
);
1142-
}
1143-
else {
1144-
statements = append(statements,
1145-
createStatement(
1146-
createCall(
1147-
createPropertyAccess(createIdentifier("Object"), "defineProperty"),
1148-
/*typeArguments*/ undefined,
1149-
[
1150-
createIdentifier("exports"),
1151-
createLiteral("__esModule"),
1152-
createObjectLiteral([
1153-
createPropertyAssignment("value", createTrue())
1154-
])
1155-
]
1156-
)
1157-
)
1158-
);
1159-
}
1160-
}
1161-
}
1162-
11631140
statements = append(statements, createExportStatement(exportName, expression, location, allowComments));
11641141
return statements;
11651142
}
11661143

1144+
function createUnderscoreUnderscoreESModule() {
1145+
let statement: Statement;
1146+
if (languageVersion === ScriptTarget.ES3) {
1147+
statement = createStatement(
1148+
createExportExpression(
1149+
createIdentifier("__esModule"),
1150+
createLiteral(true)
1151+
)
1152+
)
1153+
}
1154+
else {
1155+
statement = createStatement(
1156+
createCall(
1157+
createPropertyAccess(createIdentifier("Object"), "defineProperty"),
1158+
/*typeArguments*/ undefined,
1159+
[
1160+
createIdentifier("exports"),
1161+
createLiteral("__esModule"),
1162+
createObjectLiteral([
1163+
createPropertyAssignment("value", createLiteral(true))
1164+
])
1165+
]
1166+
)
1167+
);
1168+
}
1169+
setEmitFlags(statement, EmitFlags.CustomPrologue);
1170+
return statement;
1171+
}
1172+
11671173
/**
11681174
* Creates a call to the current file's export function to export a value.
11691175
*

src/harness/unittests/compileOnSave.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ namespace ts.projectSystem {
520520

521521
const expectedEmittedFileName = "/a/b/f1.js";
522522
assert.isTrue(host.fileExists(expectedEmittedFileName));
523-
assert.equal(host.readFile(expectedEmittedFileName), `"use strict";\r\nfunction Foo() { return 10; }\r\nexports.Foo = Foo;\r\n`);
523+
assert.equal(host.readFile(expectedEmittedFileName), `"use strict";\r\nexports.__esModule = true;\r\nfunction Foo() { return 10; }\r\nexports.Foo = Foo;\r\n`);
524524
});
525525

526526
it("shoud not emit js files in external projects", () => {

tests/baselines/reference/APISample_compile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ compile(process.argv.slice(2), {
4141
* Please log a "breaking change" issue for any API breaking change affecting this issue
4242
*/
4343
"use strict";
44+
exports.__esModule = true;
4445
var ts = require("typescript");
4546
function compile(fileNames, options) {
4647
var program = ts.createProgram(fileNames, options);

tests/baselines/reference/APISample_linter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ fileNames.forEach(fileName => {
7171
* Please log a "breaking change" issue for any API breaking change affecting this issue
7272
*/
7373
"use strict";
74+
exports.__esModule = true;
7475
var ts = require("typescript");
7576
function delint(sourceFile) {
7677
delintNode(sourceFile);

tests/baselines/reference/APISample_parseConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function createProgram(rootFiles: string[], compilerOptionsJson: string):
4343
* Please log a "breaking change" issue for any API breaking change affecting this issue
4444
*/
4545
"use strict";
46+
exports.__esModule = true;
4647
var ts = require("typescript");
4748
function printError(error) {
4849
if (!error) {

tests/baselines/reference/APISample_transform.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ console.log(JSON.stringify(result));
2323
* Please log a "breaking change" issue for any API breaking change affecting this issue
2424
*/
2525
"use strict";
26+
exports.__esModule = true;
2627
var ts = require("typescript");
2728
var source = "let x: string = 'string'";
2829
var result = ts.transpile(source, { module: ts.ModuleKind.CommonJS });

tests/baselines/reference/APISample_watcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
116116
* Please log a "breaking change" issue for any API breaking change affecting this issue
117117
*/
118118
"use strict";
119+
exports.__esModule = true;
119120
var ts = require("typescript");
120121
function watch(rootFileNames, options) {
121122
var files = {};

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedLocalVarsOfTheSameName.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export module A {
3131

3232
//// [part1.js]
3333
"use strict";
34+
exports.__esModule = true;
3435
var A;
3536
(function (A) {
3637
var Utils;
@@ -44,6 +45,7 @@ var A;
4445
})(A = exports.A || (exports.A = {}));
4546
//// [part2.js]
4647
"use strict";
48+
exports.__esModule = true;
4749
var A;
4850
(function (A) {
4951
// collision with 'Origin' var in other part of merged module

0 commit comments

Comments
 (0)