Sprout tests to double-check our destructuring
I wanted to double-check the semantics of when default values are used, so i wrote out some fixtures with sprout enabled.
Joe Savona committed
Sep 7, 2023 at 17:17 UTC
edd9c52142609b8eeaa81b64f9505ebf068f96a5
8 files changed
+172
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-at-array-hole.expect.md
new
+33
@@ -0,0 +1,33 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ // destructure slot index has a hole in the input, should return default
7
+ const [x = 42] = props.value;
8
+ return x;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [{ value: [, /* hole! */ 3.14] }],
14
+};
15
+
16
+```
17
+
18
+## Code
19
+
20
+```javascript
21
+function Component(props) {
22
+ const [t18] = props.value;
23
+ const x = t18 === undefined ? 42 : t18;
24
+ return x;
25
+}
26
+
27
+export const FIXTURE_ENTRYPOINT = {
28
+ fn: Component,
29
+ params: [{ value: [, /* hole! */ 3.14] }],
30
+};
31
+
32
+```
33
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-at-array-hole.js
new
+10
@@ -0,0 +1,10 @@
1
+function Component(props) {
2
+ // destructure slot index has a hole in the input, should return default
3
+ const [x = 42] = props.value;
4
+ return x;
5
+}
6
+
7
+export const FIXTURE_ENTRYPOINT = {
8
+ fn: Component,
9
+ params: [{ value: [, /* hole! */ 3.14] }],
10
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.expect.md
new
+33
@@ -0,0 +1,33 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ // destructure slot index has an explicit null in the input, should return null (not the default)
7
+ const [x = 42] = props.value;
8
+ return x;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [{ value: [null] }],
14
+};
15
+
16
+```
17
+
18
+## Code
19
+
20
+```javascript
21
+function Component(props) {
22
+ const [t18] = props.value;
23
+ const x = t18 === undefined ? 42 : t18;
24
+ return x;
25
+}
26
+
27
+export const FIXTURE_ENTRYPOINT = {
28
+ fn: Component,
29
+ params: [{ value: [null] }],
30
+};
31
+
32
+```
33
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.js
new
+10
@@ -0,0 +1,10 @@
1
+function Component(props) {
2
+ // destructure slot index has an explicit null in the input, should return null (not the default)
3
+ const [x = 42] = props.value;
4
+ return x;
5
+}
6
+
7
+export const FIXTURE_ENTRYPOINT = {
8
+ fn: Component,
9
+ params: [{ value: [null] }],
10
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.expect.md
new
+33
@@ -0,0 +1,33 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ // destructure slot index has an explicit undefined in the input, should return default
7
+ const [x = 42] = props.value;
8
+ return x;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [{ value: [undefined] }],
14
+};
15
+
16
+```
17
+
18
+## Code
19
+
20
+```javascript
21
+function Component(props) {
22
+ const [t18] = props.value;
23
+ const x = t18 === undefined ? 42 : t18;
24
+ return x;
25
+}
26
+
27
+export const FIXTURE_ENTRYPOINT = {
28
+ fn: Component,
29
+ params: [{ value: [undefined] }],
30
+};
31
+
32
+```
33
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.js
new
+10
@@ -0,0 +1,10 @@
1
+function Component(props) {
2
+ // destructure slot index has an explicit undefined in the input, should return default
3
+ const [x = 42] = props.value;
4
+ return x;
5
+}
6
+
7
+export const FIXTURE_ENTRYPOINT = {
8
+ fn: Component,
9
+ params: [{ value: [undefined] }],
10
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.expect.md
new
+33
@@ -0,0 +1,33 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ // destructure past end of empty array, should evaluate to default
7
+ const [x = 42] = props.value;
8
+ return x;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [{ value: [] }],
14
+};
15
+
16
+```
17
+
18
+## Code
19
+
20
+```javascript
21
+function Component(props) {
22
+ const [t18] = props.value;
23
+ const x = t18 === undefined ? 42 : t18;
24
+ return x;
25
+}
26
+
27
+export const FIXTURE_ENTRYPOINT = {
28
+ fn: Component,
29
+ params: [{ value: [] }],
30
+};
31
+
32
+```
33
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.js
new
+10
@@ -0,0 +1,10 @@
1
+function Component(props) {
2
+ // destructure past end of empty array, should evaluate to default
3
+ const [x = 42] = props.value;
4
+ return x;
5
+}
6
+
7
+export const FIXTURE_ENTRYPOINT = {
8
+ fn: Component,
9
+ params: [{ value: [] }],
10
+};