Skip to content

Commit 383cbf0

Browse files
committed
Merge pull request microsoft#5996 from RyanCavanaugh/fix5994
Disallow modifiers in object literal property assignment
2 parents 88a8345 + d3c9815 commit 383cbf0

8 files changed

Lines changed: 71 additions & 3 deletions

src/compiler/checker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15999,6 +15999,13 @@ namespace ts {
1599915999
return grammarErrorOnNode((<ShorthandPropertyAssignment>prop).equalsToken, Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment);
1600016000
}
1600116001

16002+
// Modifiers are never allowed on properties except for 'async' on a method declaration
16003+
forEach(prop.modifiers, mod => {
16004+
if (mod.kind !== SyntaxKind.AsyncKeyword || prop.kind !== SyntaxKind.MethodDeclaration) {
16005+
grammarErrorOnNode(mod, Diagnostics._0_modifier_cannot_be_used_here, getTextOfNode(mod));
16006+
}
16007+
});
16008+
1600216009
// ECMA-262 11.1.5 Object Initialiser
1600316010
// If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true
1600416011
// 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 TS1042: 'public' modifier cannot be used here.
2+
tests/cases/compiler/modifiersInObjectLiterals.ts(3,2): error TS1042: 'private' modifier cannot be used here.
3+
tests/cases/compiler/modifiersInObjectLiterals.ts(4,2): error TS1042: 'protected' modifier cannot be used here.
4+
tests/cases/compiler/modifiersInObjectLiterals.ts(5,2): error TS1042: 'abstract' modifier cannot be used here.
5+
6+
7+
==== tests/cases/compiler/modifiersInObjectLiterals.ts (4 errors) ====
8+
let data = {
9+
public foo: 'hey',
10+
~~~~~~
11+
!!! error TS1042: 'public' modifier cannot be used here.
12+
private bar: 'nay',
13+
~~~~~~~
14+
!!! error TS1042: 'private' modifier cannot be used here.
15+
protected baz: 'oh my',
16+
~~~~~~~~~
17+
!!! error TS1042: 'protected' modifier cannot be used here.
18+
abstract noWay: 'yes'
19+
~~~~~~~~
20+
!!! error TS1042: 'abstract' modifier cannot be used 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
tests/cases/compiler/objectLiteralMemberWithModifiers1.ts(1,11): error TS1042: 'public' modifier cannot be used here.
12
tests/cases/compiler/objectLiteralMemberWithModifiers1.ts(1,11): error TS1184: Modifiers cannot appear here.
23

34

4-
==== tests/cases/compiler/objectLiteralMemberWithModifiers1.ts (1 errors) ====
5+
==== tests/cases/compiler/objectLiteralMemberWithModifiers1.ts (2 errors) ====
56
var v = { public foo() { } }
67
~~~~~~
8+
!!! error TS1042: 'public' modifier cannot be used here.
9+
~~~~~~
710
!!! error TS1184: Modifiers cannot appear here.

tests/baselines/reference/objectLiteralMemberWithModifiers2.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,11): error TS1042: 'public' modifier cannot be used here.
12
tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,22): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
23
tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,22): error TS2378: A 'get' accessor must return a value.
34

45

5-
==== tests/cases/compiler/objectLiteralMemberWithModifiers2.ts (2 errors) ====
6+
==== tests/cases/compiler/objectLiteralMemberWithModifiers2.ts (3 errors) ====
67
var v = { public get foo() { } }
8+
~~~~~~
9+
!!! error TS1042: 'public' modifier cannot be used here.
710
~~~
811
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
912
~~~
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts(2,3): error TS1042: 'public' modifier cannot be used here.
12
tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts(2,14): error TS2378: A 'get' accessor must return a value.
23

34

4-
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts (1 errors) ====
5+
==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts (2 errors) ====
56
var v = {
67
public get foo() { }
8+
~~~~~~
9+
!!! error TS1042: 'public' modifier cannot be used here.
710
~~~
811
!!! error TS2378: A 'get' accessor must return a value.
912
};
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)