Skip to content

ES6 emit for new import / export syntax #2335

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

Merged
merged 34 commits into from
Mar 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e902d84
ES6 doesnt support import id = require("mod") syntax
mhegazy Mar 12, 2015
61a5bfb
Report error on export assignment with es6 and above target
mhegazy Mar 12, 2015
a6e4e04
Add tests
mhegazy Mar 12, 2015
04ea7fe
Handel isDeclaration visible for imports
mhegazy Mar 12, 2015
b52d9ec
Report error if module gen target is specified in es6
mhegazy Mar 12, 2015
4ef687c
Add tests
mhegazy Mar 12, 2015
7b3e50f
Emit in ES6 module if script target is es6 or higher
mhegazy Mar 12, 2015
8c26507
Support for emitting import declaration in es6 format
mhegazy Mar 12, 2015
3ed8bcc
Simplify module kind selection
mhegazy Mar 12, 2015
4b75484
Fix the checks with language version to use default es3
sheetalkamat Feb 13, 2015
006ed82
Remove references with exports.id as es6 module doesnt have exports.id
sheetalkamat Feb 17, 2015
29b2214
Do not rewrite substitute named import reference when generating es6 …
sheetalkamat Feb 13, 2015
05932fd
Es6 module emit for export VarDeclaration, export LexicalDeclaration
sheetalkamat Feb 17, 2015
b9f63a8
Emit es6 export ModuleDeclaration
mhegazy Mar 12, 2015
58d1959
Emit ES6 module enum declaration
mhegazy Mar 12, 2015
b091fa5
Emit export function declaration in es6 format
mhegazy Mar 12, 2015
6bcbe82
Emit export class declaration in es6 format.
mhegazy Mar 12, 2015
680cf6d
Emit export internal import equals declaration in es6 format
mhegazy Mar 12, 2015
fe9fff5
Export * and export { names } emit in es6 format
mhegazy Mar 12, 2015
b3c8bcb
Emit export default in ES6
mhegazy Mar 13, 2015
b6bbf06
Update error message
mhegazy Mar 13, 2015
8e06265
Merge branch 'master' into es6ImportExportEmit
mhegazy Mar 13, 2015
b6a6d85
Merge branch 'master' into es6ImportExportEmit
mhegazy Mar 15, 2015
c984e81
Fix issue of the default binding not elided if namedImport is reference
mhegazy Mar 16, 2015
c877b1e
Add tests
mhegazy Mar 16, 2015
090148f
Merge branch 'master' into es6ImportExportEmit
mhegazy Mar 16, 2015
5845d2d
Merge branch 'master' into es6ImportExportEmit
mhegazy Mar 16, 2015
eb954e1
Respond to code review comments
mhegazy Mar 17, 2015
cb012e0
Merge branch 'master' into es6ImportExportEmit
mhegazy Mar 17, 2015
9910869
Do not emit "export" for classes within modules, and do not write the…
mhegazy Mar 17, 2015
3d80243
Export classes defined wihtin internal modules correctelly
mhegazy Mar 17, 2015
1932f72
Handel export name bindings in internal modules in ES6
mhegazy Mar 17, 2015
766cb68
Add comments
mhegazy Mar 17, 2015
a06ce61
Merge branch 'master' into es6ImportExportEmit
mhegazy Mar 17, 2015
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
3 changes: 1 addition & 2 deletions scripts/processDiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
' ' + convertPropertyName(nameMap[name]) +
': { code: ' + diagnosticDetails.code +
', category: DiagnosticCategory.' + diagnosticDetails.category +
', key: "' + name.replace('"', '\\"') + '"' +
(diagnosticDetails.isEarly ? ', isEarly: true' : '') +
', key: "' + name.replace(/[\"]/g, '\\"') + '"' +
' },\r\n';
}

Expand Down
33 changes: 28 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ module ts {
}

function getExportsForModule(moduleSymbol: Symbol): SymbolTable {
if (compilerOptions.target < ScriptTarget.ES6) {
if (languageVersion < ScriptTarget.ES6) {
// A default export hides all other exports in CommonJS and AMD modules
let defaultSymbol = getExportAssignmentSymbol(moduleSymbol);
if (defaultSymbol) {
Expand Down Expand Up @@ -1812,6 +1812,11 @@ module ts {
case SyntaxKind.ParenthesizedType:
return isDeclarationVisible(<Declaration>node.parent);

case SyntaxKind.ImportClause:
case SyntaxKind.NamespaceImport:
case SyntaxKind.ImportSpecifier:
return false;

// Type parameters are always visible
case SyntaxKind.TypeParameter:
// Source file is always visible
Expand Down Expand Up @@ -10093,6 +10098,12 @@ module ts {
}
}
}
else {
if (languageVersion >= ScriptTarget.ES6) {
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this documented with Jonathan as a breaking change? (it seems like a very big one, erroring on an entire feature if you move to ES6).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. that is the current design. we will have a blog post about moving to the new module syntax.

}
}
}
}

Expand Down Expand Up @@ -10140,6 +10151,11 @@ module ts {
}

checkExternalModuleExports(container);

if (node.isExportEquals && 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);
}
}

