Skip to content

Commit c771636

Browse files
committed
Merge branch 'main' of https://github.com/microsoft/typescript-go into fix/1374
2 parents ad033f6 + 912f193 commit c771636

11 files changed

+182
-268
lines changed

internal/checker/checker.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,7 @@ func (c *Checker) isBlockScopedNameDeclaredBeforeUse(declaration *ast.Node, usag
19061906
func (c *Checker) isUsedInFunctionOrInstanceProperty(usage *ast.Node, declaration *ast.Node, declContainer *ast.Node) bool {
19071907
for current := usage; current != nil && current != declContainer; current = current.Parent {
19081908
if ast.IsFunctionLike(current) {
1909-
return true
1909+
return ast.GetImmediatelyInvokedFunctionExpression(current) == nil
19101910
}
19111911
if ast.IsClassStaticBlockDeclaration(current) {
19121912
return declaration.Pos() < usage.Pos()
@@ -1933,6 +1933,15 @@ func (c *Checker) isUsedInFunctionOrInstanceProperty(usage *ast.Node, declaratio
19331933
}
19341934
}
19351935
}
1936+
if current.Parent != nil && ast.IsDecorator(current.Parent) && current.Parent.AsDecorator().Expression == current {
1937+
decorator := current.Parent.AsDecorator()
1938+
if ast.IsParameter(decorator.Parent) {
1939+
return c.isUsedInFunctionOrInstanceProperty(decorator.Parent.Parent.Parent, declaration, declContainer)
1940+
}
1941+
if ast.IsMethodDeclaration(decorator.Parent) {
1942+
return c.isUsedInFunctionOrInstanceProperty(decorator.Parent.Parent, declaration, declContainer)
1943+
}
1944+
}
19361945
}
19371946
return false
19381947
}
@@ -10583,7 +10592,7 @@ func (c *Checker) checkIdentifier(node *ast.Node, checkMode CheckMode) *Type {
1058310592
return c.errorType
1058410593
}
1058510594
if symbol == c.argumentsSymbol {
10586-
if c.isInPropertyInitializerOrClassStaticBlock(node) {
10595+
if c.isInPropertyInitializerOrClassStaticBlock(node, true /*ignoreArrowFunctions*/) {
1058710596
c.error(node, diagnostics.X_arguments_cannot_be_referenced_in_property_initializers_or_class_static_initialization_blocks)
1058810597
return c.errorType
1058910598
}
@@ -11215,7 +11224,7 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node *
1121511224
}
1121611225
var diagnostic *ast.Diagnostic
1121711226
declarationName := right.Text()
11218-
if c.isInPropertyInitializerOrClassStaticBlock(node) &&
11227+
if c.isInPropertyInitializerOrClassStaticBlock(node, false /*ignoreArrowFunctions*/) &&
1121911228
!c.isOptionalPropertyDeclaration(valueDeclaration) &&
1122011229
!(ast.IsAccessExpression(node) && ast.IsAccessExpression(node.Expression())) &&
1122111230
!c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) &&
@@ -13151,25 +13160,19 @@ func (c *Checker) checkShorthandPropertyAssignment(node *ast.Node, inDestructuri
1315113160
return expressionType
1315213161
}
1315313162

13154-
func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node) bool {
13163+
func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node, ignoreArrowFunctions bool) bool {
1315513164
return ast.FindAncestorOrQuit(node, func(node *ast.Node) ast.FindAncestorResult {
1315613165
switch node.Kind {
13157-
case ast.KindPropertyDeclaration:
13166+
case ast.KindPropertyDeclaration, ast.KindClassStaticBlockDeclaration:
1315813167
return ast.FindAncestorTrue
13159-
case ast.KindPropertyAssignment, ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindSpreadAssignment,
13160-
ast.KindComputedPropertyName, ast.KindTemplateSpan, ast.KindJsxExpression, ast.KindJsxAttribute, ast.KindJsxAttributes,
13161-
ast.KindJsxSpreadAttribute, ast.KindJsxOpeningElement, ast.KindExpressionWithTypeArguments, ast.KindHeritageClause:
13162-
return ast.FindAncestorFalse
13163-
case ast.KindArrowFunction, ast.KindExpressionStatement:
13164-
if ast.IsBlock(node.Parent) && ast.IsClassStaticBlockDeclaration(node.Parent.Parent) {
13165-
return ast.FindAncestorTrue
13166-
}
13168+
case ast.KindTypeQuery, ast.KindJsxClosingElement:
1316713169
return ast.FindAncestorQuit
13170+
case ast.KindArrowFunction:
13171+
return core.IfElse(ignoreArrowFunctions, ast.FindAncestorFalse, ast.FindAncestorQuit)
13172+
case ast.KindBlock:
13173+
return core.IfElse(ast.IsFunctionLikeDeclaration(node.Parent) && node.Parent.Kind != ast.KindArrowFunction, ast.FindAncestorQuit, ast.FindAncestorFalse)
1316813174
default:
13169-
if ast.IsExpressionNode(node) {
13170-
return ast.FindAncestorFalse
13171-
}
13172-
return ast.FindAncestorQuit
13175+
return ast.FindAncestorFalse
1317313176
}
1317413177
}) != nil
1317513178
}

