Skip to content

Commit 92d7d1c

Browse files
committed
Disallow modifiers in object literal property assignment
Fixes bug microsoft#5994
1 parent 7c1ef22 commit 92d7d1c

5 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/compiler/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15969,6 +15969,11 @@ namespace ts {
1596915969
return grammarErrorOnNode((<ShorthandPropertyAssignment>prop).equalsToken, Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment);
1597015970
}
1597115971

15972+
// Modifiers cannot appear in property assignments
15973+
if (prop.modifiers && prop.modifiers.length > 0) {
15974+
grammarErrorOnNode(prop.modifiers[0], Diagnostics.Modifiers_cannot_appear_here);
15975+
}
15976+
1597215977
// ECMA-262 11.1.5 Object Initialiser
1597315978
// If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true
1597415979
// a.This production is contained in strict code and IsDataDescriptor(previous) is true and

src/compiler/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,6 +3981,7 @@ namespace ts {
39813981
}
39823982
else {
39833983
const propertyAssignment = <PropertyAssignment>createNode(SyntaxKind.PropertyAssignment, fullStart);
3984+
propertyAssignment.modifiers = modifiers;
39843985
propertyAssignment.name = propertyName;
39853986
propertyAssignment.questionToken = questionToken;
39863987
parseExpected(SyntaxKind.ColonToken);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
tests/cases/compiler/modifiersInObjectLiterals.ts(2,2): error TS1184: Modifiers cannot appear here.
2+
tests/cases/compiler/modifiersInObjectLiterals.ts(3,2): error TS1184: Modifiers cannot appear here.
3+
tests/cases/compiler/modifiersInObjectLiterals.ts(4,2): error TS1184: Modifiers cannot appear here.
4+
tests/cases/compiler/modifiersInObjectLiterals.ts(5,2): error TS1184: Modifiers cannot appear here.
5+
6+
7+
==== tests/cases/compiler/modifiersInObjectLiterals.ts (4 errors) ====
8+
let data = {
9+
public foo: 'hey',
10+
~~~~~~
11+
!!! error TS1184: Modifiers cannot appear here.
12+
private bar: 'nay',
13+
~~~~~~~
14+
!!! error TS1184: Modifiers cannot appear here.
15+
protected baz: 'oh my',
16+
~~~~~~~~~
17+
!!! error TS1184: Modifiers cannot appear here.
18+
abstract noWay: 'yes'
19+
~~~~~~~~
20+
!!! error TS1184: Modifiers cannot appear here.
21+
};
22+
23+
data.foo + data.bar + data.baz + data.noWay
24+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [modifiersInObjectLiterals.ts]
2+
let data = {
3+
public foo: 'hey',
4+
private bar: 'nay',
5+
protected baz: 'oh my',
6+
abstract noWay: 'yes'
7+
};
8+
9+
data.foo + data.bar + data.baz + data.noWay
10+
11+
12+
//// [modifiersInObjectLiterals.js]
13+
var data = {
14+
foo: 'hey',
15+
bar: 'nay',
16+
baz: 'oh my',
17+
noWay: 'yes'
18+
};
19+
data.foo + data.bar + data.baz + data.noWay;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let data = {
2+
public foo: 'hey',
3+
private bar: 'nay',
4+
protected baz: 'oh my',
5+
abstract noWay: 'yes'
6+
};
7+
8+
data.foo + data.bar + data.baz + data.noWay

0 commit comments

Comments
 (0)