Skip to content

Commit f07d8ab

Browse files
committed
fix and add test case for optional chaining
1 parent 56b6a38 commit f07d8ab

File tree

14 files changed

+274
-44
lines changed

14 files changed

+274
-44
lines changed

src/compiler/compile/nodes/shared/Expression.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default class Expression {
201201
scope = map.get(node);
202202
}
203203

204-
if (is_reference(node, parent)) {
204+
if (node.type === 'Identifier' && is_reference(node, parent)) {
205205
const { name } = flatten_reference(node);
206206

207207
if (scope.has(name)) return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/* generated by Svelte vX.Y.Z */
2+
import {
3+
SvelteComponent,
4+
attr,
5+
create_component,
6+
destroy_component,
7+
detach,
8+
element,
9+
init,
10+
insert,
11+
mount_component,
12+
safe_not_equal,
13+
set_data,
14+
space,
15+
text,
16+
transition_in,
17+
transition_out
18+
} from "svelte/internal";
19+
20+
function create_fragment(ctx) {
21+
let t0_value = /*a*/ ctx[0].normal + "";
22+
let t0;
23+
let t1_value = /*b*/ ctx[1]?.optional + "";
24+
let t1;
25+
let t2;
26+
let t3_value = /*c*/ ctx[2]["computed"] + "";
27+
let t3;
28+
let t4_value = /*d*/ ctx[3]?.["computed_optional"] + "";
29+
let t4;
30+
let t5;
31+
let t6_value = /*e*/ ctx[4]() + "";
32+
let t6;
33+
let t7_value = /*f*/ ctx[5]?.() + "";
34+
let t7;
35+
let t8;
36+
let div;
37+
let div_a_value;
38+
let div_b_value;
39+
let div_c_value;
40+
let div_d_value;
41+
let div_e_value;
42+
let div_f_value;
43+
let t9;
44+
let component;
45+
let current;
46+
47+
component = new /*Component*/ ctx[6]({
48+
props: {
49+
a: /*a*/ ctx[0].normal,
50+
b: /*b*/ ctx[1]?.optional,
51+
c: /*c*/ ctx[2]["computed"],
52+
d: /*d*/ ctx[3]?.["computed_optional"],
53+
e: /*e*/ ctx[4](),
54+
f: /*f*/ ctx[5]?.()
55+
}
56+
});
57+
58+
return {
59+
c() {
60+
t0 = text(t0_value);
61+
t1 = text(t1_value);
62+
t2 = space();
63+
t3 = text(t3_value);
64+
t4 = text(t4_value);
65+
t5 = space();
66+
t6 = text(t6_value);
67+
t7 = text(t7_value);
68+
t8 = space();
69+
div = element("div");
70+
t9 = space();
71+
create_component(component.$$.fragment);
72+
attr(div, "a", div_a_value = /*a*/ ctx[0].normal);
73+
attr(div, "b", div_b_value = /*b*/ ctx[1]?.optional);
74+
attr(div, "c", div_c_value = /*c*/ ctx[2]["computed"]);
75+
attr(div, "d", div_d_value = /*d*/ ctx[3]?.["computed_optional"]);
76+
attr(div, "e", div_e_value = /*e*/ ctx[4]());
77+
attr(div, "f", div_f_value = /*f*/ ctx[5]?.());
78+
},
79+
m(target, anchor) {
80+
insert(target, t0, anchor);
81+
insert(target, t1, anchor);
82+
insert(target, t2, anchor);
83+
insert(target, t3, anchor);
84+
insert(target, t4, anchor);
85+
insert(target, t5, anchor);
86+
insert(target, t6, anchor);
87+
insert(target, t7, anchor);
88+
insert(target, t8, anchor);
89+
insert(target, div, anchor);
90+
insert(target, t9, anchor);
91+
mount_component(component, target, anchor);
92+
current = true;
93+
},
94+
p(ctx, [dirty]) {
95+
if ((!current || dirty & /*a*/ 1) && t0_value !== (t0_value = /*a*/ ctx[0].normal + "")) set_data(t0, t0_value);
96+
if ((!current || dirty & /*b*/ 2) && t1_value !== (t1_value = /*b*/ ctx[1]?.optional + "")) set_data(t1, t1_value);
97+
if ((!current || dirty & /*c*/ 4) && t3_value !== (t3_value = /*c*/ ctx[2]["computed"] + "")) set_data(t3, t3_value);
98+
if ((!current || dirty & /*d*/ 8) && t4_value !== (t4_value = /*d*/ ctx[3]?.["computed_optional"] + "")) set_data(t4, t4_value);
99+
if ((!current || dirty & /*e*/ 16) && t6_value !== (t6_value = /*e*/ ctx[4]() + "")) set_data(t6, t6_value);
100+
if ((!current || dirty & /*f*/ 32) && t7_value !== (t7_value = /*f*/ ctx[5]?.() + "")) set_data(t7, t7_value);
101+
102+
if (!current || dirty & /*a*/ 1 && div_a_value !== (div_a_value = /*a*/ ctx[0].normal)) {
103+
attr(div, "a", div_a_value);
104+
}
105+
106+
if (!current || dirty & /*b*/ 2 && div_b_value !== (div_b_value = /*b*/ ctx[1]?.optional)) {
107+
attr(div, "b", div_b_value);
108+
}
109+
110+
if (!current || dirty & /*c*/ 4 && div_c_value !== (div_c_value = /*c*/ ctx[2]["computed"])) {
111+
attr(div, "c", div_c_value);
112+
}
113+
114+
if (!current || dirty & /*d*/ 8 && div_d_value !== (div_d_value = /*d*/ ctx[3]?.["computed_optional"])) {
115+
attr(div, "d", div_d_value);
116+
}
117+
118+
if (!current || dirty & /*e*/ 16 && div_e_value !== (div_e_value = /*e*/ ctx[4]())) {
119+
attr(div, "e", div_e_value);
120+
}
121+
122+
if (!current || dirty & /*f*/ 32 && div_f_value !== (div_f_value = /*f*/ ctx[5]?.())) {
123+
attr(div, "f", div_f_value);
124+
}
125+
126+
const component_changes = {};
127+
if (dirty & /*a*/ 1) component_changes.a = /*a*/ ctx[0].normal;
128+
if (dirty & /*b*/ 2) component_changes.b = /*b*/ ctx[1]?.optional;
129+
if (dirty & /*c*/ 4) component_changes.c = /*c*/ ctx[2]["computed"];
130+
if (dirty & /*d*/ 8) component_changes.d = /*d*/ ctx[3]?.["computed_optional"];
131+
if (dirty & /*e*/ 16) component_changes.e = /*e*/ ctx[4]();
132+
if (dirty & /*f*/ 32) component_changes.f = /*f*/ ctx[5]?.();
133+
component.$set(component_changes);
134+
},
135+
i(local) {
136+
if (current) return;
137+
transition_in(component.$$.fragment, local);
138+
current = true;
139+
},
140+
o(local) {
141+
transition_out(component.$$.fragment, local);
142+
current = false;
143+
},
144+
d(detaching) {
145+
if (detaching) detach(t0);
146+
if (detaching) detach(t1);
147+
if (detaching) detach(t2);
148+
if (detaching) detach(t3);
149+
if (detaching) detach(t4);
150+
if (detaching) detach(t5);
151+
if (detaching) detach(t6);
152+
if (detaching) detach(t7);
153+
if (detaching) detach(t8);
154+
if (detaching) detach(div);
155+
if (detaching) detach(t9);
156+
destroy_component(component, detaching);
157+
}
158+
};
159+
}
160+
161+
function instance($$self, $$props, $$invalidate) {
162+
let { a } = $$props;
163+
let { b } = $$props;
164+
let { c } = $$props;
165+
let { d } = $$props;
166+
let { e } = $$props;
167+
let { f } = $$props;
168+
let Component;
169+
170+
$$self.$set = $$props => {
171+
if ("a" in $$props) $$invalidate(0, a = $$props.a);
172+
if ("b" in $$props) $$invalidate(1, b = $$props.b);
173+
if ("c" in $$props) $$invalidate(2, c = $$props.c);
174+
if ("d" in $$props) $$invalidate(3, d = $$props.d);
175+
if ("e" in $$props) $$invalidate(4, e = $$props.e);
176+
if ("f" in $$props) $$invalidate(5, f = $$props.f);
177+
};
178+
179+
return [a, b, c, d, e, f, Component];
180+
}
181+
182+
class Component_1 extends SvelteComponent {
183+
constructor(options) {
184+
super();
185+
init(this, options, instance, create_fragment, safe_not_equal, { a: 0, b: 1, c: 2, d: 3, e: 4, f: 5 });
186+
}
187+
}
188+
189+
export default Component_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script>
2+
export let a;
3+
export let b;
4+
export let c;
5+
export let d;
6+
export let e;
7+
export let f;
8+
let Component;
9+
</script>
10+
11+
{a.normal}{b?.optional}
12+
{c['computed']}{d?.['computed_optional']}
13+
{e()}{f?.()}
14+
15+
<div
16+
a={a.normal}
17+
b={b?.optional}
18+
c={c['computed']}
19+
d={d?.['computed_optional']}
20+
e={e()}
21+
f={f?.()}
22+
/>
23+
24+
<Component
25+
a={a.normal}
26+
b={b?.optional}
27+
c={c['computed']}
28+
d={d?.['computed_optional']}
29+
e={e()}
30+
f={f?.()}
31+
/>

test/parser/samples/action-with-call/output.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
"value": "tooltip msg",
6565
"raw": "'tooltip msg'"
6666
}
67-
]
67+
],
68+
"optional": false
6869
}
6970
}
7071
],

