Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

astoptech

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astoptech - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

CHANGELOG.md

4

index.js
const binaryExpressionReduction = require('./src/binaryExpressionReduction')
const ifStatementRemoval = require('./src/ifStatementRemoval')
const negationOperatorRemoval = require('./src/negationOperatorRemoval')
const logicalExpressionReduction = require('./src/logicalExpressionReduction')

@@ -8,3 +9,4 @@ module.exports = {

ifStatementRemoval,
negationOperatorRemoval
negationOperatorRemoval,
logicalExpressionReduction
}
{
"name": "astoptech",
"version": "1.0.1",
"version": "1.0.2",
"description": "abstract syntax tree optimization techniques",
"main": "index.js",
"scripts": {
"test": "node src/binaryExpressionReduction.spec.js && node src/ifStatementRemoval.spec.js && node src/negationOperatorRemoval.spec.js"
"test": "node src/binaryExpressionReduction.spec.js && node src/ifStatementRemoval.spec.js && node src/negationOperatorRemoval.spec.js && node src/logicalExpressionReduction.spec.js"
},

@@ -26,4 +26,4 @@ "repository": {

"devDependencies": {
"abstract-syntax-tree": "^2.0.0"
"abstract-syntax-tree": "^2.1.0"
}
}

@@ -217,1 +217,39 @@ # astoptech

```
### logicalExpressionReduction
```js
const foo = "bar" || "baz"
```
The first value is truthy so it's safe to simplify the code.
```js
const foo = "bar"
```
The tree would be translated from:
```json
{
"type": "LogicalExpression",
"left": {
"type": "Literal",
"value": "bar"
},
"operator": "||",
"right": {
"type": "Literal",
"value": "baz"
}
}
```
To:
```json
{
"type": "Literal",
"value": "bar"
}
```

@@ -0,1 +1,18 @@

const ARITHMETIC_OPERATORS = ['+', '-', '*', '/', '%', '**']
function isArithmeticOperator (operator) {
return ARITHMETIC_OPERATORS.includes(operator)
}
function calculate (operator, left, right) {
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
}
}
module.exports = function binaryExpressionReduction (node) {

@@ -5,5 +22,6 @@ if (

node.left.type === 'Literal' &&
node.right.type === 'Literal'
node.right.type === 'Literal' &&
isArithmeticOperator(node.operator)
) {
return { type: 'Literal', value: node.left.value + node.right.value }
return { type: 'Literal', value: calculate(node.operator, node.left.value, node.right.value) }
}

@@ -10,0 +28,0 @@ return node

@@ -8,1 +8,22 @@ const AbstractSyntaxTree = require('abstract-syntax-tree')

assert.deepEqual(tree.source, 'const number = 4;\n')
var tree = new AbstractSyntaxTree('const number = 2 - 2;\n')
tree.replace({ enter: binaryExpressionReduction })
assert.deepEqual(tree.source, 'const number = 0;\n')
var tree = new AbstractSyntaxTree('const number = 2 * 2;\n')
tree.replace({ enter: binaryExpressionReduction })
assert.deepEqual(tree.source, 'const number = 4;\n')
var tree = new AbstractSyntaxTree('const number = 2 / 2;\n')
tree.replace({ enter: binaryExpressionReduction })
assert.deepEqual(tree.source, 'const number = 1;\n')
var tree = new AbstractSyntaxTree('const number = 5 % 2;\n')
tree.replace({ enter: binaryExpressionReduction })
assert.deepEqual(tree.source, 'const number = 1;\n')
var tree = new AbstractSyntaxTree('const number = 2 ** 3;\n')
tree.replace({ enter: binaryExpressionReduction })
assert.deepEqual(tree.source, 'const number = 8;\n')

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc