From 3c93a0729bc29cdb617ee619db34fa88a9e0347a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 25 Nov 2019 23:06:37 +0900 Subject: [PATCH] Support `exclude`. Fix #466 --- src/config/mod.rs | 66 +++++++++++++++++--- src/error.rs | 4 +- tests/projects.rs | 10 +++ tests/projects/issue-466-1/.swcrc | 10 +++ tests/projects/issue-466-1/invalid.ts | 2 + tests/projects/issue-466-1/main.ts | 1 + tests/projects/issue-466-2/.swcrc | 10 +++ tests/projects/issue-466-2/main.ts | 1 + tests/projects/issue-466-2/vendor/invalid.ts | 2 + 9 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 tests/projects/issue-466-1/.swcrc create mode 100644 tests/projects/issue-466-1/invalid.ts create mode 100644 tests/projects/issue-466-1/main.ts create mode 100644 tests/projects/issue-466-2/.swcrc create mode 100644 tests/projects/issue-466-2/main.ts create mode 100644 tests/projects/issue-466-2/vendor/invalid.ts diff --git a/src/config/mod.rs b/src/config/mod.rs index 6c37b28a522c..935711d8501d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -287,7 +287,10 @@ impl Rc { #[serde(deny_unknown_fields, rename_all = "camelCase")] pub struct Config { #[serde(default)] - pub test: Option, + pub test: Option, + + #[serde(default)] + pub exclude: Option, #[serde(default)] pub jsc: JscConfig, @@ -299,25 +302,68 @@ pub struct Config { pub minify: Option, } -impl Config { +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(untagged)] +pub enum FileMatcher { + Regex(String), + Multi(Vec), +} + +impl Default for FileMatcher { + fn default() -> Self { + Self::Regex(String::from("")) + } +} + +impl FileMatcher { pub fn matches(&self, filename: &Path) -> Result { lazy_static! { static ref CACHE: CHashMap = Default::default(); } - match self.test { - Some(ref test) => { - if !CACHE.contains_key(&*test) { - let re = Regex::new(&test).map_err(|err| Error::InvalidRegex { err })?; - CACHE.insert(test.clone(), re); + match self { + FileMatcher::Regex(ref s) => { + if !CACHE.contains_key(&*s) { + let re = Regex::new(&s).map_err(|err| Error::InvalidRegex { + regex: s.into(), + err, + })?; + CACHE.insert(s.clone(), re); + } + + let re = CACHE.get(&*s).unwrap(); + + Ok(re.is_match(&filename.to_string_lossy())) + } + FileMatcher::Multi(ref v) => { + // + for m in v { + if m.matches(filename)? { + return Ok(true); + } } - let re = CACHE.get(&*test).unwrap(); + Ok(false) + } + } + } +} - Ok(re.is_match(&filename.display().to_string())) +impl Config { + pub fn matches(&self, filename: &Path) -> Result { + if let Some(ref exclude) = self.exclude { + if exclude.matches(filename)? { + return Ok(false); + } + } + + if let Some(ref include) = self.test { + if include.matches(filename)? { + return Ok(true); } - None => Ok(true), } + + Ok(true) } } diff --git a/src/error.rs b/src/error.rs index 2942fdbe6bd1..90f9b8b1a8db 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,8 +28,8 @@ pub enum Error { #[fail(display = "sourcemap is not utf8: {}", err)] SourceMapNotUtf8 { err: FromUtf8Error }, - #[fail(display = "invalid regexp: {}", err)] - InvalidRegex { err: regex::Error }, + #[fail(display = "invalid regexp: {}: {}", regex, err)] + InvalidRegex { regex: String, err: regex::Error }, /* #[fail(display = "generated code is not utf8: {}", err)] * GeneratedCodeNotUtf8 { err: FromUtf8Error }, */ diff --git a/tests/projects.rs b/tests/projects.rs index 72707ea17b8c..191dd858c443 100644 --- a/tests/projects.rs +++ b/tests/projects.rs @@ -44,3 +44,13 @@ fn issue_467() { fn angular_core() { project("tests/projects/angular-core"); } + +#[test] +fn issue_466_1() { + project("tests/projects/issue-466-1"); +} + +#[test] +fn issue_466_2() { + project("tests/projects/issue-466-2"); +} diff --git a/tests/projects/issue-466-1/.swcrc b/tests/projects/issue-466-1/.swcrc new file mode 100644 index 000000000000..3f6d9db04c47 --- /dev/null +++ b/tests/projects/issue-466-1/.swcrc @@ -0,0 +1,10 @@ +{ + "exclude": "^invalid.ts$", + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false, + "decorators": false + } + } +} \ No newline at end of file diff --git a/tests/projects/issue-466-1/invalid.ts b/tests/projects/issue-466-1/invalid.ts new file mode 100644 index 000000000000..2e8dc811b4b8 --- /dev/null +++ b/tests/projects/issue-466-1/invalid.ts @@ -0,0 +1,2 @@ +// If swc tries to parse this file, it will result in error. +;<>/k! \ No newline at end of file diff --git a/tests/projects/issue-466-1/main.ts b/tests/projects/issue-466-1/main.ts new file mode 100644 index 000000000000..23f9bdc926b6 --- /dev/null +++ b/tests/projects/issue-466-1/main.ts @@ -0,0 +1 @@ +import foo from './module'; \ No newline at end of file diff --git a/tests/projects/issue-466-2/.swcrc b/tests/projects/issue-466-2/.swcrc new file mode 100644 index 000000000000..84a71c5b1314 --- /dev/null +++ b/tests/projects/issue-466-2/.swcrc @@ -0,0 +1,10 @@ +{ + "exclude": "vendor/", + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false, + "decorators": false + } + } +} \ No newline at end of file diff --git a/tests/projects/issue-466-2/main.ts b/tests/projects/issue-466-2/main.ts new file mode 100644 index 000000000000..23f9bdc926b6 --- /dev/null +++ b/tests/projects/issue-466-2/main.ts @@ -0,0 +1 @@ +import foo from './module'; \ No newline at end of file diff --git a/tests/projects/issue-466-2/vendor/invalid.ts b/tests/projects/issue-466-2/vendor/invalid.ts new file mode 100644 index 000000000000..2e8dc811b4b8 --- /dev/null +++ b/tests/projects/issue-466-2/vendor/invalid.ts @@ -0,0 +1,2 @@ +// If swc tries to parse this file, it will result in error. +;<>/k! \ No newline at end of file