From 4e13c49965315f0ec20b6c187c07c1f180855c75 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Sat, 5 Nov 2022 12:36:03 +0800 Subject: [PATCH] fix --- .../babel-generator/src/generators/methods.ts | 1 - packages/babel-generator/src/printer.ts | 43 ++++++++++++------- .../inner-line-commens-func/input.js | 9 ++++ .../inner-line-commens-func/output.js | 9 ++++ 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js create mode 100644 packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts index dd16d761cabb..5d30ac98453d 100644 --- a/packages/babel-generator/src/generators/methods.ts +++ b/packages/babel-generator/src/generators/methods.ts @@ -10,7 +10,6 @@ export function _params( this.token("("); this._parameters(node.params, node); this.token(")"); - this._noLineTerminator = true; this.print(node.returnType, node, node.type === "ArrowFunctionExpression"); } diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index 53d3f736e9cb..9a11289bad77 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -23,6 +23,7 @@ const ZERO_DECIMAL_INTEGER = /\.0+$/; const NON_DECIMAL_LITERAL = /^0[box]/; const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/; const HAS_NEWLINE = /[\n\r\u2028\u2029]/; +const HAS_BlOCK_COMMENT_END = /\*\//; const { needsParens } = n; @@ -904,7 +905,13 @@ class Printer { if (this._printedComments.has(comment)) return false; - if (this._noLineTerminator && HAS_NEWLINE.test(comment.value)) { + const noLineTerminator = this._noLineTerminator; + + if ( + noLineTerminator && + (HAS_NEWLINE.test(comment.value) || + HAS_BlOCK_COMMENT_END.test(comment.value)) + ) { return true; } @@ -957,7 +964,7 @@ class Printer { val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`); } - } else if (!this._noLineTerminator) { + } else if (!noLineTerminator) { val = `//${comment.value}`; } else { // It was a single-line comment, so it's guaranteed to not @@ -972,7 +979,7 @@ class Printer { this.source("start", comment.loc); this._append(val, isBlockComment); - if (!isBlockComment && !this._noLineTerminator) { + if (!isBlockComment && !noLineTerminator) { this.newline(1, true); } @@ -997,12 +1004,16 @@ class Printer { const nodeEndLine = hasLoc ? nodeLoc.end.line : 0; let lastLine = 0; let leadingCommentNewline = 0; - const { _noLineTerminator } = this; + + const maybeNewline = this._noLineTerminator + ? function () {} + : this.newline.bind(this); for (let i = 0; i < len; i++) { const comment = comments[i]; - if (hasLoc && comment.loc && !this._printedComments.has(comment)) { + const printed = this._printedComments.has(comment); + if (hasLoc && comment.loc && !printed) { const commentStartLine = comment.loc.start.line; const commentEndLine = comment.loc.end.line; if (type === COMMENT_TYPE.LEADING) { @@ -1022,11 +1033,11 @@ class Printer { } lastLine = commentEndLine; - if (!_noLineTerminator) this.newline(offset); + maybeNewline(offset); this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL); - if (!_noLineTerminator && i + 1 === len) { - this.newline( + if (i + 1 === len) { + maybeNewline( Math.max(nodeStartLine - lastLine, leadingCommentNewline), ); lastLine = nodeStartLine; @@ -1036,11 +1047,11 @@ class Printer { commentStartLine - (i === 0 ? nodeStartLine : lastLine); lastLine = commentEndLine; - if (!_noLineTerminator) this.newline(offset); + maybeNewline(offset); if (this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL)) break; - if (!_noLineTerminator && i + 1 === len) { - this.newline(Math.min(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger + if (i + 1 === len) { + maybeNewline(Math.min(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger lastLine = nodeEndLine; } } else { @@ -1048,16 +1059,18 @@ class Printer { commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine); lastLine = commentEndLine; - if (!_noLineTerminator) this.newline(offset); + maybeNewline(offset); this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL); } } else { hasLoc = false; + if (printed) continue; + if (len === 1) { const singleLine = comment.loc ? comment.loc.start.line === comment.loc.end.line - : !comment.value.includes("\n"); + : !HAS_NEWLINE.test(comment.value); const shouldSkipNewline = singleLine && @@ -1091,7 +1104,7 @@ class Printer { // /*:: b: ?string*/ // } - const skippedDueToNewlie = this._printComment( + const skippedDueToNewline = this._printComment( comment, i === 0 ? COMMENT_SKIP_NEWLINE.SKIP_LEADING @@ -1099,7 +1112,7 @@ class Printer { ? COMMENT_SKIP_NEWLINE.SKIP_TRAILING : COMMENT_SKIP_NEWLINE.DEFAULT, ); - if (skippedDueToNewlie) break; + if (skippedDueToNewline) break; } else { this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT); } diff --git a/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js new file mode 100644 index 000000000000..52b4537273a0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js @@ -0,0 +1,9 @@ +// https://github.com/babel/babel/issues/15132 + +var gen_bitlen = function gen_bitlen(s, desc) // deflate_state *s; +// tree_desc *desc; /* the tree descriptor */ +{ +}; +async ( +// 11111 /* */ +) => {}; diff --git a/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js new file mode 100644 index 000000000000..9fa0386d11ee --- /dev/null +++ b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js @@ -0,0 +1,9 @@ +// https://github.com/babel/babel/issues/15132 + +var gen_bitlen = function gen_bitlen(s, desc) +// deflate_state *s; +// tree_desc *desc; /* the tree descriptor */ +{}; +async ( + // 11111 /* */ +) => {}; \ No newline at end of file