From 007f3cc251c504e7d68edf60e8318890444a56de Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Wed, 30 Jan 2019 10:06:28 -0800 Subject: [PATCH] Add warning for redeclared computed properties. --- lib/mixins/element-mixin.js | 8 ++++++-- test/unit/property-effects.html | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/mixins/element-mixin.js b/lib/mixins/element-mixin.js index 84c590686a..e37f066609 100644 --- a/lib/mixins/element-mixin.js +++ b/lib/mixins/element-mixin.js @@ -217,8 +217,12 @@ export const ElementMixin = dedupingMixin(base => { // setup where multiple triggers for setting a property) // While we do have `hasComputedEffect` this is set on the property's // dependencies rather than itself. - if (info.computed && !proto._hasReadOnlyEffect(name)) { - proto._createComputedProperty(name, info.computed, allProps); + if (info.computed) { + if (proto._hasReadOnlyEffect(name)) { + console.warn(`Cannot redefine computed property '${name}'.`) + } else { + proto._createComputedProperty(name, info.computed, allProps); + } } if (info.readOnly && !proto._hasReadOnlyEffect(name)) { proto._createReadOnlyProperty(name, !info.computed); diff --git a/test/unit/property-effects.html b/test/unit/property-effects.html index 0b8226bb8b..e400b85b3c 100644 --- a/test/unit/property-effects.html +++ b/test/unit/property-effects.html @@ -1877,7 +1877,6 @@ suite('warn on legacy differences', () => { setup(function() { - setLegacyOptimizations(true); sinon.spy(console, 'warn'); }); @@ -1887,6 +1886,7 @@ }); test('warn if non-declared property used in binding', () => { + setLegacyOptimizations(true); Polymer({ is: 'x-warn-undeclared-binding', _template: html` @@ -1900,6 +1900,25 @@ assert.equal(console.warn.callCount, 3); }); + test('warn when re-declaring a computed property', () => { + Polymer({ + is: 'x-warn-redeclared-computed', + behaviors: [{ + properties: { + a: { computed: 'compute(x)' } + } + }, { + properties: { + a: { computed: 'compute(y)' } + } + }] + }); + const el = document.createElement('x-warn-redeclared-computed'); + document.body.appendChild(el); + document.body.removeChild(el); + assert.equal(console.warn.callCount, 1); + }); + });