test/parser/samples/animation/output.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
}
5353
],
5454
"context": {
55-
"start": 17,
56-
"end": 22,
5755
"type": "Identifier",
58-
"name": "thing"
56+
"name": "thing",
57+
"start": 17,
58+
"end": 22
5959
},
6060
"key": {
6161
"type": "Identifier",

test/parser/samples/await-catch/output.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
},
2727
"value": null,
2828
"error": {
29-
"start": 47,
30-
"end": 55,
3129
"type": "Identifier",
32-
"name": "theError"
30+
"name": "theError",
31+
"start": 47,
32+
"end": 55
3333
},
3434
"pending": {
3535
"start": 19,
@@ -152,7 +152,8 @@
152152
},
153153
"name": "message"
154154
},
155-
"computed": false
155+
"computed": false,
156+
"optional": false
156157
}
157158
}
158159
]

test/parser/samples/await-then-catch/output.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
"name": "thePromise"
2626
},
2727
"value": {
28-
"start": 46,
29-
"end": 54,
3028
"type": "Identifier",
31-
"name": "theValue"
29+
"name": "theValue",
30+
"start": 46,
31+
"end": 54
3232
},
3333
"error": {
34-
"start": 96,
35-
"end": 104,
3634
"type": "Identifier",
37-
"name": "theError"
35+
"name": "theError",
36+
"start": 96,
37+
"end": 104
3838
},
3939
"pending": {
4040
"start": 19,
@@ -209,7 +209,8 @@
209209
},
210210
"name": "message"
211211
},
212-
"computed": false
212+
"computed": false,
213+
"optional": false
213214
}
214215
}
215216
]

