Skip to content

Commit

Permalink
Fix fails on ES6 method syntax in ClientFunction arguments (closes De…
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed Mar 1, 2017
1 parent cd54490 commit 404633b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/compiler/es-next/compile-client-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ClientFunctionAPIError } from '../../errors/runtime';
import MESSAGE from '../../errors/runtime/message';

const ANONYMOUS_FN_RE = /^function\*?\s*\(/;
const ES6_OBJ_METHOD_NAME_RE = /^([A-Za-z\*\$]+)\s*\(/;
const USE_STRICT_RE = /^('|")use strict('|");?/;
const TRAILING_SEMICOLON_RE = /;\s*$/;
const REGENERATOR_FOOTPRINTS_RE = /(_index\d+\.default|_regenerator\d+\.default|regeneratorRuntime)\.wrap\(function _callee\$\(_context\)/;
Expand Down Expand Up @@ -104,12 +105,25 @@ function getDependenciesDefinition (dependencies) {
}, '');
}

function makeFnCodeSuitableForParsing (fnCode) {
// NOTE: 'function() {}' -> '(function() {})'
if (ANONYMOUS_FN_RE.test(fnCode))
return `(${fnCode})`;

// NOTE: 'myFn () {}' -> 'function myFn() {}'
var match = fnCode.match(ES6_OBJ_METHOD_NAME_RE);

if (match && match[1] !== 'function')
return `function ${fnCode}`;

return fnCode;
}

export default function compileClientFunction (fnCode, dependencies, instantiationCallsiteName, compilationCallsiteName) {
if (fnCode === ASYNC_TO_GENERATOR_OUTPUT_CODE)
throw new ClientFunctionAPIError(compilationCallsiteName, instantiationCallsiteName, MESSAGE.regeneratorInClientFunctionCode);

if (ANONYMOUS_FN_RE.test(fnCode))
fnCode = `(${fnCode})`;
fnCode = makeFnCodeSuitableForParsing(fnCode);

// NOTE: we need to recompile ES6 code for the browser if we are on newer versions of Node.
if (NODE_VER >= 4)
Expand Down
6 changes: 6 additions & 0 deletions test/server/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ describe('Compiler', function () {
it('Should polyfill Babel `typeof` artifacts', function () {
return testClientFnCompilation('typeof');
});

describe('Regression', function () {
it('Should compile ES6 object method (GH-1279)', function () {
return testClientFnCompilation('gh1279');
});
});
});

describe('Errors', function () {
Expand Down
5 changes: 5 additions & 0 deletions test/server/data/client-fn-compilation/gh1279/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function () {
return function fn () {
return true;
};
})();
17 changes: 17 additions & 0 deletions test/server/data/client-fn-compilation/gh1279/testfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import compileClientFunction from '../../../../../lib/compiler/es-next/compile-client-function';

fixture `Fixture`;

function compile (fn) {
return compileClientFunction(fn.toString());
}

test('Test', () => {
var obj = {
fn() {
return true;
}
};

return compile(obj.fn);
});

0 comments on commit 404633b

Please sign in to comment.