Skip to content

Commit

Permalink
[BUGFIX beta] assert for passing brace expanding deps to sum/max..
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Nov 14, 2017
1 parent c022509 commit 2b557dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/expand_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assert } from 'ember-debug';
@module @ember/object
*/

var END_WITH_EACH_REGEX = /\.@each$/;
const END_WITH_EACH_REGEX = /\.@each$/;

/**
Expands `pattern`, invoking `callback` for each expansion.
Expand Down
40 changes: 30 additions & 10 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import compare from '../compare';
import { isArray } from '../utils';
import { A as emberA } from '../system/native_array';

function reduceMacro(dependentKey, callback, initialValue) {
function reduceMacro(dependentKey, callback, initialValue, name) {
assert(
`Dependent key passed to Ember.computed.${name}() shouldn't contain brace expanding pattern.`,
!/[\[\]\{\}]/g.test(dependentKey)
);

let cp = new ComputedProperty(function() {
let arr = get(this, dependentKey);
if (arr === null || typeof arr !== 'object') { return initialValue; }
Expand Down Expand Up @@ -51,7 +56,11 @@ function arrayMacro(dependentKey, callback) {
return cp;
}

function multiArrayMacro(_dependentKeys, callback) {
function multiArrayMacro(_dependentKeys, callback, name) {
assert(
`Dependent keys passed to Ember.computed.${name}() shouldn't contain brace expanding pattern.`,
_dependentKeys.every((dependentKey)=> !/[\[\]\{\}]/g.test(dependentKey))
);
let dependentKeys = _dependentKeys.map(key => `${key}.[]`);

let cp = new ComputedProperty(function() {
Expand All @@ -74,7 +83,7 @@ function multiArrayMacro(_dependentKeys, callback) {
@public
*/
export function sum(dependentKey) {
return reduceMacro(dependentKey, (sum, item) => sum + item, 0);
return reduceMacro(dependentKey, (sum, item) => sum + item, 0, 'sum');
}

/**
Expand Down Expand Up @@ -120,7 +129,7 @@ export function sum(dependentKey) {
@public
*/
export function max(dependentKey) {
return reduceMacro(dependentKey, (max, item) => Math.max(max, item), -Infinity);
return reduceMacro(dependentKey, (max, item) => Math.max(max, item), -Infinity, 'max');
}

/**
Expand Down Expand Up @@ -166,7 +175,7 @@ export function max(dependentKey) {
@public
*/
export function min(dependentKey) {
return reduceMacro(dependentKey, (min, item) => Math.min(min, item), Infinity);
return reduceMacro(dependentKey, (min, item) => Math.min(min, item), Infinity, 'min');
}

/**
Expand Down Expand Up @@ -247,6 +256,10 @@ export function mapBy(dependentKey, propertyKey) {
'perhaps you meant to use "map"',
typeof propertyKey === 'string'
);
assert(
`Dependent key passed to Ember.computed.mapBy() shouldn't contain brace expanding pattern.`,
!/[\[\]\{\}]/g.test(dependentKey)
);

return map(`${dependentKey}.@each.${propertyKey}`, item => get(item, propertyKey));
}
Expand Down Expand Up @@ -346,8 +359,12 @@ export function filter(dependentKey, callback) {
@public
*/
export function filterBy(dependentKey, propertyKey, value) {
let callback;
assert(
`Dependent key passed to Ember.computed.filterBy() shouldn't contain brace expanding pattern.`,
!/[\[\]\{\}]/g.test(dependentKey)
);

let callback;
if (arguments.length === 2) {
callback = (item) => get(item, propertyKey);
} else {
Expand Down Expand Up @@ -404,7 +421,7 @@ export function uniq(...args) {
});

return uniq;
});
}, 'uniq');
}

/**
Expand Down Expand Up @@ -543,8 +560,7 @@ export function intersect(...args) {
}

return true;
});

}, 'intersect');

return emberA(results);
});
Expand Down Expand Up @@ -588,6 +604,10 @@ export function setDiff(setAProperty, setBProperty) {
assert('Ember.computed.setDiff requires exactly two dependent arrays.',
arguments.length === 2
);
assert(
`Dependent keys passed to Ember.computed.setDiff() shouldn't contain brace expanding pattern.`,
!/[\[\]\{\}]/g.test(setAProperty) && !/[\[\]\{\}]/g.test(setBProperty)
);

let cp = new ComputedProperty(function() {
let setA = this.get(setAProperty);
Expand Down Expand Up @@ -646,7 +666,7 @@ export function collect(...dependentKeys) {
}
}
return res;
});
}, 'collect');
}

/**
Expand Down

0 comments on commit 2b557dc

Please sign in to comment.