Skip to content

Commit

Permalink
[DEPRECATE] Deprecates aliasMethod
Browse files Browse the repository at this point in the history
Deprecates the aliasMethod descriptor, and refactors it to not
use the Descriptor system internall
  • Loading branch information
Chris Garrett committed Jan 31, 2019
1 parent b2aa84a commit 76d30a8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 91 deletions.
39 changes: 17 additions & 22 deletions packages/@ember/-internals/metal/lib/mixin.ts
Expand Up @@ -12,7 +12,7 @@ import {
setObservers,
wrap,
} from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import { assert, deprecate } from '@ember/debug';
import { assign } from '@ember/polyfills';
import { DEBUG } from '@glimmer/env';
import { ComputedProperty, ComputedPropertyGetter, ComputedPropertySetter } from './computed';
Expand Down Expand Up @@ -303,11 +303,11 @@ function mergeMixins(

function followMethodAlias(
obj: object,
_desc: Alias,
alias: Alias,
descs: { [key: string]: any },
values: { [key: string]: any }
) {
let altKey = _desc.methodName;
let altKey = alias.methodName;
let possibleDesc;
let desc = descs[altKey];
let value = values[altKey];
Expand Down Expand Up @@ -387,8 +387,8 @@ export function applyMixin(obj: { [key: string]: any }, mixins: Mixin[]) {
desc = descs[key];
value = values[key];

while (desc && desc instanceof Alias) {
let followed = followMethodAlias(obj, desc, descs, values);
while (value && value instanceof Alias) {
let followed = followMethodAlias(obj, value, descs, values);
desc = followed.desc;
value = followed.value;
}
Expand Down Expand Up @@ -703,23 +703,8 @@ function _keys(mixin: Mixin, ret = new Set(), seen = new Set()) {
return ret;
}

class Alias extends Descriptor {
methodName: string;

constructor(methodName: string) {
super();
this.methodName = methodName;
}

teardown(_obj: object, _keyName: string, _meta: Meta): void {
throw new Error('Method not implemented.');
}
get(_obj: object, _keyName: string) {
throw new Error('Method not implemented.');
}
set(_obj: object, _keyName: string, _value: any) {
throw new Error('Method not implemented.');
}
class Alias {
constructor(public methodName: string) {}
}

/**
Expand Down Expand Up @@ -747,11 +732,21 @@ class Alias extends Descriptor {
@method aliasMethod
@static
@deprecated Use a shared utility method instead
@for @ember/object
@param {String} methodName name of the method to alias
@public
*/
export function aliasMethod(methodName: string): Alias {
deprecate(
`You attempted to alias '${methodName}, but aliasMethod has been deprecated. Consider extracting the method into a shared utility function.`,
false,
{
id: 'object.alias-method',
until: '4.0.0',
url: 'https://emberjs.com/deprecations/v3.x#toc_object-alias-method',
}
);
return new Alias(methodName);
}

Expand Down
138 changes: 74 additions & 64 deletions packages/@ember/-internals/metal/tests/mixin/alias_method_test.js
Expand Up @@ -10,82 +10,92 @@ moduleFor(
'aliasMethod',
class extends AbstractTestCase {
['@test methods of another name are aliased when the mixin is applied'](assert) {
let MyMixin = Mixin.create({
fooMethod() {
return 'FOO';
},
barMethod: aliasMethod('fooMethod'),
});

let obj = MyMixin.apply({});
validateAliasMethod(assert, obj);
expectDeprecation(() => {
let MyMixin = Mixin.create({
fooMethod() {
return 'FOO';
},
barMethod: aliasMethod('fooMethod'),
});

let obj = MyMixin.apply({});
validateAliasMethod(assert, obj);
}, /aliasMethod has been deprecated. Consider extracting the method into a shared utility function/);
}

['@test should follow aliasMethods all the way down'](assert) {
let MyMixin = Mixin.create({
bar: aliasMethod('foo'), // put first to break ordered iteration
baz() {
return 'baz';
},
foo: aliasMethod('baz'),
});

let obj = MyMixin.apply({});
assert.equal(get(obj, 'bar')(), 'baz', 'should have followed aliasMethods');
expectDeprecation(() => {
let MyMixin = Mixin.create({
bar: aliasMethod('foo'), // put first to break ordered iteration
baz() {
return 'baz';
},
foo: aliasMethod('baz'),
});

let obj = MyMixin.apply({});
assert.equal(get(obj, 'bar')(), 'baz', 'should have followed aliasMethods');
}, /aliasMethod has been deprecated. Consider extracting the method into a shared utility function/);
}

['@test should alias methods from other dependent mixins'](assert) {
let BaseMixin = Mixin.create({
fooMethod() {
return 'FOO';
},
});

let MyMixin = Mixin.create(BaseMixin, {
barMethod: aliasMethod('fooMethod'),
});

let obj = MyMixin.apply({});
validateAliasMethod(assert, obj);
expectDeprecation(() => {
let BaseMixin = Mixin.create({
fooMethod() {
return 'FOO';
},
});

let MyMixin = Mixin.create(BaseMixin, {
barMethod: aliasMethod('fooMethod'),
});

let obj = MyMixin.apply({});
validateAliasMethod(assert, obj);
}, /aliasMethod has been deprecated. Consider extracting the method into a shared utility function/);
}

['@test should alias methods from other mixins applied at same time'](assert) {
let BaseMixin = Mixin.create({
fooMethod() {
return 'FOO';
},
});

let MyMixin = Mixin.create({
barMethod: aliasMethod('fooMethod'),
});

let obj = mixin({}, BaseMixin, MyMixin);
validateAliasMethod(assert, obj);
expectDeprecation(() => {
let BaseMixin = Mixin.create({
fooMethod() {
return 'FOO';
},
});

let MyMixin = Mixin.create({
barMethod: aliasMethod('fooMethod'),
});

let obj = mixin({}, BaseMixin, MyMixin);
validateAliasMethod(assert, obj);
}, /aliasMethod has been deprecated. Consider extracting the method into a shared utility function/);
}

['@test should alias methods from mixins already applied on object'](assert) {
let BaseMixin = Mixin.create({
quxMethod() {
return 'qux';
},
});

let MyMixin = Mixin.create({
bar: aliasMethod('foo'),
barMethod: aliasMethod('fooMethod'),
});

let obj = {
fooMethod() {
return 'FOO';
},
};

BaseMixin.apply(obj);
MyMixin.apply(obj);

validateAliasMethod(assert, obj);
expectDeprecation(() => {
let BaseMixin = Mixin.create({
quxMethod() {
return 'qux';
},
});

let MyMixin = Mixin.create({
bar: aliasMethod('foo'),
barMethod: aliasMethod('fooMethod'),
});

let obj = {
fooMethod() {
return 'FOO';
},
};

BaseMixin.apply(obj);
MyMixin.apply(obj);

validateAliasMethod(assert, obj);
}, /aliasMethod has been deprecated. Consider extracting the method into a shared utility function/);
}
}
);
11 changes: 6 additions & 5 deletions packages/@ember/-internals/runtime/lib/mixins/array.js
Expand Up @@ -11,7 +11,6 @@ import {
replaceInNativeArray,
replace,
computed,
aliasMethod,
Mixin,
hasListeners,
beginPropertyChanges,
Expand Down Expand Up @@ -180,6 +179,10 @@ function nonEnumerableComputed() {
return property;
}

function mapBy(key) {
return this.map(next => get(next, key));
}

// ..........................................................
// ARRAY
//
Expand Down Expand Up @@ -572,7 +575,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@return {Array} The mapped array.
@public
*/
getEach: aliasMethod('mapBy'),
getEach: mapBy,

/**
Sets the value on the named property for each member. This is more
Expand Down Expand Up @@ -636,9 +639,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@return {Array} The mapped array.
@public
*/
mapBy(key) {
return this.map(next => get(next, key));
},
mapBy,

/**
Returns an array with all of the items in the enumeration that the passed
Expand Down

0 comments on commit 76d30a8

Please sign in to comment.