Skip to content

Commit 78b6e30

Browse files
fix: #556 upgrade acorn, and handle function() {} syntax
1 parent ef17f8f commit 78b6e30

File tree

8 files changed

+47
-27
lines changed

8 files changed

+47
-27
lines changed

dist/gpu-browser-core.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.4.4
8-
* @date Thu Jan 02 2020 12:26:36 GMT-0500 (Eastern Standard Time)
7+
* @version 2.4.5
8+
* @date Thu Jan 02 2020 13:02:31 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -14411,11 +14411,13 @@ const utils = {
1441114411
},
1441214412

1441314413
getMinifySafeName: (fn) => {
14414-
const ast = acorn.parse(fn.toString());
14415-
if (!ast.body || !ast.body[0] || !ast.body[0].expression || !ast.body[0].expression.body || !ast.body[0].expression.body.name) {
14416-
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere`');
14414+
try {
14415+
const ast = acorn.parse(`const value = ${fn.toString()}`);
14416+
const { init } = ast.body[0].declarations[0];
14417+
return init.body.name || init.body.body[0].argument.name;
14418+
} catch (e) {
14419+
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere` or function() { return yourFunctionVariableHere; }');
1441714420
}
14418-
return ast.body[0].expression.body.name;
1441914421
}
1442014422
};
1442114423

dist/gpu-browser-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gpu-browser.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.4.4
8-
* @date Thu Jan 02 2020 12:26:36 GMT-0500 (Eastern Standard Time)
7+
* @version 2.4.5
8+
* @date Thu Jan 02 2020 13:02:31 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -18845,11 +18845,13 @@ const utils = {
1884518845
},
1884618846

1884718847
getMinifySafeName: (fn) => {
18848-
const ast = acorn.parse(fn.toString());
18849-
if (!ast.body || !ast.body[0] || !ast.body[0].expression || !ast.body[0].expression.body || !ast.body[0].expression.body.name) {
18850-
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere`');
18848+
try {
18849+
const ast = acorn.parse(`const value = ${fn.toString()}`);
18850+
const { init } = ast.body[0].declarations[0];
18851+
return init.body.name || init.body.body[0].argument.name;
18852+
} catch (e) {
18853+
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere` or function() { return yourFunctionVariableHere; }');
1885118854
}
18852-
return ast.body[0].expression.body.name;
1885318855
}
1885418856
};
1885518857

dist/gpu-browser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "2.4.4",
3+
"version": "2.4.5",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

src/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,11 +1000,13 @@ const utils = {
10001000
},
10011001

10021002
getMinifySafeName: (fn) => {
1003-
const ast = acorn.parse(fn.toString());
1004-
if (!ast.body || !ast.body[0] || !ast.body[0].expression || !ast.body[0].expression.body || !ast.body[0].expression.body.name) {
1005-
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere`');
1003+
try {
1004+
const ast = acorn.parse(`const value = ${fn.toString()}`);
1005+
const { init } = ast.body[0].declarations[0];
1006+
return init.body.name || init.body.body[0].argument.name;
1007+
} catch (e) {
1008+
throw new Error('Unrecognized function type. Please use `() => yourFunctionVariableHere` or function() { return yourFunctionVariableHere; }');
10061009
}
1007-
return ast.body[0].expression.body.name;
10081010
}
10091011
};
10101012

test/internal/utils.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,28 @@ test('flattenFunctionToString', () => {
609609
assert.ok(true);
610610
});
611611

612-
test('improper getMinifySafeName usage', () => {
612+
test('improper getMinifySafeName usage with arrow function', () => {
613613
assert.throws(() => {
614614
utils.getMinifySafeName(() => {});
615-
});
615+
}, 'Unrecognized function type.');
616616
});
617617

618-
test('proper getMinifySafeName usage', () => {
618+
test('improper getMinifySafeName usage with regular function', () => {
619+
assert.throws(() => {
620+
utils.getMinifySafeName(function() {});
621+
}, 'Unrecognized function type.');
622+
});
623+
624+
test('proper getMinifySafeName usage with arrow function', () => {
619625
function n() {}
620626
const safeName = utils.getMinifySafeName(() => n);
621627
assert.equal(safeName, 'n');
628+
});
629+
630+
test('proper getMinifySafeName usage with regular function', () => {
631+
function n() {}
632+
const safeName = utils.getMinifySafeName(function () {
633+
return n;
634+
});
635+
assert.equal(safeName, 'n');
622636
});

0 commit comments

Comments
 (0)