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

Fix compact printing of non-null assertion operators #15496

Merged
merged 3 commits into from Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -244,12 +244,15 @@ class Printer {
token(str: string, maybeNewline = false): void {
this._maybePrintInnerComments();

// space is mandatory to avoid outputting <!--
// http://javascript.spec.whatwg.org/#comment-syntax
const lastChar = this.getLastChar();
const strFirst = str.charCodeAt(0);
if (
(lastChar === charCodes.exclamationMark && str === "--") ||
(lastChar === charCodes.exclamationMark &&
// space is mandatory to avoid outputting <!--
// http://javascript.spec.whatwg.org/#comment-syntax
(str === "--" ||
// Needs spaces to avoid changing a! == 0 to a!== 0
strFirst === charCodes.equalsTo)) ||
// Need spaces for operators of the same kind to avoid: `a+++b`
(strFirst === charCodes.plusSign && lastChar === charCodes.plusSign) ||
(strFirst === charCodes.dash && lastChar === charCodes.dash) ||
Expand Down
@@ -0,0 +1,5 @@
let foo;
foo! = 1;
if (foo! === 0) {
console.log();
}
@@ -0,0 +1,5 @@
{
"compact": true,
"sourceType": "module",
"plugins": ["typescript"]
}
@@ -0,0 +1 @@
let foo;foo! =1;if(foo! ===0){console.log();}