Fix ReferenceError in the wrapNativeSuper helper (#8100)

Fix a ReferenceError caused by a typo (`_construct` instead of
`construct`) in the external `wrapNativeSuper` helper. (The typo
doesn't usually cause an error in inline helpers because `_construct`
happens to be the default name given to the `construct` helper
function.)
This commit is contained in:
chocolateboy 2018-06-02 16:00:02 +00:00 committed by Nicolò Ribaudo
parent b8dcd6f593
commit 2abd7839e1
6 changed files with 38 additions and 8 deletions

View File

@ -38,7 +38,7 @@ export default {
## Defining Helpers ## Defining Helpers
> **NOTE**: This package is only meant to be used by the packages inluded in this repository. There is currently no way for third-party plugins to define an helper. > **NOTE**: This package is only meant to be used by the packages included in this repository. There is currently no way for third-party plugins to define a helper.
Helpers are defined in the `src/helpers.js` file, and they must be valid modules which follow these guidelines: Helpers are defined in the `src/helpers.js` file, and they must be valid modules which follow these guidelines:
- They must have a default export, which is their entry-point. - They must have a default export, which is their entry-point.
@ -48,7 +48,9 @@ Helpers are defined in the `src/helpers.js` file, and they must be valid modules
```js ```js
helpers.customHelper = defineHelper(` helpers.customHelper = defineHelper(`
import dep from "dependency"; import dep from "dependency";
const foo = 2; const foo = 2;
export default function getFooTimesDepPlusX(x) { export default function getFooTimesDepPlusX(x) {
return foo * dep() + x; return foo * dep() + x;
} }

View File

@ -449,7 +449,7 @@ helpers.construct = () => template.program.ast`
if (Reflect.construct.sham) return false; if (Reflect.construct.sham) return false;
// Proxy can't be polyfilled. Every browser implemented // Proxy can't be polyfilled. Every browser implemented
// proxies before or at the same time of Reflect.construct, // proxies before or at the same time as Reflect.construct,
// so if they support Proxy they also support Reflect.construct. // so if they support Proxy they also support Reflect.construct.
if (typeof Proxy === "function") return true; if (typeof Proxy === "function") return true;
@ -488,8 +488,8 @@ helpers.construct = () => template.program.ast`
// Based on https://github.com/WebReflection/babel-plugin-transform-builtin-classes // Based on https://github.com/WebReflection/babel-plugin-transform-builtin-classes
helpers.wrapNativeSuper = () => template.program.ast` helpers.wrapNativeSuper = () => template.program.ast`
import _gPO from "getPrototypeOf"; import getPrototypeOf from "getPrototypeOf";
import _sPO from "setPrototypeOf"; import setPrototypeOf from "setPrototypeOf";
import construct from "construct"; import construct from "construct";
export default function _wrapNativeSuper(Class) { export default function _wrapNativeSuper(Class) {
@ -505,7 +505,7 @@ helpers.wrapNativeSuper = () => template.program.ast`
_cache.set(Class, Wrapper); _cache.set(Class, Wrapper);
} }
function Wrapper() { function Wrapper() {
return _construct(Class, arguments, _gPO(this).constructor) return construct(Class, arguments, getPrototypeOf(this).constructor)
} }
Wrapper.prototype = Object.create(Class.prototype, { Wrapper.prototype = Object.create(Class.prototype, {
constructor: { constructor: {
@ -516,7 +516,7 @@ helpers.wrapNativeSuper = () => template.program.ast`
} }
}); });
return _sPO(Wrapper, Class); return setPrototypeOf(Wrapper, Class);
} }
return _wrapNativeSuper(Class) return _wrapNativeSuper(Class)

View File

@ -2,7 +2,7 @@ var env = {
Array: null, Array: null,
}; };
// Wee need to use "with" to avoid leaking the modified Array to other tests. // We need to use "with" to avoid leaking the modified Array to other tests.
with (env) { with (env) {
class List extends Array {} class List extends Array {}
expect(List.prototype.__proto__).toBeUndefined(); expect(List.prototype.__proto__).toBeUndefined();

View File

@ -6,7 +6,7 @@ var env = {
} }
}; };
// Wee need to use "with" to avoid leaking the modified Array to other tests. // We need to use "with" to avoid leaking the modified Array to other tests.
with (env) { with (env) {
class List extends Array {}; class List extends Array {};
new List(); new List();

View File

@ -0,0 +1,25 @@
// basic sanity check to confirm the external wrapNativeSuper helper works
class Test1 extends Array {
name() {
return 'test1';
}
}
class Test2 extends Array {
name() {
return 'test2';
}
}
var t1 = new Test1();
var t2 = new Test2();
expect(Test1).not.toBe(Test2);
expect(t1).not.toBe(t2);
expect(t1.name()).toBe('test1');
expect(t2.name()).toBe('test2');
expect(t1).toBeInstanceOf(Test1);
expect(t2).toBeInstanceOf(Test2);
expect(t1).toBeInstanceOf(Array);
expect(t2).toBeInstanceOf(Array);

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-classes","transform-block-scoping","external-helpers"]
}