Skip to content

Commit

Permalink
fix: cached values inside attribute tags
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Aug 7, 2024
1 parent e982500 commit 97ec7db
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nine-pumas-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/tags-api-preview": patch
---

Fix issue with cached values inside attribute tags.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div
data-key="hello"
name="Frank"
>
Hello Frank!
</div>
<div
data-key="hello"
name="Frank"
>
Hello Frank!
</div>
<div
data-key="hello"
name="John"
>
Hello John!
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div
data-key="hello"
name="Frank"
>
Hello Frank!
</div>
<div
data-key="hello"
name="Frank"
>
Hello Frank!
</div>
<div
data-key="hello"
name="John"
>
Hello John!
</div>
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/misc/attribute-tags/components/test.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- use tags -->
<for|key, value| in=input>
<if=value>
<if=Array.isArray(value)>
<for|item| of=value>
<div data-key=key ...item>
<${item}/>
</div>
</for>
</if>
<else>
<div data-key=key ...value>
<${value}/>
</div>
</else>
</if>
</for>
5 changes: 5 additions & 0 deletions src/__tests__/fixtures/misc/attribute-tags/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fixture from "../../../fixture";

describe("misc attribute tags", () => {
describe("single", fixture("./templates/single.marko"));
});
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/misc/attribute-tags/templates/single.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- use tags -->
<test>
<@hello name="Frank" onClick() {
console.log('Hello Frank!');
}>
Hello Frank!
</@hello>
</test>
<test>
<for|name| of=["Frank", "John"]>
<@hello name=name onClick() {
console.log('Hello ' + name + '!');
}>
Hello ${name}!
</@hello>
</for>
</test>
32 changes: 31 additions & 1 deletion src/transform/wrapper-component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { types as t } from "@marko/compiler";
import { isNativeTag, getTagDef } from "@marko/babel-utils";
import {
isNativeTag,
isTransparentTag,
isAttributeTag,
findParentTag,
getTagDef,
} from "@marko/babel-utils";
import { taglibId } from "../util/taglib-id";
import isApi from "../util/is-api";

Expand Down Expand Up @@ -96,6 +102,14 @@ export function ensureLifecycle(tag: t.NodePath<t.MarkoTag>, client = true) {
isNativeTag(root as t.NodePath<t.MarkoTag>)
);

if (isAttributeTagLike(tag)) {
if (isTransparentTag(root as t.NodePath<t.MarkoTag>)) {
root = findParentTag(root as t.NodePath<t.MarkoTag>) || program;
}

root = findParentTag(root as t.NodePath<t.MarkoTag>) || program;
}

if (root.node) {
const roots = lifecycleRootsForProgram.get(program)!;
let extra = root.node.extra;
Expand Down Expand Up @@ -182,3 +196,19 @@ function buildNestedLifecycle(tag: t.NodePath<t.MarkoTag>): t.Statement[] {

return result;
}

function isAttributeTagLike(tag: t.NodePath<t.MarkoTag>) {
if (isAttributeTag(tag)) {
return true;
}

if (isTransparentTag(tag)) {
for (const child of tag.get("body").get("body")) {
if (child.isMarkoTag() && isAttributeTagLike(child)) {
return true;
}
}
}

return false;
}

0 comments on commit 97ec7db

Please sign in to comment.