function getModuleStatements(node: Declaration): ModuleElement[] {
Expand Down Expand Up @@ -10182,7 +10198,7 @@ module ts {
if (!links.exportsChecked) {
let defaultSymbol = getExportAssignmentSymbol(moduleSymbol);
if (defaultSymbol) {
if (hasExportedMembers(moduleSymbol)) {
if (languageVersion < ScriptTarget.ES6 && hasExportedMembers(moduleSymbol)) {
let declaration = getDeclarationOfAliasSymbol(defaultSymbol) || defaultSymbol.valueDeclaration;
error(declaration, Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements);
}
Expand Down Expand Up @@ -11005,7 +11021,15 @@ module ts {

function getExportNameSubstitution(symbol: Symbol, location: Node): string {
if (isExternalModuleSymbol(symbol.parent)) {
return "exports." + unescapeIdentifier(symbol.name);
var symbolName = unescapeIdentifier(symbol.name);
// If this is es6 or higher, just use the name of the export
// no need to qualify it.
if (languageVersion >= ScriptTarget.ES6) {
return symbolName;
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment why we don't prefix export name with 'exports.' when emitting for ES6+?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

}
else {
return "exports." + symbolName;
}
}
let node = location;
let containerSymbol = getParentOfSymbol(symbol);
Expand Down Expand Up @@ -11033,7 +11057,7 @@ module ts {
return getExportNameSubstitution(exportSymbol, node.parent);
}
// Named imports from ES6 import declarations are rewritten
if (symbol.flags & SymbolFlags.Alias) {
if (symbol.flags & SymbolFlags.Alias && languageVersion < ScriptTarget.ES6) {
return getAliasNameSubstitution(symbol);
}
}
Expand Down Expand Up @@ -11069,7 +11093,6 @@ module ts {
return true;
}
}
return forEachChild(node, isReferencedAliasDeclaration);
}

function isImplementationOfOverload(node: FunctionLikeDeclaration) {
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ module ts {
Unterminated_Unicode_escape_sequence: { code: 1199, category: DiagnosticCategory.Error, key: "Unterminated Unicode escape sequence." },
Line_terminator_not_permitted_before_arrow: { code: 1200, category: DiagnosticCategory.Error, key: "Line terminator not permitted before arrow." },
A_type_annotation_on_an_export_statement_is_only_allowed_in_an_ambient_external_module_declaration: { code: 1201, category: DiagnosticCategory.Error, key: "A type annotation on an export statement is only allowed in an ambient external module declaration." },
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: { code: 1202, category: DiagnosticCategory.Error, key: "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." },
Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead: { code: 1203, category: DiagnosticCategory.Error, key: "Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead." },
Cannot_compile_external_modules_into_amd_or_commonjs_when_targeting_es6_or_higher: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile external modules into amd or commonjs when targeting es6 or higher." },
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,18 @@
"category": "Error",
"code": 1201
},
"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.": {
"category": "Error",
"code": 1202
},
"Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead.": {
"category": "Error",
"code": 1203
},
"Cannot compile external modules into amd or commonjs when targeting es6 or higher.": {
"category": "Error",
"code": 1204
},

"Duplicate identifier '{0}'.": {
"category": "Error",
Expand Down
Loading