Skip to content

Commit dd660dc

Browse files
committed
Merge pull request microsoft#4811 from weswigham/es6-module-type
Support modules when targeting ES6 and an ES6 ModuleKind
2 parents 5a77d67 + 8ff551c commit dd660dc

File tree

63 files changed

+999
-384
lines changed

Some content is hidden

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

63 files changed

+999
-384
lines changed

src/compiler/checker.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace ts {
4444

4545
let compilerOptions = host.getCompilerOptions();
4646
let languageVersion = compilerOptions.target || ScriptTarget.ES3;
47+
let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None;
4748

4849
let emitResolver = createResolver();
4950

@@ -13379,9 +13380,9 @@ namespace ts {
1337913380
}
1338013381
}
1338113382
else {
13382-
if (languageVersion >= ScriptTarget.ES6 && !isInAmbientContext(node)) {
13383+
if (modulekind === ModuleKind.ES6 && !isInAmbientContext(node)) {
1338313384
// Import equals declaration is deprecated in es6 or above
13384-
grammarErrorOnNode(node, Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_or_import_d_from_mod_instead);
13385+
grammarErrorOnNode(node, Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_6_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead);
1338513386
}
1338613387
}
1338713388
}
@@ -13456,11 +13457,11 @@ namespace ts {
1345613457
checkExternalModuleExports(<SourceFile | ModuleDeclaration>container);
1345713458

1345813459
if (node.isExportEquals && !isInAmbientContext(node)) {
13459-
if (languageVersion >= ScriptTarget.ES6) {
13460-
// export assignment is deprecated in es6 or above
13461-
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead);
13460+
if (modulekind === ModuleKind.ES6) {
13461+
// export assignment is not supported in es6 modules
13462+
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_modules_Consider_using_export_default_or_another_module_format_instead);
1346213463
}
13463-
else if (compilerOptions.module === ModuleKind.System) {
13464+
else if (modulekind === ModuleKind.System) {
1346413465
// system modules does not support export assignment
1346513466
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system);
1346613467
}

src/compiler/commandLineParser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ namespace ts {
7676
"amd": ModuleKind.AMD,
7777
"system": ModuleKind.System,
7878
"umd": ModuleKind.UMD,
79+
"es6": ModuleKind.ES6,
7980
},
80-
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_or_umd,
81+
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es6,
8182
paramType: Diagnostics.KIND,
82-
error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_or_umd
83+
error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_umd_or_es6
8384
},
8485
{
8586
name: "newLine",

src/compiler/diagnosticMessages.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,15 @@
619619
"category": "Error",
620620
"code": 1200
621621
},
622-
"Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"' or 'import d from \"mod\"' instead.": {
622+
"Import assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"', 'import d from \"mod\"', or another module format instead.": {
623623
"category": "Error",
624624
"code": 1202
625625
},
626-
"Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead.": {
626+
"Export assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'export default' or another module format instead.": {
627627
"category": "Error",
628628
"code": 1203
629629
},
630-
"Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher.": {
630+
"Cannot compile modules into 'es6' when targeting 'ES5' or lower.": {
631631
"category": "Error",
632632
"code": 1204
633633
},
@@ -2101,7 +2101,7 @@
21012101
"category": "Message",
21022102
"code": 6015
21032103
},
2104-
"Specify module code generation: 'commonjs', 'amd', 'system' or 'umd'": {
2104+
"Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es6'": {
21052105
"category": "Message",
21062106
"code": 6016
21072107
},
@@ -2185,7 +2185,7 @@
21852185
"category": "Error",
21862186
"code": 6045
21872187
},
2188-
"Argument for '--module' option must be 'commonjs', 'amd', 'system' or 'umd'.": {
2188+
"Argument for '--module' option must be 'commonjs', 'amd', 'system', 'umd', or 'es6'.": {
21892189
"category": "Error",
21902190
"code": 6046
21912191
},

src/compiler/emitter.ts

+31-35
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
6464

6565
let compilerOptions = host.getCompilerOptions();
6666
let languageVersion = compilerOptions.target || ScriptTarget.ES3;
67+
let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None;
6768
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
6869
let diagnostics: Diagnostic[] = [];
6970
let newLine = host.getNewLine();
@@ -188,6 +189,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
188189

189190
/** If removeComments is true, no leading-comments needed to be emitted **/
190191
let emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos: number) { } : emitLeadingCommentsOfPositionWorker;
192+
193+
let moduleEmitDelegates: Map<(node: SourceFile, startIndex: number) => void> = {
194+
[ModuleKind.ES6]: emitES6Module,
195+
[ModuleKind.AMD]: emitAMDModule,
196+
[ModuleKind.System]: emitSystemModule,
197+
[ModuleKind.UMD]: emitUMDModule,
198+
[ModuleKind.CommonJS]: emitCommonJSModule,
199+
};
191200

192201
if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) {
193202
initializeEmitterWithSourceMaps();
@@ -1493,7 +1502,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
14931502
if (container) {
14941503
if (container.kind === SyntaxKind.SourceFile) {
14951504
// Identifier references module export
1496-
if (languageVersion < ScriptTarget.ES6 && compilerOptions.module !== ModuleKind.System) {
1505+
if (modulekind !== ModuleKind.ES6 && modulekind !== ModuleKind.System) {
14971506
write("exports.");
14981507
}
14991508
}
@@ -1503,7 +1512,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
15031512
write(".");
15041513
}
15051514
}
1506-
else if (languageVersion < ScriptTarget.ES6) {
1515+
else if (modulekind !== ModuleKind.ES6) {
15071516
let declaration = resolver.getReferencedImportDeclaration(node);
15081517
if (declaration) {
15091518
if (declaration.kind === SyntaxKind.ImportClause) {
@@ -3056,7 +3065,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30563065
write(getGeneratedNameForNode(container));
30573066
write(".");
30583067
}
3059-
else if (languageVersion < ScriptTarget.ES6 && compilerOptions.module !== ModuleKind.System) {
3068+
else if (modulekind !== ModuleKind.ES6 && modulekind !== ModuleKind.System) {
30603069
write("exports.");
30613070
}
30623071
}
@@ -3076,7 +3085,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30763085
if (node.parent.kind === SyntaxKind.SourceFile) {
30773086
Debug.assert(!!(node.flags & NodeFlags.Default) || node.kind === SyntaxKind.ExportAssignment);
30783087
// only allow export default at a source file level
3079-
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
3088+
if (modulekind === ModuleKind.CommonJS || modulekind === ModuleKind.AMD || modulekind === ModuleKind.UMD) {
30803089
if (!currentSourceFile.symbol.exports["___esModule"]) {
30813090
if (languageVersion === ScriptTarget.ES5) {
30823091
// default value of configurable, enumerable, writable are `false`.
@@ -3098,7 +3107,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30983107
emitStart(node);
30993108

31003109
// emit call to exporter only for top level nodes
3101-
if (compilerOptions.module === ModuleKind.System && node.parent === currentSourceFile) {
3110+
if (modulekind === ModuleKind.System && node.parent === currentSourceFile) {
31023111
// emit export default <smth> as
31033112
// export("default", <smth>)
31043113
write(`${exportFunctionForFile}("`);
@@ -3134,7 +3143,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
31343143
}
31353144

31363145
function emitExportMemberAssignments(name: Identifier) {
3137-
if (compilerOptions.module === ModuleKind.System) {
3146+
if (modulekind === ModuleKind.System) {
31383147
return;
31393148
}
31403149

@@ -3154,7 +3163,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
31543163
}
31553164

31563165
function emitExportSpecifierInSystemModule(specifier: ExportSpecifier): void {
3157-
Debug.assert(compilerOptions.module === ModuleKind.System);
3166+
Debug.assert(modulekind === ModuleKind.System);
31583167

31593168
if (!resolver.getReferencedValueDeclaration(specifier.propertyName || specifier.name) && !resolver.isValueAliasDeclaration(specifier) ) {
31603169
return;
@@ -3491,7 +3500,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
34913500

34923501
function isES6ExportedDeclaration(node: Node) {
34933502
return !!(node.flags & NodeFlags.Export) &&
3494-
languageVersion >= ScriptTarget.ES6 &&
3503+
modulekind === ModuleKind.ES6 &&
34953504
node.parent.kind === SyntaxKind.SourceFile;
34963505
}
34973506

@@ -5257,7 +5266,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
52575266
write(";");
52585267
}
52595268
if (languageVersion < ScriptTarget.ES6 && node.parent === currentSourceFile) {
5260-
if (compilerOptions.module === ModuleKind.System && (node.flags & NodeFlags.Export)) {
5269+
if (modulekind === ModuleKind.System && (node.flags & NodeFlags.Export)) {
52615270
// write the call to exporter for enum
52625271
writeLine();
52635272
write(`${exportFunctionForFile}("`);
@@ -5379,7 +5388,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
53795388
write(" = {}));");
53805389
emitEnd(node);
53815390
if (!isES6ExportedDeclaration(node) && node.name.kind === SyntaxKind.Identifier && node.parent === currentSourceFile) {
5382-
if (compilerOptions.module === ModuleKind.System && (node.flags & NodeFlags.Export)) {
5391+
if (modulekind === ModuleKind.System && (node.flags & NodeFlags.Export)) {
53835392
writeLine();
53845393
write(`${exportFunctionForFile}("`);
53855394
emitDeclarationName(node);
@@ -5443,7 +5452,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
54435452
}
54445453

54455454
function emitImportDeclaration(node: ImportDeclaration) {
5446-
if (languageVersion < ScriptTarget.ES6) {
5455+
if (modulekind !== ModuleKind.ES6) {
54475456
return emitExternalImportDeclaration(node);
54485457
}
54495458

@@ -5494,7 +5503,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
54945503
let isExportedImport = node.kind === SyntaxKind.ImportEqualsDeclaration && (node.flags & NodeFlags.Export) !== 0;
54955504
let namespaceDeclaration = getNamespaceDeclarationNode(node);
54965505

5497-
if (compilerOptions.module !== ModuleKind.AMD) {
5506+
if (modulekind !== ModuleKind.AMD) {
54985507
emitLeadingComments(node);
54995508
emitStart(node);
55005509
if (namespaceDeclaration && !isDefaultImport(node)) {
@@ -5606,15 +5615,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
56065615
}
56075616

56085617
function emitExportDeclaration(node: ExportDeclaration) {
5609-
Debug.assert(compilerOptions.module !== ModuleKind.System);
5618+
Debug.assert(modulekind !== ModuleKind.System);
56105619

5611-
if (languageVersion < ScriptTarget.ES6) {
5620+
if (modulekind !== ModuleKind.ES6) {
56125621
if (node.moduleSpecifier && (!node.exportClause || resolver.isValueAliasDeclaration(node))) {
56135622
emitStart(node);
56145623
let generatedName = getGeneratedNameForNode(node);
56155624
if (node.exportClause) {
56165625
// export { x, y, ... } from "foo"
5617-
if (compilerOptions.module !== ModuleKind.AMD) {
5626+
if (modulekind !== ModuleKind.AMD) {
56185627
write("var ");
56195628
write(generatedName);
56205629
write(" = ");
@@ -5641,7 +5650,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
56415650
// export * from "foo"
56425651
writeLine();
56435652
write("__export(");
5644-
if (compilerOptions.module !== ModuleKind.AMD) {
5653+
if (modulekind !== ModuleKind.AMD) {
56455654
emitRequire(getExternalModuleName(node));
56465655
}
56475656
else {
@@ -5674,7 +5683,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
56745683
}
56755684

56765685
function emitExportOrImportSpecifierList(specifiers: ImportOrExportSpecifier[], shouldEmit: (node: Node) => boolean) {
5677-
Debug.assert(languageVersion >= ScriptTarget.ES6);
5686+
Debug.assert(modulekind === ModuleKind.ES6);
56785687

56795688
let needsComma = false;
56805689
for (let specifier of specifiers) {
@@ -5694,7 +5703,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
56945703

56955704
function emitExportAssignment(node: ExportAssignment) {
56965705
if (!node.isExportEquals && resolver.isValueAliasDeclaration(node)) {
5697-
if (languageVersion >= ScriptTarget.ES6) {
5706+
if (modulekind === ModuleKind.ES6) {
56985707
writeLine();
56995708
emitStart(node);
57005709
write("export default ");
@@ -5709,7 +5718,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
57095718
else {
57105719
writeLine();
57115720
emitStart(node);
5712-
if (compilerOptions.module === ModuleKind.System) {
5721+
if (modulekind === ModuleKind.System) {
57135722
write(`${exportFunctionForFile}("default",`);
57145723
emit(node.expression);
57155724
write(")");
@@ -6155,7 +6164,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
61556164
}
61566165

61576166
function isCurrentFileSystemExternalModule() {
6158-
return compilerOptions.module === ModuleKind.System && isExternalModule(currentSourceFile);
6167+
return modulekind === ModuleKind.System && isExternalModule(currentSourceFile);
61596168
}
61606169

61616170
function emitSystemModuleBody(node: SourceFile, dependencyGroups: DependencyGroup[], startIndex: number): void {
@@ -6713,21 +6722,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67136722
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
67146723

67156724
if (isExternalModule(node) || compilerOptions.isolatedModules) {
6716-
if (languageVersion >= ScriptTarget.ES6) {
6717-
emitES6Module(node, startIndex);
6718-
}
6719-
else if (compilerOptions.module === ModuleKind.AMD) {
6720-
emitAMDModule(node, startIndex);
6721-
}
6722-
else if (compilerOptions.module === ModuleKind.System) {
6723-
emitSystemModule(node, startIndex);
6724-
}
6725-
else if (compilerOptions.module === ModuleKind.UMD) {
6726-
emitUMDModule(node, startIndex);
6727-
}
6728-
else {
6729-
emitCommonJSModule(node, startIndex);
6730-
}
6725+
let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS];
6726+
emitModule(node, startIndex);
67316727
}
67326728
else {
67336729
externalImports = undefined;

src/compiler/program.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,9 @@ namespace ts {
10221022
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_modules_unless_the_module_flag_is_provided));
10231023
}
10241024

1025-
// Cannot specify module gen target when in es6 or above
1026-
if (options.module && languageVersion >= ScriptTarget.ES6) {
1027-
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_commonjs_amd_system_or_umd_when_targeting_ES6_or_higher));
1025+
// Cannot specify module gen target of es6 when below es6
1026+
if (options.module === ModuleKind.ES6 && languageVersion < ScriptTarget.ES6) {
1027+
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_es6_when_targeting_ES5_or_lower));
10281028
}
10291029

10301030
// there has to be common source directory if user specified --outdir || --sourceRoot

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,7 @@ namespace ts {
20762076
AMD = 2,
20772077
UMD = 3,
20782078
System = 4,
2079+
ES6 = 5,
20792080
}
20802081

20812082
export const enum JsxEmit {

0 commit comments

Comments
 (0)