From b6cd5744486e337389059dd2abff172eedd8b808 Mon Sep 17 00:00:00 2001 From: Christopher Garrett Date: Wed, 28 Nov 2018 19:39:56 -0800 Subject: [PATCH] [BUGFIX beta] Ensures Ember.A is a constructor The recent change to transpilation has made `new A()` break, because A is an arrow function and arrow functions cannot be constructors. This is a pretty common pattern in Ember apps and likely constitutes a breaking change, so this PR fixes it by changing the arrow function to a normal function, and deprecates this behavior. --- .../-internals/runtime/lib/mixins/array.js | 29 +++++++++++++++++-- .../tests/system/native_array/a_test.js | 10 +++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/@ember/-internals/runtime/lib/mixins/array.js b/packages/@ember/-internals/runtime/lib/mixins/array.js index 443d9123343..f5604d91cbb 100644 --- a/packages/@ember/-internals/runtime/lib/mixins/array.js +++ b/packages/@ember/-internals/runtime/lib/mixins/array.js @@ -1641,9 +1641,34 @@ let A; if (ENV.EXTEND_PROTOTYPES.Array) { NativeArray.apply(Array.prototype); - A = arr => arr || []; + + A = function(arr) { + if (this instanceof A) { + deprecate( + '`new A()` has been deprecated, please update to calling A as a function: `A()`', + false, + { + id: 'array.new-array-wrapper', + until: '3.9.0', + url: 'https://emberjs.com/deprecations/v3.x#toc_new-array-wrapper', + } + ); + } + return arr || []; + }; } else { - A = arr => { + A = function(arr) { + if (this instanceof A) { + deprecate( + '`new A()` has been deprecated, please update to calling A as a function: `A()`', + false, + { + id: 'array.new-array-wrapper', + until: '3.9.0', + url: 'https://emberjs.com/deprecations/v3.x#toc_new-array-wrapper', + } + ); + } if (!arr) { arr = []; } diff --git a/packages/@ember/-internals/runtime/tests/system/native_array/a_test.js b/packages/@ember/-internals/runtime/tests/system/native_array/a_test.js index c2f8de2946e..85d7e614680 100644 --- a/packages/@ember/-internals/runtime/tests/system/native_array/a_test.js +++ b/packages/@ember/-internals/runtime/tests/system/native_array/a_test.js @@ -12,5 +12,15 @@ moduleFor( assert.ok(EmberArray.detect(A()), 'returned an ember array'); assert.ok(EmberArray.detect(A([1, 2])), 'returned an ember array'); } + + ['@test new Ember.A'](assert) { + expectDeprecation(() => { + assert.deepEqual(new A([1, 2]), [1, 2], 'array values were not be modified'); + assert.deepEqual(new A(), [], 'returned an array with no arguments'); + assert.deepEqual(new A(null), [], 'returned an array with a null argument'); + assert.ok(EmberArray.detect(new A()), 'returned an ember array'); + assert.ok(EmberArray.detect(new A([1, 2])), 'returned an ember array'); + }); + } } );