Skip to content

Commit 4689fc9

Browse files
authored
Merge pull request #238 from kwonoj/feat-unstable-exclude
2 parents f8557e9 + c093fa9 commit 4689fc9

File tree

12 files changed

+165
-12
lines changed

12 files changed

+165
-12
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resolver = "2"
1212
#lto = true
1313

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

1818
getrandom = { version = "0.2.15" }
@@ -25,3 +25,4 @@ resolver = "2"
2525
tracing = { version = "0.1.37" }
2626
tracing-subscriber = { version = "0.3.17" }
2727
wasm-bindgen = { version = "0.2.92" }
28+
wax = { version = "0.6.0" }

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swc-plugin-coverage-instrument",
3-
"version": "0.0.23",
3+
"version": "0.0.24",
44
"description": "SWC coverage instrumentation plugin",
55
"main": "./target/wasm32-wasi/release/swc_plugin_coverage.wasm",
66
"napi": {

packages/istanbul-oxide/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
license = "MIT"
66
name = "istanbul-oxide"
77
repository = "https://github.com/kwonoj/swc-coverage-instrument"
8-
version = "0.0.23"
8+
version = "0.0.24"
99

1010
[dependencies]
1111
indexmap = { workspace = true, features = ["serde"] }

packages/swc-coverage-instrument/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
license = "MIT"
66
name = "swc-coverage-instrument"
77
repository = "https://github.com/kwonoj/swc-coverage-instrument"
8-
version = "0.0.23"
8+
version = "0.0.24"
99

1010
[dependencies]
1111
istanbul-oxide = { workspace = true }

packages/swc-coverage-instrument/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ let visitor = swc_coverage_instrument::create_coverage_instrumentation_visitor(
2929
let fold = as_folder(visitor);
3030
```
3131

32-
`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.
32+
`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.
33+
34+
For the logging, this package does not init any subscriber by itself. Caller should setup proper `tracing-subscriber` as needed.

packages/swc-coverage-instrument/src/options/instrument_options.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub struct InstrumentOptions {
2727
pub input_source_map: Option<SourceMap>,
2828
pub instrument_log: InstrumentLogOptions,
2929
pub debug_initial_coverage_comment: bool,
30+
// Allow to specify which files should be excluded from instrumentation.
31+
// This option accepts an array of wax(https://crates.io/crates/wax)-compatible glob patterns
32+
// and will match against the filename provided by swc's core.
33+
pub unstable_exclude: Option<Vec<String>>,
3034
}
3135

3236
impl Default for InstrumentOptions {
@@ -39,6 +43,7 @@ impl Default for InstrumentOptions {
3943
input_source_map: Default::default(),
4044
instrument_log: Default::default(),
4145
debug_initial_coverage_comment: false,
46+
unstable_exclude: Default::default(),
4247
}
4348
}
4449
}

packages/swc-plugin-coverage/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
license = "MIT"
66
name = "swc-plugin-coverage"
77
repository = "https://github.com/kwonoj/swc-coverage-instrument"
8-
version = "0.0.23"
8+
version = "0.0.24"
99

1010
[lib]
1111
crate-type = ["cdylib"]
@@ -16,3 +16,4 @@ swc-coverage-instrument = { workspace = true }
1616
swc_core = { workspace = true, features = ["ecma_plugin_transform"] }
1717
tracing = { workspace = true }
1818
tracing-subscriber = { workspace = true, features = ["fmt"] }
19+
wax = { workspace = true }

packages/swc-plugin-coverage/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use swc_coverage_instrument::{
1010
};
1111

1212
use tracing_subscriber::fmt::format::FmtSpan;
13+
use wax::Pattern;
1314

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

61+
// Unstable option to exclude files from coverage. If pattern is wax(https://crates.io/crates/wax)
62+
// compatible glob and the filename matches the pattern, the file will not be instrumented.
63+
// Note that the filename is provided by swc's core, may not be the full absolute path to the file name.
64+
if let Some(exclude) = &instrument_options.unstable_exclude {
65+
match wax::any(exclude.iter().map(|s| s.as_ref()).collect::<Vec<&str>>()) {
66+
Ok(p) => {
67+
if p.is_match(filename) {
68+
return program;
69+
}
70+
}
71+
Err(e) => {
72+
println!("Could not parse unstable_exclude option, will be ignored");
73+
println!("{:#?}", e);
74+
}
75+
}
76+
}
77+
6078
initialize_instrumentation_log(&instrument_options.instrument_log);
6179

6280
let visitor = create_coverage_instrumentation_visitor(

spec/plugin.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { assert } from "chai";
2+
import { getCoverageMagicConstants } from "./swc-coverage-instrument-wasm/pkg/swc_coverage_instrument_wasm";
3+
import { instrumentSync } from "./util/verifier";
4+
5+
// dummy: initiate wasm compilation before any test runs
6+
getCoverageMagicConstants();
7+
instrumentSync(`console.log('boo')`, "anon");
8+
9+
const tryDescribe = process.env.SWC_TRANSFORM_CUSTOM ? describe.skip : describe;
10+
11+
tryDescribe("Plugin options", () => {
12+
it("should able to exclude", () => {
13+
const code = `console.log('hello');`;
14+
15+
const output = instrumentSync(
16+
code,
17+
"somepath/file/excluded.js",
18+
undefined,
19+
{
20+
unstableExclude: ["somepath/**/excluded.*"],
21+
},
22+
);
23+
24+
assert.equal(
25+
output.code,
26+
`"use strict";
27+
${code}
28+
`,
29+
);
30+
});
31+
});

spec/swc-coverage-custom-transform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ napi-derive = { version = "2.12.3", default-features = false, features = [
2121
] }
2222
serde = { version = "1.0.160", features = ["derive"] }
2323
serde_json = { version = "1.0.96", features = ["unbounded_depth"] }
24-
swc-coverage-instrument = { version = "0.0.23", path = "../../packages/swc-coverage-instrument" }
24+
swc-coverage-instrument = { version = "0.0.24", path = "../../packages/swc-coverage-instrument" }
2525

2626
swc_core = { version = "0.96.2", features = [
2727
"common_concurrent",

0 commit comments

Comments
 (0)