Skip to content

Commit

Permalink
Add warning for redeclared computed properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Jan 30, 2019
1 parent 63dadbf commit 007f3cc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/mixins/element-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion test/unit/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,6 @@
suite('warn on legacy differences', () => {

setup(function() {
setLegacyOptimizations(true);
sinon.spy(console, 'warn');
});

Expand All @@ -1887,6 +1886,7 @@
});

test('warn if non-declared property used in binding', () => {
setLegacyOptimizations(true);
Polymer({
is: 'x-warn-undeclared-binding',
_template: html`
Expand All @@ -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);
});

});
</script>

Expand Down

0 comments on commit 007f3cc

Please sign in to comment.