Skip to content

Commit 442adde

Browse files
committed
hoist source level class declarations, fix error message
1 parent 8c80792 commit 442adde

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10623,7 +10623,7 @@ module ts {
1062310623
}
1062410624
else if (compilerOptions.module === ModuleKind.System) {
1062510625
// system modules does not support export assignment
10626-
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_with_module_flag_is_system);
10626+
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system);
1062710627
}
1062810628
}
1062910629
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module ts {
169169
Ambient_const_enums_are_not_allowed_when_the_separateCompilation_flag_is_provided: { code: 1209, category: DiagnosticCategory.Error, key: "Ambient const enums are not allowed when the '--separateCompilation' flag is provided." },
170170
Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode: { code: 1210, category: DiagnosticCategory.Error, key: "Invalid use of '{0}'. Class definitions are automatically in strict mode." },
171171
A_class_declaration_without_the_default_modifier_must_have_a_name: { code: 1211, category: DiagnosticCategory.Error, key: "A class declaration without the 'default' modifier must have a name" },
172-
Export_assignment_is_not_supported_with_module_flag_is_system: { code: 1212, category: DiagnosticCategory.Error, key: "Export assignment is not supported with '--module' flag is 'system'." },
172+
Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1212, category: DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." },
173173
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
174174
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." },
175175
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@
667667
"category": "Error",
668668
"code": 1211
669669
},
670-
"Export assignment is not supported with '--module' flag is 'system'.": {
670+
"Export assignment is not supported when '--module' flag is 'system'.": {
671671
"category": "Error",
672672
"code": 1212
673673
},

src/compiler/emitter.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,7 +3818,9 @@ var __param = this.__param || function(index, decorator) { return function (targ
38183818

38193819
function emitClassLikeDeclarationBelowES6(node: ClassLikeDeclaration) {
38203820
if (node.kind === SyntaxKind.ClassDeclaration) {
3821-
write("var ");
3821+
if (!currentFileIsEmittedAsSystemModule() || !isSourceFileLevelDeclaration(node)) {
3822+
write("var ");
3823+
}
38223824
emitDeclarationName(node);
38233825
write(" = ");
38243826
}
@@ -4827,20 +4829,25 @@ var __param = this.__param || function(index, decorator) { return function (targ
48274829
}
48284830

48294831
function hoistTopLevelVariableAndFunctionDeclarations(node: SourceFile): void {
4830-
let hoistedLocals: Identifier[];
4832+
let hoistedVars: (Identifier | ClassDeclaration)[];
48314833
let hoistedFunctionDeclarations: FunctionDeclaration[];
48324834

48334835
visit(node);
48344836

4835-
if (hoistedLocals) {
4837+
if (hoistedVars) {
48364838
writeLine();
48374839
write("var ");
4838-
for (let i = 0; i < hoistedLocals.length; ++i) {
4839-
let local = hoistedLocals[i];
4840+
for (let i = 0; i < hoistedVars.length; ++i) {
4841+
let local = hoistedVars[i];
48404842
if (i !== 0) {
48414843
write(", ");
48424844
}
4843-
emit(local);
4845+
if (local.kind === SyntaxKind.ClassDeclaration) {
4846+
emitDeclarationName(<ClassDeclaration>local);
4847+
}
4848+
else {
4849+
emit(local);
4850+
}
48444851
}
48454852
write(";")
48464853
}
@@ -4858,11 +4865,17 @@ var __param = this.__param || function(index, decorator) { return function (targ
48584865
return;
48594866
}
48604867

4868+
if (node.kind === SyntaxKind.ClassDeclaration) {
4869+
// TODO: rename block scoped classes
4870+
(hoistedVars || (hoistedVars = [])).push(<ClassDeclaration>node);
4871+
return;
4872+
}
4873+
48614874
if (node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement) {
48624875
let name = (<VariableDeclaration | BindingElement>node).name;
48634876
if (name.kind === SyntaxKind.Identifier) {
48644877
renameNonTopLevelLetAndConst(name);
4865-
(hoistedLocals || (hoistedLocals = [])).push(<Identifier>name);
4878+
(hoistedVars || (hoistedVars = [])).push(<Identifier>name);
48664879
}
48674880
else {
48684881
forEachChild(name, visit);

0 commit comments

Comments
 (0)