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.4 to 1.0.5

src/utilities/isGlobalProperty.js

4

CHANGELOG.md
# CHANGELOG
## 1.0.5
- improve: logicalExpressionReduction
## 1.0.4

@@ -4,0 +8,0 @@

2

package.json
{
"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) {

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