Comparing version 1.0.4 to 1.0.5
# CHANGELOG | ||
## 1.0.5 | ||
- improve: logicalExpressionReduction | ||
## 1.0.4 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "astoptech", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "abstract syntax tree optimization techniques", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,3 @@ | ||
const isGlobalProperty = require('./utilities/isGlobalProperty') | ||
const LOGICAL_OPERATORS = ['&&', '||'] | ||
@@ -7,17 +9,52 @@ | ||
function getGlobalProperty (name) { | ||
switch (name) { | ||
case 'Infinity': return Infinity | ||
case 'NaN': return NaN | ||
case 'undefined': return undefined | ||
case 'null': return null | ||
} | ||
} | ||
function getNodeValue (node) { | ||
return isGlobalProperty(node) ? getGlobalProperty(node.name) : node.value | ||
} | ||
function identifier (name) { | ||
return { type: 'Identifier', name } | ||
} | ||
function literal (value) { | ||
return { type: 'Literal', value } | ||
} | ||
function serialize (value) { | ||
switch (value) { | ||
case Infinity: return identifier('Infinity') | ||
case NaN: return identifier('NaN') | ||
case undefined: return identifier('undefined') | ||
case null: return identifier('null') | ||
default: return literal(value) | ||
} | ||
} | ||
function evaluate (operator, left, right) { | ||
switch (operator) { | ||
case '&&': return left && right | ||
case '||': return left || right | ||
case '&&': return serialize(left && right) | ||
case '||': return serialize(left || right) | ||
} | ||
} | ||
function isNodeSupported (node) { | ||
return node.type === 'Literal' || isGlobalProperty(node) | ||
} | ||
module.exports = function logicalExpressionReduction (node) { | ||
if (node.type === 'LogicalExpression' && | ||
node.left.type === 'Literal' && | ||
node.right.type === 'Literal' && | ||
isNodeSupported(node.left) && | ||
isNodeSupported(node.right) && | ||
isLogicalOperator(node.operator)) { | ||
return { type: 'Literal', value: evaluate(node.operator, node.left.value, node.right.value) } | ||
return evaluate(node.operator, getNodeValue(node.left), getNodeValue(node.right)) | ||
} | ||
return node | ||
} |
@@ -5,8 +5,24 @@ const AbstractSyntaxTree = require('abstract-syntax-tree') | ||
var tree = new AbstractSyntaxTree('const foo = "bar" && "baz";\n') | ||
var tree = new AbstractSyntaxTree('const foo = "bar" && "baz"') | ||
tree.replace({ enter: logicalExpressionReduction }) | ||
assert.deepStrictEqual(tree.source, 'const foo = "baz";\n') | ||
var tree = new AbstractSyntaxTree('const foo = "bar" || "baz";\n') | ||
var tree = new AbstractSyntaxTree('const foo = "bar" || "baz"') | ||
tree.replace({ enter: logicalExpressionReduction }) | ||
assert.deepStrictEqual(tree.source, 'const foo = "bar";\n') | ||
var tree = new AbstractSyntaxTree('const foo = undefined || "foo"') | ||
tree.replace({ enter: logicalExpressionReduction }) | ||
assert.deepStrictEqual(tree.source, 'const foo = "foo";\n') | ||
var tree = new AbstractSyntaxTree('const foo = null || "foo"') | ||
tree.replace({ enter: logicalExpressionReduction }) | ||
assert.deepStrictEqual(tree.source, 'const foo = "foo";\n') | ||
var tree = new AbstractSyntaxTree('const foo = NaN || "foo"') | ||
tree.replace({ enter: logicalExpressionReduction }) | ||
assert.deepStrictEqual(tree.source, 'const foo = "foo";\n') | ||
var tree = new AbstractSyntaxTree('const foo = Infinity || "foo"') | ||
tree.replace({ enter: logicalExpressionReduction }) | ||
assert.deepStrictEqual(tree.source, 'const foo = Infinity;\n') |
@@ -1,7 +0,3 @@ | ||
const GLOBAL_PROPERTIES = ['Infinity', 'NaN', 'undefined', 'null'] | ||
const isGlobalProperty = require('./utilities/isGlobalProperty') | ||
function isGlobalProperty (node) { | ||
return node.type === 'Identifier' && GLOBAL_PROPERTIES.includes(node.name) | ||
} | ||
function getTypeOfGlobalProperty (name) { | ||
@@ -8,0 +4,0 @@ switch (name) { |
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
28719
19
436