Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resolver = "2"
#lto = true

[workspace.dependencies]
istanbul-oxide = { path = "./packages/istanbul-oxide", version = "0.0.23" }
istanbul-oxide = { path = "./packages/istanbul-oxide", version = "0.0.24" }
swc-coverage-instrument = { path = "./packages/swc-coverage-instrument" }

getrandom = { version = "0.2.15" }
Expand All @@ -25,3 +25,4 @@ resolver = "2"
tracing = { version = "0.1.37" }
tracing-subscriber = { version = "0.3.17" }
wasm-bindgen = { version = "0.2.92" }
wax = { version = "0.6.0" }
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swc-plugin-coverage-instrument",
"version": "0.0.23",
"version": "0.0.24",
"description": "SWC coverage instrumentation plugin",
"main": "./target/wasm32-wasi/release/swc_plugin_coverage.wasm",
"napi": {
Expand Down
2 changes: 1 addition & 1 deletion packages/istanbul-oxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "MIT"
name = "istanbul-oxide"
repository = "https://github.com/kwonoj/swc-coverage-instrument"
version = "0.0.23"
version = "0.0.24"

[dependencies]
indexmap = { workspace = true, features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion packages/swc-coverage-instrument/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "MIT"
name = "swc-coverage-instrument"
repository = "https://github.com/kwonoj/swc-coverage-instrument"
version = "0.0.23"
version = "0.0.24"

[dependencies]
istanbul-oxide = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion packages/swc-coverage-instrument/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ let visitor = swc_coverage_instrument::create_coverage_instrumentation_visitor(
let fold = as_folder(visitor);
```

`InstrumentationOptions` is a subset of istanbul's instrumentation options. Refer [istanbul's option](https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-instrument/src/instrumenter.js#L16-L27=) for the same configuration flags. For the logging, this package does not init any subscriber by itself. Caller should setup proper `tracing-subscriber` as needed.
`InstrumentationOptions` is a subset of istanbul's instrumentation options. Refer [istanbul's option](https://github.com/istanbuljs/istanbuljs/blob/master/packages/istanbul-lib-instrument/src/instrumenter.js#L16-L27=) for the same configuration flags. However there are few exceptions or differences, referencing [InstrumentOptions](https://github.com/kwonoj/swc-plugin-coverage-instrument/blob/main/packages/swc-coverage-instrument/src/options/instrument_options.rs) will list all possible options.

For the logging, this package does not init any subscriber by itself. Caller should setup proper `tracing-subscriber` as needed.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub struct InstrumentOptions {
pub input_source_map: Option<SourceMap>,
pub instrument_log: InstrumentLogOptions,
pub debug_initial_coverage_comment: bool,
// Allow to specify which files should be excluded from instrumentation.
// This option accepts an array of wax(https://crates.io/crates/wax)-compatible glob patterns
// and will match against the filename provided by swc's core.
pub unstable_exclude: Option<Vec<String>>,
}

impl Default for InstrumentOptions {
Expand All @@ -39,6 +43,7 @@ impl Default for InstrumentOptions {
input_source_map: Default::default(),
instrument_log: Default::default(),
debug_initial_coverage_comment: false,
unstable_exclude: Default::default(),
}
}
}
3 changes: 2 additions & 1 deletion packages/swc-plugin-coverage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "MIT"
name = "swc-plugin-coverage"
repository = "https://github.com/kwonoj/swc-coverage-instrument"
version = "0.0.23"
version = "0.0.24"

[lib]
crate-type = ["cdylib"]
Expand All @@ -16,3 +16,4 @@ swc-coverage-instrument = { workspace = true }
swc_core = { workspace = true, features = ["ecma_plugin_transform"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["fmt"] }
wax = { workspace = true }
18 changes: 18 additions & 0 deletions packages/swc-plugin-coverage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use swc_coverage_instrument::{
};

use tracing_subscriber::fmt::format::FmtSpan;
use wax::Pattern;

fn initialize_instrumentation_log(log_options: &InstrumentLogOptions) {
let log_level = match log_options.level.as_deref() {
Expand Down Expand Up @@ -57,6 +58,23 @@ pub fn process(program: Program, metadata: TransformPluginProgramMetadata) -> Pr
Default::default()
};

// Unstable option to exclude files from coverage. If pattern is wax(https://crates.io/crates/wax)
// compatible glob and the filename matches the pattern, the file will not be instrumented.
// Note that the filename is provided by swc's core, may not be the full absolute path to the file name.
if let Some(exclude) = &instrument_options.unstable_exclude {
match wax::any(exclude.iter().map(|s| s.as_ref()).collect::<Vec<&str>>()) {
Ok(p) => {
if p.is_match(filename) {
return program;
}
}
Err(e) => {
println!("Could not parse unstable_exclude option, will be ignored");
println!("{:#?}", e);
}
}
}

initialize_instrumentation_log(&instrument_options.instrument_log);

let visitor = create_coverage_instrumentation_visitor(
Expand Down
31 changes: 31 additions & 0 deletions spec/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assert } from "chai";
import { getCoverageMagicConstants } from "./swc-coverage-instrument-wasm/pkg/swc_coverage_instrument_wasm";
import { instrumentSync } from "./util/verifier";

// dummy: initiate wasm compilation before any test runs
getCoverageMagicConstants();
instrumentSync(`console.log('boo')`, "anon");

const tryDescribe = process.env.SWC_TRANSFORM_CUSTOM ? describe.skip : describe;

tryDescribe("Plugin options", () => {
it("should able to exclude", () => {
const code = `console.log('hello');`;

const output = instrumentSync(
code,
"somepath/file/excluded.js",
undefined,
{
unstableExclude: ["somepath/**/excluded.*"],
},
);

assert.equal(
output.code,
`"use strict";
${code}
`,
);
});
});
2 changes: 1 addition & 1 deletion spec/swc-coverage-custom-transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ napi-derive = { version = "2.12.3", default-features = false, features = [
] }
serde = { version = "1.0.160", features = ["derive"] }
serde_json = { version = "1.0.96", features = ["unbounded_depth"] }
swc-coverage-instrument = { version = "0.0.23", path = "../../packages/swc-coverage-instrument" }
swc-coverage-instrument = { version = "0.0.24", path = "../../packages/swc-coverage-instrument" }

swc_core = { version = "0.96.2", features = [
"common_concurrent",
Expand Down