Skip to content

Commit

Permalink
Merge pull request #6 from marko-js/el-refs-on-dynamic-tag
Browse files Browse the repository at this point in the history
fix: add support for element refs on dynamic tags
  • Loading branch information
DylanPiercey authored Oct 21, 2021
2 parents 4daa2a9 + 02b7d3e commit 2907aa2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello
</div>
21 changes: 21 additions & 0 deletions src/__tests__/fixtures/misc/dynamic-tag-var/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { spy, resetHistory } from "sinon";
import fixture from "../../../fixture";

const onValue = spy();

describe("misc dynamic tag vars", () => {
describe(
"basic",
fixture("./templates/basic.marko", [
{ onValue },
({ expect }) => {
expect(onValue)
.to.be.calledOnce.with.property("args")
.with.property("0")
.with.property("0")
.that.has.text("Hello");
resetHistory();
},
])
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<attrs/{ onValue }/>
<effect=(() => onValue(el()))/>
<const/name="div"/>
<${name}/el>Hello</>
7 changes: 5 additions & 2 deletions src/transform/cached-function/transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { importNamed, isNativeTag } from "@marko/babel-utils";
import { importNamed, isNativeTag, isDynamicTag } from "@marko/babel-utils";
import { types as t } from "@marko/compiler";
import { Visitor } from "@marko/compiler/babel-types";
import { ensureLifecycle } from "../wrapper-component";
Expand All @@ -16,7 +16,10 @@ const depsVisitor = {
if (binding) {
const bindingTag = binding.path;

if (bindingTag.isMarkoTag() && !isNativeTag(bindingTag)) {
if (
bindingTag.isMarkoTag() &&
!(isNativeTag(bindingTag) || isDynamicTag(bindingTag))
) {
let isDep = false;

if (isCoreTag("const", binding.path)) {
Expand Down
6 changes: 3 additions & 3 deletions src/transform/native-tag-var/transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { types as t } from "@marko/compiler";
import { importDefault, isNativeTag } from "@marko/babel-utils";
import { importDefault, isNativeTag, isDynamicTag } from "@marko/babel-utils";
import { closest } from "../wrapper-component";

export default {
Expand All @@ -11,7 +11,7 @@ export default {
} = tag;
const tagVar = node.var!;

if (!tagVar || !isNativeTag(tag)) {
if (!tagVar || !(isNativeTag(tag) || isDynamicTag(tag))) {
return;
}

Expand Down Expand Up @@ -42,7 +42,7 @@ export default {
tag.pushContainer("attributes", t.markoAttribute("key", keyString));
},
exit(tag) {
if (tag.node.var && isNativeTag(tag)) {
if (tag.node.var && (isNativeTag(tag) || isDynamicTag(tag))) {
// Don't remove the tag variable until it has been hoisted.
tag.node.var = null;
}
Expand Down

0 comments on commit 2907aa2

Please sign in to comment.