testdata/baselines/reference/submodule/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.errors.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(3,10): error
22
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(9,10): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
33
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(15,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
44
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(21,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
5+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(33,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
6+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(40,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
7+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(42,16): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
58
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(66,6): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.
69
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
10+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(77,9): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
11+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(96,26): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
12+
argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(102,15): error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
713

814

9-
==== argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (6 errors) ====
15+
==== argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts (12 errors) ====
1016
function A() {
1117
return class T {
1218
a = arguments
@@ -48,15 +54,21 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
4854
function D() {
4955
return class T {
5056
a = () => arguments // should error
57+
~~~~~~~~~
58+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
5159
}
5260
}
5361

5462
function D1() {
5563
return class T {
5664
a = () => {
5765
arguments; // should error
66+
~~~~~~~~~
67+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
5868
const b = () => {
5969
return arguments; // should error
70+
~~~~~~~~~
71+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
6072
}
6173

6274
function f() {
@@ -96,6 +108,8 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
96108
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
97109
while(1) {
98110
arguments // should error
111+
~~~~~~~~~
112+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
99113
}
100114
}
101115
}
@@ -115,12 +129,16 @@ argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.ts(75,7): error
115129
function D5() {
116130
return class T {
117131
a = (() => { return arguments; })() // should error
132+
~~~~~~~~~
133+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
118134
}
119135
}
120136

121137
function D6() {
122138
return class T {
123139
a = (x = arguments) => {} // should error
140+
~~~~~~~~~
141+
!!! error TS2815: 'arguments' cannot be referenced in property initializers or class static initialization blocks.
124142
}
125143
}
126144

testdata/baselines/reference/submodule/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.errors.txt.diff

Lines changed: 0 additions & 71 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ function D() {
8181
>T : typeof T
8282

8383
a = () => arguments // should error
84-
>a : () => IArguments
85-
>() => arguments : () => IArguments
86-
>arguments : IArguments
84+
>a : () => any
85+
>() => arguments : () => any
86+
>arguments : any
8787
}
8888
}
8989

@@ -99,14 +99,14 @@ function D1() {
9999
>() => { arguments; // should error const b = () => { return arguments; // should error } function f() { return arguments; // ok } } : () => void
100100

101101
arguments; // should error
102-
>arguments : IArguments
102+
>arguments : any
103103

104104
const b = () => {
105-
>b : () => IArguments
106-
>() => { return arguments; // should error } : () => IArguments
105+
>b : () => any
106+
>() => { return arguments; // should error } : () => any
107107

108108
return arguments; // should error
109-
>arguments : IArguments
109+
>arguments : any
110110
}
111111

112112
function f() {
@@ -175,7 +175,7 @@ function D3() {
175175
>1 : 1
176176

177177
arguments // should error
178-
>arguments : IArguments
178+
>arguments : any
179179
}
180180
}
181181
}
@@ -208,11 +208,11 @@ function D5() {
208208
>T : typeof T
209209

210210
a = (() => { return arguments; })() // should error
211-
>a : IArguments
212-
>(() => { return arguments; })() : IArguments
213-
>(() => { return arguments; }) : () => IArguments
214-
>() => { return arguments; } : () => IArguments
215-
>arguments : IArguments
211+
>a : any
212+
>(() => { return arguments; })() : any
213+
>(() => { return arguments; }) : () => any
214+
>() => { return arguments; } : () => any
215+
>arguments : any
216216
}
217217
}
218218

@@ -224,10 +224,10 @@ function D6() {
224224
>T : typeof T
225225

226226
a = (x = arguments) => {} // should error
227-
>a : (x?: IArguments) => void
228-
>(x = arguments) => {} : (x?: IArguments) => void
229-
>x : IArguments
230-
>arguments : IArguments
227+
>a : (x?: any) => void
228+
>(x = arguments) => {} : (x?: any) => void
229+
>x : any
230+
>arguments : any
231231
}
232232
}
233233

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
11
--- old.argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types
22
+++ new.argumentsUsedInClassFieldInitializerOrStaticInitializationBlock.types
3-
@@= skipped -174, +174 lines =@@
4-
>1 : 1
3+
@@= skipped -80, +80 lines =@@
4+
>T : typeof T
55

6-
arguments // should error
7-
->arguments : any
8-
+>arguments : IArguments
6+
a = () => arguments // should error
7+
->a : () => IArguments
8+
->() => arguments : () => IArguments
9+
->arguments : IArguments
10+
+>a : () => any
11+
+>() => arguments : () => any
12+
+>arguments : any
13+
}
14+
}
15+
16+
@@= skipped -18, +18 lines =@@
17+
>() => { arguments; // should error const b = () => { return arguments; // should error } function f() { return arguments; // ok } } : () => void
18+
19+
arguments; // should error
20+
->arguments : IArguments
21+
+>arguments : any
22+
23+
const b = () => {
24+
->b : () => IArguments
25+
->() => { return arguments; // should error } : () => IArguments
26+
+>b : () => any
27+
+>() => { return arguments; // should error } : () => any
28+
29+
return arguments; // should error
30+
->arguments : IArguments
31+
+>arguments : any
932
}
10-
}
11-
}
33+
34+
function f() {
35+
@@= skipped -109, +109 lines =@@
36+
>T : typeof T
37+
38+
a = (() => { return arguments; })() // should error
39+
->a : IArguments
40+
->(() => { return arguments; })() : IArguments
41+
->(() => { return arguments; }) : () => IArguments
42+
->() => { return arguments; } : () => IArguments
43+
->arguments : IArguments
44+
+>a : any
45+
+>(() => { return arguments; })() : any
46+
+>(() => { return arguments; }) : () => any
47+
+>() => { return arguments; } : () => any
48+
+>arguments : any
49+
}
50+
}
51+
52+
@@= skipped -16, +16 lines =@@
53+
>T : typeof T
54+
55+
a = (x = arguments) => {} // should error
56+
->a : (x?: IArguments) => void
57+
->(x = arguments) => {} : (x?: IArguments) => void
58+
->x : IArguments
59+
->arguments : IArguments
60+
+>a : (x?: any) => void
61+
+>(x = arguments) => {} : (x?: any) => void
62+
+>x : any
63+
+>arguments : any
64+
}
65+
}

0 commit comments

Comments
 (0)