Skip to content

Commit

Permalink
fix: string prop destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Varixo committed Dec 25, 2024
1 parent 48af55c commit d8a19ca
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/qwik/src/optimizer/core/src/props_destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,25 @@ fn transform_pat(
}
ast::ObjectPatProp::KeyValue(ref v) => {
if matches!(v.key, ast::PropName::Ident(_) | ast::PropName::Str(_)) {
let (key_atom, key_ident) = match &v.key {
let (key_atom, prop) = match &v.key {
ast::PropName::Str(ref key) => {
let key_str: &str = &key.value;
let key_atom = Atom::from(key_str);
(
key_atom.clone(),
ast::IdentName::new(key_atom.clone(), DUMMY_SP),
ast::MemberProp::Computed(ast::ComputedPropName {
span: DUMMY_SP,
expr: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str {
span: DUMMY_SP,
value: key_atom,
raw: None,
}))),
}),
)
}
ast::PropName::Ident(ref key) => (key.sym.clone(), key.clone()),
ast::PropName::Ident(ref key) => {
(key.sym.clone(), ast::MemberProp::Ident(key.clone()))
}
_ => {
continue;
}
Expand All @@ -356,7 +365,7 @@ fn transform_pat(
box ast::Pat::Ident(ref ident) => {
let access = ast::Expr::Member(ast::MemberExpr {
obj: Box::new(new_ident.clone()),
prop: ast::MemberProp::Ident(key_ident),
prop,
span: DUMMY_SP,
});

Expand All @@ -370,7 +379,7 @@ fn transform_pat(
if is_const_expr(value.as_ref(), props_transform.global_collect, None) {
let access = ast::Expr::Member(ast::MemberExpr {
obj: Box::new(new_ident.clone()),
prop: ast::MemberProp::Ident(key_ident),
prop,
span: DUMMY_SP,
});
local.push((
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
source: packages/qwik/src/optimizer/core/src/test.rs
assertion_line: 3736
expression: output
snapshot_kind: text
---
==INPUT==


import { component$, useSignal } from "@qwik.dev/core";
export default component$((props) => {
const { 'bind:value': bindValue } = props;
const test = useSignal(bindValue);
return (
<>
{test.value}
</>
);
});

============================= test.js ==

import { componentQrl } from "@qwik.dev/core";
import { qrl } from "@qwik.dev/core";
export default /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrl(()=>import("./test.tsx_test_component_LUXeXe0DQrg"), "test_component_LUXeXe0DQrg"));


Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;AAEE,6BAAe,mHAQZ\"}")
============================= test.tsx_test_component_LUXeXe0DQrg.js (ENTRY POINT)==

import { Fragment as _Fragment } from "@qwik.dev/core/jsx-runtime";
import { _jsxSorted } from "@qwik.dev/core";
import { _wrapProp } from "@qwik.dev/core";
import { useSignal } from "@qwik.dev/core";
export const test_component_LUXeXe0DQrg = (props)=>{
const test = useSignal(props["bind:value"]);
return /*#__PURE__*/ _jsxSorted(_Fragment, null, null, _wrapProp(test), 3, "u6_0");
};


Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;;0CAE4B,CAAC;IAE1B,MAAM,OAAO,UADuB;IAEpC,qBACC,4CACC;AAGH\"}")
/*
{
"origin": "test.tsx",
"name": "test_component_LUXeXe0DQrg",
"entry": null,
"displayName": "test.tsx_test_component",
"hash": "LUXeXe0DQrg",
"canonicalFilename": "test.tsx_test_component_LUXeXe0DQrg",
"path": "",
"extension": "js",
"parent": null,
"ctxKind": "function",
"ctxName": "component$",
"captures": false,
"loc": [
88,
238
]
}
*/
== DIAGNOSTICS ==

[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
source: packages/qwik/src/optimizer/core/src/test.rs
assertion_line: 3758
expression: output
snapshot_kind: text
---
==INPUT==


import { component$, useSignal } from "@qwik.dev/core";
export default component$((props) => {
const { test, ...rest } = props;
const test = useSignal(rest['bind:value']);
return (
<>
{test.value}
</>
);
});

============================= test.js ==

import { componentQrl } from "@qwik.dev/core";
import { qrl } from "@qwik.dev/core";
export default /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrl(()=>import("./test.tsx_test_component_LUXeXe0DQrg"), "test_component_LUXeXe0DQrg"));


Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;AAEE,6BAAe,mHAQZ\"}")
============================= test.tsx_test_component_LUXeXe0DQrg.js (ENTRY POINT)==

import { Fragment as _Fragment } from "@qwik.dev/core/jsx-runtime";
import { _fnSignal } from "@qwik.dev/core";
import { _jsxSorted } from "@qwik.dev/core";
import { _restProps } from "@qwik.dev/core";
import { useSignal } from "@qwik.dev/core";
export const test_component_LUXeXe0DQrg = (props)=>{
const rest = _restProps(props, [
"test"
]);
useSignal(rest['bind:value']);
return /*#__PURE__*/ _jsxSorted(_Fragment, null, null, _fnSignal((p0)=>p0.test.value, [
props
], "p0.test.value"), 3, "u6_0");
};


Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;;;0CAE4B,CAAC;4BACA;;;IACb,UAAU,IAAI,CAAC,aAAa;IACzC,qBACC,kDACC,GAJM,KAID,KAAK;;;AAGb\"}")
/*
{
"origin": "test.tsx",
"name": "test_component_LUXeXe0DQrg",
"entry": null,
"displayName": "test.tsx_test_component",
"hash": "LUXeXe0DQrg",
"canonicalFilename": "test.tsx_test_component_LUXeXe0DQrg",
"path": "",
"extension": "js",
"parent": null,
"ctxKind": "function",
"ctxName": "component$",
"captures": false,
"loc": [
88,
237
]
}
*/
== DIAGNOSTICS ==

[]
44 changes: 44 additions & 0 deletions packages/qwik/src/optimizer/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3731,6 +3731,50 @@ fn destructure_args_colon_props() {
});
}

#[test]
fn destructure_args_colon_props2() {
test_input!(TestInput {
code: r#"
import { component$, useSignal } from "@qwik.dev/core";
export default component$((props) => {
const { 'bind:value': bindValue } = props;
const test = useSignal(bindValue);
return (
<>
{test.value}
</>
);
});
"#
.to_string(),
transpile_ts: true,
transpile_jsx: true,
..TestInput::default()
});
}

#[test]
fn destructure_args_colon_props3() {
test_input!(TestInput {
code: r#"
import { component$, useSignal } from "@qwik.dev/core";
export default component$((props) => {
const { test, ...rest } = props;
const test = useSignal(rest['bind:value']);
return (
<>
{test.value}
</>
);
});
"#
.to_string(),
transpile_ts: true,
transpile_jsx: true,
..TestInput::default()
});
}

#[test]
fn should_handle_dangerously_set_inner_html() {
test_input!(TestInput {
Expand Down

0 comments on commit d8a19ca

Please sign in to comment.