Skip to content

Remove console/debugger's parent statement if it's conditional#3453

Closed
heavyk wants to merge 2 commits intobabel:masterfrom
heavyk:remove-console-debugger
Closed

Remove console/debugger's parent statement if it's conditional#3453
heavyk wants to merge 2 commits intobabel:masterfrom
heavyk:remove-console-debugger

Conversation

@heavyk
Copy link
Copy Markdown

@heavyk heavyk commented Apr 5, 2016

if (debug) console.log('a debug statement')
if (something == 'else') debugger
switch (...) {
  case '...':
  // ...
  default:
    debugger
}

the if statements will produce an error that says: statement expected but null found (because an if-statement must have a body). this PR attempts to correct that by checking to see if the path's parent is the statement's parent.

the logic took me a bit to figure out because:

  • for debugger (maybe because it's not a statement?), if the statement's parent is the debugger statement's direct parent, remove it (not totally sure about this one)
  • for console the opposite: if the statement's parent is not the path's direct parent (like an if-statement) remove it

I would really like to know if there is any improvement that can be made to this PR - helper methods I missed or something else?

@heavyk heavyk force-pushed the remove-console-debugger branch from 52a5ef4 to 279d337 Compare April 6, 2016 21:02
@loganfsmyth
Copy link
Copy Markdown
Member

I'd recommend a different approach to this more along the lines of what we do here: https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/lib/removal-hooks.js#L9

Along the lines of:

if (parent.isIfStatement() && (self.key === 'consequent' || self.key === 'alternate')){
    self.replaceWith(t.blockStatement([]));
}

so

if (something == 'else') debugger

would become

if (something == 'else') {}

@heavyk
Copy link
Copy Markdown
Author

heavyk commented Apr 7, 2016

thanks! that's exactly what I was looking for! I'll update tomorrow and add tests too

@loganfsmyth
Copy link
Copy Markdown
Member

Thanks!

@hzoo hzoo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories awaiting reply labels Apr 19, 2016
@danez
Copy link
Copy Markdown
Member

danez commented Jul 18, 2016

Closing in favor of #3583

@danez danez closed this Jul 18, 2016
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 7, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

awaiting reply outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants