From 92d7d1c9536f1b02ae50f2ace609f5c1e627ac16 Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Tue, 8 Dec 2015 10:11:29 -0800 Subject: [PATCH] Disallow modifiers in object literal property assignment Fixes bug #5994 --- src/compiler/checker.ts | 5 ++++ src/compiler/parser.ts | 1 + .../modifiersInObjectLiterals.errors.txt | 24 +++++++++++++++++++ .../reference/modifiersInObjectLiterals.js | 19 +++++++++++++++ .../compiler/modifiersInObjectLiterals.ts | 8 +++++++ 5 files changed, 57 insertions(+) create mode 100644 tests/baselines/reference/modifiersInObjectLiterals.errors.txt create mode 100644 tests/baselines/reference/modifiersInObjectLiterals.js create mode 100644 tests/cases/compiler/modifiersInObjectLiterals.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 30d4818592b..9b694d9084e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15969,6 +15969,11 @@ namespace ts { return grammarErrorOnNode((prop).equalsToken, Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); } + // Modifiers cannot appear in property assignments + if (prop.modifiers && prop.modifiers.length > 0) { + grammarErrorOnNode(prop.modifiers[0], Diagnostics.Modifiers_cannot_appear_here); + } + // ECMA-262 11.1.5 Object Initialiser // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true // a.This production is contained in strict code and IsDataDescriptor(previous) is true and diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index e4262458d30..ad108fe4115 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3981,6 +3981,7 @@ namespace ts { } else { const propertyAssignment = createNode(SyntaxKind.PropertyAssignment, fullStart); + propertyAssignment.modifiers = modifiers; propertyAssignment.name = propertyName; propertyAssignment.questionToken = questionToken; parseExpected(SyntaxKind.ColonToken); diff --git a/tests/baselines/reference/modifiersInObjectLiterals.errors.txt b/tests/baselines/reference/modifiersInObjectLiterals.errors.txt new file mode 100644 index 00000000000..c23a8c3c630 --- /dev/null +++ b/tests/baselines/reference/modifiersInObjectLiterals.errors.txt @@ -0,0 +1,24 @@ +tests/cases/compiler/modifiersInObjectLiterals.ts(2,2): error TS1184: Modifiers cannot appear here. +tests/cases/compiler/modifiersInObjectLiterals.ts(3,2): error TS1184: Modifiers cannot appear here. +tests/cases/compiler/modifiersInObjectLiterals.ts(4,2): error TS1184: Modifiers cannot appear here. +tests/cases/compiler/modifiersInObjectLiterals.ts(5,2): error TS1184: Modifiers cannot appear here. + + +==== tests/cases/compiler/modifiersInObjectLiterals.ts (4 errors) ==== + let data = { + public foo: 'hey', + ~~~~~~ +!!! error TS1184: Modifiers cannot appear here. + private bar: 'nay', + ~~~~~~~ +!!! error TS1184: Modifiers cannot appear here. + protected baz: 'oh my', + ~~~~~~~~~ +!!! error TS1184: Modifiers cannot appear here. + abstract noWay: 'yes' + ~~~~~~~~ +!!! error TS1184: Modifiers cannot appear here. + }; + + data.foo + data.bar + data.baz + data.noWay + \ No newline at end of file diff --git a/tests/baselines/reference/modifiersInObjectLiterals.js b/tests/baselines/reference/modifiersInObjectLiterals.js new file mode 100644 index 00000000000..0a59b13c21e --- /dev/null +++ b/tests/baselines/reference/modifiersInObjectLiterals.js @@ -0,0 +1,19 @@ +//// [modifiersInObjectLiterals.ts] +let data = { + public foo: 'hey', + private bar: 'nay', + protected baz: 'oh my', + abstract noWay: 'yes' +}; + +data.foo + data.bar + data.baz + data.noWay + + +//// [modifiersInObjectLiterals.js] +var data = { + foo: 'hey', + bar: 'nay', + baz: 'oh my', + noWay: 'yes' +}; +data.foo + data.bar + data.baz + data.noWay; diff --git a/tests/cases/compiler/modifiersInObjectLiterals.ts b/tests/cases/compiler/modifiersInObjectLiterals.ts new file mode 100644 index 00000000000..58fa64f82e9 --- /dev/null +++ b/tests/cases/compiler/modifiersInObjectLiterals.ts @@ -0,0 +1,8 @@ +let data = { + public foo: 'hey', + private bar: 'nay', + protected baz: 'oh my', + abstract noWay: 'yes' +}; + +data.foo + data.bar + data.baz + data.noWay