Skip to content

Commit

Permalink
refactor: introduce ensureNoLineTerminator
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Sep 29, 2022
1 parent 09b3980 commit 9dfae22
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ class Printer {
return this._indentRepeat * this._indent;
}

ensureNoLineTerminator(fn: () => void) {
const { _noLineTerminator } = this;
this._noLineTerminator = true;
try {
fn();
} finally {
this._noLineTerminator = _noLineTerminator;
}
}

printTerminatorless(node: t.Node, parent: t.Node, isLabel: boolean) {
/**
* Set some state that will be modified if a newline has been inserted before any
Expand All @@ -482,9 +492,9 @@ class Printer {
* `undefined` will be returned and not `foo` due to the terminator.
*/
if (isLabel) {
this._noLineTerminator = true;
this.print(node, parent);
this._noLineTerminator = false;
this.ensureNoLineTerminator(() => {
this.print(node, parent);
});
} else {
const terminatorState = {
printed: false,
Expand Down

0 comments on commit 9dfae22

Please sign in to comment.