Skip to content

Commit cd2b725

Browse files
committed
fix: non standard tmpl literals inside legacy dynamic attrs
1 parent ef1dc75 commit cd2b725

File tree

12 files changed

+65
-7
lines changed

12 files changed

+65
-7
lines changed

.changeset/orange-rice-hope.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"marko-widgets": patch
3+
"@marko/compat-v4": patch
4+
---
5+
6+
Fix issue where non standard template literals were not transformed when used inside of the legacy dynamic attributes syntax.

packages/compat-v4/src/migrate/dynamic-attributes.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { types as t } from "@marko/compiler";
22
import { diagnosticDeprecate, parseExpression } from "@marko/babel-utils";
3+
import { migrateNonStandardTemplateLiterals } from "./non-standard-template-literals";
34

45
export default {
56
MarkoAttribute(attr) {
@@ -15,7 +16,7 @@ export default {
1516
label:
1617
'The "<tag ${attributes}>" syntax is deprecated. Please use "...attributes" instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-w‐*-Attributes',
1718
fix() {
18-
attr.replaceWith(
19+
const [replacement] = attr.replaceWith(
1920
t.markoSpreadAttribute(
2021
start != null && end != null
2122
? parseExpression(
@@ -27,6 +28,8 @@ export default {
2728
: parseExpression(attr.hub.file, name.slice(2, -1)),
2829
),
2930
);
31+
32+
migrateNonStandardTemplateLiterals(replacement.get("value"));
3033
},
3134
});
3235
}

packages/compat-v4/src/migrate/non-standard-template-literals/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ export default {
1616
for (const arg of tag.get("arguments") as t.NodePath<
1717
t.Expression | t.SpreadElement
1818
>[]) {
19-
checkPath(arg);
19+
migrateNonStandardTemplateLiterals(arg);
2020
}
2121
}
2222

2323
for (const attr of tag.get("attributes")) {
24-
checkPath(attr.get("value"));
24+
migrateNonStandardTemplateLiterals(attr.get("value"));
2525

2626
if (attr.isMarkoAttribute()) {
2727
if (attr.node.arguments) {
2828
for (const arg of attr.get("arguments") as t.NodePath<
2929
t.Expression | t.SpreadElement
3030
>[]) {
31-
checkPath(arg);
31+
migrateNonStandardTemplateLiterals(arg);
3232
}
3333
}
3434
}
3535
}
3636
},
3737
} satisfies t.Visitor;
3838

39-
function checkPath(path: t.NodePath<t.Node>) {
39+
export function migrateNonStandardTemplateLiterals(path: t.NodePath<t.Node>) {
4040
if (path.isStringLiteral()) {
4141
StringLiteral(path);
4242
} else {

tests/fixtures-class/non-standard-template-literals/__snapshots__/auto-migrate.expected/template.marko

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ $ const fromScriptlet = "${SCRIPLET}";
66
$ const a = 1;
77
$ const b = 2;
88
$ const c = 3;
9+
$ const d = "d";
910
<div class=a id=`${a}` data-other=b/>
1011
<div id="a">
1112
${fromStatic}
@@ -39,5 +40,7 @@ $ const c = 3;
3940
<div id="j">
4041
${'\${abc}'}
4142
</div>
43+
<div ...a id="c"/>
44+
<div ...d id="c"/>
4245
$ const handler = console.log;
4346
<button onClick(handler)/>

tests/fixtures-class/non-standard-template-literals/__snapshots__/dom.expected.md

+7
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@
5454
>
5555
${abc}
5656
</div>
57+
<div
58+
id="c"
59+
/>
60+
<div
61+
d=""
62+
id="c"
63+
/>
5764
<button />
5865
```

tests/fixtures-class/non-standard-template-literals/__snapshots__/dom.expected/template.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function _toString(value) {
77
}
88
const fromStatic = "${STATIC}";
99
import _marko_class_merge from "marko/src/runtime/helpers/class-value.js";
10+
import _marko_merge_attrs from "marko/src/runtime/vdom/helpers/merge-attrs.js";
1011
import _marko_renderer from "marko/src/runtime/components/renderer.js";
1112
import { r as _marko_registerComponent } from "marko/src/runtime/components/registry.js";
1213
_marko_registerComponent(_marko_componentType, () => _marko_template);
@@ -16,6 +17,7 @@ _marko_template._ = _marko_renderer(function (input, out, _componentDef, _compon
1617
const a = 1;
1718
const b = 2;
1819
const c = 3;
20+
const d = "d";
1921
out.e("div", {
2022
"class": _marko_class_merge(a),
2123
"id": `${a}`,
@@ -73,8 +75,14 @@ _marko_template._ = _marko_renderer(function (input, out, _componentDef, _compon
7375
}, "10", _component, null, 1);
7476
out.t('\${abc}', _component);
7577
out.ee();
78+
out.e("div", _marko_merge_attrs(a, {
79+
"id": "c"
80+
}), "11", _component, 0, 4);
81+
out.e("div", _marko_merge_attrs(d, {
82+
"id": "c"
83+
}), "12", _component, 0, 4);
7684
const handler = console.log;
77-
out.e("button", null, "11", _component, 0, 0, {
85+
out.e("button", null, "13", _component, 0, 0, {
7886
"onclick": _componentDef.d("click", handler, false)
7987
});
8088
}, {

tests/fixtures-class/non-standard-template-literals/__snapshots__/html.expected.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write
2-
<!--M#s0-0--><div id="1" data-other=2></div><div id=a>${STATIC}</div><div id=b>${SCRIPLET}</div><div id=c>1</div><div id=d>abc}</div><div id=e>abc}</div><div id=f>abc}</div><div id=g>abcd}ef</div><div id=h>abc3</div><div id=i>abcdef</div><div id=j>${abc}</div><button></button><!--M/--><script>$MC=(window.$MC||[]).concat({"w":[["s0-0",0,{"renderBody":null},{"f":1,"r":null}]],"t":["<fixture-dir>/template.marko"]})</script>
2+
<!--M#s0-0--><div id="1" data-other=2></div><div id=a>${STATIC}</div><div id=b>${SCRIPLET}</div><div id=c>1</div><div id=d>abc}</div><div id=e>abc}</div><div id=f>abc}</div><div id=g>abcd}ef</div><div id=h>abc3</div><div id=i>abcdef</div><div id=j>${abc}</div><div id=c></div><div id=c d></div><button></button><!--M/--><script>$MC=(window.$MC||[]).concat({"w":[["s0-0",0,{"renderBody":null},{"f":1,"r":null}]],"t":["<fixture-dir>/template.marko"]})</script>
33

44
# Render
55
```html
@@ -57,5 +57,12 @@
5757
>
5858
${abc}
5959
</div>
60+
<div
61+
id="c"
62+
/>
63+
<div
64+
d=""
65+
id="c"
66+
/>
6067
<button />
6168
```

tests/fixtures-class/non-standard-template-literals/__snapshots__/html.expected/template.js

+8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import _marko_class_merge from "marko/src/runtime/helpers/class-value.js";
1010
import _marko_attr from "marko/src/runtime/html/helpers/attr.js";
1111
import { d as _marko_escape_double_quotes } from "marko/src/runtime/html/helpers/escape-quotes.js";
1212
import { x as _marko_escapeXml } from "marko/src/runtime/html/helpers/escape-xml.js";
13+
import _marko_merge_attrs from "marko/src/runtime/html/helpers/merge-attrs.js";
1314
import _marko_renderer from "marko/src/runtime/components/renderer.js";
1415
const _marko_component = {};
1516
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
1617
const fromScriptlet = "${SCRIPLET}";
1718
const a = 1;
1819
const b = 2;
1920
const c = 3;
21+
const d = "d";
2022
out.w(`<div${_marko_attr("class", _marko_class_merge(a))} id="${_marko_escape_double_quotes(a)}"${_marko_attr("data-other", b)}></div>`);
2123
out.w("<div id=a>");
2224
out.w(_marko_escapeXml(fromStatic));
@@ -50,6 +52,12 @@ _marko_template._ = _marko_renderer(function (input, out, _componentDef, _compon
5052
out.w("<div id=j>");
5153
out.w("${abc}");
5254
out.w("</div>");
55+
out.w(`<div${_marko_merge_attrs(a, {
56+
"id": "c"
57+
})}></div>`);
58+
out.w(`<div${_marko_merge_attrs(d, {
59+
"id": "c"
60+
})}></div>`);
5361
const handler = console.log;
5462
out.w("<button></button>");
5563
}, {

tests/fixtures-class/non-standard-template-literals/__snapshots__/hydrate.expected.md

+7
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@
5454
>
5555
${abc}
5656
</div>
57+
<div
58+
id="c"
59+
/>
60+
<div
61+
d=""
62+
id="c"
63+
/>
5764
<button />
5865
```

tests/fixtures-class/non-standard-template-literals/__snapshots__/manual-migrate-0.expected/template.marko

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ $ const fromScriptlet = "${SCRIPLET}";
33
$ const a = 1;
44
$ const b = 2;
55
$ const c = 3;
6+
$ const d = "d";
67
<div class="${a}" id=`${"${a}"}` data-other="${b}"/>
78
<div id="a">
89
${fromStatic}
@@ -34,5 +35,7 @@ $ const c = 3;
3435
<div id="j">
3536
${'\${abc}'}
3637
</div>
38+
<div ${"${a}"} id="c"/>
39+
<div ${"${d}"} id="c"/>
3740
$ const handler = console.log;
3841
<button onClick("${handler}")/>

tests/fixtures-class/non-standard-template-literals/__snapshots__/manual-migrate-1.expected/template.marko

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ $ const fromScriptlet = "${SCRIPLET}";
33
$ const a = 1;
44
$ const b = 2;
55
$ const c = 3;
6+
$ const d = "d";
67
<div class="${a}" id=`${"${a}"}` data-other="${b}"/>
78
<div id="a">
89
${fromStatic}
@@ -36,5 +37,7 @@ $ const c = 3;
3637
<div id="j">
3738
${'\${abc}'}
3839
</div>
40+
<div ${"${a}"} id="c"/>
41+
<div ${"${d}"} id="c"/>
3942
$ const handler = console.log;
4043
<button onClick("${handler}")/>

tests/fixtures-class/non-standard-template-literals/template.marko

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ $ const fromScriptlet = "${SCRIPLET}";
33
$ const a = 1;
44
$ const b = 2;
55
$ const c = 3;
6+
$ const d = "d";
67
<div class="${a}" id=`${"${a}"}` data-other="${b}"/>
78
<#a>${fromStatic}</>
89
<#b>${fromScriptlet}</>
@@ -14,6 +15,8 @@ $ const c = 3;
1415
<#h>${"abc${c}"}</>
1516
<#i>${"abc${{x: 1}.missing}def"}</>
1617
<#j>${'\${abc}'}</>
18+
<#c ${"${a}"}></>
19+
<#c ${"${d}"}></>
1720

1821
$ const handler = console.log;
1922
<button onClick("${handler}")/>

0 commit comments

Comments
 (0)