Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
{
"plugins": ["transform-decorators-legacy"],
"presets": ["es2015", "stage-0", "react"]
"plugins": [
"transform-decorators-legacy",
"transform-export-extensions",
"transform-decorators-legacy",
"transform-object-rest-spread",
"transform-es2015-literals",
"transform-class-properties",
"transform-es2015-shorthand-properties",
"check-es2015-constants",
"transform-es2015-spread",
"transform-es2015-parameters",
"transform-es2015-destructuring",
"transform-es2015-modules-commonjs",
"transform-react-jsx",
"transform-flow-strip-types",
"transform-react-display-name",
"syntax-jsx"
],
"env": {
"development": {
"plugins": [
"transform-es2015-template-literals",
"transform-es2015-arrow-functions",
"transform-decorators-legacy",
"transform-class-properties",
"transform-es2015-classes"
]
}
}
}
32 changes: 28 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"scripts": {
"build": "rimraf modules dist && webpack && babel src --out-dir modules",
"test": "mocha --compilers js:babel-core/register --recursive ./test",
"test:harmony": "NODE_ENV=harmony npm run test",
"test:harmony:watch": "NODE_ENV=harmony npm run test -- --watch",
"test:watch": "npm run test -- --watch",
"prepublish": "npm run build"
},
Expand All @@ -28,10 +30,32 @@
"babel-cli": "^6.3.17",
"babel-core": "^6.3.21",
"babel-loader": "^6.2.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.3.3",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"babel-plugin-check-es2015-constants": "^6.3.13",
"babel-plugin-syntax-jsx": "^6.3.13",
"babel-plugin-transform-class-properties": "^6.5.0",
"babel-plugin-transform-decorators-legacy": "^1.2.0",
"babel-plugin-transform-es2015-arrow-functions": "^6.3.13",
"babel-plugin-transform-es2015-block-scoped-functions": "^6.3.13",
"babel-plugin-transform-es2015-block-scoping": "^6.3.13",
"babel-plugin-transform-es2015-classes": "^6.3.13",
"babel-plugin-transform-es2015-computed-properties": "^6.3.13",
"babel-plugin-transform-es2015-destructuring": "^6.3.13",
"babel-plugin-transform-es2015-for-of": "^6.3.13",
"babel-plugin-transform-es2015-function-name": "^6.3.13",
"babel-plugin-transform-es2015-literals": "^6.3.13",
"babel-plugin-transform-es2015-modules-commonjs": "^6.3.13",
"babel-plugin-transform-es2015-object-super": "^6.3.13",
"babel-plugin-transform-es2015-parameters": "^6.3.13",
"babel-plugin-transform-es2015-shorthand-properties": "^6.3.13",
"babel-plugin-transform-es2015-spread": "^6.3.13",
"babel-plugin-transform-es2015-sticky-regex": "^6.3.13",
"babel-plugin-transform-es2015-template-literals": "^6.3.13",
"babel-plugin-transform-es2015-unicode-regex": "^6.3.13",
"babel-plugin-transform-export-extensions": "^6.5.0",
"babel-plugin-transform-flow-strip-types": "^6.5.0",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-plugin-transform-react-display-name": "^6.4.0",
"babel-plugin-transform-react-jsx": "^6.4.0",
"expect": "^1.9.0",
"mocha": "^2.2.4",
"react": "^0.14.0",
Expand Down
26 changes: 22 additions & 4 deletions src/createClassProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,35 @@ function proxyClass(InitialComponent) {
return !isEqualDescriptor(staticDescriptors[key], currentDescriptor);
}

function instantiate(factory, context, params) {
const component = factory();

try {
return component.apply(context, params);
} catch (err) {
// Native ES6 class instantiation
const instance = new component(...params);

Object.keys(instance).forEach(key => {
if (RESERVED_STATICS.indexOf(key) > -1) {
return;
}
context[key] = instance[key];
})
}
}

try {
// Create a proxy constructor with matching name
ProxyComponent = new Function('getCurrentComponent',
ProxyComponent = new Function('factory', 'instantiate',
`return function ${InitialComponent.name || 'ProxyComponent'}() {
return getCurrentComponent().apply(this, arguments);
return instantiate(factory, this, arguments);
}`
)(() => CurrentComponent);
)(() => CurrentComponent, instantiate);
} catch (err) {
// Some environments may forbid dynamic evaluation
ProxyComponent = function () {
return CurrentComponent.apply(this, arguments);
return instantiate(() => CurrentComponent, this, arguments);
};
}

Expand Down
12 changes: 10 additions & 2 deletions test/consistency.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,15 @@ describe('consistency', () => {

it('should not crash if new Function() throws', () => {
let oldFunction = global.Function;
global.Function = function () { throw new Error(); }

global.Function = class extends oldFunction {
constructor () {
super();

throw new Error();
}
};

try {
expect(() => {
const proxy = createProxy(Bar);
Expand All @@ -238,4 +246,4 @@ describe('consistency', () => {
}
});
});
});
});