Comparing version 1.0.1 to 1.0.2
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18595
13
250
255