test/parser/samples/dynamic-import/output.json

+12-6
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@
275275
},
276276
"name": "then"
277277
},
278-
"computed": false
278+
"computed": false,
279+
"optional": false
279280
},
280281
"arguments": [
281282
{
@@ -403,7 +404,8 @@
403404
},
404405
"name": "log"
405406
},
406-
"computed": false
407+
"computed": false,
408+
"optional": false
407409
},
408410
"arguments": [
409411
{
@@ -452,21 +454,25 @@
452454
},
453455
"name": "default"
454456
},
455-
"computed": false
457+
"computed": false,
458+
"optional": false
456459
}
457-
]
460+
],
461+
"optional": false
458462
}
459463
}
460464
]
461465
}
462466
}
463-
]
467+
],
468+
"optional": false
464469
}
465470
}
466471
]
467472
}
468473
}
469-
]
474+
],
475+
"optional": false
470476
}
471477
}
472478
],

test/parser/samples/each-block-else/output.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
}
5858
],
5959
"context": {
60-
"start": 18,
61-
"end": 24,
6260
"type": "Identifier",
63-
"name": "animal"
61+
"name": "animal",
62+
"start": 18,
63+
"end": 24
6464
},
6565
"else": {
6666
"start": 50,

test/parser/samples/each-block-indexed/output.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@
8585
}
8686
],
8787
"context": {
88-
"start": 18,
89-
"end": 24,
9088
"type": "Identifier",
91-
"name": "animal"
89+
"name": "animal",
90+
"start": 18,
91+
"end": 24
9292
},
9393
"index": "i"
9494
}

0 commit comments

Comments
 (0)