Skip to content

Commit

Permalink
Support exclude.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Nov 25, 2019
1 parent ac78901 commit 3c93a07
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 12 deletions.
66 changes: 56 additions & 10 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ impl Rc {
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Config {
#[serde(default)]
pub test: Option<String>,
pub test: Option<FileMatcher>,

#[serde(default)]
pub exclude: Option<FileMatcher>,

#[serde(default)]
pub jsc: JscConfig,
Expand All @@ -299,25 +302,68 @@ pub struct Config {
pub minify: Option<bool>,
}

impl Config {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FileMatcher {
Regex(String),
Multi(Vec<FileMatcher>),
}

impl Default for FileMatcher {
fn default() -> Self {
Self::Regex(String::from(""))
}
}

impl FileMatcher {
pub fn matches(&self, filename: &Path) -> Result<bool, Error> {
lazy_static! {
static ref CACHE: CHashMap<String, Regex> = 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<bool, Error> {
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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }, */
Expand Down
10 changes: 10 additions & 0 deletions tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
10 changes: 10 additions & 0 deletions tests/projects/issue-466-1/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"exclude": "^invalid.ts$",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": false
}
}
}
2 changes: 2 additions & 0 deletions tests/projects/issue-466-1/invalid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// If swc tries to parse this file, it will result in error.
;<>/k!
1 change: 1 addition & 0 deletions tests/projects/issue-466-1/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo from './module';
10 changes: 10 additions & 0 deletions tests/projects/issue-466-2/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"exclude": "vendor/",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": false
}
}
}
1 change: 1 addition & 0 deletions tests/projects/issue-466-2/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo from './module';
2 changes: 2 additions & 0 deletions tests/projects/issue-466-2/vendor/invalid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// If swc tries to parse this file, it will result in error.
;<>/k!

0 comments on commit 3c93a07

Please sign in to comment.