Skip to content

Commit

Permalink
fix(es/utils): Fix string evaluation of array literals (#7731)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #7714.
  • Loading branch information
kdy1 authored Jul 31, 2023
1 parent ffe99a9 commit e8c58cf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"defaults": true,
"evaluate": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`${[1, 1]}`)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("1,1");
9 changes: 4 additions & 5 deletions crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,11 @@ pub trait ExprExt {
Unknown => return Value::Unknown,
})),
Expr::Array(ArrayLit { ref elems, .. }) => {
let mut first = true;
let mut buf = String::new();
let len = elems.len();
// null, undefined is "" in array literal.
for elem in elems {
for (idx, elem) in elems.iter().enumerate() {
let last = idx == len - 1;
let e = match *elem {
Some(ref elem) => {
let ExprOrSpread { ref expr, .. } = *elem;
Expand All @@ -1055,9 +1056,7 @@ pub trait ExprExt {
};
buf.push_str(&e);

if first {
first = false;
} else {
if !last {
buf.push(',');
}
}
Expand Down

1 comment on commit e8c58cf

@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: e8c58cf Previous: 5ea6f27 Ratio
es/full/bugs-1 286399 ns/iter (± 4146) 288684 ns/iter (± 7764) 0.99
es/full/minify/libraries/antd 1328947096 ns/iter (± 17678071) 1428074388 ns/iter (± 34502780) 0.93
es/full/minify/libraries/d3 280606412 ns/iter (± 4500215) 295115324 ns/iter (± 9258315) 0.95
es/full/minify/libraries/echarts 1065187174 ns/iter (± 13041819) 1160952815 ns/iter (± 6056683) 0.92
es/full/minify/libraries/jquery 84042464 ns/iter (± 191850) 87712407 ns/iter (± 257019) 0.96
es/full/minify/libraries/lodash 97953954 ns/iter (± 393353) 102312090 ns/iter (± 701308) 0.96
es/full/minify/libraries/moment 49764362 ns/iter (± 245158) 51674079 ns/iter (± 374407) 0.96
es/full/minify/libraries/react 17876598 ns/iter (± 43410) 18371072 ns/iter (± 187076) 0.97
es/full/minify/libraries/terser 218127902 ns/iter (± 1459706) 237454536 ns/iter (± 1823341) 0.92
es/full/minify/libraries/three 384823793 ns/iter (± 1834390) 425755143 ns/iter (± 3835225) 0.90
es/full/minify/libraries/typescript 2670755159 ns/iter (± 12225713) 2838904362 ns/iter (± 16417791) 0.94
es/full/minify/libraries/victory 559185076 ns/iter (± 7709760) 631880503 ns/iter (± 10110217) 0.88
es/full/minify/libraries/vue 120215591 ns/iter (± 1018985) 126555427 ns/iter (± 988338) 0.95
es/full/codegen/es3 33875 ns/iter (± 170) 34357 ns/iter (± 61) 0.99
es/full/codegen/es5 33851 ns/iter (± 209) 34388 ns/iter (± 45) 0.98
es/full/codegen/es2015 33970 ns/iter (± 183) 34400 ns/iter (± 89) 0.99
es/full/codegen/es2016 33918 ns/iter (± 109) 34363 ns/iter (± 84) 0.99
es/full/codegen/es2017 33961 ns/iter (± 99) 34266 ns/iter (± 59) 0.99
es/full/codegen/es2018 33870 ns/iter (± 128) 34300 ns/iter (± 69) 0.99
es/full/codegen/es2019 33919 ns/iter (± 189) 34410 ns/iter (± 77) 0.99
es/full/codegen/es2020 33981 ns/iter (± 119) 34410 ns/iter (± 54) 0.99
es/full/all/es3 172033698 ns/iter (± 1300662) 173394047 ns/iter (± 2037616) 0.99
es/full/all/es5 164568602 ns/iter (± 870303) 166595598 ns/iter (± 1067767) 0.99
es/full/all/es2015 124759133 ns/iter (± 689761) 124858951 ns/iter (± 1145887) 1.00
es/full/all/es2016 123593162 ns/iter (± 762039) 124675729 ns/iter (± 788220) 0.99
es/full/all/es2017 123432693 ns/iter (± 862394) 124242153 ns/iter (± 1000410) 0.99
es/full/all/es2018 121356533 ns/iter (± 649516) 121636958 ns/iter (± 762959) 1.00
es/full/all/es2019 120673560 ns/iter (± 844074) 120962138 ns/iter (± 1775998) 1.00
es/full/all/es2020 115957208 ns/iter (± 889161) 116094130 ns/iter (± 461004) 1.00
es/full/parser 541413 ns/iter (± 6017) 528731 ns/iter (± 6074) 1.02
es/full/base/fixer 18629 ns/iter (± 93) 19043 ns/iter (± 151) 0.98
es/full/base/resolver_and_hygiene 85729 ns/iter (± 269) 81188 ns/iter (± 282) 1.06
serialization of serde 292 ns/iter (± 0) 303 ns/iter (± 1) 0.96
css/minify/libraries/bootstrap 28983567 ns/iter (± 75257) 29500623 ns/iter (± 126101) 0.98
css/visitor/compare/clone 1634562 ns/iter (± 1876) 1653903 ns/iter (± 6777) 0.99
css/visitor/compare/visit_mut_span 1777442 ns/iter (± 4995) 1783264 ns/iter (± 9825) 1.00
css/visitor/compare/visit_mut_span_panic 1842665 ns/iter (± 4954) 1857520 ns/iter (± 5578) 0.99
css/visitor/compare/fold_span 2587596 ns/iter (± 10293) 2595607 ns/iter (± 10876) 1.00
css/visitor/compare/fold_span_panic 2790441 ns/iter (± 44723) 2800603 ns/iter (± 19116) 1.00
css/lexer/bootstrap_5_1_3 4415929 ns/iter (± 10152) 4488338 ns/iter (± 2412) 0.98
css/lexer/foundation_6_7_4 3713673 ns/iter (± 4149) 3790196 ns/iter (± 4896) 0.98
css/lexer/tailwind_3_1_1 707001 ns/iter (± 802) 721803 ns/iter (± 2307) 0.98
css/parser/bootstrap_5_1_3 19962858 ns/iter (± 203042) 20223160 ns/iter (± 33714) 0.99
css/parser/foundation_6_7_4 15996211 ns/iter (± 194593) 15867769 ns/iter (± 26062) 1.01
css/parser/tailwind_3_1_1 2973577 ns/iter (± 6598) 3085546 ns/iter (± 3969) 0.96
es/codegen/colors 734001 ns/iter (± 402502) 734079 ns/iter (± 402547) 1.00
es/codegen/large 2906199 ns/iter (± 1550505) 3132293 ns/iter (± 1662586) 0.93
es/codegen/with-parser/colors 44230 ns/iter (± 640) 44469 ns/iter (± 582) 0.99
es/codegen/with-parser/large 485408 ns/iter (± 1128) 479495 ns/iter (± 1127) 1.01
es/minify/libraries/antd 1289542832 ns/iter (± 13031214) 1208221748 ns/iter (± 9438274) 1.07
es/minify/libraries/d3 267925104 ns/iter (± 3386082) 245706691 ns/iter (± 1479692) 1.09
es/minify/libraries/echarts 1062099418 ns/iter (± 13575991) 961976843 ns/iter (± 6485582) 1.10
es/minify/libraries/jquery 78466506 ns/iter (± 792787) 74255114 ns/iter (± 400515) 1.06
es/minify/libraries/lodash 92724952 ns/iter (± 917078) 88535292 ns/iter (± 269280) 1.05
es/minify/libraries/moment 45095132 ns/iter (± 225468) 43636918 ns/iter (± 141999) 1.03
es/minify/libraries/react 16741212 ns/iter (± 91693) 16055801 ns/iter (± 79220) 1.04
es/minify/libraries/terser 211134318 ns/iter (± 1837773) 190502361 ns/iter (± 1100596) 1.11
es/minify/libraries/three 375950563 ns/iter (± 6741220) 332990818 ns/iter (± 1944729) 1.13
es/minify/libraries/typescript 2461919444 ns/iter (± 25180919) 2355118667 ns/iter (± 18650795) 1.05
es/minify/libraries/victory 507048454 ns/iter (± 6567209) 507695924 ns/iter (± 6840990) 1.00
es/minify/libraries/vue 111186873 ns/iter (± 1718221) 107766082 ns/iter (± 2339040) 1.03
es/visitor/compare/clone 1939320 ns/iter (± 9421) 1938914 ns/iter (± 6630) 1.00
es/visitor/compare/visit_mut_span 2278953 ns/iter (± 5167) 2294681 ns/iter (± 5804) 0.99
es/visitor/compare/visit_mut_span_panic 2328458 ns/iter (± 7894) 2341029 ns/iter (± 9921) 0.99
es/visitor/compare/fold_span 3358391 ns/iter (± 4924) 3345061 ns/iter (± 9341) 1.00
es/visitor/compare/fold_span_panic 3477161 ns/iter (± 5222) 3477041 ns/iter (± 10955) 1.00
es/lexer/colors 12621 ns/iter (± 47) 12606 ns/iter (± 38) 1.00
es/lexer/angular 6029159 ns/iter (± 38358) 5987055 ns/iter (± 15176) 1.01
es/lexer/backbone 797420 ns/iter (± 11212) 774959 ns/iter (± 2060) 1.03
es/lexer/jquery 4410517 ns/iter (± 7172) 4362268 ns/iter (± 2181) 1.01
es/lexer/jquery mobile 6836436 ns/iter (± 18808) 6690398 ns/iter (± 3901) 1.02
es/lexer/mootools 3533091 ns/iter (± 7947) 3469054 ns/iter (± 2156) 1.02
es/lexer/underscore 658332 ns/iter (± 494) 653014 ns/iter (± 718) 1.01
es/lexer/three 20995383 ns/iter (± 38049) 20532852 ns/iter (± 12933) 1.02
es/lexer/yui 3791901 ns/iter (± 1506) 3718732 ns/iter (± 1727) 1.02
es/parser/colors 25935 ns/iter (± 113) 26591 ns/iter (± 80) 0.98
es/parser/angular 13295092 ns/iter (± 64075) 13313499 ns/iter (± 87890) 1.00
es/parser/backbone 1964552 ns/iter (± 9820) 1973553 ns/iter (± 9791) 1.00
es/parser/jquery 10708081 ns/iter (± 34993) 10769732 ns/iter (± 56008) 0.99
es/parser/jquery mobile 16434661 ns/iter (± 109317) 16558841 ns/iter (± 133293) 0.99
es/parser/mootools 8253668 ns/iter (± 20940) 8264353 ns/iter (± 17855) 1.00
es/parser/underscore 1692111 ns/iter (± 7616) 1697882 ns/iter (± 32782) 1.00
es/parser/three 46168566 ns/iter (± 353694) 47463386 ns/iter (± 322197) 0.97
es/parser/yui 8167694 ns/iter (± 40035) 8167088 ns/iter (± 33584) 1.00
es/preset-env/usage/builtin_type 137477 ns/iter (± 32229) 139185 ns/iter (± 33031) 0.99
es/preset-env/usage/property 17125 ns/iter (± 131) 16884 ns/iter (± 75) 1.01
es/resolver/typescript 87075283 ns/iter (± 1039596) 89200349 ns/iter (± 1319658) 0.98
es/fixer/typescript 62843274 ns/iter (± 696003) 65869682 ns/iter (± 337041) 0.95
es/hygiene/typescript 134776265 ns/iter (± 1057500) 134848916 ns/iter (± 1718093) 1.00
es/resolver_with_hygiene/typescript 245215297 ns/iter (± 3335233) 248175965 ns/iter (± 2354553) 0.99
es/visitor/base-perf/module_clone 60802 ns/iter (± 349) 60241 ns/iter (± 702) 1.01
es/visitor/base-perf/fold_empty 64380 ns/iter (± 394) 63765 ns/iter (± 278) 1.01
es/visitor/base-perf/fold_noop_impl_all 64273 ns/iter (± 210) 63612 ns/iter (± 135) 1.01
es/visitor/base-perf/fold_noop_impl_vec 64017 ns/iter (± 343) 63690 ns/iter (± 264) 1.01
es/visitor/base-perf/boxing_boxed_clone 57 ns/iter (± 2) 59 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_unboxed_clone 38 ns/iter (± 0) 40 ns/iter (± 0) 0.95
es/visitor/base-perf/boxing_boxed 108 ns/iter (± 0) 106 ns/iter (± 0) 1.02
es/visitor/base-perf/boxing_unboxed 80 ns/iter (± 0) 75 ns/iter (± 0) 1.07
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2439 ns/iter (± 15) 2520 ns/iter (± 16) 0.97
es/base/parallel/resolver/typescript 4251056662 ns/iter (± 231647292) 3829210873 ns/iter (± 309462116) 1.11
es/base/parallel/hygiene/typescript 1493789283 ns/iter (± 31032257) 1455946036 ns/iter (± 17405461) 1.03
misc/visitors/time-complexity/time 5 137 ns/iter (± 6) 150 ns/iter (± 0) 0.91
misc/visitors/time-complexity/time 10 279 ns/iter (± 34) 453 ns/iter (± 16) 0.62
misc/visitors/time-complexity/time 15 525 ns/iter (± 3) 875 ns/iter (± 7) 0.60
misc/visitors/time-complexity/time 20 1055 ns/iter (± 2) 1282 ns/iter (± 4) 0.82
misc/visitors/time-complexity/time 40 3611 ns/iter (± 15) 5001 ns/iter (± 2) 0.72
misc/visitors/time-complexity/time 60 7390 ns/iter (± 15) 10679 ns/iter (± 39) 0.69
es/full-target/es2016 241074 ns/iter (± 679) 234046 ns/iter (± 568) 1.03
es/full-target/es2017 227119 ns/iter (± 813) 220931 ns/iter (± 1523) 1.03
es/full-target/es2018 213751 ns/iter (± 335) 209878 ns/iter (± 625) 1.02
es2020_nullish_coalescing 70285 ns/iter (± 7554) 68232 ns/iter (± 381) 1.03
es2020_optional_chaining 78582 ns/iter (± 339) 78334 ns/iter (± 341) 1.00
es2022_class_properties 118259 ns/iter (± 368) 117782 ns/iter (± 3625) 1.00
es2018_object_rest_spread 74062 ns/iter (± 187) 72935 ns/iter (± 190) 1.02
es2019_optional_catch_binding 63645 ns/iter (± 213) 61249 ns/iter (± 160) 1.04
es2017_async_to_generator 64407 ns/iter (± 192) 60463 ns/iter (± 163) 1.07
es2016_exponentiation 69715 ns/iter (± 264) 66310 ns/iter (± 289) 1.05
es2015_arrow 71437 ns/iter (± 775) 69411 ns/iter (± 223) 1.03
es2015_block_scoped_fn 68758 ns/iter (± 208) 65653 ns/iter (± 288) 1.05
es2015_block_scoping 119554 ns/iter (± 623) 121782 ns/iter (± 555) 0.98

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

Please sign in to comment.