[hoisting] Add failing tests
The current plan is to add support for hoisting function declarations, let/const declarations, but not var yet
Lauren Tan committed
Sep 5, 2023 at 17:04 UTC
31f0f426e189cb53495db12b05a2a0299eac380a
8 files changed
+182
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-function-declaration.expect.md
new
+30
@@ -0,0 +1,30 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function hoisting() {
6
+ function bar() {
7
+ return x;
8
+ }
9
+ return baz(); // OK: FuncDecls are HoistableDeclarations that have both declaration and value hoisting
10
+ function baz() {
11
+ return bar();
12
+ }
13
+}
14
+
15
+export const FIXTURE_ENTRYPOINT = {
16
+ fn: hoisting,
17
+ params: [],
18
+ isComponent: false,
19
+};
20
+
21
+```
22
+
23
+
24
+## Error
25
+
26
+```
27
+[ReactForget] Invariant: Expected value for identifier `5` to be initialized. (5:5)
28
+```
29
+
30
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-function-declaration.js
new
+15
@@ -0,0 +1,15 @@
1
+function hoisting() {
2
+ function bar() {
3
+ return x;
4
+ }
5
+ return baz(); // OK: FuncDecls are HoistableDeclarations that have both declaration and value hoisting
6
+ function baz() {
7
+ return bar();
8
+ }
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: hoisting,
13
+ params: [],
14
+ isComponent: false,
15
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-function-expression.expect.md
new
+31
@@ -0,0 +1,31 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function hoisting() {
6
+ const foo = () => {
7
+ return bar();
8
+ };
9
+ const bar = () => {
10
+ return 1;
11
+ };
12
+
13
+ return foo(); // OK: bar's value is only accessed outside of its TDZ
14
+}
15
+
16
+export const FIXTURE_ENTRYPOINT = {
17
+ fn: hoisting,
18
+ params: [],
19
+ isComponent: false,
20
+};
21
+
22
+```
23
+
24
+
25
+## Error
26
+
27
+```
28
+[ReactForget] Invariant: EnterSSA: Expected identifier to be defined before being used. Identifier bar$0 is undefined (5:7)
29
+```
30
+
31
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-function-expression.js
new
+16
@@ -0,0 +1,16 @@
1
+function hoisting() {
2
+ const foo = () => {
3
+ return bar();
4
+ };
5
+ const bar = () => {
6
+ return 1;
7
+ };
8
+
9
+ return foo(); // OK: bar's value is only accessed outside of its TDZ
10
+}
11
+
12
+export const FIXTURE_ENTRYPOINT = {
13
+ fn: hoisting,
14
+ params: [],
15
+ isComponent: false,
16
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-let-const-declaration.expect.md
new
+29
@@ -0,0 +1,29 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function hoisting() {
6
+ const foo = () => {
7
+ return bar + baz;
8
+ };
9
+ let bar = 3;
10
+ const baz = 2;
11
+ return foo(); // OK: called outside of TDZ for bar/baz
12
+}
13
+
14
+export const FIXTURE_ENTRYPOINT = {
15
+ fn: hoisting,
16
+ params: [],
17
+ isComponent: false,
18
+};
19
+
20
+```
21
+
22
+
23
+## Error
24
+
25
+```
26
+[ReactForget] Invariant: EnterSSA: Expected identifier to be defined before being used. Identifier bar$0 is undefined (5:5)
27
+```
28
+
29
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-let-const-declaration.js
new
+14
@@ -0,0 +1,14 @@
1
+function hoisting() {
2
+ const foo = () => {
3
+ return bar + baz;
4
+ };
5
+ let bar = 3;
6
+ const baz = 2;
7
+ return foo(); // OK: called outside of TDZ for bar/baz
8
+}
9
+
10
+export const FIXTURE_ENTRYPOINT = {
11
+ fn: hoisting,
12
+ params: [],
13
+ isComponent: false,
14
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-var-declaration.expect.md
new
+31
@@ -0,0 +1,31 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function hoisting() {
6
+ function addOne(b) {
7
+ // a is undefined (only the declaration is hoisted, not the init) but shouldn't throw
8
+ return a + b;
9
+ }
10
+ const result = addOne(2);
11
+ var a = 1;
12
+
13
+ return result; // OK: returns NaN. The code is semantically wrong but technically correct
14
+}
15
+
16
+export const FIXTURE_ENTRYPOINT = {
17
+ fn: hoisting,
18
+ params: [],
19
+ isComponent: false,
20
+};
21
+
22
+```
23
+
24
+
25
+## Error
26
+
27
+```
28
+[ReactForget] Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration (7:7)
29
+```
30
+
31
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-hoisting-simple-var-declaration.js
new
+16
@@ -0,0 +1,16 @@
1
+function hoisting() {
2
+ function addOne(b) {
3
+ // a is undefined (only the declaration is hoisted, not the init) but shouldn't throw
4
+ return a + b;
5
+ }
6
+ const result = addOne(2);
7
+ var a = 1;
8
+
9
+ return result; // OK: returns NaN. The code is semantically wrong but technically correct
10
+}
11
+
12
+export const FIXTURE_ENTRYPOINT = {
13
+ fn: hoisting,
14
+ params: [],
15
+ isComponent: false,
16
+};