Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for BigInt literals when using the acorn plugin #2640

Merged
merged 1 commit into from Jan 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -66,6 +66,7 @@
"@types/chokidar": "^1.7.5",
"@types/minimist": "^1.2.0",
"@types/pretty-ms": "^3.0.0",
"acorn-bigint": "^0.3.1",
"acorn-dynamic-import": "^4.0.0",
"acorn-import-meta": "^0.3.0",
"acorn-jsx": "^5.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/ast/nodes/BinaryExpression.ts
Expand Up @@ -28,7 +28,8 @@ const binaryOperators: {
'|': (left: any, right: any) => left | right,
'^': (left: any, right: any) => left ^ right,
'&': (left: any, right: any) => left & right,
'**': (left: any, right: any) => Math.pow(left, right),
// At the moment, "**" will be transpiled to Math.pow
'**': (left: any, right: any) => left ** right,
in: () => UNKNOWN_VALUE,
instanceof: () => UNKNOWN_VALUE
};
Expand Down
10 changes: 7 additions & 3 deletions src/ast/nodes/Literal.ts
Expand Up @@ -28,11 +28,15 @@ export default class Literal<T = LiteralValue> extends NodeBase {
private members: { [key: string]: MemberDescription };

getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown {
if (path.length > 0) {
if (
path.length > 0 ||
// unknown literals such as bigints can also be null but do not start with an "n"
(this.value === null && this.context.code.charCodeAt(this.start) !== 110) ||
typeof this.value === 'bigint'
) {
return UNKNOWN_VALUE;
}
// not sure why we need this type cast here
return <any>this.value;
return this.value as any;
}

getReturnExpressionWhenCalledAtPath(path: ObjectPath) {
Expand Down
8 changes: 8 additions & 0 deletions test/form/samples/big-int/_config.js
@@ -0,0 +1,8 @@
const bigInt = require('acorn-bigint');

module.exports = {
description: 'supports bigint via acorn plugin',
options: {
acornInjectPlugins: [bigInt]
}
};
20 changes: 20 additions & 0 deletions test/form/samples/big-int/_expected.js
@@ -0,0 +1,20 @@
if (1n > 0) {
console.log('ok');
}

if (1n + 1n > 0) {
console.log('ok');
}

if (1n ** 1n > 0) {
console.log('ok');
}

const max = 2n**64n;
const min = -max;

function isSafe (int) {
return min<=int && int<=max;
}

export default isSafe;
18 changes: 18 additions & 0 deletions test/form/samples/big-int/main.js
@@ -0,0 +1,18 @@
if (1n > 0) {
console.log('ok');
}

if (1n + 1n > 0) {
console.log('ok');
}

if (1n ** 1n > 0) {
console.log('ok');
}

const max = 2n**64n;
const min = -max;

export default function isSafe (int) {
return min<=int && int<=max;
};