buxlabs / abstract-syntax-tree

A library for working with abstract syntax trees.
MIT License
110 stars 13 forks source link

Improve `binaryExpressionReduction` to handle nested binary expressions #91

Closed emilos closed 4 years ago

emilos commented 4 years ago

Right now the reduction method can only optimize a flat expression like:

{
  type: 'BinaryExpression',
  operator: '+',
  left: { type: 'Literal', value: 'foo' },
  right: { type: 'Literal', value: 'bar' }
}

it would be great if it could optimize nested binary expressions too, e.g.:

{
  type: 'BinaryExpression',
  operator: '+',
  left: {
    type: 'BinaryExpression',
    operator: '+',
    left: { type: 'Literal', value: 'foo' },
    right: { type: 'Literal', value: 'bar' }
  },
  right: { type: 'Literal', value: 'baz' }
}