Skip to content

Commit

Permalink
fix(codegen): preserve newlines between comments (#6014)
Browse files Browse the repository at this point in the history
fixes #6010
  • Loading branch information
Boshen committed Sep 24, 2024
1 parent 5c323a2 commit 9ca202a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
23 changes: 13 additions & 10 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_syntax::identifier::is_line_terminator;
use rustc_hash::FxHashMap;

use oxc_ast::{Comment, CommentKind, Trivias};
use oxc_syntax::identifier::is_line_terminator;

use crate::Codegen;

Expand Down Expand Up @@ -34,24 +34,28 @@ impl<'a> Codegen<'a> {
let Some(source_text) = self.source_text else { return };
let Some(comments) = self.leading_comments.remove(&start) else { return };

let first = comments.first().unwrap();
if first.preceded_by_newline {
if comments.first().is_some_and(|c| c.preceded_by_newline) {
// Skip printing newline if this comment is already on a newline.
if self.peek_nth(0).is_some_and(|c| c != '\n' && c != '\t') {
self.print_char(b'\n');
self.print_hard_newline();
self.print_indent();
}
}

for comment in &comments {
let s = comment.real_span().source_text(source_text);
for (i, comment) in comments.iter().enumerate() {
if i >= 1 && comment.preceded_by_newline {
self.print_hard_newline();
self.print_indent();
}

let comment_source = comment.real_span().source_text(source_text);
match comment.kind {
CommentKind::Line => {
self.print_str(s);
self.print_str(comment_source);
}
CommentKind::Block => {
// Print block comments with our own indentation.
let lines = s.split(is_line_terminator);
let lines = comment_source.split(is_line_terminator);
for line in lines {
if !line.starts_with("/*") {
self.print_indent();
Expand All @@ -65,8 +69,7 @@ impl<'a> Codegen<'a> {
}
}

let last = comments.last().unwrap();
if last.is_line() || last.followed_by_newline {
if comments.last().is_some_and(|c| c.is_line() || c.followed_by_newline) {
self.print_hard_newline();
self.print_indent();
}
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_codegen/tests/integration/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ use crate::snapshot;
fn comment() {
let cases = vec![
r"
/**
* Top level
*
* @module
*/
/** This is a description of the foo function. */
function foo() {
}
/**
* Preserve newline
*/
/**
* Represents a book.
* @constructor
Expand All @@ -19,6 +29,10 @@ function Book(title, author) {
/** Class representing a point. */
class Point {
/**
* Preserve newline
*/
/**
* Create a point.
* @param {number} x - The x value.
Expand Down
28 changes: 27 additions & 1 deletion crates/oxc_codegen/tests/integration/snapshots/jsodc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ source: crates/oxc_codegen/tests/integration/main.rs
---
########## 0

/**
* Top level
*
* @module
*/

/** This is a description of the foo function. */
function foo() {
}

/**
* Preserve newline
*/

/**
* Represents a book.
* @constructor
Expand All @@ -18,6 +28,10 @@ function Book(title, author) {

/** Class representing a point. */
class Point {
/**
* Preserve newline
*/

/**
* Create a point.
* @param {number} x - The x value.
Expand Down Expand Up @@ -93,9 +107,17 @@ export enum DefinitionKind {
}

----------
/**
* Top level
*
* @module
*/
/** This is a description of the foo function. */
function foo() {}
/**
* Preserve newline
*/
/**
* Represents a book.
* @constructor
* @param {string} title - The title of the book.
Expand All @@ -104,6 +126,9 @@ function foo() {}
function Book(title, author) {}
/** Class representing a point. */
class Point {
/**
* Preserve newline
*/
/**
* Create a point.
* @param {number} x - The x value.
Expand Down Expand Up @@ -132,7 +157,8 @@ const Point = class {};
/**
* Shirt module.
* @module my/shirt
*//** Button the shirt. */
*/
/** Button the shirt. */
exports.button = function() {};
/** Unbutton the shirt. */
exports.unbutton = function() {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oxc_span::SourceType;
// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_isolated_declarations --example isolated_declarations`
// or `just watch "run -p oxc_isolated_declarations --example isolated_declarations"`
// or `just example isolated_declarations`

fn main() {
let name = env::args().nth(1).unwrap_or_else(|| "test.tsx".to_string());
Expand Down

0 comments on commit 9ca202a

Please sign in to comment.