Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of unnecessary grouping operators #6

Merged
merged 11 commits into from
Jun 11, 2019
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ function minifier(safe_words, mutate_storages) {
return through(mutate)

function mutate(node) {
// remove unnecessary grouping operators
if(is_unnecessary_group(node)) return

if(node.parent) for(var current = node.parent; current.parent; current = current.parent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For stylistic/readability purposes, can you give this if statement braces, and encapsulate the for loop within it?

if(is_unnecessary_group(current)) {
current.parent.children[current.parent.children.indexOf(current)] = current.children[0]
current.children[0].parent = current.parent
}
}

if(should_mutate(node)) {
var t = node.parent.parent.children[1]
if(mutate_storages || (t.type === 'placeholder' || t.token.data === 'const')) {
Expand All @@ -45,4 +55,8 @@ function minifier(safe_words, mutate_storages) {
return base &&
!safe_words.hasOwnProperty(node.token.data)
}

function is_unnecessary_group(node) {
return node.type === 'group' && node.children[0].lbp >= node.parent.lbp
}
}