Skip to content

Commit 912f193

Browse files
AndaristZzzen
andauthored
Port "check usage before declaration for decorators" (#1829)
Co-authored-by: Zzzen <[email protected]>
1 parent 8cf32b4 commit 912f193

File tree

5 files changed

+56
-117
lines changed

5 files changed

+56
-117
lines changed

internal/checker/checker.go

Lines changed: 10 additions & 1 deletion
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
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ blockScopedVariablesUseBeforeDef.ts(100,12): error TS2448: Block-scoped variable
55
blockScopedVariablesUseBeforeDef.ts(111,28): error TS2448: Block-scoped variable 'a' used before its declaration.
66
blockScopedVariablesUseBeforeDef.ts(112,21): error TS2448: Block-scoped variable 'a' used before its declaration.
77
blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable 'a' used before its declaration.
8+
blockScopedVariablesUseBeforeDef.ts(128,9): error TS2448: Block-scoped variable 'foo' used before its declaration.
9+
blockScopedVariablesUseBeforeDef.ts(131,9): error TS2448: Block-scoped variable 'foo' used before its declaration.
10+
blockScopedVariablesUseBeforeDef.ts(153,20): error TS2450: Enum 'Enum' used before its declaration.
811

912

10-
==== blockScopedVariablesUseBeforeDef.ts (7 errors) ====
13+
==== blockScopedVariablesUseBeforeDef.ts (10 errors) ====
1114
function foo0() {
1215
let a = x;
1316
~
@@ -157,9 +160,15 @@ blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable
157160
const promise = (async () => {
158161
promise
159162
foo
163+
~~~
164+
!!! error TS2448: Block-scoped variable 'foo' used before its declaration.
165+
!!! related TS2728 blockScopedVariablesUseBeforeDef.ts:134:11: 'foo' is declared here.
160166
await null
161167
promise
162168
foo
169+
~~~
170+
!!! error TS2448: Block-scoped variable 'foo' used before its declaration.
171+
!!! related TS2728 blockScopedVariablesUseBeforeDef.ts:134:11: 'foo' is declared here.
163172
})()
164173

165174
const foo = 1;
@@ -182,6 +191,9 @@ blockScopedVariablesUseBeforeDef.ts(122,22): error TS2448: Block-scoped variable
182191

183192
function foo18() {
184193
let a = (() => Enum.Yes)();
194+
~~~~
195+
!!! error TS2450: Enum 'Enum' used before its declaration.
196+
!!! related TS2728 blockScopedVariablesUseBeforeDef.ts:154:10: 'Enum' is declared here.
185197
enum Enum {
186198
No = 0,
187199
Yes = 1,

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

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

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ decoratorUsedBeforeDeclaration.ts(4,11): error TS2450: Enum 'Enum' used before i
66
decoratorUsedBeforeDeclaration.ts(4,16): error TS2729: Property 'No' is used before its initialization.
77
decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before its declaration.
88
decoratorUsedBeforeDeclaration.ts(5,14): error TS2729: Property 'No' is used before its initialization.
9+
decoratorUsedBeforeDeclaration.ts(12,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
10+
decoratorUsedBeforeDeclaration.ts(12,11): error TS2450: Enum 'Enum' used before its declaration.
11+
decoratorUsedBeforeDeclaration.ts(13,9): error TS2450: Enum 'Enum' used before its declaration.
12+
decoratorUsedBeforeDeclaration.ts(18,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
13+
decoratorUsedBeforeDeclaration.ts(24,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
14+
decoratorUsedBeforeDeclaration.ts(24,18): error TS2450: Enum 'Enum' used before its declaration.
15+
decoratorUsedBeforeDeclaration.ts(24,33): error TS2450: Enum 'Enum' used before its declaration.
16+
decoratorUsedBeforeDeclaration.ts(28,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
917

1018

11-
==== decoratorUsedBeforeDeclaration.ts (8 errors) ====
19+
==== decoratorUsedBeforeDeclaration.ts (16 errors) ====
1220
@lambda(Enum.No)
1321
~~~~~~
1422
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
@@ -45,22 +53,46 @@ decoratorUsedBeforeDeclaration.ts(5,14): error TS2729: Property 'No' is used bef
4553
}
4654

4755
@lambda(Enum.No)
56+
~~~~~~
57+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
58+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
59+
~~~~
60+
!!! error TS2450: Enum 'Enum' used before its declaration.
61+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
4862
@deco(Enum.No)
63+
~~~~
64+
!!! error TS2450: Enum 'Enum' used before its declaration.
65+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
4966
greet() {
5067
return "Hello, " + this.greeting;
5168
}
5269

5370
@lambda
71+
~~~~~~
72+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
73+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
5474
@deco
5575
greet1() {
5676
return "Hello, " + this.greeting;
5777
}
5878

5979
greet2(@lambda(Enum.No) @deco(Enum.No) param) {
80+
~~~~~~
81+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
82+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
83+
~~~~
84+
!!! error TS2450: Enum 'Enum' used before its declaration.
85+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
86+
~~~~
87+
!!! error TS2450: Enum 'Enum' used before its declaration.
88+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
6089
return "Hello, " + this.greeting;
6190
}
6291

6392
greet3(@lambda @deco param) {
93+
~~~~~~
94+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
95+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
6496
return "Hello, " + this.greeting;
6597
}
6698
}

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

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

0 commit comments

Comments
 (0)