Skip to content

Commit 74beda7

Browse files
author
Robert Jackson
committed
[BUGFIX lts] Add missing Babel helpers.
Due to the way Ember itself is compiled, we have custom versions of the `@babel/helpers` that we inline into the IIFE that contains Ember. This is obviously pretty unideal (largely because it forces us to be misaligned with Babel and therefore brittle to reasonable changes that they make). These helpers have been pretty stable for quite a while, but recently Babel 7.9.0 introduced a couple of new helpers: * `createSuper` allows subclassing from native classes, even when partially transpiled). * `createForOfIteratorHelperLoose` Versions of `ember-source` prior to 3.13 fully transpiled with the Babel version in `emberjs/ember.js`s _own_ `yarn.lock` at publish time. The build process was significantly changed in `[email protected]` so that `ember-source` would behave just like a normal addon. This was a **huge** improvement!! Unfortunately, those changes also made us vulnerable to changes made to Babel (new helpers added). We **must** resolve that issue (likely by removing these custom external helpers in favor of using `includeExternalHelpers` from `ember-cli-babel`), but in the meantime this commit adds the required helpers which should fix the reported issue in supported Ember versions. (cherry picked from commit a4a1206)
1 parent ba421ca commit 74beda7

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

packages/external-helpers/lib/external-helpers.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
/* globals Reflect */
2+
13
import { DEBUG } from '@glimmer/env';
24

35
const setPrototypeOf = Object.setPrototypeOf;
6+
const getPrototypeOf = Object.getPrototypeOf;
7+
8+
const hasReflectConstruct = typeof Reflect === 'object' && typeof Reflect.construct === 'function';
49

510
const nativeWrapperCache = new Map();
611

@@ -117,3 +122,74 @@ export function objectDestructuringEmpty(obj) {
117122
throw new TypeError('Cannot destructure undefined');
118123
}
119124
}
125+
126+
/*
127+
Differs from default implementation by checking for _any_ `Reflect.construct`
128+
(the default implementation tries to ensure that `Reflect.construct` is truly
129+
the native one).
130+
131+
Original source: https://github.com/babel/babel/blob/v7.9.2/packages/babel-helpers/src/helpers.js#L738-L757
132+
*/
133+
export function createSuper(Derived) {
134+
return function() {
135+
let Super = getPrototypeOf(Derived);
136+
let result;
137+
138+
if (hasReflectConstruct) {
139+
// NOTE: This doesn't work if this.__proto__.constructor has been modified.
140+
let NewTarget = getPrototypeOf(this).constructor;
141+
result = Reflect.construct(Super, arguments, NewTarget);
142+
} else {
143+
result = Super.apply(this, arguments);
144+
}
145+
146+
return possibleConstructorReturn(this, result);
147+
};
148+
}
149+
150+
/*
151+
Does not differ from default implementation.
152+
*/
153+
function arrayLikeToArray(arr, len) {
154+
if (len == null || len > arr.length) len = arr.length;
155+
156+
let arr2 = new Array(len);
157+
for (let i = 0; i < len; i++) {
158+
arr2[i] = arr[i];
159+
}
160+
161+
return arr2;
162+
}
163+
164+
/*
165+
Does not differ from default implementation.
166+
*/
167+
function unsupportedIterableToArray(o, minLen) {
168+
if (!o) return;
169+
if (typeof o === 'string') return arrayLikeToArray(o, minLen);
170+
let n = Object.prototype.toString.call(o).slice(8, -1);
171+
if (n === 'Object' && o.constructor) n = o.constructor.name;
172+
if (n === 'Map' || n === 'Set') return Array.from(n);
173+
if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
174+
return arrayLikeToArray(o, minLen);
175+
}
176+
177+
/*
178+
Does not differ from default implementation.
179+
*/
180+
export function createForOfIteratorHelperLoose(o) {
181+
let i = 0;
182+
if (typeof Symbol === 'undefined' || o[Symbol.iterator] == null) {
183+
// Fallback for engines without symbol support
184+
if (Array.isArray(o) || (o = unsupportedIterableToArray(o)))
185+
return function() {
186+
if (i >= o.length) return { done: true };
187+
return { done: false, value: o[i++] };
188+
};
189+
throw new TypeError(
190+
'Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.'
191+
);
192+
}
193+
i = o[Symbol.iterator]();
194+
return i.next.bind(i);
195+
}

0 commit comments

Comments
 (0)