Skip to content

Commit

Permalink
Remove computed deep each
Browse files Browse the repository at this point in the history
  • Loading branch information
nlfurniss authored and mixonic committed Jul 31, 2021
1 parent c3a7fd9 commit a0f486a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 65 deletions.
29 changes: 1 addition & 28 deletions packages/@ember/-internals/metal/lib/chain-tags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, meta as metaFor, peekMeta } from '@ember/-internals/meta';
import { isObject } from '@ember/-internals/utils';
import { assert, deprecate } from '@ember/debug';
import { assert } from '@ember/debug';
import { _WeakSet } from '@glimmer/util';
import {
combine,
Expand Down Expand Up @@ -89,33 +89,6 @@ function getChainTags(
lastSegmentEnd = segmentEnd + 1;
segmentEnd = path.indexOf('.', lastSegmentEnd);

// There should be exactly one segment after an `@each` (i.e. `@each.foo`, not `@each.foo.bar`)
deprecate(
`When using @each in a dependent-key or an observer, ` +
`you can only chain one property level deep after ` +
`the @each. That is, \`${path.slice(0, segmentEnd)}\` ` +
`is allowed but \`${path}\` (which is what you passed) ` +
`is not.\n\n` +
`This was never supported. Currently, the extra segments ` +
`are silently ignored, i.e. \`${path}\` behaves exactly ` +
`the same as \`${path.slice(0, segmentEnd)}\`. ` +
`In the future, this will throw an error.\n\n` +
`If the current behavior is acceptable for your use case, ` +
`please remove the extraneous segments by changing your ` +
`key to \`${path.slice(0, segmentEnd)}\`. ` +
`Otherwise, please create an intermediary computed property ` +
`or switch to using tracked properties.`,
segmentEnd === -1,
{
until: '3.17.0',
id: 'ember-metal.computed-deep-each',
for: 'ember-source',
since: {
enabled: '3.13.0-beta.3',
},
}
);

let arrLength = current.length;

if (
Expand Down
11 changes: 6 additions & 5 deletions packages/@ember/-internals/metal/lib/computed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, meta as metaFor } from '@ember/-internals/meta';
import { inspect, toString } from '@ember/-internals/utils';
import { assert, warn } from '@ember/debug';
import { assert } from '@ember/debug';
import EmberError from '@ember/error';
import { isDestroyed } from '@glimmer/destroyable';
import { DEBUG } from '@glimmer/env';
Expand Down Expand Up @@ -351,13 +351,14 @@ export class ComputedProperty extends ComputedDescriptor {
let args: string[] = [];

function addArg(property: string): void {
warn(
assert(
`Dependent keys containing @each only work one level deep. ` +
`You used the key "${property}" which is invalid. ` +
`Please create an intermediary computed property.`,
DEEP_EACH_REGEX.test(property) === false,
{ id: 'ember-metal.computed-deep-each' }
`Please create an intermediary computed property or ` +
`switch to using tracked properties.`,
DEEP_EACH_REGEX.test(property) === false
);

args.push(property);
}

Expand Down
38 changes: 6 additions & 32 deletions packages/@ember/-internals/metal/tests/computed_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ moduleFor(
// assert.deepEqual(cp._dependentKeys, ['qux', 'zoopa.[]']);
// }

['@test defining a computed property with a dependent key more than one level deep beyond @each is not supported']() {
['@test defining a computed property with a dependent key more than one level deep beyond @each throws an assertion']() {
expectNoWarning(() => {
obj = {};
defineProperty(
Expand All @@ -185,50 +185,24 @@ moduleFor(
);
});

expectWarning(() => {
let expected = /Dependent keys containing @each only work one level deep./;

expectAssertion(() => {
obj = {};
defineProperty(
obj,
'someProp',
computed('todos.@each.owner.name', () => {})
);
}, /You used the key "todos\.@each\.owner\.name" which is invalid\. /);
}, expected);

expectWarning(() => {
expectAssertion(() => {
obj = {};
defineProperty(
obj,
'someProp',
computed('todos.@each.owner.@each.name', () => {})
);
}, /You used the key "todos\.@each\.owner\.@each\.name" which is invalid\. /);

let expected = new RegExp(
'When using @each in a dependent-key or an observer, ' +
'you can only chain one property level deep after the @each\\. ' +
'That is, `todos\\.@each\\.owner` is allowed but ' +
'`todos\\.@each\\.owner\\.name` \\(which is what you passed\\) is not\\.\n\n' +
'This was never supported\\. Currently, the extra segments ' +
'are silently ignored, i\\.e\\. `todos\\.@each\\.owner\\.name` ' +
'behaves exactly the same as `todos\\.@each\\.owner`\\. ' +
'In the future, this will throw an error\\.\n\n' +
'If the current behavior is acceptable for your use case, ' +
'please remove the extraneous segments by changing your key to ' +
'`todos\\.@each\\.owner`\\. Otherwise, please create an ' +
'intermediary computed property or switch to using tracked properties\\.'
);

expectDeprecation(() => {
obj = {
todos: [],
};
defineProperty(
obj,
'someProp',
computed('todos.@each.owner.name', () => {})
);

get(obj, 'someProp');
}, expected);
}
}
Expand Down

0 comments on commit a0f486a

Please sign in to comment.