From d0d779b66afb1329118aee155ee526c29c1113fc Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Thu, 31 Jan 2019 13:40:38 -0800 Subject: [PATCH 1/3] [DEPRECATE] Deprecates aliasMethod Deprecates the aliasMethod descriptor, and refactors it to not use the Descriptor system internall --- packages/@ember/-internals/metal/lib/mixin.ts | 39 +++-- .../metal/tests/mixin/alias_method_test.js | 138 ++++++++++-------- .../-internals/runtime/lib/mixins/array.js | 11 +- 3 files changed, 97 insertions(+), 91 deletions(-) diff --git a/packages/@ember/-internals/metal/lib/mixin.ts b/packages/@ember/-internals/metal/lib/mixin.ts index 4b085530dca..5cd76f662a7 100644 --- a/packages/@ember/-internals/metal/lib/mixin.ts +++ b/packages/@ember/-internals/metal/lib/mixin.ts @@ -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'; @@ -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]; @@ -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; } @@ -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) {} } /** @@ -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); } diff --git a/packages/@ember/-internals/metal/tests/mixin/alias_method_test.js b/packages/@ember/-internals/metal/tests/mixin/alias_method_test.js index d00cdd9d535..3532d2f2809 100644 --- a/packages/@ember/-internals/metal/tests/mixin/alias_method_test.js +++ b/packages/@ember/-internals/metal/tests/mixin/alias_method_test.js @@ -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/); } } ); diff --git a/packages/@ember/-internals/runtime/lib/mixins/array.js b/packages/@ember/-internals/runtime/lib/mixins/array.js index ceb17776819..697b9ac8536 100644 --- a/packages/@ember/-internals/runtime/lib/mixins/array.js +++ b/packages/@ember/-internals/runtime/lib/mixins/array.js @@ -11,7 +11,6 @@ import { replaceInNativeArray, replace, computed, - aliasMethod, Mixin, hasListeners, beginPropertyChanges, @@ -180,6 +179,10 @@ function nonEnumerableComputed() { return property; } +function mapBy(key) { + return this.map(next => get(next, key)); +} + // .......................................................... // ARRAY // @@ -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 @@ -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 From 29f5c2a39381f4c1141055e65ad95aa6644e3871 Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Fri, 1 Feb 2019 12:43:01 -0800 Subject: [PATCH 2/3] svelte alias method --- packages/@ember/-internals/metal/lib/mixin.ts | 93 ++++++++++++------- packages/@ember/deprecated-features/index.ts | 1 + 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/packages/@ember/-internals/metal/lib/mixin.ts b/packages/@ember/-internals/metal/lib/mixin.ts index 5cd76f662a7..ab374685281 100644 --- a/packages/@ember/-internals/metal/lib/mixin.ts +++ b/packages/@ember/-internals/metal/lib/mixin.ts @@ -13,6 +13,7 @@ import { wrap, } from '@ember/-internals/utils'; import { assert, deprecate } from '@ember/debug'; +import { ALIAS_METHOD } from '@ember/deprecated-features'; import { assign } from '@ember/polyfills'; import { DEBUG } from '@glimmer/env'; import { ComputedProperty, ComputedPropertyGetter, ComputedPropertySetter } from './computed'; @@ -301,28 +302,37 @@ function mergeMixins( } } -function followMethodAlias( +let followMethodAlias: ( obj: object, alias: Alias, descs: { [key: string]: any }, values: { [key: string]: any } -) { - let altKey = alias.methodName; - let possibleDesc; - let desc = descs[altKey]; - let value = values[altKey]; - - if (desc !== undefined || value !== undefined) { - // do nothing - } else if ((possibleDesc = descriptorFor(obj, altKey)) !== undefined) { - desc = possibleDesc; - value = undefined; - } else { - desc = undefined; - value = obj[altKey]; - } +) => { desc: any; value: any }; + +if (ALIAS_METHOD) { + followMethodAlias = function( + obj: object, + alias: Alias, + descs: { [key: string]: any }, + values: { [key: string]: any } + ) { + let altKey = alias.methodName; + let possibleDesc; + let desc = descs[altKey]; + let value = values[altKey]; + + if (desc !== undefined || value !== undefined) { + // do nothing + } else if ((possibleDesc = descriptorFor(obj, altKey)) !== undefined) { + desc = possibleDesc; + value = undefined; + } else { + desc = undefined; + value = obj[altKey]; + } - return { desc, value }; + return { desc, value }; + }; } function updateObserversAndListeners( @@ -387,10 +397,12 @@ export function applyMixin(obj: { [key: string]: any }, mixins: Mixin[]) { desc = descs[key]; value = values[key]; - while (value && value instanceof Alias) { - let followed = followMethodAlias(obj, value, descs, values); - desc = followed.desc; - value = followed.value; + if (ALIAS_METHOD) { + while (value && value instanceof AliasImpl) { + let followed = followMethodAlias(obj, value, descs, values); + desc = followed.desc; + value = followed.value; + } } if (desc === undefined && value === undefined) { @@ -703,8 +715,17 @@ function _keys(mixin: Mixin, ret = new Set(), seen = new Set()) { return ret; } -class Alias { - constructor(public methodName: string) {} +declare class Alias { + public methodName: string; + constructor(methodName: string); +} + +let AliasImpl: typeof Alias; + +if (ALIAS_METHOD) { + AliasImpl = class AliasImpl { + constructor(public methodName: string) {} + } as typeof Alias; } /** @@ -737,17 +758,21 @@ class Alias { @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); +export let aliasMethod: (methodName: string) => any; + +if (ALIAS_METHOD) { + aliasMethod = 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 AliasImpl(methodName); + }; } // .......................................................... diff --git a/packages/@ember/deprecated-features/index.ts b/packages/@ember/deprecated-features/index.ts index 92eaa91c476..f7ac4f77cb2 100644 --- a/packages/@ember/deprecated-features/index.ts +++ b/packages/@ember/deprecated-features/index.ts @@ -10,3 +10,4 @@ export const ROUTER_EVENTS = !!'3.9.0'; export const TRANSITION_STATE = !!'3.9.0'; export const COMPONENT_MANAGER_STRING_LOOKUP = !!'4.0.0'; export const JQUERY_INTEGRATION = !!'3.9.0'; +export const ALIAS_METHOD = !!'4.0.0'; From 1f5438bd37af9f38d077e260a9c3cb7e5a56b09c Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Fri, 1 Feb 2019 13:23:32 -0800 Subject: [PATCH 3/3] update svelte version --- packages/@ember/deprecated-features/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/@ember/deprecated-features/index.ts b/packages/@ember/deprecated-features/index.ts index f7ac4f77cb2..48d0f4ed233 100644 --- a/packages/@ember/deprecated-features/index.ts +++ b/packages/@ember/deprecated-features/index.ts @@ -1,5 +1,8 @@ /* eslint-disable no-implicit-coercion */ +// These versions should be the version that the deprecation was _introduced_, +// not the version that the feature will be removed. + export const SEND_ACTION = !!'3.4.0'; export const EMBER_EXTEND_PROTOTYPES = !!'3.2.0-beta.5'; export const RUN_SYNC = !!'3.0.0-beta.4'; @@ -10,4 +13,4 @@ export const ROUTER_EVENTS = !!'3.9.0'; export const TRANSITION_STATE = !!'3.9.0'; export const COMPONENT_MANAGER_STRING_LOOKUP = !!'4.0.0'; export const JQUERY_INTEGRATION = !!'3.9.0'; -export const ALIAS_METHOD = !!'4.0.0'; +export const ALIAS_METHOD = !!'3.9.0';