diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 777a25a4c12..50a3aee9955 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15999,6 +15999,13 @@ namespace ts { return grammarErrorOnNode((prop).equalsToken, Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); } + // Modifiers are never allowed on properties except for 'async' on a method declaration + forEach(prop.modifiers, mod => { + if (mod.kind !== SyntaxKind.AsyncKeyword || prop.kind !== SyntaxKind.MethodDeclaration) { + grammarErrorOnNode(mod, Diagnostics._0_modifier_cannot_be_used_here, getTextOfNode(mod)); + } + }); + // 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 a6e1c03db25..f5273e38ecf 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..25382123390 --- /dev/null +++ b/tests/baselines/reference/modifiersInObjectLiterals.errors.txt @@ -0,0 +1,24 @@ +tests/cases/compiler/modifiersInObjectLiterals.ts(2,2): error TS1042: 'public' modifier cannot be used here. +tests/cases/compiler/modifiersInObjectLiterals.ts(3,2): error TS1042: 'private' modifier cannot be used here. +tests/cases/compiler/modifiersInObjectLiterals.ts(4,2): error TS1042: 'protected' modifier cannot be used here. +tests/cases/compiler/modifiersInObjectLiterals.ts(5,2): error TS1042: 'abstract' modifier cannot be used here. + + +==== tests/cases/compiler/modifiersInObjectLiterals.ts (4 errors) ==== + let data = { + public foo: 'hey', + ~~~~~~ +!!! error TS1042: 'public' modifier cannot be used here. + private bar: 'nay', + ~~~~~~~ +!!! error TS1042: 'private' modifier cannot be used here. + protected baz: 'oh my', + ~~~~~~~~~ +!!! error TS1042: 'protected' modifier cannot be used here. + abstract noWay: 'yes' + ~~~~~~~~ +!!! error TS1042: 'abstract' modifier cannot be used 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/baselines/reference/objectLiteralMemberWithModifiers1.errors.txt b/tests/baselines/reference/objectLiteralMemberWithModifiers1.errors.txt index 9e739194f54..182eab1b4cf 100644 --- a/tests/baselines/reference/objectLiteralMemberWithModifiers1.errors.txt +++ b/tests/baselines/reference/objectLiteralMemberWithModifiers1.errors.txt @@ -1,7 +1,10 @@ +tests/cases/compiler/objectLiteralMemberWithModifiers1.ts(1,11): error TS1042: 'public' modifier cannot be used here. tests/cases/compiler/objectLiteralMemberWithModifiers1.ts(1,11): error TS1184: Modifiers cannot appear here. -==== tests/cases/compiler/objectLiteralMemberWithModifiers1.ts (1 errors) ==== +==== tests/cases/compiler/objectLiteralMemberWithModifiers1.ts (2 errors) ==== var v = { public foo() { } } ~~~~~~ +!!! error TS1042: 'public' modifier cannot be used here. + ~~~~~~ !!! error TS1184: Modifiers cannot appear here. \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralMemberWithModifiers2.errors.txt b/tests/baselines/reference/objectLiteralMemberWithModifiers2.errors.txt index be2bea5e241..0d1822339b6 100644 --- a/tests/baselines/reference/objectLiteralMemberWithModifiers2.errors.txt +++ b/tests/baselines/reference/objectLiteralMemberWithModifiers2.errors.txt @@ -1,9 +1,12 @@ +tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,11): error TS1042: 'public' modifier cannot be used here. tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,22): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. tests/cases/compiler/objectLiteralMemberWithModifiers2.ts(1,22): error TS2378: A 'get' accessor must return a value. -==== tests/cases/compiler/objectLiteralMemberWithModifiers2.ts (2 errors) ==== +==== tests/cases/compiler/objectLiteralMemberWithModifiers2.ts (3 errors) ==== var v = { public get foo() { } } + ~~~~~~ +!!! error TS1042: 'public' modifier cannot be used here. ~~~ !!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. ~~~ diff --git a/tests/baselines/reference/parserAccessors10.errors.txt b/tests/baselines/reference/parserAccessors10.errors.txt index d6a2b99eaca..b309b5e3947 100644 --- a/tests/baselines/reference/parserAccessors10.errors.txt +++ b/tests/baselines/reference/parserAccessors10.errors.txt @@ -1,9 +1,12 @@ +tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts(2,3): error TS1042: 'public' modifier cannot be used here. tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts(2,14): error TS2378: A 'get' accessor must return a value. -==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts (1 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/Accessors/parserAccessors10.ts (2 errors) ==== var v = { public get foo() { } + ~~~~~~ +!!! error TS1042: 'public' modifier cannot be used here. ~~~ !!! error TS2378: A 'get' accessor must return a value. }; \ No newline at end of file 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