[rust][sema] Test for function hoisting
Joe Savona committed
Aug 14, 2023 at 17:36 UTC
f2a1a6a6e4fccb1fb6470f9e61b6ea111bf25a41
2 files changed
+279
compiler/forget/crates/forget_semantic_analysis/tests/fixtures/function-hoisting.js
new
+22
@@ -0,0 +1,22 @@
1
+function Component(props) {
2
+ foo();
3
+ if (props) {
4
+ {
5
+ for (;;) {
6
+ while (true) {
7
+ do {
8
+ function foo() {
9
+ bar();
10
+ if (props) {
11
+ const _ = function bar() {};
12
+ }
13
+ }
14
+ break;
15
+ } while (true);
16
+ break;
17
+ }
18
+ break;
19
+ }
20
+ }
21
+ }
22
+}
compiler/forget/crates/forget_semantic_analysis/tests/snapshots/analysis_test__fixtures@function-hoisting.js.snap
new
+257
@@ -0,0 +1,257 @@
1
+---
2
+source: crates/forget_semantic_analysis/tests/analysis_test.rs
3
+expression: "format!(\"Input:\\n{input}\\n\\nAnalysis:\\n{output}\")"
4
+input_file: crates/forget_semantic_analysis/tests/fixtures/function-hoisting.js
5
+---
6
+Input:
7
+function Component(props) {
8
+ foo();
9
+ if (props) {
10
+ {
11
+ for (;;) {
12
+ while (true) {
13
+ do {
14
+ function foo() {
15
+ bar();
16
+ if (props) {
17
+ const _ = function bar() {};
18
+ }
19
+ }
20
+ break;
21
+ } while (true);
22
+ break;
23
+ }
24
+ break;
25
+ }
26
+ }
27
+ }
28
+}
29
+
30
+
31
+Analysis:
32
+Scope {
33
+ id: ScopeId(
34
+ 0,
35
+ ),
36
+ kind: Global,
37
+ declarations: {},
38
+ references: [],
39
+ children: [
40
+ Scope {
41
+ id: ScopeId(
42
+ 1,
43
+ ),
44
+ kind: Module,
45
+ declarations: {
46
+ "Component": Declaration {
47
+ id: DeclarationId(
48
+ 0,
49
+ ),
50
+ kind: FunctionDeclaration,
51
+ scope: ScopeId(
52
+ 1,
53
+ ),
54
+ },
55
+ },
56
+ references: [],
57
+ children: [
58
+ Scope {
59
+ id: ScopeId(
60
+ 2,
61
+ ),
62
+ kind: Function,
63
+ declarations: {
64
+ "props": Declaration {
65
+ id: DeclarationId(
66
+ 1,
67
+ ),
68
+ kind: FunctionDeclaration,
69
+ scope: ScopeId(
70
+ 2,
71
+ ),
72
+ },
73
+ "foo": Declaration {
74
+ id: DeclarationId(
75
+ 2,
76
+ ),
77
+ kind: FunctionDeclaration,
78
+ scope: ScopeId(
79
+ 2,
80
+ ),
81
+ },
82
+ },
83
+ references: [
84
+ Reference {
85
+ id: ReferenceId(
86
+ 1,
87
+ ),
88
+ kind: Read,
89
+ declaration: DeclarationId(
90
+ 2,
91
+ ),
92
+ declaration (name): "foo",
93
+ scope: ScopeId(
94
+ 2,
95
+ ),
96
+ },
97
+ Reference {
98
+ id: ReferenceId(
99
+ 2,
100
+ ),
101
+ kind: Read,
102
+ declaration: DeclarationId(
103
+ 1,
104
+ ),
105
+ declaration (name): "props",
106
+ scope: ScopeId(
107
+ 2,
108
+ ),
109
+ },
110
+ ],
111
+ children: [
112
+ Scope {
113
+ id: ScopeId(
114
+ 3,
115
+ ),
116
+ kind: Block,
117
+ declarations: {},
118
+ references: [],
119
+ children: [
120
+ Scope {
121
+ id: ScopeId(
122
+ 4,
123
+ ),
124
+ kind: Block,
125
+ declarations: {},
126
+ references: [],
127
+ children: [
128
+ Scope {
129
+ id: ScopeId(
130
+ 5,
131
+ ),
132
+ kind: Block,
133
+ declarations: {},
134
+ references: [],
135
+ children: [
136
+ Scope {
137
+ id: ScopeId(
138
+ 6,
139
+ ),
140
+ kind: Block,
141
+ declarations: {},
142
+ references: [],
143
+ children: [
144
+ Scope {
145
+ id: ScopeId(
146
+ 7,
147
+ ),
148
+ kind: Block,
149
+ declarations: {},
150
+ references: [],
151
+ children: [
152
+ Scope {
153
+ id: ScopeId(
154
+ 8,
155
+ ),
156
+ kind: Function,
157
+ declarations: {},
158
+ references: [
159
+ Reference {
160
+ id: ReferenceId(
161
+ 0,
162
+ ),
163
+ kind: Read,
164
+ declaration: DeclarationId(
165
+ 1,
166
+ ),
167
+ declaration (name): "props",
168
+ scope: ScopeId(
169
+ 8,
170
+ ),
171
+ },
172
+ ],
173
+ children: [
174
+ Scope {
175
+ id: ScopeId(
176
+ 9,
177
+ ),
178
+ kind: Block,
179
+ declarations: {
180
+ "_": Declaration {
181
+ id: DeclarationId(
182
+ 3,
183
+ ),
184
+ kind: Const,
185
+ scope: ScopeId(
186
+ 9,
187
+ ),
188
+ },
189
+ },
190
+ references: [],
191
+ children: [
192
+ Scope {
193
+ id: ScopeId(
194
+ 10,
195
+ ),
196
+ kind: Function,
197
+ declarations: {
198
+ "bar": Declaration {
199
+ id: DeclarationId(
200
+ 4,
201
+ ),
202
+ kind: FunctionDeclaration,
203
+ scope: ScopeId(
204
+ 10,
205
+ ),
206
+ },
207
+ },
208
+ references: [],
209
+ children: [
210
+ Scope {
211
+ id: ScopeId(
212
+ 11,
213
+ ),
214
+ kind: Function,
215
+ declarations: {},
216
+ references: [],
217
+ children: [],
218
+ },
219
+ ],
220
+ },
221
+ ],
222
+ },
223
+ ],
224
+ },
225
+ ],
226
+ },
227
+ ],
228
+ },
229
+ ],
230
+ },
231
+ ],
232
+ },
233
+ ],
234
+ },
235
+ ],
236
+ },
237
+ ],
238
+ },
239
+ ],
240
+}
241
+Diagnostic(
242
+ DiagnosticData {
243
+ message: "Undefined variable",
244
+ span: Some(
245
+ SourceSpan {
246
+ offset: SourceOffset(
247
+ 0,
248
+ ),
249
+ length: 1,
250
+ },
251
+ ),
252
+ related_information: [],
253
+ severity: InvalidSyntax,
254
+ data: [],
255
+ },
256
+)
257
+