Skip to content

Commit c3f45f0

Browse files
committed
fix(optimizer): support wrapping type-asserted variables in templates
1 parent a588b48 commit c3f45f0

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

.changeset/shy-walls-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik.dev/core': patch
3+
---
4+
5+
fix: support wrapping type-asserted variables in templates
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
source: packages/qwik/src/optimizer/core/src/test.rs
3+
assertion_line: 4360
4+
expression: output
5+
snapshot_kind: text
6+
---
7+
==INPUT==
8+
9+
10+
import { component$, useSignal } from '@qwik.dev/core';
11+
12+
export default component$(() => {
13+
const count = useSignal(0);
14+
return (
15+
<div>
16+
{(count as any).value}
17+
</div>
18+
);
19+
});
20+
21+
============================= test.js ==
22+
23+
import { componentQrl } from "@qwik.dev/core";
24+
import { qrl } from "@qwik.dev/core";
25+
export default /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrl(()=>import("./test.tsx_test_component_LUXeXe0DQrg"), "test_component_LUXeXe0DQrg"));
26+
27+
28+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;AAGE,6BAAe,mHAOZ\"}")
29+
============================= test.tsx_test_component_LUXeXe0DQrg.js (ENTRY POINT)==
30+
31+
import { _jsxSorted } from "@qwik.dev/core";
32+
import { _wrapProp } from "@qwik.dev/core";
33+
import { useSignal } from "@qwik.dev/core";
34+
export const test_component_LUXeXe0DQrg = ()=>{
35+
const count = useSignal(0);
36+
return /*#__PURE__*/ _jsxSorted("div", null, null, _wrapProp(count), 3, "u6_0");
37+
};
38+
39+
40+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;0CAG4B;IACzB,MAAM,QAAQ,UAAU;IACxB,qBACC,WAAC,6BACE;AAGL\"}")
41+
/*
42+
{
43+
"origin": "test.tsx",
44+
"name": "test_component_LUXeXe0DQrg",
45+
"entry": null,
46+
"displayName": "test.tsx_test_component",
47+
"hash": "LUXeXe0DQrg",
48+
"canonicalFilename": "test.tsx_test_component_LUXeXe0DQrg",
49+
"path": "",
50+
"extension": "js",
51+
"parent": null,
52+
"ctxKind": "function",
53+
"ctxName": "component$",
54+
"captures": false,
55+
"loc": [
56+
89,
57+
198
58+
]
59+
}
60+
*/
61+
== DIAGNOSTICS ==
62+
63+
[]

packages/qwik/src/optimizer/core/src/test.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4355,6 +4355,28 @@ fn should_not_wrap_var_template_string() {
43554355
});
43564356
}
43574357

4358+
#[test]
4359+
fn should_wrap_type_asserted_variables_in_template() {
4360+
test_input!(TestInput {
4361+
code: r#"
4362+
import { component$, useSignal } from '@qwik.dev/core';
4363+
4364+
export default component$(() => {
4365+
const count = useSignal(0);
4366+
return (
4367+
<div>
4368+
{(count as any).value}
4369+
</div>
4370+
);
4371+
});
4372+
"#
4373+
.to_string(),
4374+
transpile_ts: true,
4375+
transpile_jsx: true,
4376+
..TestInput::default()
4377+
});
4378+
}
4379+
43584380
// TODO(misko): Make this test work by implementing strict serialization.
43594381
// #[test]
43604382
// fn example_of_synchronous_qrl_that_cant_be_serialized() {

packages/qwik/src/optimizer/core/src/transform.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,18 @@ impl<'a> QwikTransform<'a> {
585585

586586
// Handle `obj.prop` case
587587
if let ast::Expr::Member(member) = folded.clone() {
588-
if let ast::Expr::Ident(_) = *member.obj {
588+
let obj_expr = if let ast::Expr::Paren(paren_expr) = (*member.obj).clone() {
589+
// for example (obj as any).prop
590+
paren_expr.expr
591+
} else {
592+
member.obj
593+
};
594+
595+
if let ast::Expr::Ident(_) = *obj_expr {
589596
let prop_sym = prop_to_string(&member.prop);
590597
if let Some(prop_sym) = prop_sym {
591598
let id = self.ensure_core_import(&_WRAP_PROP);
592-
return (Some(make_wrap(&id, member.obj, prop_sym)), is_const);
599+
return (Some(make_wrap(&id, obj_expr, prop_sym)), is_const);
593600
}
594601
}
595602
}

0 commit comments

Comments
 (0)