rspivak / slimit

SlimIt - a JavaScript minifier/parser in Python
MIT License
551 stars 94 forks source link

Switch default statement position not preserved (it is not always the last statement) #94

Open metatoaster opened 7 years ago

metatoaster commented 7 years ago

The intent of the following code is that every other number than 0 or 2 will trigger the default condition, and every default condition will also trigger 2. However, the switch statement pulls that attribute out and this results in loss of information which results in the code not being correctly regenerated (i.e. the default is the last to be printed, instead of being printed before case 2: case statements).

>>> tree = p.parse("""
... switch (v) {
...   case 0:
...     console.log('0');
...     break;
...   default:
...     console.log('default');
...   case 2:
...     console.log('2');
... };
... """)
>>> print(tree.to_ecma())
switch (v) {
  case 0:
    console.log('0');
    break;
  case 2:
    console.log('2');
  default:
    console.log('default');
}
;