Skip to content

Commit

Permalink
Merge pull request #2335 from Microsoft/es6ImportExportEmit
Browse files Browse the repository at this point in the history
ES6 emit for new import / export syntax
  • Loading branch information
mhegazy committed Mar 17, 2015
2 parents 6155832 + a06ce61 commit 42a0c34
Show file tree
Hide file tree
Showing 200 changed files with 5,480 additions and 181 deletions.
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);
}
}
}
}

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;
}
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

0 comments on commit 42a0c34

Please sign in to comment.