Skip to content

Commit

Permalink
🐛 Prevents Setting from registering a dependency (#4081)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekwoka authored Mar 8, 2024
1 parent cfcf718 commit 28d1206
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/alpinejs/src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ let mergeProxyTrap = {
if (name == Symbol.unscopables) return false;

return objects.some((obj) =>
Object.prototype.hasOwnProperty.call(obj, name) ||
Reflect.has(obj, name)
);
},
Expand All @@ -65,8 +66,9 @@ let mergeProxyTrap = {
},

set({ objects }, name, value, thisProxy) {
const target = objects.find((obj) =>
Reflect.has(obj, name)
const target =
objects.find((obj) =>
Object.prototype.hasOwnProperty.call(obj, name)
) || objects[objects.length - 1];
const descriptor = Object.getOwnPropertyDescriptor(target, name);
if (descriptor?.set && descriptor?.get)
Expand Down
30 changes: 27 additions & 3 deletions tests/cypress/integration/scope.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ test(
this.value++;
}
}
document.addEventListener("alpine:init", () =>
document.addEventListener("alpine:init", () =>
Alpine.data("counter", () => new Counter())
)
);
</script>
<div x-data="counter">
<button type="button" @click="increment" x-text=value></button>
<button
type="button"
@click="increment"
x-text="value"
></button>
</div>
`,
],
Expand All @@ -85,3 +89,23 @@ test(
}
);

test(
"setting value doesn't register a dependency",
[
html`
<div x-data="{ message: 'original' }">
<button
x-effect="message = 'effected'"
@click="message = 'clicked'"
x-text="message"
></button>
</div>
;
`,
],
({ get }) => {
get("button").should(haveText("effected"));
get("button").click();
get("button").should(haveText("clicked"));
}
);

0 comments on commit 28d1206

Please sign in to comment.