mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 21:54:36 +03:00
test(es): Add tests for .swcrc
(#4398)
This commit is contained in:
parent
3afc4aec6f
commit
5134b4f9be
34
crates/swc/src/config/issue-4390.json
Normal file
34
crates/swc/src/config/issue-4390.json
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
@ -1,16 +1,24 @@
|
||||
use serde_json;
|
||||
|
||||
use super::Rc;
|
||||
use crate::Options;
|
||||
use crate::{parse_swcrc, Options};
|
||||
|
||||
#[test]
|
||||
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]
|
||||
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]
|
||||
|
@ -1269,6 +1269,12 @@ impl Compiler {
|
||||
|
||||
#[tracing::instrument(level = "info", skip_all)]
|
||||
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 {
|
||||
let line = e.line();
|
||||
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(
|
||||
content.trim_start_matches('\u{feff}').as_bytes(),
|
||||
s.trim_start_matches('\u{feff}').as_bytes(),
|
||||
)) {
|
||||
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_err(convert_json_err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user