Skip to content

Commit

Permalink
fix(es): Respect exclude option (#6054)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedLamineAllal authored Oct 6, 2022
1 parent 02a0cd3 commit 69da081
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,12 @@ impl Compiler {
self.run(move || {
let _timer = timer!("Compiler.parse");

if let FileName::Real(ref path) = name {
if !opts.config.matches(path)? {
return Ok(None);
}
}

let config = self.read_config(opts, name)?;
let config = match config {
Some(v) => v,
Expand Down
98 changes: 96 additions & 2 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::{
use rayon::prelude::*;
use swc::{
config::{
BuiltInput, Config, IsModule, JscConfig, ModuleConfig, Options, SourceMapsConfig,
TransformConfig,
BuiltInput, Config, FileMatcher, IsModule, JscConfig, ModuleConfig, Options,
SourceMapsConfig, TransformConfig,
},
Compiler, TransformOutput,
};
Expand Down Expand Up @@ -972,3 +972,97 @@ fn bom() {
fn json_schema() {
project("tests/projects/json-schema")
}

#[test]
fn issue_6009() {
// any of the files to not be excluded should contains
// `export function hello(){`
let files_to_not_excludes = ["input.ts"];
// file to be excluded should contain the pattern `.*.spec.ts`
let files_to_exclude = ["input.spec.ts"];

testing::run_test2(false, |cm, handler| {
let c = swc::Compiler::new(cm.clone());

let get_fm = |file_name: &str| {
let full_path_str = format!("{}{}", "tests/projects/issue-6009/", file_name);
let file_path = Path::new(&full_path_str);
cm.load_file(file_path).expect("failed to load file")
};

let get_options = |exclude: Option<FileMatcher>| Options {
config: Config {
exclude,
jsc: JscConfig {
syntax: Some(Syntax::Typescript(TsConfig {
..Default::default()
})),
..Default::default()
},
..Default::default()
},
..Default::default()
};

for file in files_to_not_excludes {
let result = c.process_js_file(
get_fm(file),
&handler,
&get_options(Some(FileMatcher::Regex(".*\\.spec.ts$".into()))),
);

match result {
Ok(r) => {
assert!(
r.code.contains("export function hello() {"),
"Failed to compile! it doesn't contain the right code! `export function \
hello() {{`"
);
}
Err(out) => panic!("Failed to compile where it should not!\nErr:{:?}", out),
}
}

for file in files_to_exclude {
let fm = get_fm(file);
let options = get_options(Some(FileMatcher::Regex(".*\\.spec.ts$".into())));

let result = c.process_js_file(fm.clone(), &handler, &options);

match result {
Ok(out) => {
panic!(
"Expected to return an error because the file is being excluded. And that \
didn't happen!\nTransformOutput: {:?}",
out
);
}
Err(err) => {
let expected_error_msg = "failed to process input file";

assert!(
err.to_string().contains(expected_error_msg),
"Not expected error! received: {}, expected: {}",
&err.to_string(),
expected_error_msg
)
}
}

// test parsing input
let config = c
.parse_js_as_input(fm.clone(), None, &handler, &options, &fm.name, None, |_| {
noop()
})
.unwrap();

assert!(
config.is_none(),
"config should be None when file excluded. But got a no None value instead!"
);
}

Ok(())
})
.unwrap()
}
10 changes: 10 additions & 0 deletions crates/swc/tests/projects/issue-6009/input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* The whole files is about testing exclude and test file matching through options
*/
import { hello } from './input';

describe('This file gonna be ignored', () => {
test('hello return', () => {
expect(hello()).toBe('Hello SWC!');
});
});
6 changes: 6 additions & 0 deletions crates/swc/tests/projects/issue-6009/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The whole files is about testing exclude and test file matching through options
*/
export function hello() {
return 'Hello SWC!';
}

1 comment on commit 69da081

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 69da081 Previous: d65fba1 Ratio
es/full/minify/libraries/antd 1914086265 ns/iter (± 63729396) 1947993696 ns/iter (± 63219512) 0.98
es/full/minify/libraries/d3 367413519 ns/iter (± 22992611) 386147302 ns/iter (± 12932743) 0.95
es/full/minify/libraries/echarts 1516729518 ns/iter (± 89748843) 1558655987 ns/iter (± 61909697) 0.97
es/full/minify/libraries/jquery 103414747 ns/iter (± 7051978) 92020873 ns/iter (± 3383754) 1.12
es/full/minify/libraries/lodash 121454306 ns/iter (± 10733408) 140714902 ns/iter (± 10016678) 0.86
es/full/minify/libraries/moment 56801421 ns/iter (± 9698762) 57052134 ns/iter (± 2601345) 1.00
es/full/minify/libraries/react 20339182 ns/iter (± 3770173) 20372859 ns/iter (± 1844716) 1.00
es/full/minify/libraries/terser 284026230 ns/iter (± 27631240) 296549906 ns/iter (± 21346058) 0.96
es/full/minify/libraries/three 490900197 ns/iter (± 24119067) 536828296 ns/iter (± 44775035) 0.91
es/full/minify/libraries/typescript 3276667367 ns/iter (± 73237589) 3521607016 ns/iter (± 169638800) 0.93
es/full/minify/libraries/victory 750583365 ns/iter (± 31191868) 809157952 ns/iter (± 68307913) 0.93
es/full/minify/libraries/vue 136964868 ns/iter (± 16935321) 142169357 ns/iter (± 15623321) 0.96
es/full/codegen/es3 33930 ns/iter (± 944) 33867 ns/iter (± 1000) 1.00
es/full/codegen/es5 33731 ns/iter (± 575) 33767 ns/iter (± 2023) 1.00
es/full/codegen/es2015 33888 ns/iter (± 1280) 33889 ns/iter (± 3629) 1.00
es/full/codegen/es2016 33845 ns/iter (± 1215) 34648 ns/iter (± 2768) 0.98
es/full/codegen/es2017 33759 ns/iter (± 809) 33841 ns/iter (± 2922) 1.00
es/full/codegen/es2018 33619 ns/iter (± 1452) 33797 ns/iter (± 6660) 0.99
es/full/codegen/es2019 33594 ns/iter (± 546) 34331 ns/iter (± 7694) 0.98
es/full/codegen/es2020 33632 ns/iter (± 699) 33813 ns/iter (± 563) 0.99
es/full/all/es3 191409475 ns/iter (± 7590145) 207421605 ns/iter (± 39893428) 0.92
es/full/all/es5 182156167 ns/iter (± 9846303) 198959695 ns/iter (± 25940098) 0.92
es/full/all/es2015 151710007 ns/iter (± 16116881) 154907698 ns/iter (± 9539175) 0.98
es/full/all/es2016 154261408 ns/iter (± 8363714) 154884859 ns/iter (± 15983923) 1.00
es/full/all/es2017 151239787 ns/iter (± 7045717) 156548110 ns/iter (± 14974471) 0.97
es/full/all/es2018 143006667 ns/iter (± 4688939) 152083592 ns/iter (± 16740783) 0.94
es/full/all/es2019 142677036 ns/iter (± 8067173) 149614349 ns/iter (± 9523247) 0.95
es/full/all/es2020 137329130 ns/iter (± 4483117) 139844741 ns/iter (± 5116408) 0.98
es/full/parser 737741 ns/iter (± 25211) 734882 ns/iter (± 25302) 1.00
es/full/base/fixer 26397 ns/iter (± 2878) 26058 ns/iter (± 429) 1.01
es/full/base/resolver_and_hygiene 94269 ns/iter (± 4866) 93856 ns/iter (± 1566) 1.00
serialization of ast node 215 ns/iter (± 4) 213 ns/iter (± 3) 1.01
serialization of serde 220 ns/iter (± 5) 220 ns/iter (± 3) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.