From b97fd5938125ba9db7120e2f9f99b278e8d7cab0 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sat, 29 Jan 2022 13:16:04 +0100 Subject: [PATCH] Warn about reusing mutated attrs object - fixes #2719 --- render/render.js | 3 +++ render/tests/test-attributes.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/render/render.js b/render/render.js index 5d5da438..9f1dae3e 100644 --- a/render/render.js +++ b/render/render.js @@ -800,6 +800,9 @@ module.exports = function($window) { if ("selectedIndex" in attrs) setAttr(vnode, "selectedIndex", null, attrs.selectedIndex, undefined) } function updateAttrs(vnode, old, attrs, ns) { + if (old && old === attrs) { + console.warn("Don't reuse attrs object, use new object for every redraw, this will throw in next major") + } if (attrs != null) { // If you assign an input type that is not supported by IE 11 with an assignment expression, an error will occur. // diff --git a/render/tests/test-attributes.js b/render/tests/test-attributes.js index 14a3b406..7df14578 100644 --- a/render/tests/test-attributes.js +++ b/render/tests/test-attributes.js @@ -756,4 +756,21 @@ o.spec("attributes", function() { o(succeeded).equals(true) }) }) + o.spec("mutate attr object", function() { + o("warn when reusing attrs object", function() { + const _consoleWarn = console.warn + console.warn = o.spy() + + const attrs = {className: "on"} + render(root, {tag: "input", attrs}) + + attrs.className = "off" + render(root, {tag: "input", attrs}) + + o(console.warn.callCount).equals(1) + o(console.warn.args[0]).equals("Don't reuse attrs object, use new object for every redraw, this will throw in next major") + + console.warn = _consoleWarn + }) + }) })