Skip to content

Commit

Permalink
fix: honor existing id with w-bind
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Aug 11, 2023
1 parent 1393fe0 commit dd221c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-deers-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"marko-widgets": patch
---

Fix issue where user provided id on an element with `w-bind` was being replaced.
37 changes: 22 additions & 15 deletions packages/marko-widgets/src/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "path";
import { importDefault, parseExpression } from "@marko/babel-utils";
import { types as t } from "@marko/compiler";
import { version } from "marko/package.json";
import { getAttribute } from "@marko/compat-utils";
import { getAttribute, hasAttribute } from "@marko/compat-utils";
import { optimizeHTMLWrites } from "./optmize-html-writes";

export default {
Expand Down Expand Up @@ -57,23 +57,30 @@ export default {
),
]),
);
wBindAttr.replaceWithMultiple([
t.markoAttribute("key", wBindIdentifier),
t.markoAttribute(
"id",
t.logicalExpression(
"&&",
wBindIdentifier,
t.callExpression(
t.memberExpression(
componentInstanceIdentifier,
t.identifier("elId"),

if (hasAttribute(tag, "id")) {
wBindAttr.replaceWith(t.markoAttribute("key", wBindIdentifier));
} else {
wBindAttr.replaceWithMultiple([
t.markoAttribute("key", wBindIdentifier),
t.markoAttribute(
"id",
t.logicalExpression(
"&&",
wBindIdentifier,
t.callExpression(
t.memberExpression(
componentInstanceIdentifier,
t.identifier("elId"),
),
[wBindIdentifier],
),
[wBindIdentifier],
),
),
),
]);
]);
}
} else if (hasAttribute(tag, "id")) {
wBindAttr.replaceWith(t.markoAttribute("key", t.stringLiteral("_wbind")));
} else {
wBindAttr.replaceWithMultiple([
t.markoAttribute("key", t.stringLiteral("_wbind")),
Expand Down

0 comments on commit dd221c1

Please sign in to comment.