test(es): Add tests for .swcrc (#4398)

This commit is contained in:
Donny/강동윤 2022-04-22 18:23:04 +09:00 committed by GitHub
parent 3afc4aec6f
commit 5134b4f9be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 8 deletions

View File

@ -0,0 +1,34 @@
[
{
"test": ".tsx?$",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true,
"dynamicImport": true
},
"transform": {
"react": {
"refresh": true
}
}
}
},
{
"test": ".jsx?$",
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": true,
"decorators": true,
"dynamicImport": true
},
"transform": {
"react": {
"refresh": true
}
}
}
}
]

View File

@ -1,16 +1,24 @@
use serde_json; use serde_json;
use super::Rc; use crate::{parse_swcrc, Options};
use crate::Options;
#[test] #[test]
fn object() { fn object() {
let _: Rc = serde_json::from_str(include_str!("object.json")).expect("failed to parse"); let rc = parse_swcrc(include_str!("object.json")).expect("failed to parse");
dbg!(&rc);
} }
#[test] #[test]
fn array() { fn array() {
let _: Rc = serde_json::from_str(include_str!("array.json")).expect("failed to parse"); let rc = parse_swcrc(include_str!("array.json")).expect("failed to parse");
dbg!(&rc);
}
#[test]
fn issue_4390() {
let rc = parse_swcrc(include_str!("issue-4390.json")).expect("failed to parse");
dbg!(&rc);
} }
#[test] #[test]

View File

@ -1269,6 +1269,12 @@ impl Compiler {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
fn load_swcrc(path: &Path) -> Result<Rc, Error> { fn load_swcrc(path: &Path) -> Result<Rc, Error> {
let content = read_to_string(path).context("failed to read config (.swcrc) file")?;
parse_swcrc(&content)
}
fn parse_swcrc(s: &str) -> Result<Rc, Error> {
fn convert_json_err(e: serde_json::Error) -> Error { fn convert_json_err(e: serde_json::Error) -> Error {
let line = e.line(); let line = e.line();
let column = e.column(); let column = e.column();
@ -1285,15 +1291,13 @@ fn load_swcrc(path: &Path) -> Result<Rc, Error> {
)) ))
} }
let content = read_to_string(path).context("failed to read config (.swcrc) file")?;
if let Ok(v) = serde_json::from_reader(StripComments::new( if let Ok(v) = serde_json::from_reader(StripComments::new(
content.trim_start_matches('\u{feff}').as_bytes(), s.trim_start_matches('\u{feff}').as_bytes(),
)) { )) {
return Ok(v); return Ok(v);
} }
serde_json::from_reader::<StripComments<&[u8]>, Config>(StripComments::new(content.as_bytes())) serde_json::from_reader::<StripComments<&[u8]>, Config>(StripComments::new(s.as_bytes()))
.map(Rc::Single) .map(Rc::Single)
.map_err(convert_json_err) .map_err(convert_json_err)
} }