From 69da0817982eda2e9e9e755e3526f83286e874b1 Mon Sep 17 00:00:00 2001 From: Mohamed Lamine Allal Date: Thu, 6 Oct 2022 04:18:03 +0100 Subject: [PATCH] fix(es): Respect exclude option (#6054) --- crates/swc/src/lib.rs | 6 ++ crates/swc/tests/projects.rs | 98 ++++++++++++++++++- .../tests/projects/issue-6009/input.spec.ts | 10 ++ crates/swc/tests/projects/issue-6009/input.ts | 6 ++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 crates/swc/tests/projects/issue-6009/input.spec.ts create mode 100644 crates/swc/tests/projects/issue-6009/input.ts diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index 725e39b7b0b0..e3c6b40cf623 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -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, diff --git a/crates/swc/tests/projects.rs b/crates/swc/tests/projects.rs index 6bc574741dea..1ac31c3d8d1b 100644 --- a/crates/swc/tests/projects.rs +++ b/crates/swc/tests/projects.rs @@ -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, }; @@ -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| 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() +} diff --git a/crates/swc/tests/projects/issue-6009/input.spec.ts b/crates/swc/tests/projects/issue-6009/input.spec.ts new file mode 100644 index 000000000000..117e19504b5f --- /dev/null +++ b/crates/swc/tests/projects/issue-6009/input.spec.ts @@ -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!'); + }); +}); \ No newline at end of file diff --git a/crates/swc/tests/projects/issue-6009/input.ts b/crates/swc/tests/projects/issue-6009/input.ts new file mode 100644 index 000000000000..59dd322be27c --- /dev/null +++ b/crates/swc/tests/projects/issue-6009/input.ts @@ -0,0 +1,6 @@ +/** + * The whole files is about testing exclude and test file matching through options + */ +export function hello() { + return 'Hello SWC!'; +}