Skip to content

Commit

Permalink
feat(rust): support Comments::PreserveLegalComments (#2615)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
hyf0 authored Nov 6, 2024
1 parent 2863d64 commit 19de25e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ daachorse = "1.0.0"
dashmap = "6.0.0"
derivative = "2.2.0"
dunce = "1.0.4" # Normalize Windows paths to the most compatible format, avoiding UNC where possible
either = "1.13.0"
futures = "0.3.30"
glob = "0.3.1"
glob-match = "0.2.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"config": {
"comments": "preserveLegalComments"
},
"expectExecuted": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

## main.js

```js
//#region main.js
//! Some comments1
/*! Some comments2 */
foo;
bar;
//#endregion
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

//! Some comments1
/*! Some comments2 */
foo;bar;
Original file line number Diff line number Diff line change
Expand Up @@ -4018,6 +4018,10 @@ snapshot_kind: text
- main-!~{000}~.js => main-TEEZnbpv.js
# tests/rolldown/function/comments/preserve_legal_comments
- main-!~{000}~.js => main-v1eqisai.js
# tests/rolldown/function/define/node_env
- main-!~{000}~.js => main-281Y6vFN.js
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ anyhow = { workspace = true }
arcstr = { workspace = true }
bitflags = { workspace = true }
dashmap = { workspace = true }
either = { workspace = true }
glob-match = { workspace = true }
itertools = { workspace = true }
lightningcss = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ pub enum Comments {
None,
/// Keep comments as much as possible
Preserve,
/// Keep legal comments only
PreserveLegalComments,
}
15 changes: 7 additions & 8 deletions crates/rolldown_common/src/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{
use crate::{EcmaAstIdx, EcmaView, IndexModules, Interop, Module, ModuleType};
use std::ops::{Deref, DerefMut};

use either::Either;
use oxc::codegen::LegalComment;
use rolldown_ecmascript::{EcmaAst, EcmaCompiler, PrintOptions};
use rolldown_rstr::Rstr;
use rustc_hash::FxHashSet;
Expand Down Expand Up @@ -174,21 +176,18 @@ impl NormalModule {
ModuleRenderArgs::Ecma { ast } => {
let enable_sourcemap = options.sourcemap.is_some() && !self.is_virtual();

let enable_comments = match options.comments {
Comments::None => false,
Comments::Preserve => true,
let comments = match options.comments {
Comments::None => Either::Left(false),
Comments::Preserve => Either::Left(true),
Comments::PreserveLegalComments => Either::Right(LegalComment::Inline),
};

// Because oxc codegen sourcemap is last of sourcemap chain,
// If here no extra sourcemap need remapping, we using it as final module sourcemap.
// So here make sure using correct `source_name` and `source_content.
let render_output = EcmaCompiler::print_with(
ast,
PrintOptions {
sourcemap: enable_sourcemap,
filename: self.id.to_string(),
comments: enable_comments,
},
PrintOptions { sourcemap: enable_sourcemap, filename: self.id.to_string(), comments },
);
if !self.ecma_view.mutations.is_empty() {
let mut magic_string = string_wizard::MagicString::new(&render_output.code);
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ workspace = true
[dependencies]
anyhow = { workspace = true }
arcstr = { workspace = true }
either = { workspace = true }
oxc = { workspace = true }
rolldown_error = { workspace = true }
self_cell = { workspace = true }
12 changes: 9 additions & 3 deletions crates/rolldown_ecmascript/src/ecma_compiler.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::path::PathBuf;

use arcstr::ArcStr;
use either::Either;
use oxc::{
allocator::Allocator,
codegen::{CodeGenerator, Codegen, CodegenOptions, CodegenReturn},
codegen::{CodeGenerator, Codegen, CodegenOptions, CodegenReturn, LegalComment},
minifier::{Minifier, MinifierOptions},
parser::{ParseOptions, Parser},
sourcemap::SourceMap,
Expand Down Expand Up @@ -63,10 +64,15 @@ impl EcmaCompiler {
}

pub fn print_with(ast: &EcmaAst, options: PrintOptions) -> CodegenReturn {
let (is_print_full_comments, legal_comments) = match options.comments {
Either::Left(value) => (value, LegalComment::None),
Either::Right(value) => (false, value),
};
CodeGenerator::new()
.with_options(CodegenOptions {
comments: options.comments,
comments: is_print_full_comments,
source_map_path: options.sourcemap.then(|| PathBuf::from(options.filename)),
legal_comments,
..CodegenOptions::default()
})
.build(ast.program())
Expand Down Expand Up @@ -102,7 +108,7 @@ fn basic_test() {
}

pub struct PrintOptions {
pub comments: bool,
pub comments: Either</* is print full comments */ bool, LegalComment>,
pub filename: String,
pub sourcemap: bool,
}
7 changes: 7 additions & 0 deletions crates/rolldown_testing/_config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@
"enum": [
"preserve"
]
},
{
"description": "Keep legal comments only",
"type": "string",
"enum": [
"preserveLegalComments"
]
}
]
},
Expand Down

0 comments on commit 19de25e

Please sign in to comment.