fix(es/compat): Handle nested functions in private_field (#3355)

This commit is contained in:
Austaras 2022-01-25 04:55:43 +08:00 committed by GitHub
parent df2ca10cb6
commit 29aaac1f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use std::{iter, mem};
use std::iter;
use swc_atoms::JsWord;
use swc_common::{collections::AHashSet, util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
@ -98,7 +98,8 @@ pub(super) struct FieldAccessFolder<'a> {
macro_rules! take_vars {
($name:ident, $T:tt) => {
fn $name(&mut self, f: &mut $T) {
assert!(self.vars.is_empty());
let old_var = self.vars.take();
if f.body.is_none() {
return;
}
@ -111,12 +112,14 @@ macro_rules! take_vars {
Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: mem::take(&mut self.vars),
decls: self.vars.take(),
declare: false,
})),
)
}
self.vars = old_var;
}
};
}

View File

@ -5632,6 +5632,57 @@ test!(
"
);
test!(
syntax(),
|_| class_properties(class_properties::Config { loose: false }),
issue_3229_1,
"
class A {
B() {
1;
C.#D++;
E(function() {});
}
}
",
"
class A {
B() {
var _C, _this_D;
1;
_classPrivateFieldSet(_C = C, _D, (_this_D = +_classPrivateFieldGet(_C, _D)) + 1), _this_D;
E(function() {});
}
}
"
);
test!(
syntax(),
|_| class_properties(class_properties::Config { loose: false }),
issue_3229_2,
"
class A {
foo() {
A.#b += 123
class B {
foo() {}
}
}
}
",
"
class A {
foo() {
var _A;
_classPrivateFieldSet(_A = A, _b, _classPrivateFieldGet(_A, _b) + 123);
class B {
foo() {}
}
}
}
"
);
#[testing::fixture("tests/fixture/classes/**/exec.js")]
fn exec(input: PathBuf) {
let src = read_to_string(&input).unwrap();