From 28fc0a28b0b77066746dbedb1441f27449a2cb60 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Fri, 2 Nov 2018 17:43:13 +0200 Subject: [PATCH] add new error message 'Cannot assign to {0} because it is a constant.' --- src/compiler/checker.ts | 7 +- src/compiler/diagnosticMessages.json | 4 ++ .../constDeclarations-access.errors.txt | 4 +- .../constDeclarations-access2.errors.txt | 68 +++++++++---------- .../constDeclarations-errors.errors.txt | 4 +- .../reference/constWithNonNull.errors.txt | 4 +- tests/baselines/reference/for-of2.errors.txt | 4 +- .../reference/redefineArray.errors.txt | 4 +- 8 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7df0414b90b..3c99001be1b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15891,7 +15891,12 @@ namespace ts { return errorType; } if (isReadonlySymbol(localOrExportSymbol)) { - error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(symbol)); + if (localOrExportSymbol.flags & SymbolFlags.Variable) { + error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant, symbolToString(symbol)); + } + else { + error(node, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(symbol)); + } return errorType; } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index e2dd0a1b7cd..4b6d97b5332 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2124,6 +2124,10 @@ "category": "Error", "code": 2587 }, + "Cannot assign to '{0}' because it is a constant.": { + "category": "Error", + "code": 2588 + }, "JSX element attributes type '{0}' may not be a union type.": { "category": "Error", "code": 2600 diff --git a/tests/baselines/reference/constDeclarations-access.errors.txt b/tests/baselines/reference/constDeclarations-access.errors.txt index c9c730456e2..4accefa01bc 100644 --- a/tests/baselines/reference/constDeclarations-access.errors.txt +++ b/tests/baselines/reference/constDeclarations-access.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/file2.ts(1,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +tests/cases/compiler/file2.ts(1,1): error TS2588: Cannot assign to 'x' because it is a constant. ==== tests/cases/compiler/file1.ts (0 errors) ==== @@ -7,4 +7,4 @@ tests/cases/compiler/file2.ts(1,1): error TS2540: Cannot assign to 'x' because i ==== tests/cases/compiler/file2.ts (1 errors) ==== x++; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. \ No newline at end of file +!!! error TS2588: Cannot assign to 'x' because it is a constant. \ No newline at end of file diff --git a/tests/baselines/reference/constDeclarations-access2.errors.txt b/tests/baselines/reference/constDeclarations-access2.errors.txt index dc8b4d5b90b..dfb796e25fd 100644 --- a/tests/baselines/reference/constDeclarations-access2.errors.txt +++ b/tests/baselines/reference/constDeclarations-access2.errors.txt @@ -1,20 +1,20 @@ -tests/cases/compiler/constDeclarations-access2.ts(4,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(5,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(6,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(7,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(8,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(9,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(10,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(11,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(12,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(13,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(14,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(15,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(17,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(18,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(19,3): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(20,3): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. -tests/cases/compiler/constDeclarations-access2.ts(22,5): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +tests/cases/compiler/constDeclarations-access2.ts(4,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(5,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(6,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(7,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(8,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(9,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(10,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(11,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(12,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(13,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(14,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(15,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(17,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(18,1): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(19,3): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(20,3): error TS2588: Cannot assign to 'x' because it is a constant. +tests/cases/compiler/constDeclarations-access2.ts(22,5): error TS2588: Cannot assign to 'x' because it is a constant. ==== tests/cases/compiler/constDeclarations-access2.ts (17 errors) ==== @@ -23,57 +23,57 @@ tests/cases/compiler/constDeclarations-access2.ts(22,5): error TS2540: Cannot as // Errors x = 1; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x += 2; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x -= 3; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x *= 4; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x /= 5; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x %= 6; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x <<= 7; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x >>= 8; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x >>>= 9; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x &= 10; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x |= 11; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x ^= 12; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x++; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. x--; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. ++x; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. --x; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. ++((x)); ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. // OK var a = x + 1; diff --git a/tests/baselines/reference/constDeclarations-errors.errors.txt b/tests/baselines/reference/constDeclarations-errors.errors.txt index 52579561707..d945ce10aa5 100644 --- a/tests/baselines/reference/constDeclarations-errors.errors.txt +++ b/tests/baselines/reference/constDeclarations-errors.errors.txt @@ -4,7 +4,7 @@ tests/cases/compiler/constDeclarations-errors.ts(4,7): error TS1155: 'const' dec tests/cases/compiler/constDeclarations-errors.ts(4,11): error TS1155: 'const' declarations must be initialized. tests/cases/compiler/constDeclarations-errors.ts(4,15): error TS1155: 'const' declarations must be initialized. tests/cases/compiler/constDeclarations-errors.ts(4,27): error TS1155: 'const' declarations must be initialized. -tests/cases/compiler/constDeclarations-errors.ts(9,27): error TS2540: Cannot assign to 'c8' because it is a constant or a read-only property. +tests/cases/compiler/constDeclarations-errors.ts(9,27): error TS2588: Cannot assign to 'c8' because it is a constant. tests/cases/compiler/constDeclarations-errors.ts(12,11): error TS1155: 'const' declarations must be initialized. tests/cases/compiler/constDeclarations-errors.ts(15,20): error TS1155: 'const' declarations must be initialized. @@ -32,7 +32,7 @@ tests/cases/compiler/constDeclarations-errors.ts(15,20): error TS1155: 'const' d // error, assigning to a const for(const c8 = 0; c8 < 1; c8++) { } ~~ -!!! error TS2540: Cannot assign to 'c8' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'c8' because it is a constant. // error, can not be unintalized for(const c9; c9 < 1;) { } diff --git a/tests/baselines/reference/constWithNonNull.errors.txt b/tests/baselines/reference/constWithNonNull.errors.txt index 58667c417da..840b26c5f35 100644 --- a/tests/baselines/reference/constWithNonNull.errors.txt +++ b/tests/baselines/reference/constWithNonNull.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/constWithNonNull.ts(4,1): error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +tests/cases/compiler/constWithNonNull.ts(4,1): error TS2588: Cannot assign to 'x' because it is a constant. ==== tests/cases/compiler/constWithNonNull.ts (1 errors) ==== @@ -7,5 +7,5 @@ tests/cases/compiler/constWithNonNull.ts(4,1): error TS2540: Cannot assign to 'x declare const x: number | undefined; x!++; ~ -!!! error TS2540: Cannot assign to 'x' because it is a constant or a read-only property. +!!! error TS2588: Cannot assign to 'x' because it is a constant. \ No newline at end of file diff --git a/tests/baselines/reference/for-of2.errors.txt b/tests/baselines/reference/for-of2.errors.txt index 3bf3b679898..f1ff169a58e 100644 --- a/tests/baselines/reference/for-of2.errors.txt +++ b/tests/baselines/reference/for-of2.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/es6/for-ofStatements/for-of2.ts(1,7): error TS1155: 'const' declarations must be initialized. -tests/cases/conformance/es6/for-ofStatements/for-of2.ts(2,6): error TS2540: Cannot assign to 'v' because it is a constant or a read-only property. +tests/cases/conformance/es6/for-ofStatements/for-of2.ts(2,6): error TS2588: Cannot assign to 'v' because it is a constant. ==== tests/cases/conformance/es6/for-ofStatements/for-of2.ts (2 errors) ==== @@ -8,4 +8,4 @@ tests/cases/conformance/es6/for-ofStatements/for-of2.ts(2,6): error TS2540: Cann !!! error TS1155: 'const' declarations must be initialized. for (v of []) { } ~ -!!! error TS2540: Cannot assign to 'v' because it is a constant or a read-only property. \ No newline at end of file +!!! error TS2588: Cannot assign to 'v' because it is a constant. \ No newline at end of file diff --git a/tests/baselines/reference/redefineArray.errors.txt b/tests/baselines/reference/redefineArray.errors.txt index 6a9cf954798..dd3727b0216 100644 --- a/tests/baselines/reference/redefineArray.errors.txt +++ b/tests/baselines/reference/redefineArray.errors.txt @@ -1,7 +1,7 @@ -tests/cases/compiler/redefineArray.ts(1,1): error TS2540: Cannot assign to 'Array' because it is a constant or a read-only property. +tests/cases/compiler/redefineArray.ts(1,1): error TS2588: Cannot assign to 'Array' because it is a constant. ==== tests/cases/compiler/redefineArray.ts (1 errors) ==== Array = function (n:number, s:string) {return n;}; ~~~~~ -!!! error TS2540: Cannot assign to 'Array' because it is a constant or a read-only property. \ No newline at end of file +!!! error TS2588: Cannot assign to 'Array' because it is a constant. \ No newline at end of file