Skip to content

Commit

Permalink
feat(es): Support trailing commas in .swcrc (#6289)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanholmerin committed Oct 28, 2022
1 parent 9f39013 commit 244b0af
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc/Cargo.toml
Expand Up @@ -49,7 +49,7 @@ base64 = "0.13.0"
dashmap = "5.1.0"
either = "1"
indexmap = { version = "1.6.1", features = ["serde"] }
json_comments = "0.2.0"
jsonc-parser = { version = "0.21.0", features = ["serde"] }
lru = "0.7.1"
once_cell = "1.10.0"
parking_lot = "0.12.0"
Expand Down
5 changes: 5 additions & 0 deletions crates/swc/src/config/jsonc.json
@@ -0,0 +1,5 @@
{
"jsc": {
// comment & trailing comma
},
}
6 changes: 6 additions & 0 deletions crates/swc/src/config/tests.rs
Expand Up @@ -28,3 +28,9 @@ fn issue_1532() {
let err = res.expect_err("should fail");
assert!(err.to_string().contains("unknown variant `esnext`"));
}

#[test]
fn jsonc() {
let rc = parse_swcrc(include_str!("jsonc.json")).expect("failed to parse");
dbg!(&rc);
}
20 changes: 14 additions & 6 deletions crates/swc/src/lib.rs
Expand Up @@ -122,7 +122,7 @@ use anyhow::{bail, Context, Error};
use atoms::JsWord;
use common::{collections::AHashMap, comments::SingleThreadedComments, errors::HANDLER};
use config::{IsModule, JsMinifyCommentOption, JsMinifyOptions, OutputCharset};
use json_comments::StripComments;
use jsonc_parser::{parse_to_serde_value, ParseOptions};
use once_cell::sync::Lazy;
use serde::Serialize;
use serde_json::error::Category;
Expand Down Expand Up @@ -1175,13 +1175,21 @@ fn parse_swcrc(s: &str) -> Result<Rc, Error> {
))
}

if let Ok(v) = serde_json::from_reader(StripComments::new(
s.trim_start_matches('\u{feff}').as_bytes(),
)) {
return Ok(v);
let v = parse_to_serde_value(
s.trim_start_matches('\u{feff}'),
&ParseOptions {
allow_comments: true,
allow_trailing_commas: true,
allow_loose_object_property_names: false,
},
)?
.ok_or_else(|| Error::msg("failed to deserialize empty .swcrc (json) file"))?;

if let Ok(rc) = serde_json::from_value(v.clone()) {
return Ok(rc);
}

serde_json::from_reader::<StripComments<&[u8]>, Config>(StripComments::new(s.as_bytes()))
serde_json::from_value(v)
.map(Rc::Single)
.map_err(convert_json_err)
}
Expand Down

1 comment on commit 244b0af

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 244b0af Previous: 0376da7 Ratio
es/full/bugs-1 379746 ns/iter (± 34815) 375490 ns/iter (± 33572) 1.01
es/full/minify/libraries/antd 1917007793 ns/iter (± 61747902) 1989444821 ns/iter (± 108177648) 0.96
es/full/minify/libraries/d3 438675633 ns/iter (± 93823906) 426720872 ns/iter (± 16164038) 1.03
es/full/minify/libraries/echarts 1598880581 ns/iter (± 76288343) 1835654997 ns/iter (± 251193198) 0.87
es/full/minify/libraries/jquery 112824853 ns/iter (± 3622829) 114033253 ns/iter (± 17304024) 0.99
es/full/minify/libraries/lodash 128822271 ns/iter (± 5297865) 125193868 ns/iter (± 29535757) 1.03
es/full/minify/libraries/moment 65857880 ns/iter (± 4042870) 68482815 ns/iter (± 9789246) 0.96
es/full/minify/libraries/react 20263735 ns/iter (± 906821) 21124710 ns/iter (± 4148212) 0.96
es/full/minify/libraries/terser 381711869 ns/iter (± 46215732) 369794963 ns/iter (± 51121524) 1.03
es/full/minify/libraries/three 668138508 ns/iter (± 134363702) 674775646 ns/iter (± 110437041) 0.99
es/full/minify/libraries/typescript 4029384803 ns/iter (± 632741919) 4434437221 ns/iter (± 1191856652) 0.91
es/full/minify/libraries/victory 945692815 ns/iter (± 110554423) 995783465 ns/iter (± 99987727) 0.95
es/full/minify/libraries/vue 194450820 ns/iter (± 6896740) 210060577 ns/iter (± 12394326) 0.93
es/full/codegen/es3 38783 ns/iter (± 14798) 40660 ns/iter (± 6051) 0.95
es/full/codegen/es5 38610 ns/iter (± 4292) 43049 ns/iter (± 7382) 0.90
es/full/codegen/es2015 36017 ns/iter (± 3284) 46788 ns/iter (± 7279) 0.77
es/full/codegen/es2016 36659 ns/iter (± 7598) 42079 ns/iter (± 6872) 0.87
es/full/codegen/es2017 36057 ns/iter (± 138691) 45043 ns/iter (± 7847) 0.80
es/full/codegen/es2018 37054 ns/iter (± 8426) 39600 ns/iter (± 8035) 0.94
es/full/codegen/es2019 35643 ns/iter (± 2189) 40801 ns/iter (± 5802) 0.87
es/full/codegen/es2020 36208 ns/iter (± 4466) 42279 ns/iter (± 7049) 0.86
es/full/all/es3 253949255 ns/iter (± 42093371) 253417206 ns/iter (± 38615783) 1.00
es/full/all/es5 261283391 ns/iter (± 69178786) 245352716 ns/iter (± 43153142) 1.06
es/full/all/es2015 202696809 ns/iter (± 22266420) 185147796 ns/iter (± 115203301) 1.09
es/full/all/es2016 201557766 ns/iter (± 67587285) 209490296 ns/iter (± 42000210) 0.96
es/full/all/es2017 194487727 ns/iter (± 54424808) 211955454 ns/iter (± 55317427) 0.92
es/full/all/es2018 196565290 ns/iter (± 50287166) 200841622 ns/iter (± 27310943) 0.98
es/full/all/es2019 188730145 ns/iter (± 44587291) 197275826 ns/iter (± 54734301) 0.96
es/full/all/es2020 183392020 ns/iter (± 82509494) 194821511 ns/iter (± 37652085) 0.94
es/full/parser 848082 ns/iter (± 245263) 1046825 ns/iter (± 117561) 0.81
es/full/base/fixer 27567 ns/iter (± 1702) 36658 ns/iter (± 6550) 0.75
es/full/base/resolver_and_hygiene 95065 ns/iter (± 5640) 129103 ns/iter (± 36780) 0.74
serialization of ast node 216 ns/iter (± 10) 230 ns/iter (± 29) 0.94
serialization of serde 220 ns/iter (± 7) 275 ns/iter (± 50) 0.80

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.