Skip to content

Commit 81a9316

Browse files
committed
fix: better attr tags handling
1 parent 8503f18 commit 81a9316

File tree

16 files changed

+998
-1242
lines changed

16 files changed

+998
-1242
lines changed

.changeset/orange-needles-brush.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 compat with latest @marko/compiler attribute tags changes.

package-lock.json

+822-1,166
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+23-23
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,41 @@
2323
"version": "changeset version && npm i --package-lock-only"
2424
},
2525
"devDependencies": {
26-
"@babel/plugin-transform-modules-commonjs": "^7.22.5",
27-
"@babel/preset-typescript": "^7.22.5",
28-
"@babel/register": "^7.22.5",
29-
"@changesets/changelog-github": "^0.4.8",
30-
"@changesets/cli": "^2.26.2",
31-
"@marko/compiler": "^5.37.11",
26+
"@babel/plugin-transform-modules-commonjs": "^7.25.9",
27+
"@babel/preset-typescript": "^7.26.0",
28+
"@babel/register": "^7.25.9",
29+
"@changesets/changelog-github": "^0.5.0",
30+
"@changesets/cli": "^2.27.9",
31+
"@marko/compiler": "^5.37.24",
3232
"@testing-library/dom": "^9.3.1",
33-
"@types/babel__register": "^7.17.0",
34-
"@types/jsdom": "^21.1.1",
35-
"@types/mocha": "^10.0.1",
33+
"@types/babel__register": "^7.17.3",
34+
"@types/jsdom": "^21.1.7",
35+
"@types/mocha": "^10.0.9",
3636
"@types/node": "^20.4.10",
37-
"@types/resolve": "^1.20.2",
37+
"@types/resolve": "^1.20.6",
3838
"@typescript-eslint/eslint-plugin": "^6.3.0",
3939
"@typescript-eslint/parser": "^6.3.0",
4040
"c8": "^8.0.1",
41-
"context-require": "^1.4.0",
42-
"esbuild": "^0.19.1",
41+
"context-require": "^1.4.2",
42+
"esbuild": "^0.24.0",
4343
"eslint": "^8.47.0",
44-
"eslint-config-prettier": "^9.0.0",
45-
"eslint-import-resolver-typescript": "^3.6.0",
46-
"eslint-plugin-import": "^2.28.0",
44+
"eslint-config-prettier": "^9.1.0",
45+
"eslint-import-resolver-typescript": "^3.6.3",
46+
"eslint-plugin-import": "^2.31.0",
4747
"husky": "^8.0.3",
4848
"jsdom": "^22.1.0",
49-
"lint-staged": "^13.2.3",
50-
"marko": "^5.35.16",
51-
"mocha": "^10.2.0",
49+
"lint-staged": "^15.2.10",
50+
"marko": "^5.35.33",
51+
"mocha": "^10.8.2",
5252
"mocha-snap": "^5.0.0",
53-
"prettier": "^3.0.1",
54-
"pretty-format": "^29.6.2",
53+
"prettier": "^3.3.3",
54+
"pretty-format": "^29.7.0",
5555
"replace": "^1.2.2",
56-
"resolve": "^1.22.4",
56+
"resolve": "^1.22.8",
5757
"resolve.exports": "^2.0.2",
58-
"sort-package-json": "^2.5.1",
58+
"sort-package-json": "^2.10.1",
5959
"tsconfig-paths": "^4.2.0",
60-
"typescript": "^5.1.6",
60+
"typescript": "^5.6.3",
6161
"~preload": "file:scripts/preload.js"
6262
}
6363
}

packages/compat-v4/src/components/await/migrate.ts

+26-13
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ export default {
144144
);
145145
}
146146

147-
const rootChildren: t.MarkoTagBody["body"] = [];
147+
const rootAttrTags: t.MarkoTag["attributeTags"] = [];
148148

