From a5a6c6f2423870d61504d344ef1080795cebda14 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 17 Oct 2014 15:15:22 -0700 Subject: [PATCH] Allow const and let declarations to be exported in modules. Also ensure that const module elements are not used as references. --- src/compiler/checker.ts | 25 ++++- .../diagnosticInformationMap.generated.ts | 5 +- src/compiler/diagnosticMessages.json | 8 +- src/compiler/parser.ts | 7 +- .../constDeclarations-access2.errors.txt | 7 +- .../reference/constDeclarations-access2.js | 3 + .../constDeclarations-access3.errors.txt | 102 +++++++++++++++++ .../reference/constDeclarations-access3.js | 83 ++++++++++++++ .../constDeclarations-access4.errors.txt | 102 +++++++++++++++++ .../reference/constDeclarations-access4.js | 79 ++++++++++++++ .../constDeclarations-access5.errors.txt | 103 ++++++++++++++++++ .../reference/constDeclarations-access5.js | 89 +++++++++++++++ ...nstDeclarations-invalidContexts.errors.txt | 36 +++--- .../reference/constDeclarations2.errors.txt | 20 ---- .../baselines/reference/constDeclarations2.js | 26 +++++ .../reference/constDeclarations2.types | 18 +++ ...letDeclarations-invalidContexts.errors.txt | 36 +++--- .../reference/letDeclarations2.errors.txt | 11 -- tests/baselines/reference/letDeclarations2.js | 19 ++++ .../reference/letDeclarations2.types | 11 ++ .../compiler/constDeclarations-access2.ts | 2 + .../compiler/constDeclarations-access3.ts | 45 ++++++++ .../compiler/constDeclarations-access4.ts | 45 ++++++++ .../compiler/constDeclarations-access5.ts | 48 ++++++++ 24 files changed, 843 insertions(+), 87 deletions(-) create mode 100644 tests/baselines/reference/constDeclarations-access3.errors.txt create mode 100644 tests/baselines/reference/constDeclarations-access3.js create mode 100644 tests/baselines/reference/constDeclarations-access4.errors.txt create mode 100644 tests/baselines/reference/constDeclarations-access4.js create mode 100644 tests/baselines/reference/constDeclarations-access5.errors.txt create mode 100644 tests/baselines/reference/constDeclarations-access5.js delete mode 100644 tests/baselines/reference/constDeclarations2.errors.txt create mode 100644 tests/baselines/reference/constDeclarations2.js create mode 100644 tests/baselines/reference/constDeclarations2.types delete mode 100644 tests/baselines/reference/letDeclarations2.errors.txt create mode 100644 tests/baselines/reference/letDeclarations2.js create mode 100644 tests/baselines/reference/letDeclarations2.types create mode 100644 tests/cases/compiler/constDeclarations-access3.ts create mode 100644 tests/cases/compiler/constDeclarations-access4.ts create mode 100644 tests/cases/compiler/constDeclarations-access5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0065d138e9e..bb96151997a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5550,12 +5550,27 @@ module ts { } } - function isConstVariableReference(n: Node) { - if (n.kind === SyntaxKind.Identifier) { - var symbol = findSymbol(n); - return symbol && (symbol.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(symbol) & NodeFlags.Const) !== 0; + function isConstVariableReference(n: Node): boolean { + switch (n.kind) { + case SyntaxKind.Identifier: + case SyntaxKind.PropertyAccess: + var symbol = findSymbol(n); + return symbol && (symbol.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(symbol) & NodeFlags.Const) !== 0; + case SyntaxKind.IndexedAccess: + var index = (n).index; + var symbol = findSymbol((n).object); + if (symbol && index.kind === SyntaxKind.StringLiteral) { + var name = (index).text; + var apparentType = getApparentType(getTypeOfSymbol(symbol)); + var prop = getPropertyOfApparentType(apparentType, name); + return prop && (prop.flags & SymbolFlags.Variable) !== 0 && (getDeclarationFlagsFromSymbol(prop) & NodeFlags.Const) !== 0; + } + return false; + case SyntaxKind.ParenExpression: + return isConstVariableReference((n).expression); + default: + return false; } - return false; } if (!isReferenceOrErrorExpression(n)) { diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index d96214ae1d9..ef0ab4327a1 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -119,9 +119,8 @@ module ts { let_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1153, category: DiagnosticCategory.Error, key: "'let' declarations are only available when targeting ECMAScript 6 and higher." }, const_declarations_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1154, category: DiagnosticCategory.Error, key: "'const' declarations are only available when targeting ECMAScript 6 and higher." }, const_must_be_intialized: { code: 1155, category: DiagnosticCategory.Error, key: "const must be intialized." }, - const_must_be_declared_inside_a_block: { code: 1156, category: DiagnosticCategory.Error, key: "const must be declared inside a block." }, - let_must_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "let must be declared inside a block." }, - Only_var_declarations_can_be_exported: { code: 1158, category: DiagnosticCategory.Error, key: "Only var declarations can be exported." }, + const_declarations_must_be_declared_inside_a_block: { code: 1156, category: DiagnosticCategory.Error, key: "'const' declarations must be declared inside a block." }, + let_declarations_must_be_declared_inside_a_block: { code: 1157, category: DiagnosticCategory.Error, key: "'let' declarations must be declared inside a block." }, 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 ee1a82b9c13..34ecabe31ae 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -467,18 +467,14 @@ "category": "Error", "code": 1155 }, - "const must be declared inside a block.": { + "'const' declarations must be declared inside a block.": { "category": "Error", "code": 1156 }, - "let must be declared inside a block.": { + "'let' declarations must be declared inside a block.": { "category": "Error", "code": 1157 }, - "Only var declarations can be exported.": { - "category": "Error", - "code": 1158 - }, "Duplicate identifier '{0}'.": { "category": "Error", diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 305ef595041..2ee564c7302 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3180,15 +3180,12 @@ module ts { } else if (!allowLetAndConstDeclarations) { if (node.flags & NodeFlags.Let) { - grammarErrorOnNode(node, Diagnostics.let_must_be_declared_inside_a_block); + grammarErrorOnNode(node, Diagnostics.let_declarations_must_be_declared_inside_a_block); } else if (node.flags & NodeFlags.Const) { - grammarErrorOnNode(node, Diagnostics.const_must_be_declared_inside_a_block); + grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_declared_inside_a_block); } } - else if (node.flags & NodeFlags.Export && node.flags & NodeFlags.BlockScoped) { - grammarErrorOnNode(node, Diagnostics.Only_var_declarations_can_be_exported); - } return node; } diff --git a/tests/baselines/reference/constDeclarations-access2.errors.txt b/tests/baselines/reference/constDeclarations-access2.errors.txt index f3bf7d1d750..6a360e11014 100644 --- a/tests/baselines/reference/constDeclarations-access2.errors.txt +++ b/tests/baselines/reference/constDeclarations-access2.errors.txt @@ -14,9 +14,10 @@ tests/cases/compiler/constDeclarations-access2.ts(18,1): error TS2449: The opera tests/cases/compiler/constDeclarations-access2.ts(19,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. tests/cases/compiler/constDeclarations-access2.ts(20,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. tests/cases/compiler/constDeclarations-access2.ts(21,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access2.ts(23,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. -==== tests/cases/compiler/constDeclarations-access2.ts (16 errors) ==== +==== tests/cases/compiler/constDeclarations-access2.ts (17 errors) ==== const x = 0 @@ -71,6 +72,10 @@ tests/cases/compiler/constDeclarations-access2.ts(21,3): error TS2449: The opera ~ !!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + ++((x)); + ~~~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + // OK var a = x + 1; diff --git a/tests/baselines/reference/constDeclarations-access2.js b/tests/baselines/reference/constDeclarations-access2.js index e2a1bcd9fa5..fd51c932636 100644 --- a/tests/baselines/reference/constDeclarations-access2.js +++ b/tests/baselines/reference/constDeclarations-access2.js @@ -21,6 +21,8 @@ x--; ++x; --x; +++((x)); + // OK var a = x + 1; @@ -57,6 +59,7 @@ x++; x--; ++x; --x; +++((x)); // OK var a = x + 1; function f(v) { diff --git a/tests/baselines/reference/constDeclarations-access3.errors.txt b/tests/baselines/reference/constDeclarations-access3.errors.txt new file mode 100644 index 00000000000..577b087dff0 --- /dev/null +++ b/tests/baselines/reference/constDeclarations-access3.errors.txt @@ -0,0 +1,102 @@ +tests/cases/compiler/constDeclarations-access3.ts(8,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(9,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(10,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(11,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(12,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(13,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(14,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(15,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(16,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(17,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(18,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(19,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(21,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(22,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(23,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(24,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(26,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access3.ts(28,1): error TS2450: Left-hand side of assignment expression cannot be a constant. + + +==== tests/cases/compiler/constDeclarations-access3.ts (18 errors) ==== + + + module M { + export const x = 0; + } + + // Errors + M.x = 1; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x += 2; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x -= 3; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x *= 4; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x /= 5; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x %= 6; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x <<= 7; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x >>= 8; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x >>>= 9; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x &= 10; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x |= 11; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x ^= 12; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + + M.x++; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + M.x--; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + ++M.x; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + --M.x; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + + ++((M.x)); + ~~~~~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + + M["x"] = 0; + ~~~~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + + // OK + var a = M.x + 1; + + function f(v: number) { } + f(M.x); + + if (M.x) { } + + M.x; + (M.x); + + -M.x; + +M.x; + + M.x.toString(); + \ No newline at end of file diff --git a/tests/baselines/reference/constDeclarations-access3.js b/tests/baselines/reference/constDeclarations-access3.js new file mode 100644 index 00000000000..9ac4880bdd1 --- /dev/null +++ b/tests/baselines/reference/constDeclarations-access3.js @@ -0,0 +1,83 @@ +//// [constDeclarations-access3.ts] + + +module M { + export const x = 0; +} + +// Errors +M.x = 1; +M.x += 2; +M.x -= 3; +M.x *= 4; +M.x /= 5; +M.x %= 6; +M.x <<= 7; +M.x >>= 8; +M.x >>>= 9; +M.x &= 10; +M.x |= 11; +M.x ^= 12; + +M.x++; +M.x--; +++M.x; +--M.x; + +++((M.x)); + +M["x"] = 0; + +// OK +var a = M.x + 1; + +function f(v: number) { } +f(M.x); + +if (M.x) { } + +M.x; +(M.x); + +-M.x; ++M.x; + +M.x.toString(); + + +//// [constDeclarations-access3.js] +var M; +(function (M) { + M.x = 0; +})(M || (M = {})); +// Errors +M.x = 1; +M.x += 2; +M.x -= 3; +M.x *= 4; +M.x /= 5; +M.x %= 6; +M.x <<= 7; +M.x >>= 8; +M.x >>>= 9; +M.x &= 10; +M.x |= 11; +M.x ^= 12; +M.x++; +M.x--; +++M.x; +--M.x; +++((M.x)); +M["x"] = 0; +// OK +var a = M.x + 1; +function f(v) { +} +f(M.x); +if (M.x) { +} +M.x; +(M.x); +-M.x; ++M.x; +M.x.toString(); diff --git a/tests/baselines/reference/constDeclarations-access4.errors.txt b/tests/baselines/reference/constDeclarations-access4.errors.txt new file mode 100644 index 00000000000..9745c0d2624 --- /dev/null +++ b/tests/baselines/reference/constDeclarations-access4.errors.txt @@ -0,0 +1,102 @@ +tests/cases/compiler/constDeclarations-access4.ts(8,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(9,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(10,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(11,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(12,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(13,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(14,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(15,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(16,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(17,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(18,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(19,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(21,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(22,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(23,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(24,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(26,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations-access4.ts(28,1): error TS2450: Left-hand side of assignment expression cannot be a constant. + + +==== tests/cases/compiler/constDeclarations-access4.ts (18 errors) ==== + + + declare module M { + const x: number; + } + + // Errors + M.x = 1; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x += 2; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x -= 3; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x *= 4; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x /= 5; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x %= 6; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x <<= 7; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x >>= 8; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x >>>= 9; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x &= 10; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x |= 11; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + M.x ^= 12; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + + M.x++; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + M.x--; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + ++M.x; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + --M.x; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + + ++((M.x)); + ~~~~~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + + M["x"] = 0; + ~~~~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + + // OK + var a = M.x + 1; + + function f(v: number) { } + f(M.x); + + if (M.x) { } + + M.x; + (M.x); + + -M.x; + +M.x; + + M.x.toString(); + \ No newline at end of file diff --git a/tests/baselines/reference/constDeclarations-access4.js b/tests/baselines/reference/constDeclarations-access4.js new file mode 100644 index 00000000000..b226e746bba --- /dev/null +++ b/tests/baselines/reference/constDeclarations-access4.js @@ -0,0 +1,79 @@ +//// [constDeclarations-access4.ts] + + +declare module M { + const x: number; +} + +// Errors +M.x = 1; +M.x += 2; +M.x -= 3; +M.x *= 4; +M.x /= 5; +M.x %= 6; +M.x <<= 7; +M.x >>= 8; +M.x >>>= 9; +M.x &= 10; +M.x |= 11; +M.x ^= 12; + +M.x++; +M.x--; +++M.x; +--M.x; + +++((M.x)); + +M["x"] = 0; + +// OK +var a = M.x + 1; + +function f(v: number) { } +f(M.x); + +if (M.x) { } + +M.x; +(M.x); + +-M.x; ++M.x; + +M.x.toString(); + + +//// [constDeclarations-access4.js] +// Errors +M.x = 1; +M.x += 2; +M.x -= 3; +M.x *= 4; +M.x /= 5; +M.x %= 6; +M.x <<= 7; +M.x >>= 8; +M.x >>>= 9; +M.x &= 10; +M.x |= 11; +M.x ^= 12; +M.x++; +M.x--; +++M.x; +--M.x; +++((M.x)); +M["x"] = 0; +// OK +var a = M.x + 1; +function f(v) { +} +f(M.x); +if (M.x) { +} +M.x; +(M.x); +-M.x; ++M.x; +M.x.toString(); diff --git a/tests/baselines/reference/constDeclarations-access5.errors.txt b/tests/baselines/reference/constDeclarations-access5.errors.txt new file mode 100644 index 00000000000..23200e50ee4 --- /dev/null +++ b/tests/baselines/reference/constDeclarations-access5.errors.txt @@ -0,0 +1,103 @@ +tests/cases/compiler/constDeclarations_access_2.ts(4,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(5,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(6,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(7,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(8,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(9,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(10,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(11,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(12,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(13,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(14,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(15,1): error TS2450: Left-hand side of assignment expression cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(17,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(18,1): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(19,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(20,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(22,3): error TS2449: The operand of an increment or decrement operator cannot be a constant. +tests/cases/compiler/constDeclarations_access_2.ts(24,1): error TS2450: Left-hand side of assignment expression cannot be a constant. + + +==== tests/cases/compiler/constDeclarations_access_2.ts (18 errors) ==== + /// + import m = require('constDeclarations_access_1'); + // Errors + m.x = 1; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x += 2; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x -= 3; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x *= 4; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x /= 5; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x %= 6; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x <<= 7; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x >>= 8; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x >>>= 9; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x &= 10; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x |= 11; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m.x ^= 12; + ~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + m + m.x++; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + m.x--; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + ++m.x; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + --m.x; + ~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + + ++((m.x)); + ~~~~~~~ +!!! error TS2449: The operand of an increment or decrement operator cannot be a constant. + + m["x"] = 0; + ~~~~~~ +!!! error TS2450: Left-hand side of assignment expression cannot be a constant. + + // OK + var a = m.x + 1; + + function f(v: number) { } + f(m.x); + + if (m.x) { } + + m.x; + (m.x); + + -m.x; + +m.x; + + m.x.toString(); + +==== tests/cases/compiler/constDeclarations_access_1.ts (0 errors) ==== + + + export const x = 0; + \ No newline at end of file diff --git a/tests/baselines/reference/constDeclarations-access5.js b/tests/baselines/reference/constDeclarations-access5.js new file mode 100644 index 00000000000..7acc95ad055 --- /dev/null +++ b/tests/baselines/reference/constDeclarations-access5.js @@ -0,0 +1,89 @@ +//// [tests/cases/compiler/constDeclarations-access5.ts] //// + +//// [constDeclarations_access_1.ts] + + +export const x = 0; + +//// [constDeclarations_access_2.ts] +/// +import m = require('constDeclarations_access_1'); +// Errors +m.x = 1; +m.x += 2; +m.x -= 3; +m.x *= 4; +m.x /= 5; +m.x %= 6; +m.x <<= 7; +m.x >>= 8; +m.x >>>= 9; +m.x &= 10; +m.x |= 11; +m.x ^= 12; +m +m.x++; +m.x--; +++m.x; +--m.x; + +++((m.x)); + +m["x"] = 0; + +// OK +var a = m.x + 1; + +function f(v: number) { } +f(m.x); + +if (m.x) { } + +m.x; +(m.x); + +-m.x; ++m.x; + +m.x.toString(); + + +//// [constDeclarations_access_1.js] +define(["require", "exports"], function (require, exports) { + exports.x = 0; +}); +//// [constDeclarations_access_2.js] +define(["require", "exports", 'constDeclarations_access_1'], function (require, exports, m) { + // Errors + m.x = 1; + m.x += 2; + m.x -= 3; + m.x *= 4; + m.x /= 5; + m.x %= 6; + m.x <<= 7; + m.x >>= 8; + m.x >>>= 9; + m.x &= 10; + m.x |= 11; + m.x ^= 12; + m; + m.x++; + m.x--; + ++m.x; + --m.x; + ++((m.x)); + m["x"] = 0; + // OK + var a = m.x + 1; + function f(v) { + } + f(m.x); + if (m.x) { + } + m.x; + (m.x); + -m.x; + +m.x; + m.x.toString(); +}); diff --git a/tests/baselines/reference/constDeclarations-invalidContexts.errors.txt b/tests/baselines/reference/constDeclarations-invalidContexts.errors.txt index b7aef3dabbe..bc229185e83 100644 --- a/tests/baselines/reference/constDeclarations-invalidContexts.errors.txt +++ b/tests/baselines/reference/constDeclarations-invalidContexts.errors.txt @@ -1,12 +1,12 @@ -tests/cases/compiler/constDeclarations-invalidContexts.ts(4,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(6,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(9,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(12,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(17,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(20,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(23,5): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(26,12): error TS1156: const must be declared inside a block. -tests/cases/compiler/constDeclarations-invalidContexts.ts(29,29): error TS1156: const must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(4,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(6,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(9,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(12,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(17,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(20,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(23,5): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(26,12): error TS1156: 'const' declarations must be declared inside a block. +tests/cases/compiler/constDeclarations-invalidContexts.ts(29,29): error TS1156: 'const' declarations must be declared inside a block. tests/cases/compiler/constDeclarations-invalidContexts.ts(16,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'. @@ -16,21 +16,21 @@ tests/cases/compiler/constDeclarations-invalidContexts.ts(16,7): error TS2410: A if (true) const c1 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. else const c2 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. while (true) const c3 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. do const c4 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. while (true); var obj; @@ -39,27 +39,27 @@ tests/cases/compiler/constDeclarations-invalidContexts.ts(16,7): error TS2410: A !!! error TS2410: All symbols within a 'with' block will be resolved to 'any'. const c5 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. for (var i = 0; i < 10; i++) const c6 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. for (var i2 in {}) const c7 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. if (true) label: const c8 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. while (false) label2: label3: label4: const c9 = 0; ~~~~~~~~~~~~~ -!!! error TS1156: const must be declared inside a block. +!!! error TS1156: 'const' declarations must be declared inside a block. diff --git a/tests/baselines/reference/constDeclarations2.errors.txt b/tests/baselines/reference/constDeclarations2.errors.txt deleted file mode 100644 index 39317b599f9..00000000000 --- a/tests/baselines/reference/constDeclarations2.errors.txt +++ /dev/null @@ -1,20 +0,0 @@ -tests/cases/compiler/constDeclarations2.ts(4,5): error TS1158: Only var declarations can be exported. -tests/cases/compiler/constDeclarations2.ts(5,5): error TS1158: Only var declarations can be exported. -tests/cases/compiler/constDeclarations2.ts(6,5): error TS1158: Only var declarations can be exported. - - -==== tests/cases/compiler/constDeclarations2.ts (3 errors) ==== - - // No error - module M { - export const c1 = false; - ~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1158: Only var declarations can be exported. - export const c2: number = 23; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1158: Only var declarations can be exported. - export const c3 = 0, c4 :string = "", c5 = null; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1158: Only var declarations can be exported. - } - \ No newline at end of file diff --git a/tests/baselines/reference/constDeclarations2.js b/tests/baselines/reference/constDeclarations2.js new file mode 100644 index 00000000000..e4ee67ebdcd --- /dev/null +++ b/tests/baselines/reference/constDeclarations2.js @@ -0,0 +1,26 @@ +//// [constDeclarations2.ts] + +// No error +module M { + export const c1 = false; + export const c2: number = 23; + export const c3 = 0, c4 :string = "", c5 = null; +} + + +//// [constDeclarations2.js] +// No error +var M; +(function (M) { + M.c1 = false; + M.c2 = 23; + M.c3 = 0, M.c4 = "", M.c5 = null; +})(M || (M = {})); + + +//// [constDeclarations2.d.ts] +declare module M { + const c1: boolean; + const c2: number; + const c3: number, c4: string, c5: any; +} diff --git a/tests/baselines/reference/constDeclarations2.types b/tests/baselines/reference/constDeclarations2.types new file mode 100644 index 00000000000..c81eca96b0d --- /dev/null +++ b/tests/baselines/reference/constDeclarations2.types @@ -0,0 +1,18 @@ +=== tests/cases/compiler/constDeclarations2.ts === + +// No error +module M { +>M : typeof M + + export const c1 = false; +>c1 : boolean + + export const c2: number = 23; +>c2 : number + + export const c3 = 0, c4 :string = "", c5 = null; +>c3 : number +>c4 : string +>c5 : any +} + diff --git a/tests/baselines/reference/letDeclarations-invalidContexts.errors.txt b/tests/baselines/reference/letDeclarations-invalidContexts.errors.txt index 3877f61641a..0e86e84e784 100644 --- a/tests/baselines/reference/letDeclarations-invalidContexts.errors.txt +++ b/tests/baselines/reference/letDeclarations-invalidContexts.errors.txt @@ -1,12 +1,12 @@ -tests/cases/compiler/letDeclarations-invalidContexts.ts(4,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(6,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(9,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(12,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(17,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(20,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(23,5): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(26,12): error TS1157: let must be declared inside a block. -tests/cases/compiler/letDeclarations-invalidContexts.ts(29,29): error TS1157: let must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(4,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(6,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(9,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(12,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(17,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(20,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(23,5): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(26,12): error TS1157: 'let' declarations must be declared inside a block. +tests/cases/compiler/letDeclarations-invalidContexts.ts(29,29): error TS1157: 'let' declarations must be declared inside a block. tests/cases/compiler/letDeclarations-invalidContexts.ts(16,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'. @@ -16,21 +16,21 @@ tests/cases/compiler/letDeclarations-invalidContexts.ts(16,7): error TS2410: All if (true) let l1 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. else let l2 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. while (true) let l3 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. do let l4 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. while (true); var obj; @@ -39,27 +39,27 @@ tests/cases/compiler/letDeclarations-invalidContexts.ts(16,7): error TS2410: All !!! error TS2410: All symbols within a 'with' block will be resolved to 'any'. let l5 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. for (var i = 0; i < 10; i++) let l6 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. for (var i2 in {}) let l7 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. if (true) label: let l8 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. while (false) label2: label3: label4: let l9 = 0; ~~~~~~~~~~~ -!!! error TS1157: let must be declared inside a block. +!!! error TS1157: 'let' declarations must be declared inside a block. diff --git a/tests/baselines/reference/letDeclarations2.errors.txt b/tests/baselines/reference/letDeclarations2.errors.txt deleted file mode 100644 index 384912a2bb4..00000000000 --- a/tests/baselines/reference/letDeclarations2.errors.txt +++ /dev/null @@ -1,11 +0,0 @@ -tests/cases/compiler/letDeclarations2.ts(4,5): error TS1158: Only var declarations can be exported. - - -==== tests/cases/compiler/letDeclarations2.ts (1 errors) ==== - - module M { - let l1 = "s"; - export let l2 = 0; - ~~~~~~~~~~~~~~~~~~ -!!! error TS1158: Only var declarations can be exported. - } \ No newline at end of file diff --git a/tests/baselines/reference/letDeclarations2.js b/tests/baselines/reference/letDeclarations2.js new file mode 100644 index 00000000000..8a6f039f2ca --- /dev/null +++ b/tests/baselines/reference/letDeclarations2.js @@ -0,0 +1,19 @@ +//// [letDeclarations2.ts] + +module M { + let l1 = "s"; + export let l2 = 0; +} + +//// [letDeclarations2.js] +var M; +(function (M) { + let l1 = "s"; + M.l2 = 0; +})(M || (M = {})); + + +//// [letDeclarations2.d.ts] +declare module M { + let l2: number; +} diff --git a/tests/baselines/reference/letDeclarations2.types b/tests/baselines/reference/letDeclarations2.types new file mode 100644 index 00000000000..2fa08b6d940 --- /dev/null +++ b/tests/baselines/reference/letDeclarations2.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/letDeclarations2.ts === + +module M { +>M : typeof M + + let l1 = "s"; +>l1 : string + + export let l2 = 0; +>l2 : number +} diff --git a/tests/cases/compiler/constDeclarations-access2.ts b/tests/cases/compiler/constDeclarations-access2.ts index e46889c1126..e3f83940d1d 100644 --- a/tests/cases/compiler/constDeclarations-access2.ts +++ b/tests/cases/compiler/constDeclarations-access2.ts @@ -21,6 +21,8 @@ x--; ++x; --x; +++((x)); + // OK var a = x + 1; diff --git a/tests/cases/compiler/constDeclarations-access3.ts b/tests/cases/compiler/constDeclarations-access3.ts new file mode 100644 index 00000000000..370288dab97 --- /dev/null +++ b/tests/cases/compiler/constDeclarations-access3.ts @@ -0,0 +1,45 @@ +// @target: ES6 + + +module M { + export const x = 0; +} + +// Errors +M.x = 1; +M.x += 2; +M.x -= 3; +M.x *= 4; +M.x /= 5; +M.x %= 6; +M.x <<= 7; +M.x >>= 8; +M.x >>>= 9; +M.x &= 10; +M.x |= 11; +M.x ^= 12; + +M.x++; +M.x--; +++M.x; +--M.x; + +++((M.x)); + +M["x"] = 0; + +// OK +var a = M.x + 1; + +function f(v: number) { } +f(M.x); + +if (M.x) { } + +M.x; +(M.x); + +-M.x; ++M.x; + +M.x.toString(); diff --git a/tests/cases/compiler/constDeclarations-access4.ts b/tests/cases/compiler/constDeclarations-access4.ts new file mode 100644 index 00000000000..492f6a31d6d --- /dev/null +++ b/tests/cases/compiler/constDeclarations-access4.ts @@ -0,0 +1,45 @@ +// @target: ES6 + + +declare module M { + const x: number; +} + +// Errors +M.x = 1; +M.x += 2; +M.x -= 3; +M.x *= 4; +M.x /= 5; +M.x %= 6; +M.x <<= 7; +M.x >>= 8; +M.x >>>= 9; +M.x &= 10; +M.x |= 11; +M.x ^= 12; + +M.x++; +M.x--; +++M.x; +--M.x; + +++((M.x)); + +M["x"] = 0; + +// OK +var a = M.x + 1; + +function f(v: number) { } +f(M.x); + +if (M.x) { } + +M.x; +(M.x); + +-M.x; ++M.x; + +M.x.toString(); diff --git a/tests/cases/compiler/constDeclarations-access5.ts b/tests/cases/compiler/constDeclarations-access5.ts new file mode 100644 index 00000000000..1590d9289ef --- /dev/null +++ b/tests/cases/compiler/constDeclarations-access5.ts @@ -0,0 +1,48 @@ +// @target: ES6 +// @module: amd + + +// @Filename: constDeclarations_access_1.ts +export const x = 0; + +// @Filename: constDeclarations_access_2.ts +/// +import m = require('constDeclarations_access_1'); +// Errors +m.x = 1; +m.x += 2; +m.x -= 3; +m.x *= 4; +m.x /= 5; +m.x %= 6; +m.x <<= 7; +m.x >>= 8; +m.x >>>= 9; +m.x &= 10; +m.x |= 11; +m.x ^= 12; +m +m.x++; +m.x--; +++m.x; +--m.x; + +++((m.x)); + +m["x"] = 0; + +// OK +var a = m.x + 1; + +function f(v: number) { } +f(m.x); + +if (m.x) { } + +m.x; +(m.x); + +-m.x; ++m.x; + +m.x.toString();