Skip to content

Commit

Permalink
fix(es/compat): Handle nullish in fn expr scope (#7980)
Browse files Browse the repository at this point in the history
**Related issue:**

  - Closes: #7977
  • Loading branch information
magic-akari authored Sep 21, 2023
1 parent 95285e8 commit 5050f58
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
26 changes: 26 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7977/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"dynamicImport": true
},
"transform": {
"react": {
"runtime": "automatic"
}
},
"baseUrl": "./",
"target": "es2019",
"keepClassNames": true,
"minify": {
"compress": false
}
},
"isModule": true,
"module": {
"type": "es6"
},
"minify": false
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-7xxx/7977/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const foo = () => baz() ?? qux;
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7977/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const foo = ()=>{
var _baz;
return (_baz = baz()) !== null && _baz !== void 0 ? _baz : qux;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::mem::take;

use serde::Deserialize;
use swc_common::{util::take::Take, Span, DUMMY_SP};
use swc_common::{util::take::Take, Span, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{alias_if_required, undefined, StmtLike};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
Expand Down Expand Up @@ -192,6 +192,35 @@ impl VisitMut for NullishCoalescing {
_ => {}
}
}

fn visit_mut_block_stmt_or_expr(&mut self, n: &mut BlockStmtOrExpr) {
let vars = self.vars.take();
n.visit_mut_children_with(self);

if !self.vars.is_empty() {
if let BlockStmtOrExpr::Expr(expr) = n {
// expr
// { var decl = init; return expr; }
let span = expr.span();
let stmts = vec![
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: self.vars.take(),
declare: false,
}
.into(),
Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(expr.take()),
}),
];
*n = BlockStmtOrExpr::BlockStmt(BlockStmt { span, stmts });
}
}

self.vars = vars;
}
}

#[tracing::instrument(level = "info", skip_all)]
Expand Down

1 comment on commit 5050f58

