Skip to content

Commit

Permalink
Treat bigints as unknown for now on all systems to avoid accidentally (
Browse files Browse the repository at this point in the history
…#2640)

passing them to Math.pow or having "null" wrongly as a literal value
  • Loading branch information
lukastaegert committed Jan 5, 2019
1 parent ee6db13 commit be7afed
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
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;
};

0 comments on commit be7afed

Please sign in to comment.