Skip to content

Commit

Permalink
fix: #556 upgrade acorn, and handle function() {} syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
robertleeplummerjr committed Jan 2, 2020
1 parent ef17f8f commit 78b6e30
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 27 deletions.
14 changes: 8 additions & 6 deletions dist/gpu-browser-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.4.4
* @date Thu Jan 02 2020 12:26:36 GMT-0500 (Eastern Standard Time)
* @version 2.4.5
* @date Thu Jan 02 2020 13:02:31 GMT-0500 (Eastern Standard Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -14411,11 +14411,13 @@ const utils = {
},

getMinifySafeName: (fn) => {
const ast = acorn.parse(fn.toString());
if (!ast.body || !ast.body[0] || !ast.body[0].expression || !ast.body[0].expression.body || !ast.body[0].expression.body.name) {
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere`');
try {
const ast = acorn.parse(`const value = ${fn.toString()}`);
const { init } = ast.body[0].declarations[0];
return init.body.name || init.body.body[0].argument.name;
} catch (e) {
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere` or function() { return yourFunctionVariableHere; }');
}
return ast.body[0].expression.body.name;
}
};

Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions dist/gpu-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.4.4
* @date Thu Jan 02 2020 12:26:36 GMT-0500 (Eastern Standard Time)
* @version 2.4.5
* @date Thu Jan 02 2020 13:02:31 GMT-0500 (Eastern Standard Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -18845,11 +18845,13 @@ const utils = {
},

getMinifySafeName: (fn) => {
const ast = acorn.parse(fn.toString());
if (!ast.body || !ast.body[0] || !ast.body[0].expression || !ast.body[0].expression.body || !ast.body[0].expression.body.name) {
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere`');
try {
const ast = acorn.parse(`const value = ${fn.toString()}`);
const { init } = ast.body[0].declarations[0];
return init.body.name || init.body.body[0].argument.name;
} catch (e) {
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere` or function() { return yourFunctionVariableHere; }');
}
return ast.body[0].expression.body.name;
}
};

Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "2.4.4",
"version": "2.4.5",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down
10 changes: 6 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,11 +1000,13 @@ const utils = {
},

getMinifySafeName: (fn) => {
const ast = acorn.parse(fn.toString());
if (!ast.body || !ast.body[0] || !ast.body[0].expression || !ast.body[0].expression.body || !ast.body[0].expression.body.name) {
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere`');
try {
const ast = acorn.parse(`const value = ${fn.toString()}`);
const { init } = ast.body[0].declarations[0];
return init.body.name || init.body.body[0].argument.name;
} catch (e) {
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere` or function() { return yourFunctionVariableHere; }');
}
return ast.body[0].expression.body.name;
}
};

Expand Down
20 changes: 17 additions & 3 deletions test/internal/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,28 @@ test('flattenFunctionToString', () => {
assert.ok(true);
});

test('improper getMinifySafeName usage', () => {
test('improper getMinifySafeName usage with arrow function', () => {
assert.throws(() => {
utils.getMinifySafeName(() => {});
});
}, 'Unrecognized function type.');
});

test('proper getMinifySafeName usage', () => {
test('improper getMinifySafeName usage with regular function', () => {
assert.throws(() => {
utils.getMinifySafeName(function() {});
}, 'Unrecognized function type.');
});

test('proper getMinifySafeName usage with arrow function', () => {
function n() {}
const safeName = utils.getMinifySafeName(() => n);
assert.equal(safeName, 'n');
});

test('proper getMinifySafeName usage with regular function', () => {
function n() {}
const safeName = utils.getMinifySafeName(function () {
return n;
});
assert.equal(safeName, 'n');
});

0 comments on commit 78b6e30

Please sign in to comment.