Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/compat): Handle temporary variable injection position #7171

Merged
merged 2 commits into from Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,3 +1,5 @@
use std::mem;

use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::Parallel;
Expand Down Expand Up @@ -174,34 +176,44 @@ impl VisitMut for Operators {
}
}

/// [swc_ecma_ast::ModuleItem] is the top level Item in the current
/// implementation of JavaScript until the proposal for
/// [module-declarations] and [module-expressions] are officially added.
///
/// [module declarations]: https://github.com/tc39/proposal-module-declarations.
/// [module-expressions]: https://github.com/tc39/proposal-module-expressions
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
let vars = self.vars.take();
n.visit_mut_children_with(self);

if !self.vars.is_empty() {
let vars = mem::replace(&mut self.vars, vars);
if !vars.is_empty() {
prepend_stmt(
n,
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: self.vars.take(),
decls: vars,
}
.into(),
)
}
}

fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
let vars = self.vars.take();
n.visit_mut_children_with(self);

if !self.vars.is_empty() {
let vars = mem::replace(&mut self.vars, vars);
if !vars.is_empty() {
prepend_stmt(
n,
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: self.vars.take(),
decls: vars,
}
.into(),
)
Expand Down
Expand Up @@ -81,6 +81,23 @@ test!(
"
);

test!(
syntax(),
|_| tr(),
issue_7169,
"function myFunc(options) {
options.context ||= {}
const closure = function() {}
}",
"
function myFunc(options) {
var _options;
(_options = options).context || (_options.context = {});
const closure = function() {};
}
"
);

test_exec!(
syntax(),
|_| tr(),
Expand Down