Skip to content

Commit

Permalink
Fix throw statement with leading comment on argument (#5932)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Feb 28, 2021
1 parent 349302c commit c2a98c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/shared/babel-ast-utils/src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,21 @@ export const generator = {
GENERATOR.ReturnStatement.call(this, node, state);
}
},
ThrowStatement(node, state) {
// Add parentheses if there are leading comments
if (node.argument?.leadingComments?.length > 0) {
let indent = state.indent.repeat(state.indentLevel);
state.write('throw (' + state.lineEnd);
state.write(indent + state.indent);
state.indentLevel++;
this[node.argument.type](node.argument, state);
state.indentLevel--;
state.write(state.lineEnd);
state.write(indent + ');');
} else {
GENERATOR.ThrowStatement.call(this, node, state);
}
},
};

// Make every node support comments. Important for preserving /*@__PURE__*/ comments for terser.
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/babel-ast-utils/test/fixtures/control.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
throw (
/*comment*/
new Error("abc")
);
if (a > b) {} else {}
if (c != d) {}
var a = b > c ? d : e;
Expand Down

0 comments on commit c2a98c1

Please sign in to comment.