@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: 5050f58 Previous: b3d3a7b Ratio
es/full/bugs-1 283714 ns/iter (± 3147) 286886 ns/iter (± 6299) 0.99
es/full/minify/libraries/antd 1339585321 ns/iter (± 11383027) 1280831070 ns/iter (± 8690557) 1.05
es/full/minify/libraries/d3 279454536 ns/iter (± 5681672) 273433265 ns/iter (± 4512194) 1.02
es/full/minify/libraries/echarts 1078354084 ns/iter (± 11615394) 1017957232 ns/iter (± 5231996) 1.06
es/full/minify/libraries/jquery 84235991 ns/iter (± 206634) 83047561 ns/iter (± 118117) 1.01
es/full/minify/libraries/lodash 97253450 ns/iter (± 247738) 96159626 ns/iter (± 194911) 1.01
es/full/minify/libraries/moment 49808170 ns/iter (± 127334) 49107041 ns/iter (± 61522) 1.01
es/full/minify/libraries/react 17995463 ns/iter (± 49165) 17815133 ns/iter (± 33370) 1.01
es/full/minify/libraries/terser 220093650 ns/iter (± 883206) 213711347 ns/iter (± 505964) 1.03
es/full/minify/libraries/three 387572718 ns/iter (± 2131838) 377898638 ns/iter (± 891271) 1.03
es/full/minify/libraries/typescript 2671877831 ns/iter (± 9556131) 2583859208 ns/iter (± 10052705) 1.03
es/full/minify/libraries/victory 574715486 ns/iter (± 11529932) 550200784 ns/iter (± 2526255) 1.04
es/full/minify/libraries/vue 119572445 ns/iter (± 188057) 117807924 ns/iter (± 224833) 1.01
es/full/codegen/es3 33556 ns/iter (± 80) 34019 ns/iter (± 84) 0.99
es/full/codegen/es5 33576 ns/iter (± 74) 34031 ns/iter (± 58) 0.99
es/full/codegen/es2015 33537 ns/iter (± 82) 33969 ns/iter (± 77) 0.99
es/full/codegen/es2016 33599 ns/iter (± 96) 33871 ns/iter (± 82) 0.99
es/full/codegen/es2017 33570 ns/iter (± 134) 34003 ns/iter (± 115) 0.99
es/full/codegen/es2018 33609 ns/iter (± 37) 34015 ns/iter (± 49) 0.99
es/full/codegen/es2019 33701 ns/iter (± 104) 34036 ns/iter (± 130) 0.99
es/full/codegen/es2020 33609 ns/iter (± 74) 34025 ns/iter (± 109) 0.99
es/full/all/es3 171124151 ns/iter (± 1433263) 168407277 ns/iter (± 1638634) 1.02
es/full/all/es5 162472324 ns/iter (± 1268656) 159229894 ns/iter (± 934335) 1.02
es/full/all/es2015 121403413 ns/iter (± 991019) 119489798 ns/iter (± 663427) 1.02
es/full/all/es2016 120724861 ns/iter (± 634791) 117978914 ns/iter (± 1003334) 1.02
es/full/all/es2017 119360188 ns/iter (± 532385) 118327526 ns/iter (± 1438429) 1.01
es/full/all/es2018 117673215 ns/iter (± 1052584) 115939010 ns/iter (± 1239135) 1.01
es/full/all/es2019 117012255 ns/iter (± 470754) 114689955 ns/iter (± 647582) 1.02
es/full/all/es2020 112481717 ns/iter (± 525771) 110566140 ns/iter (± 440831) 1.02
es/full/parser 498471 ns/iter (± 4840) 487871 ns/iter (± 4949) 1.02
es/full/base/fixer 19065 ns/iter (± 121) 20659 ns/iter (± 89) 0.92
es/full/base/resolver_and_hygiene 83879 ns/iter (± 233) 82370 ns/iter (± 368) 1.02
serialization of serde 294 ns/iter (± 3) 299 ns/iter (± 0) 0.98
css/minify/libraries/bootstrap 28543529 ns/iter (± 59200) 28415042 ns/iter (± 39724) 1.00
css/visitor/compare/clone 1670900 ns/iter (± 3879) 1642276 ns/iter (± 2151) 1.02
css/visitor/compare/visit_mut_span 1783365 ns/iter (± 7578) 1772999 ns/iter (± 3476) 1.01
css/visitor/compare/visit_mut_span_panic 1871447 ns/iter (± 4963) 1860192 ns/iter (± 5057) 1.01
css/visitor/compare/fold_span 2578113 ns/iter (± 26427) 2580176 ns/iter (± 10854) 1.00
css/visitor/compare/fold_span_panic 2755681 ns/iter (± 7226) 2766375 ns/iter (± 15074) 1.00
css/lexer/bootstrap_5_1_3 4592245 ns/iter (± 4657) 4439354 ns/iter (± 7113) 1.03
css/lexer/foundation_6_7_4 3858189 ns/iter (± 2459) 3731274 ns/iter (± 7762) 1.03
css/lexer/tailwind_3_1_1 730625 ns/iter (± 682) 708835 ns/iter (± 464) 1.03
css/parser/bootstrap_5_1_3 19660285 ns/iter (± 24063) 19521899 ns/iter (± 86828) 1.01
css/parser/foundation_6_7_4 15640681 ns/iter (± 35093) 15422977 ns/iter (± 24029) 1.01
css/parser/tailwind_3_1_1 2980627 ns/iter (± 8009) 2983275 ns/iter (± 5362) 1.00
es/codegen/colors 726969 ns/iter (± 397178) 728618 ns/iter (± 397204) 1.00
es/codegen/large 3134067 ns/iter (± 1668549) 3131327 ns/iter (± 1665572) 1.00
es/codegen/with-parser/colors 45123 ns/iter (± 88) 44995 ns/iter (± 495) 1.00
es/codegen/with-parser/large 491276 ns/iter (± 1048) 488519 ns/iter (± 865) 1.01
es/minify/libraries/antd 1167072647 ns/iter (± 12143288) 1122004852 ns/iter (± 10030039) 1.04
es/minify/libraries/d3 240079099 ns/iter (± 1884336) 237599643 ns/iter (± 371889) 1.01
es/minify/libraries/echarts 924386910 ns/iter (± 5354545) 879861196 ns/iter (± 2899891) 1.05
es/minify/libraries/jquery 73050637 ns/iter (± 99037) 72903042 ns/iter (± 94737) 1.00
es/minify/libraries/lodash 88160460 ns/iter (± 811005) 86836419 ns/iter (± 162207) 1.02
es/minify/libraries/moment 43332589 ns/iter (± 83845) 43175817 ns/iter (± 42122) 1.00
es/minify/libraries/react 15946865 ns/iter (± 72952) 15993196 ns/iter (± 25938) 1.00
es/minify/libraries/terser 186807801 ns/iter (± 501357) 184753829 ns/iter (± 320259) 1.01
es/minify/libraries/three 325962335 ns/iter (± 1188976) 316714995 ns/iter (± 650530) 1.03
es/minify/libraries/typescript 2259686884 ns/iter (± 18073596) 2201499311 ns/iter (± 6968000) 1.03
es/minify/libraries/victory 480438900 ns/iter (± 2799588) 470648405 ns/iter (± 1358203) 1.02
es/minify/libraries/vue 105990924 ns/iter (± 407263) 105130218 ns/iter (± 161808) 1.01
es/visitor/compare/clone 1945641 ns/iter (± 4025) 1932371 ns/iter (± 4230) 1.01
es/visitor/compare/visit_mut_span 2291278 ns/iter (± 8153) 2268748 ns/iter (± 2542) 1.01
es/visitor/compare/visit_mut_span_panic 2326980 ns/iter (± 2549) 2325960 ns/iter (± 3920) 1.00
es/visitor/compare/fold_span 3380129 ns/iter (± 10781) 3361531 ns/iter (± 5810) 1.01
es/visitor/compare/fold_span_panic 3522108 ns/iter (± 7555) 3493679 ns/iter (± 7652) 1.01
es/lexer/colors 13130 ns/iter (± 33) 12882 ns/iter (± 29) 1.02
es/lexer/angular 6092427 ns/iter (± 9770) 6097854 ns/iter (± 11787) 1.00
es/lexer/backbone 795706 ns/iter (± 3666) 776252 ns/iter (± 2504) 1.03
es/lexer/jquery 4538298 ns/iter (± 17388) 4465378 ns/iter (± 16399) 1.02
es/lexer/jquery mobile 6868191 ns/iter (± 6058) 6786456 ns/iter (± 19277) 1.01
es/lexer/mootools 3555397 ns/iter (± 9782) 3551894 ns/iter (± 13279) 1.00
es/lexer/underscore 664989 ns/iter (± 746) 658402 ns/iter (± 3658) 1.01
es/lexer/three 21381642 ns/iter (± 51681) 21344793 ns/iter (± 26041) 1.00
es/lexer/yui 3821121 ns/iter (± 2670) 3783477 ns/iter (± 11012) 1.01
es/parser/colors 27746 ns/iter (± 111) 27255 ns/iter (± 206) 1.02
es/parser/angular 13621588 ns/iter (± 64911) 13429081 ns/iter (± 46099) 1.01
es/parser/backbone 1994639 ns/iter (± 8330) 1977842 ns/iter (± 8146) 1.01
es/parser/jquery 10905732 ns/iter (± 27780) 10792994 ns/iter (± 35635) 1.01
es/parser/jquery mobile 16714245 ns/iter (± 158354) 16587359 ns/iter (± 41200) 1.01
es/parser/mootools 8414553 ns/iter (± 25501) 8326966 ns/iter (± 17904) 1.01
es/parser/underscore 1728652 ns/iter (± 11247) 1695115 ns/iter (± 8876) 1.02
es/parser/three 47507538 ns/iter (± 447506) 46381167 ns/iter (± 173460) 1.02
es/parser/yui 8330368 ns/iter (± 47254) 8212332 ns/iter (± 26583) 1.01
es/preset-env/usage/builtin_type 135187 ns/iter (± 31756) 137495 ns/iter (± 33081) 0.98
es/preset-env/usage/property 16568 ns/iter (± 63) 16332 ns/iter (± 99) 1.01
es/resolver/typescript 91118366 ns/iter (± 764997) 89837524 ns/iter (± 1247573) 1.01
es/fixer/typescript 64377332 ns/iter (± 487149) 63533513 ns/iter (± 334373) 1.01
es/hygiene/typescript 131656206 ns/iter (± 535625) 129586045 ns/iter (± 391645) 1.02
es/resolver_with_hygiene/typescript 244507106 ns/iter (± 1459174) 241327438 ns/iter (± 857015) 1.01
es/visitor/base-perf/module_clone 59391 ns/iter (± 384) 59846 ns/iter (± 210) 0.99
es/visitor/base-perf/fold_empty 63455 ns/iter (± 374) 62906 ns/iter (± 142) 1.01
es/visitor/base-perf/fold_noop_impl_all 63232 ns/iter (± 422) 63440 ns/iter (± 376) 1.00
es/visitor/base-perf/fold_noop_impl_vec 63557 ns/iter (± 286) 63357 ns/iter (± 340) 1.00
es/visitor/base-perf/boxing_boxed_clone 56 ns/iter (± 0) 56 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 40 ns/iter (± 0) 39 ns/iter (± 0) 1.03
es/visitor/base-perf/boxing_boxed 109 ns/iter (± 0) 106 ns/iter (± 0) 1.03
es/visitor/base-perf/boxing_unboxed 78 ns/iter (± 0) 75 ns/iter (± 0) 1.04
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2534 ns/iter (± 6) 2548 ns/iter (± 10) 0.99
es/base/parallel/resolver/typescript 3769292673 ns/iter (± 288136507) 3802314304 ns/iter (± 252032441) 0.99
es/base/parallel/hygiene/typescript 1434206475 ns/iter (± 17977738) 1470554017 ns/iter (± 15579213) 0.98
misc/visitors/time-complexity/time 5 154 ns/iter (± 1) 149 ns/iter (± 0) 1.03
misc/visitors/time-complexity/time 10 453 ns/iter (± 4) 439 ns/iter (± 4) 1.03
misc/visitors/time-complexity/time 15 871 ns/iter (± 35) 797 ns/iter (± 2) 1.09
misc/visitors/time-complexity/time 20 1284 ns/iter (± 1) 1282 ns/iter (± 41) 1.00
misc/visitors/time-complexity/time 40 4421 ns/iter (± 39) 5258 ns/iter (± 6) 0.84
misc/visitors/time-complexity/time 60 9016 ns/iter (± 12) 9413 ns/iter (± 900) 0.96
es/full-target/es2016 235142 ns/iter (± 632) 229247 ns/iter (± 515) 1.03
es/full-target/es2017 222979 ns/iter (± 505) 217790 ns/iter (± 374) 1.02
es/full-target/es2018 212081 ns/iter (± 800) 206368 ns/iter (± 757) 1.03
es2020_nullish_coalescing 71219 ns/iter (± 366) 69242 ns/iter (± 573) 1.03
es2020_optional_chaining 79692 ns/iter (± 303) 75210 ns/iter (± 6158) 1.06
es2022_class_properties 115508 ns/iter (± 378) 113688 ns/iter (± 498) 1.02
es2018_object_rest_spread 75322 ns/iter (± 219) 73476 ns/iter (± 275) 1.03
es2019_optional_catch_binding 65051 ns/iter (± 228) 62967 ns/iter (± 223) 1.03
es2017_async_to_generator 65092 ns/iter (± 180) 62655 ns/iter (± 274) 1.04
es2016_exponentiation 69371 ns/iter (± 261) 67133 ns/iter (± 181) 1.03
es2015_arrow 72457 ns/iter (± 280) 69811 ns/iter (± 271) 1.04
es2015_block_scoped_fn 68815 ns/iter (± 262) 66963 ns/iter (± 272) 1.03
es2015_block_scoping 123393 ns/iter (± 414) 118211 ns/iter (± 564) 1.04

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

Please sign in to comment.