-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d34cfa
commit 2df4850
Showing
6 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::mem; | ||
|
||
use swc_ecma_ast as ast; | ||
use swc_ecma_visit::{Fold, FoldWith}; | ||
|
||
/// Merges adjacent variable declarations. | ||
pub struct MergeVars; | ||
|
||
impl Fold for MergeVars { | ||
fn fold_stmts(&mut self, stmts: Vec<ast::Stmt>) -> Vec<ast::Stmt> { | ||
let stmts = stmts.fold_children_with(self); | ||
|
||
let var_to_stmt = |var| ast::Stmt::Decl(ast::Decl::Var(var)); | ||
|
||
let mut out = Vec::with_capacity(stmts.len()); | ||
let mut buffered_var = None; | ||
for stmt in stmts { | ||
match (stmt, &mut buffered_var) { | ||
(ast::Stmt::Decl(ast::Decl::Var(cur)), buf @ None) => { | ||
// no buffer yet, buffer this decl | ||
*buf = Some(cur); | ||
} | ||
(ast::Stmt::Decl(ast::Decl::Var(cur)), Some(buf)) if cur.kind == buf.kind => { | ||
// same kind, add to buffer | ||
buf.decls.extend(cur.decls); | ||
} | ||
(ast::Stmt::Decl(ast::Decl::Var(cur)), Some(_)) => { | ||
// different kinds, swap into buffer | ||
let buffered_var = mem::replace(&mut buffered_var, Some(cur)); | ||
if let Some(buf) = buffered_var { | ||
out.push(var_to_stmt(buf)); | ||
} | ||
} | ||
(stmt, _) => { | ||
// not a var decl, flush buffer | ||
if let Some(buf) = buffered_var.take() { | ||
out.push(var_to_stmt(buf)); | ||
} | ||
out.push(stmt); | ||
} | ||
} | ||
} | ||
if let Some(buf) = buffered_var { | ||
out.push(var_to_stmt(buf)); | ||
} | ||
out | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::opt_ast::merge_vars; | ||
|
||
case!(basic, || merge_vars::MergeVars, r#" | ||
var x; | ||
var y; | ||
var z; | ||
let a; | ||
let b; | ||
let c; | ||
const d; | ||
const e; | ||
const f; | ||
"#, @r###" | ||
var x, y, z; | ||
let a, b, c; | ||
const d, e, f; | ||
"###); | ||
|
||
case!(basic_values, || merge_vars::MergeVars, r#" | ||
var x; | ||
var y = 1; | ||
var z; | ||
let a; | ||
let b = 2; | ||
let c; | ||
const d; | ||
const e = 3; | ||
const f; | ||
"#, @r###" | ||
var x, y = 1, z; | ||
let a, b = 2, c; | ||
const d, e = 3, f; | ||
"###); | ||
|
||
case!(inner_scopes, || merge_vars::MergeVars, r#" | ||
if (foo) { | ||
var a; | ||
var b = 1; | ||
} | ||
"#, @r###" | ||
if (foo) { | ||
var a, b = 1; | ||
} | ||
"###); | ||
|
||
case!(inner_fn_scopes, || merge_vars::MergeVars, r#" | ||
function foo() { | ||
var a; | ||
var b = 1; | ||
} | ||
"#, @r###" | ||
function foo() { | ||
var a, b = 1; | ||
} | ||
"###); | ||
|
||
case!(bail_nondecl, || merge_vars::MergeVars, r#" | ||
var a; | ||
foo(); | ||
var b = 1; | ||
"#, @r###" | ||
var a; | ||
foo(); | ||
var b = 1; | ||
"###); | ||
|
||
case!(bail_different_types, || merge_vars::MergeVars, r#" | ||
var x; | ||
let y; | ||
const z; | ||
var a; | ||
"#, @r###" | ||
var x; | ||
let y; | ||
const z; | ||
var a; | ||
"###); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters