From 442addecf570e5229a65179183dd3cd8178d9246 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sat, 11 Apr 2015 06:14:19 -0700 Subject: [PATCH] hoist source level class declarations, fix error message --- src/compiler/checker.ts | 2 +- .../diagnosticInformationMap.generated.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/emitter.ts | 27 ++++++++++++++----- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9594339a266..b839b2dbda6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10623,7 +10623,7 @@ module ts { } else if (compilerOptions.module === ModuleKind.System) { // system modules does not support export assignment - grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_with_module_flag_is_system); + grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system); } } } diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index b18b59377b0..fd86522a9d3 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -169,7 +169,7 @@ module ts { 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." }, 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." }, 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" }, - 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'." }, + 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'." }, 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." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index cf7e60fa311..2ea5ac6f545 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -667,7 +667,7 @@ "category": "Error", "code": 1211 }, - "Export assignment is not supported with '--module' flag is 'system'.": { + "Export assignment is not supported when '--module' flag is 'system'.": { "category": "Error", "code": 1212 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index fcac5c9c2df..f2e8d0160a4 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3818,7 +3818,9 @@ var __param = this.__param || function(index, decorator) { return function (targ function emitClassLikeDeclarationBelowES6(node: ClassLikeDeclaration) { if (node.kind === SyntaxKind.ClassDeclaration) { - write("var "); + if (!currentFileIsEmittedAsSystemModule() || !isSourceFileLevelDeclaration(node)) { + write("var "); + } emitDeclarationName(node); write(" = "); } @@ -4827,20 +4829,25 @@ var __param = this.__param || function(index, decorator) { return function (targ } function hoistTopLevelVariableAndFunctionDeclarations(node: SourceFile): void { - let hoistedLocals: Identifier[]; + let hoistedVars: (Identifier | ClassDeclaration)[]; let hoistedFunctionDeclarations: FunctionDeclaration[]; visit(node); - if (hoistedLocals) { + if (hoistedVars) { writeLine(); write("var "); - for (let i = 0; i < hoistedLocals.length; ++i) { - let local = hoistedLocals[i]; + for (let i = 0; i < hoistedVars.length; ++i) { + let local = hoistedVars[i]; if (i !== 0) { write(", "); } - emit(local); + if (local.kind === SyntaxKind.ClassDeclaration) { + emitDeclarationName(local); + } + else { + emit(local); + } } write(";") } @@ -4858,11 +4865,17 @@ var __param = this.__param || function(index, decorator) { return function (targ return; } + if (node.kind === SyntaxKind.ClassDeclaration) { + // TODO: rename block scoped classes + (hoistedVars || (hoistedVars = [])).push(node); + return; + } + if (node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement) { let name = (node).name; if (name.kind === SyntaxKind.Identifier) { renameNonTopLevelLetAndConst(name); - (hoistedLocals || (hoistedLocals = [])).push(name); + (hoistedVars || (hoistedVars = [])).push(name); } else { forEachChild(name, visit);