Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support modules when targeting ES6 and an ES6 ModuleKind #4811

Merged
merged 8 commits into from
Sep 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace ts {

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

let emitResolver = createResolver();

Expand Down Expand Up @@ -13379,9 +13380,9 @@ namespace ts {
}
}
else {
if (languageVersion >= ScriptTarget.ES6 && !isInAmbientContext(node)) {
if (modulekind === ModuleKind.ES6 && !isInAmbientContext(node)) {
// Import equals declaration is deprecated in es6 or above
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);
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);
}
}
}
Expand Down Expand Up @@ -13456,11 +13457,11 @@ namespace ts {
checkExternalModuleExports(<SourceFile | ModuleDeclaration>container);

if (node.isExportEquals && !isInAmbientContext(node)) {
if (languageVersion >= ScriptTarget.ES6) {
// export assignment is deprecated in es6 or above
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead);
if (modulekind === ModuleKind.ES6) {
// export assignment is not supported in es6 modules
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_modules_Consider_using_export_default_or_another_module_format_instead);
}
else if (compilerOptions.module === ModuleKind.System) {
else if (modulekind === ModuleKind.System) {
// system modules does not support export assignment
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system);
}
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ namespace ts {
"amd": ModuleKind.AMD,
"system": ModuleKind.System,
"umd": ModuleKind.UMD,
"es6": ModuleKind.ES6,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do not need that. just leave target as es6 and allow for additional --module flag. today we make it an error to use --target es6 with --module; relaxing this should be sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internally we checked for language version like it was a module kind in a number of places. Is it just a matter of not wanting to make it able to be specified explicitly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not support target: es5 with module: es6, so why allow it and add additional burden on users.
--target es6 with no --module is implied to be es6, if another --module tareget is specified, emit should use that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, so I'm just removing it as an option for the --module command line flag.

},
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_or_umd,
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es6,
paramType: Diagnostics.KIND,
error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_or_umd
error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_umd_or_es6
},
{
name: "newLine",
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,15 @@
"category": "Error",
"code": 1200
},
"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.": {
"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.": {
"category": "Error",
"code": 1202
},
"Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead.": {
"Export assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'export default' or another module format instead.": {
"category": "Error",
"code": 1203
},
"Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher.": {
"Cannot compile modules into 'es6' when targeting 'ES5' or lower.": {
"category": "Error",
"code": 1204
},
Expand Down Expand Up @@ -2101,7 +2101,7 @@
"category": "Message",
"code": 6015
},
"Specify module code generation: 'commonjs', 'amd', 'system' or 'umd'": {
"Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es6'": {
"category": "Message",
"code": 6016
},
Expand Down Expand Up @@ -2185,7 +2185,7 @@
"category": "Error",
"code": 6045
},
"Argument for '--module' option must be 'commonjs', 'amd', 'system' or 'umd'.": {
"Argument for '--module' option must be 'commonjs', 'amd', 'system', 'umd', or 'es6'.": {
"category": "Error",
"code": 6046
},
Expand Down
66 changes: 31 additions & 35 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi

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

/** If removeComments is true, no leading-comments needed to be emitted **/
let emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos: number) { } : emitLeadingCommentsOfPositionWorker;

let moduleEmitDelegates: Map<(node: SourceFile, startIndex: number) => void> = {
[ModuleKind.ES6]: emitES6Module,
[ModuleKind.AMD]: emitAMDModule,
[ModuleKind.System]: emitSystemModule,
[ModuleKind.UMD]: emitUMDModule,
[ModuleKind.CommonJS]: emitCommonJSModule,
};

if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) {
initializeEmitterWithSourceMaps();
Expand Down Expand Up @@ -1493,7 +1502,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
if (container) {
if (container.kind === SyntaxKind.SourceFile) {
// Identifier references module export
if (languageVersion < ScriptTarget.ES6 && compilerOptions.module !== ModuleKind.System) {
if (modulekind !== ModuleKind.ES6 && modulekind !== ModuleKind.System) {
write("exports.");
}
}
Expand All @@ -1503,7 +1512,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(".");
}
}
else if (languageVersion < ScriptTarget.ES6) {
else if (modulekind !== ModuleKind.ES6) {
let declaration = resolver.getReferencedImportDeclaration(node);
if (declaration) {
if (declaration.kind === SyntaxKind.ImportClause) {
Expand Down Expand Up @@ -3056,7 +3065,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(getGeneratedNameForNode(container));
write(".");
}
else if (languageVersion < ScriptTarget.ES6 && compilerOptions.module !== ModuleKind.System) {
else if (modulekind !== ModuleKind.ES6 && modulekind !== ModuleKind.System) {
write("exports.");
}
}
Expand All @@ -3076,7 +3085,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
if (node.parent.kind === SyntaxKind.SourceFile) {
Debug.assert(!!(node.flags & NodeFlags.Default) || node.kind === SyntaxKind.ExportAssignment);
// only allow export default at a source file level
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
if (modulekind === ModuleKind.CommonJS || modulekind === ModuleKind.AMD || modulekind === ModuleKind.UMD) {
if (!currentSourceFile.symbol.exports["___esModule"]) {
if (languageVersion === ScriptTarget.ES5) {
// default value of configurable, enumerable, writable are `false`.
Expand All @@ -3098,7 +3107,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitStart(node);

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

function emitExportMemberAssignments(name: Identifier) {
if (compilerOptions.module === ModuleKind.System) {
if (modulekind === ModuleKind.System) {
return;
}

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

function emitExportSpecifierInSystemModule(specifier: ExportSpecifier): void {
Debug.assert(compilerOptions.module === ModuleKind.System);
Debug.assert(modulekind === ModuleKind.System);

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

function isES6ExportedDeclaration(node: Node) {
return !!(node.flags & NodeFlags.Export) &&
languageVersion >= ScriptTarget.ES6 &&
modulekind === ModuleKind.ES6 &&
node.parent.kind === SyntaxKind.SourceFile;
}

Expand Down Expand Up @@ -5257,7 +5266,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(";");
}
if (languageVersion < ScriptTarget.ES6 && node.parent === currentSourceFile) {
if (compilerOptions.module === ModuleKind.System && (node.flags & NodeFlags.Export)) {
if (modulekind === ModuleKind.System && (node.flags & NodeFlags.Export)) {
// write the call to exporter for enum
writeLine();
write(`${exportFunctionForFile}("`);
Expand Down Expand Up @@ -5379,7 +5388,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(" = {}));");
emitEnd(node);
if (!isES6ExportedDeclaration(node) && node.name.kind === SyntaxKind.Identifier && node.parent === currentSourceFile) {
if (compilerOptions.module === ModuleKind.System && (node.flags & NodeFlags.Export)) {
if (modulekind === ModuleKind.System && (node.flags & NodeFlags.Export)) {
writeLine();
write(`${exportFunctionForFile}("`);
emitDeclarationName(node);
Expand Down Expand Up @@ -5443,7 +5452,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}

function emitImportDeclaration(node: ImportDeclaration) {
if (languageVersion < ScriptTarget.ES6) {
if (modulekind !== ModuleKind.ES6) {
return emitExternalImportDeclaration(node);
}

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

if (compilerOptions.module !== ModuleKind.AMD) {
if (modulekind !== ModuleKind.AMD) {
emitLeadingComments(node);
emitStart(node);
if (namespaceDeclaration && !isDefaultImport(node)) {
Expand Down Expand Up @@ -5606,15 +5615,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}

function emitExportDeclaration(node: ExportDeclaration) {
Debug.assert(compilerOptions.module !== ModuleKind.System);
Debug.assert(modulekind !== ModuleKind.System);

if (languageVersion < ScriptTarget.ES6) {
if (modulekind !== ModuleKind.ES6) {
if (node.moduleSpecifier && (!node.exportClause || resolver.isValueAliasDeclaration(node))) {
emitStart(node);
let generatedName = getGeneratedNameForNode(node);
if (node.exportClause) {
// export { x, y, ... } from "foo"
if (compilerOptions.module !== ModuleKind.AMD) {
if (modulekind !== ModuleKind.AMD) {
write("var ");
write(generatedName);
write(" = ");
Expand All @@ -5641,7 +5650,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
// export * from "foo"
writeLine();
write("__export(");
if (compilerOptions.module !== ModuleKind.AMD) {
if (modulekind !== ModuleKind.AMD) {
emitRequire(getExternalModuleName(node));
}
else {
Expand Down Expand Up @@ -5674,7 +5683,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}

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

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

function emitExportAssignment(node: ExportAssignment) {
if (!node.isExportEquals && resolver.isValueAliasDeclaration(node)) {
if (languageVersion >= ScriptTarget.ES6) {
if (modulekind === ModuleKind.ES6) {
writeLine();
emitStart(node);
write("export default ");
Expand All @@ -5709,7 +5718,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
else {
writeLine();
emitStart(node);
if (compilerOptions.module === ModuleKind.System) {
if (modulekind === ModuleKind.System) {
write(`${exportFunctionForFile}("default",`);
emit(node.expression);
write(")");
Expand Down Expand Up @@ -6155,7 +6164,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}

function isCurrentFileSystemExternalModule() {
return compilerOptions.module === ModuleKind.System && isExternalModule(currentSourceFile);
return modulekind === ModuleKind.System && isExternalModule(currentSourceFile);
}

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

if (isExternalModule(node) || compilerOptions.isolatedModules) {
if (languageVersion >= ScriptTarget.ES6) {
emitES6Module(node, startIndex);
}
else if (compilerOptions.module === ModuleKind.AMD) {
emitAMDModule(node, startIndex);
}
else if (compilerOptions.module === ModuleKind.System) {
emitSystemModule(node, startIndex);
}
else if (compilerOptions.module === ModuleKind.UMD) {
emitUMDModule(node, startIndex);
}
else {
emitCommonJSModule(node, startIndex);
}
let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS];
emitModule(node, startIndex);
}
else {
externalImports = undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,9 @@ namespace ts {
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_modules_unless_the_module_flag_is_provided));
}

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

// there has to be common source directory if user specified --outdir || --sourceRoot
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,7 @@ namespace ts {
AMD = 2,
UMD = 3,
System = 4,
ES6 = 5,
}

export const enum JsxEmit {
Expand Down
Loading