Skip to content

Commit 8031bd3

Browse files
AndaristZzzen
andcommitted
Port "check usage before declaration for decorators"
Co-authored-by: Zzzen <[email protected]>
1 parent e729a0a commit 8031bd3

File tree

5 files changed

+66
-109
lines changed

5 files changed

+66
-109
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
@@ -4,9 +4,17 @@ decoratorUsedBeforeDeclaration.ts(2,7): error TS2450: Enum 'Enum' used before it
44
decoratorUsedBeforeDeclaration.ts(4,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
55
decoratorUsedBeforeDeclaration.ts(4,11): error TS2450: Enum 'Enum' used before its declaration.
66
decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before its declaration.
7+
decoratorUsedBeforeDeclaration.ts(12,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
8+
decoratorUsedBeforeDeclaration.ts(12,11): error TS2450: Enum 'Enum' used before its declaration.
9+
decoratorUsedBeforeDeclaration.ts(13,9): error TS2450: Enum 'Enum' used before its declaration.
10+
decoratorUsedBeforeDeclaration.ts(18,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
11+
decoratorUsedBeforeDeclaration.ts(24,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
12+
decoratorUsedBeforeDeclaration.ts(24,18): error TS2450: Enum 'Enum' used before its declaration.
13+
decoratorUsedBeforeDeclaration.ts(24,33): error TS2450: Enum 'Enum' used before its declaration.
14+
decoratorUsedBeforeDeclaration.ts(28,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
715

816

9-
==== decoratorUsedBeforeDeclaration.ts (6 errors) ====
17+
==== decoratorUsedBeforeDeclaration.ts (14 errors) ====
1018
@lambda(Enum.No)
1119
~~~~~~
1220
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
@@ -37,22 +45,46 @@ decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before it
3745
}
3846

3947
@lambda(Enum.No)
48+
~~~~~~
49+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
50+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
51+
~~~~
52+
!!! error TS2450: Enum 'Enum' used before its declaration.
53+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
4054
@deco(Enum.No)
55+
~~~~
56+
!!! error TS2450: Enum 'Enum' used before its declaration.
57+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
4158
greet() {
4259
return "Hello, " + this.greeting;
4360
}
4461

4562
@lambda
63+
~~~~~~
64+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
65+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
4666
@deco
4767
greet1() {
4868
return "Hello, " + this.greeting;
4969
}
5070

5171
greet2(@lambda(Enum.No) @deco(Enum.No) param) {
72+
~~~~~~
73+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
74+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
75+
~~~~
76+
!!! error TS2450: Enum 'Enum' used before its declaration.
77+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
78+
~~~~
79+
!!! error TS2450: Enum 'Enum' used before its declaration.
80+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
5281
return "Hello, " + this.greeting;
5382
}
5483

5584
greet3(@lambda @deco param) {
85+
~~~~~~
86+
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
87+
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
5688
return "Hello, " + this.greeting;
5789
}
5890
}

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

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,19 @@
77
-decoratorUsedBeforeDeclaration.ts(4,16): error TS2729: Property 'No' is used before its initialization.
88
decoratorUsedBeforeDeclaration.ts(5,9): error TS2450: Enum 'Enum' used before its declaration.
99
-decoratorUsedBeforeDeclaration.ts(5,14): error TS2729: Property 'No' is used before its initialization.
10-
-decoratorUsedBeforeDeclaration.ts(12,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
11-
-decoratorUsedBeforeDeclaration.ts(12,11): error TS2450: Enum 'Enum' used before its declaration.
12-
-decoratorUsedBeforeDeclaration.ts(13,9): error TS2450: Enum 'Enum' used before its declaration.
13-
-decoratorUsedBeforeDeclaration.ts(18,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
14-
-decoratorUsedBeforeDeclaration.ts(24,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
15-
-decoratorUsedBeforeDeclaration.ts(24,18): error TS2450: Enum 'Enum' used before its declaration.
16-
-decoratorUsedBeforeDeclaration.ts(24,33): error TS2450: Enum 'Enum' used before its declaration.
17-
-decoratorUsedBeforeDeclaration.ts(28,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
18-
-
19-
-
10+
decoratorUsedBeforeDeclaration.ts(12,4): error TS2448: Block-scoped variable 'lambda' used before its declaration.
11+
decoratorUsedBeforeDeclaration.ts(12,11): error TS2450: Enum 'Enum' used before its declaration.
12+
decoratorUsedBeforeDeclaration.ts(13,9): error TS2450: Enum 'Enum' used before its declaration.
13+
@@= skipped -13, +11 lines =@@
14+
decoratorUsedBeforeDeclaration.ts(28,11): error TS2448: Block-scoped variable 'lambda' used before its declaration.
15+
16+
2017
-==== decoratorUsedBeforeDeclaration.ts (16 errors) ====
21-
+
22-
+
23-
+==== decoratorUsedBeforeDeclaration.ts (6 errors) ====
18+
+==== decoratorUsedBeforeDeclaration.ts (14 errors) ====
2419
@lambda(Enum.No)
2520
~~~~~~
2621
!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
27-
@@= skipped -33, +23 lines =@@
22+
@@= skipped -20, +20 lines =@@
2823
~~~~
2924
!!! error TS2450: Enum 'Enum' used before its declaration.
3025
!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
@@ -40,51 +35,4 @@
4035
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:36:3: 'No' is declared here.
4136
greeting: string;
4237

43-
constructor(message: string) {
44-
@@= skipped -17, +11 lines =@@
45-
}
46-
47-
@lambda(Enum.No)
48-
- ~~~~~~
49-
-!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
50-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
51-
- ~~~~
52-
-!!! error TS2450: Enum 'Enum' used before its declaration.
53-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
54-
@deco(Enum.No)
55-
- ~~~~
56-
-!!! error TS2450: Enum 'Enum' used before its declaration.
57-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
58-
greet() {
59-
return "Hello, " + this.greeting;
60-
}
61-
62-
@lambda
63-
- ~~~~~~
64-
-!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
65-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
66-
@deco
67-
greet1() {
68-
return "Hello, " + this.greeting;
69-
}
70-
71-
greet2(@lambda(Enum.No) @deco(Enum.No) param) {
72-
- ~~~~~~
73-
-!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
74-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
75-
- ~~~~
76-
-!!! error TS2450: Enum 'Enum' used before its declaration.
77-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
78-
- ~~~~
79-
-!!! error TS2450: Enum 'Enum' used before its declaration.
80-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:35:6: 'Enum' is declared here.
81-
return "Hello, " + this.greeting;
82-
}
83-
84-
greet3(@lambda @deco param) {
85-
- ~~~~~~
86-
-!!! error TS2448: Block-scoped variable 'lambda' used before its declaration.
87-
-!!! related TS2728 decoratorUsedBeforeDeclaration.ts:40:7: 'lambda' is declared here.
88-
return "Hello, " + this.greeting;
89-
}
90-
}
38+
constructor(message: string) {

0 commit comments

Comments
 (0)