Skip to content

Commit 42bff6b

Browse files
committed
Merge pull request microsoft#3772 from Microsoft/allowModifier
Allow modifier on class member for classDeclaration/classExpression inside function
2 parents aaf0f78 + 8e15a42 commit 42bff6b

9 files changed

+155
-21
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11448,31 +11448,18 @@ namespace ts {
1144811448
forEach(node.declarationList.declarations, checkSourceElement);
1144911449
}
1145011450

11451-
function checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node: Node) {
11452-
if (node.modifiers) {
11453-
if (inBlockOrObjectLiteralExpression(node)) {
11454-
if (isAsyncFunctionLike(node)) {
11455-
if (node.modifiers.length > 1) {
11456-
return grammarErrorOnFirstToken(node, Diagnostics.Modifiers_cannot_appear_here);
11457-
}
11458-
}
11459-
else {
11451+
function checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node: Node) {
11452+
// We only disallow modifier on a method declaration if it is a property of object-literal-expression
11453+
if (node.modifiers && node.parent.kind === SyntaxKind.ObjectLiteralExpression){
11454+
if (isAsyncFunctionLike(node)) {
11455+
if (node.modifiers.length > 1) {
1146011456
return grammarErrorOnFirstToken(node, Diagnostics.Modifiers_cannot_appear_here);
1146111457
}
1146211458
}
11463-
}
11464-
}
11465-
11466-
function inBlockOrObjectLiteralExpression(node: Node) {
11467-
while (node) {
11468-
if (node.kind === SyntaxKind.Block || node.kind === SyntaxKind.ObjectLiteralExpression) {
11469-
return true;
11459+
else {
11460+
return grammarErrorOnFirstToken(node, Diagnostics.Modifiers_cannot_appear_here);
1147011461
}
11471-
11472-
node = node.parent;
1147311462
}
11474-
11475-
return false;
1147611463
}
1147711464

1147811465
function checkExpressionStatement(node: ExpressionStatement) {
@@ -15030,7 +15017,7 @@ namespace ts {
1503015017
}
1503115018

1503215019
function checkGrammarMethod(node: MethodDeclaration) {
15033-
if (checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) ||
15020+
if (checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) ||
1503415021
checkGrammarFunctionLikeDeclaration(node) ||
1503515022
checkGrammarForGenerator(node)) {
1503615023
return true;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [modifierOnClassDeclarationMemberInFunction.ts]
2+
3+
function f() {
4+
class C {
5+
public baz = 1;
6+
static foo() { }
7+
public bar() { }
8+
}
9+
}
10+
11+
//// [modifierOnClassDeclarationMemberInFunction.js]
12+
function f() {
13+
var C = (function () {
14+
function C() {
15+
this.baz = 1;
16+
}
17+
C.foo = function () { };
18+
C.prototype.bar = function () { };
19+
return C;
20+
})();
21+
}
22+
23+
24+
//// [modifierOnClassDeclarationMemberInFunction.d.ts]
25+
declare function f(): void;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/classes/classDeclarations/modifierOnClassDeclarationMemberInFunction.ts ===
2+
3+
function f() {
4+
>f : Symbol(f, Decl(modifierOnClassDeclarationMemberInFunction.ts, 0, 0))
5+
6+
class C {
7+
>C : Symbol(C, Decl(modifierOnClassDeclarationMemberInFunction.ts, 1, 14))
8+
9+
public baz = 1;
10+
>baz : Symbol(baz, Decl(modifierOnClassDeclarationMemberInFunction.ts, 2, 13))
11+
12+
static foo() { }
13+
>foo : Symbol(C.foo, Decl(modifierOnClassDeclarationMemberInFunction.ts, 3, 23))
14+
15+
public bar() { }
16+
>bar : Symbol(bar, Decl(modifierOnClassDeclarationMemberInFunction.ts, 4, 24))
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/classes/classDeclarations/modifierOnClassDeclarationMemberInFunction.ts ===
2+
3+
function f() {
4+
>f : () => void
5+
6+
class C {
7+
>C : C
8+
9+
public baz = 1;
10+
>baz : number
11+
>1 : number
12+
13+
static foo() { }
14+
>foo : () => void
15+
16+
public bar() { }
17+
>bar : () => void
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [modifierOnClassExpressionMemberInFunction.ts]
2+
3+
function g() {
4+
var x = class C {
5+
public prop1 = 1;
6+
private foo() { }
7+
static prop2 = 43;
8+
}
9+
}
10+
11+
//// [modifierOnClassExpressionMemberInFunction.js]
12+
function g() {
13+
var x = (function () {
14+
function C() {
15+
this.prop1 = 1;
16+
}
17+
C.prototype.foo = function () { };
18+
C.prop2 = 43;
19+
return C;
20+
})();
21+
}
22+
23+
24+
//// [modifierOnClassExpressionMemberInFunction.d.ts]
25+
declare function g(): void;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/classes/classExpressions/modifierOnClassExpressionMemberInFunction.ts ===
2+
3+
function g() {
4+
>g : Symbol(g, Decl(modifierOnClassExpressionMemberInFunction.ts, 0, 0))
5+
6+
var x = class C {
7+
>x : Symbol(x, Decl(modifierOnClassExpressionMemberInFunction.ts, 2, 7))
8+
>C : Symbol(C, Decl(modifierOnClassExpressionMemberInFunction.ts, 2, 11))
9+
10+
public prop1 = 1;
11+
>prop1 : Symbol(C.prop1, Decl(modifierOnClassExpressionMemberInFunction.ts, 2, 21))
12+
13+
private foo() { }
14+
>foo : Symbol(C.foo, Decl(modifierOnClassExpressionMemberInFunction.ts, 3, 25))
15+
16+
static prop2 = 43;
17+
>prop2 : Symbol(C.prop2, Decl(modifierOnClassExpressionMemberInFunction.ts, 4, 25))
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/classes/classExpressions/modifierOnClassExpressionMemberInFunction.ts ===
2+
3+
function g() {
4+
>g : () => void
5+
6+
var x = class C {
7+
>x : typeof C
8+
>class C { public prop1 = 1; private foo() { } static prop2 = 43; } : typeof C
9+
>C : typeof C
10+
11+
public prop1 = 1;
12+
>prop1 : number
13+
>1 : number
14+
15+
private foo() { }
16+
>foo : () => void
17+
18+
static prop2 = 43;
19+
>prop2 : number
20+
>43 : number
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @declaration: true
2+
3+
function f() {
4+
class C {
5+
public baz = 1;
6+
static foo() { }
7+
public bar() { }
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @declaration: true
2+
// @declaration: true
3+
4+
function g() {
5+
var x = class C {
6+
public prop1 = 1;
7+
private foo() { }
8+
static prop2 = 43;
9+
}
10+
}

0 commit comments

Comments
 (0)