Skip to content

Commit

Permalink
fix: issue with bound let always operating as controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Nov 1, 2021
1 parent 417732a commit aaff1d8
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
2
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
2
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
1
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
2
</div>
<button>
Increment
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
3
</div>
<button>
Increment
</button>
12 changes: 11 additions & 1 deletion src/__tests__/fixtures/let/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe(
);

describe(
"<let> bound",
"<let> bound controlled",
fixture("./templates/bound.marko", [
{ value: 1, valueChange() {} },
increment,
Expand All @@ -21,6 +21,16 @@ describe(
])
);

describe(
"<let> bound uncontrolled",
fixture("./templates/bound.marko", [
{ value: 1 },
{ value: 2 },
increment,
increment,
])
);

describe(
"<let> read assignment",
fixture("./templates/read-assignment.marko", [
Expand Down
18 changes: 10 additions & 8 deletions src/transform/attribute-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ const AttributeVisitor = {
}

const tempId = t.identifier("_");
attr.node.bound = false;
attr.insertAfter(
t.markoAttribute(
`${attr.node.name}Change`,
t.arrowFunctionExpression(
[tempId],
t.assignmentExpression("=", value.node, tempId)
)

const changeAttr = t.markoAttribute(
`${attr.node.name}Change`,
t.arrowFunctionExpression(
[tempId],
t.assignmentExpression("=", value.node, tempId)
)
);

attr.node.bound = false;
changeAttr.extra = { ___wasBound: true };
attr.insertAfter(changeAttr);
},
} as t.Visitor;

Expand Down
23 changes: 17 additions & 6 deletions src/util/replace-assignments/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ export default function replaceAssignments(
}

if (value) {
assignment.replaceWith(
t.callExpression(importDefault(file, __dirname, "assign"), [
fnExpression,
value,
])
);
const parent = assignment.parentPath!;
if (
// If the assignment was from a bound attribute
// we just replace the attr value with the change function.
parent.isFunction() &&
parent.parentPath!.isMarkoAttribute() &&
parent.parentPath!.node.extra?.___wasBound
) {
parent.replaceWith(fnExpression);
} else {
assignment.replaceWith(
t.callExpression(importDefault(file, __dirname, "assign"), [
fnExpression,
value,
])
);
}
}
}
}

0 comments on commit aaff1d8

Please sign in to comment.