Skip to content

Commit c876b08

Browse files
committed
feat(next/swc): support experimental coverage instrument
1 parent 42e3365 commit c876b08

File tree

9 files changed

+278
-16
lines changed

9 files changed

+278
-16
lines changed

packages/next-swc/Cargo.lock

Lines changed: 83 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/next-swc/crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ swc_ecma_loader = { version = "0.30.2", features = ["node", "lru"] }
3333
swc_ecmascript = { version = "0.164.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
3434
swc_plugin_runner = { version = "0.56.0", optional = true, default-features = false }
3535
swc_cached = "0.1.1"
36+
swc-coverage-instrument = "0.0.7"
3637
tracing = { version = "0.1.32", features = ["release_max_level_info"] }
3738
wasmer = { version = "2.3.0", optional = true, default-features = false }
3839
wasmer-wasi = { version = "2.3.0", optional = true, default-features = false }

packages/next-swc/crates/core/src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use swc_common::{FileName, SourceFile, SourceMap};
4343
use swc_ecmascript::ast::EsVersion;
4444
use swc_ecmascript::parser::parse_file_as_module;
4545
use swc_ecmascript::transforms::pass::noop;
46-
use swc_ecmascript::visit::Fold;
46+
use swc_ecmascript::visit::{as_folder, Fold};
4747

4848
pub mod amp_attributes;
4949
mod auto_cjs;
@@ -104,9 +104,12 @@ pub struct TransformOptions {
104104

105105
#[serde(default)]
106106
pub modularize_imports: Option<modularize_imports::Config>,
107+
108+
#[serde(default)]
109+
pub coverage_instrument: Option<swc_coverage_instrument::InstrumentOptions>,
107110
}
108111

109-
pub fn custom_before_pass<'a, C: Comments + 'a>(
112+
pub fn custom_before_pass<'a, C: Comments + 'a + std::clone::Clone>(
110113
cm: Arc<SourceMap>,
111114
file: Arc<SourceFile>,
112115
opts: &'a TransformOptions,
@@ -182,8 +185,8 @@ pub fn custom_before_pass<'a, C: Comments + 'a>(
182185
Either::Left(swc_emotion::EmotionTransformer::new(
183186
config.clone(),
184187
path,
185-
cm,
186-
comments,
188+
cm.clone(),
189+
comments.clone(),
187190
))
188191
})
189192
} else {
@@ -194,7 +197,18 @@ pub fn custom_before_pass<'a, C: Comments + 'a>(
194197
match &opts.modularize_imports {
195198
Some(config) => Either::Left(modularize_imports::modularize_imports(config.clone())),
196199
None => Either::Right(noop()),
197-
}
200+
},
201+
match &opts.coverage_instrument {
202+
Some(config) => Either::Left(as_folder(
203+
swc_coverage_instrument::create_coverage_instrumentation_visitor(
204+
cm,
205+
comments,
206+
config.clone(),
207+
file.name.to_string()
208+
)
209+
)),
210+
None => Either::Right(noop()),
211+
},
198212
)
199213
}
200214

packages/next-swc/crates/core/tests/fixture.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use next_swc::{
1010
};
1111
use std::path::PathBuf;
1212
use swc_common::{chain, comments::SingleThreadedComments, FileName, Mark};
13+
use swc_coverage_instrument::create_coverage_instrumentation_visitor;
1314
use swc_ecma_transforms_testing::{test, test_fixture};
1415
use swc_ecmascript::{
1516
parser::{EsConfig, Syntax},
@@ -209,3 +210,21 @@ fn shake_exports_fixture_default(input: PathBuf) {
209210
&output,
210211
);
211212
}
213+
214+
#[fixture("tests/fixture/coverage-instrument/input.js")]
215+
fn coverage_instrument_simple_default(input: PathBuf) {
216+
let output = input.parent().unwrap().join("output.js");
217+
test_fixture(
218+
syntax(),
219+
&|tr| {
220+
swc_ecmascript::visit::as_folder(create_coverage_instrumentation_visitor(
221+
tr.cm.clone(),
222+
tr.comments.clone(),
223+
Default::default(),
224+
"anon".to_string(),
225+
))
226+
},
227+
&input,
228+
&output,
229+
);
230+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function* x() { yield 1; yield 2; };
2+
var k;
3+
output = 0;
4+
for (k of x()) {
5+
output += k;
6+
}

0 commit comments

Comments
 (0)