Skip to content

Commit

Permalink
Rewrite in typescript and actually evaluate the AST instead of evaling
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay committed Feb 9, 2018
1 parent d9b4c71 commit 01d409c
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 191 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ logs
results
npm-debug.log
node_modules
lib
yarn.lock
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bracketSpacing": false,
"singleQuote": true,
"trailingComma": "all"
}
129 changes: 0 additions & 129 deletions index.js

This file was deleted.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
{
"name": "constantinople",
"version": "3.1.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"description": "Determine whether a JavaScript expression evaluates to a constant (using acorn)",
"keywords": [
"acorn",
"ast",
"tooling"
],
"dependencies": {
"@types/babylon": "^6.16.2",
"babel-types": "^6.16.0",
"babylon-walk": "^1.0.2",
"is-expression-babylon": "^1.1.0"
"babylon": "^6.18.0",
"babylon-walk": "^1.0.2"
},
"devDependencies": {
"mocha": "*"
"@types/node": "^9.4.4",
"mocha": "*",
"typescript": "^2.7.1"
},
"scripts": {
"prepublish": "npm run build",
"build": "tsc",
"pretest": "npm run build",
"test": "mocha -R spec"
},
"repository": {
Expand Down
76 changes: 76 additions & 0 deletions src/binaryOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export type Operator =
| '+'
| '-'
| '/'
| '%'
| '*'
| '**'
| '&'
| '|'
| '>>'
| '>>>'
| '<<'
| '^'
| '=='
| '==='
| '!='
| '!=='
| 'in'
| 'instanceof'
| '>'
| '<'
| '>='
| '<=';

export default function binaryOperation(
operator: Operator,
left: any,
right: any,
): any {
switch (operator) {
case '+':
return left + right;
case '-':
return left - right;
case '/':
return left / right;
case '%':
return left % right;
case '*':
return left * right;
case '**':
return left ** right;
case '&':
return left & right;
case '|':
return left | right;
case '>>':
return left >> right;
case '>>>':
return left >>> right;
case '<<':
return left << right;
case '^':
return left ^ right;
case '==':
return left == right;
case '===':
return left === right;
case '!=':
return left != right;
case '!==':
return left !== right;
case 'in':
return left in right;
case 'instanceof':
return left instanceof right;
case '>':
return left > right;
case '<':
return left < right;
case '>=':
return left >= right;
case '<=':
return left <= right;
}
}
Loading

3 comments on commit 01d409c

@ebenito
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrección de seguridad

@bananabr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the security issue here?

@bananabr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. Got it.

Please # to comment.