149149
if (thenChildren.length) {
150-
rootChildren.push(
150+
rootAttrTags.push(
151151
t.markoTag(
152152
t.stringLiteral("@then"),
153153
[],
@@ -157,7 +157,7 @@ export default {
157157
}
158158

159159
if (placeholderChildren.length) {
160-
rootChildren.push(
160+
rootAttrTags.push(
161161
t.markoTag(
162162
t.stringLiteral("@placeholder"),
163163
[],
@@ -179,7 +179,7 @@ export default {
179179
),
180180
],
181181
);
182-
rootChildren.push(
182+
rootAttrTags.push(
183183
t.markoTag(
184184
t.stringLiteral("@catch"),
185185
[],
@@ -199,7 +199,7 @@ export default {
199199
),
200200
);
201201
} else if (errorChildren.length) {
202-
rootChildren.push(
202+
rootAttrTags.push(
203203
t.markoTag(
204204
t.stringLiteral("@catch"),
205205
[],
@@ -208,14 +208,27 @@ export default {
208208
);
209209
}
210210

211-
tag.replaceWith(
212-
t.markoTag(
213-
t.stringLiteral("await"),
214-
tag.node.attributes,
215-
t.markoTagBody(rootChildren),
216-
[providerExpression],
217-
),
218-
);
211+
if (tag.node.attributeTags) {
212+
tag.replaceWith(
213+
t.markoTag(
214+
t.stringLiteral("await"),
215+
tag.node.attributes,
216+
t.markoTagBody([]),
217+
[providerExpression],
218+
undefined,
219+
rootAttrTags,
220+
),
221+
);
222+
} else {
223+
tag.replaceWith(
224+
t.markoTag(
225+
t.stringLiteral("await"),
226+
tag.node.attributes,
227+
t.markoTagBody(rootAttrTags),
228+
[providerExpression],
229+
),
230+
);
231+
}
219232
},
220233
});
221234
},

packages/compat-v4/src/components/for/migrate.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export default {
273273
t.markoTag(
274274
t.stringLiteral("for"),
275275
[t.markoAttribute(valueAttr, inExpression!)],
276-
t.markoTagBody(node.body.body, params),
276+
markoTagBody(node.body.body, params, node.body.attributeTags),
277277
),
278278
);
279279
},
@@ -384,7 +384,7 @@ export default {
384384
t.markoTag(
385385
t.stringLiteral("for"),
386386
attrs,
387-
t.markoTagBody(node.body.body, params),
387+
markoTagBody(node.body.body, params, node.body.attributeTags),
388388
),
389389
);
390390
},
@@ -471,18 +471,24 @@ export default {
471471
t.markoTag(
472472
t.stringLiteral("for"),
473473
attrs,
474-
t.markoTagBody(node.body.body, [forRange.varName]),
474+
markoTagBody(
475+
node.body.body,
476+
[forRange.varName],
477+
node.body.attributeTags,
478+
),
475479
),
476480
);
477481
} else {
478482
const whileTag = t.markoTag(
479483
t.stringLiteral("while"),
480484
[],
481485
updateNode
482-
? t.markoTagBody(
486+
? markoTagBody(
483487
node.body.body.concat(
484488
t.markoScriptlet([t.expressionStatement(updateNode)]),
485489
),
490+
undefined,
491+
node.body.attributeTags,
486492
)
487493
: node.body,
488494
[testNode || t.booleanLiteral(true)],
@@ -599,6 +605,19 @@ function forInitToRange(
599605
};
600606
}
601607

608+
function markoTagBody(
609+
body: t.MarkoTagBody["body"],
610+
params?: t.MarkoTagBody["params"],
611+
attributeTags?: t.MarkoTagBody["attributeTags"],
612+
) {
613+
const node = t.markoTagBody(body, params);
614+
if (attributeTags) {
615+
node.attributeTags = true;
616+
}
617+
618+
return node;
619+
}
620+
602621
function isIdentifierNamed(node: t.Node, name: string) {
603622
return node.type === "Identifier" && node.name === name;
604623
}

packages/compat-v4/src/components/include/migrate.ts

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export default {
6161
tagNameExpression,
6262
exprToAttrs(inputExpression).concat(tag.node.attributes),
6363
tag.node.body,
64+
undefined,
65+
undefined,
66+
tag.node.attributeTags,
6467
),
6568
);
6669
} else {
@@ -85,6 +88,9 @@ export default {
8588
tagNameExpression,
8689
exprToAttrs(inputExpression).concat(tag.node.attributes),
8790
tag.node.body,
91+
undefined,
92+
undefined,
93+
tag.node.attributeTags,
8894
),
8995
]),
9096
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { isAttributeTag, isTransparentTag } from "@marko/babel-utils";
2+
import { types as t } from "@marko/compiler";
3+
4+
export default {
5+
Program: {
6+
exit(program) {
7+
for (const child of program.get("body")) {
8+
if (child.isMarkoTag()) {
9+
hoistAttrTags(child);
10+
}
11+
}
12+
},
13+
},
14+
} satisfies t.Visitor;
15+
16+
function hoistAttrTags(tag: t.NodePath<t.MarkoTag>) {
17+
let hadAttrTags = false;
18+
const isControlFlow = isTransparentTag(tag);
19+
const body = tag.get("body");
20+
for (const child of body.get("body")) {
21+
if (child.isMarkoTag()) {
22+
const hoistedChildren = hoistAttrTags(child);
23+
if (
24+
isAttributeTag(child) ||
25+
(isTransparentTag(child) && hoistedChildren)
26+
) {
27+
if (!isControlFlow) {
28+
tag.pushContainer("attributeTags", child.node);
29+
child.remove();
30+
}
31+
hadAttrTags = true;
32+
}
33+
}
34+
}
35+
36+
if (isControlFlow && hadAttrTags) {
37+
body.node.attributeTags = true;
38+
}
39+
40+
return hadAttrTags;
41+
}

packages/compat-v4/src/migrate/if-directive.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { types as t } from "@marko/compiler";
2-
import { diagnosticDeprecate } from "@marko/babel-utils";
2+
import { diagnosticDeprecate, isAttributeTag } from "@marko/babel-utils";
33
import { willMigrateAllAttrs } from "@marko/compat-utils";
44

55
export default {
@@ -44,14 +44,9 @@ export default {
4444
label: `The "${name}" directive is deprecated. Please use "<${name}>" tag instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-control-flow-attributes`,
4545
fix() {
4646
attr.remove();
47-
tag.replaceWith(
48-
t.markoTag(
49-
t.stringLiteral(name),
50-
[],
51-
t.markoTagBody([tag.node]),
52-
args,
53-
),
54-
);
47+
const body = t.markoTagBody([tag.node]);
48+
if (isAttributeTag(tag)) body.attributeTags = true;
49+
tag.replaceWith(t.markoTag(t.stringLiteral(name), [], body, args));
5550
},
5651
});
5752
},

packages/compat-v4/src/migrate/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import keyModifier from "./key-modifier";
1111
import refAttribute from "./ref-attribute";
1212
import nativeTagNamespacedAttribute from "./native-tag-namespaced-attribute";
1313
import tagArguments from "./tag-arguments";
14+
import hoistAttrTags from "./hoist-attr-tags";
1415

1516
export default [
1617
ifDirective,
@@ -25,4 +26,5 @@ export default [
2526
nonStandardTemplateLiterals,
2627
nativeTagNamespacedAttribute,
2728
tagArguments,
29+
hoistAttrTags,
2830
] satisfies Visitor[];

packages/compat-v4/src/migrate/legacy-attribute-tags.ts

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default {
5858
node.body,
5959
node.arguments,
6060
node.var,
61+
node.attributeTags,
6162
),
6263
);
6364
},

packages/compat-v4/src/migrate/loop-directive.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { types as t } from "@marko/compiler";
2-
import { diagnosticDeprecate } from "@marko/babel-utils";
2+
import { diagnosticDeprecate, isAttributeTag } from "@marko/babel-utils";
33
import { willMigrateAllAttrs } from "@marko/compat-utils";
44

55
export default {
@@ -24,14 +24,9 @@ export default {
2424
label: `The "${name}(x)" directive is deprecated. Please use "<${name}>" tag instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-control-flow-attributes`,
2525
fix() {
2626
attr.remove();
27-
tag.replaceWith(
28-
t.markoTag(
29-
t.stringLiteral(name),
30-
[],
31-
t.markoTagBody([tag.node]),
32-
args,
33-
),
34-
);
27+
const body = t.markoTagBody([tag.node]);
28+
if (isAttributeTag(tag)) body.attributeTags = true;
29+
tag.replaceWith(t.markoTag(t.stringLiteral(name), [], body, args));
3530
},
3631
});
3732
},

0 commit comments

Comments
 (0)