Skip to content

fix(es/compat): Generate OptCall for OptCall for private fields #8031

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

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class A {
var _class_private_field_get1;
var _this_getInstance;
_class_private_field_get(this, _fieldFunc).call(this);
((_class_private_field_get1 = _class_private_field_get(this, _fieldFunc)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.call)(this);
(_class_private_field_get1 = _class_private_field_get(this, _fieldFunc)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.call(this);
const func = _class_private_field_get(this, _fieldFunc);
func();
new (_class_private_field_get(this, _fieldFunc))();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class A {
test() {
var _class_static_private_field_spec_get1;
_class_static_private_field_spec_get(A, A, _fieldFunc).call(A);
((_class_static_private_field_spec_get1 = _class_static_private_field_spec_get(A, A, _fieldFunc)) === null || _class_static_private_field_spec_get1 === void 0 ? void 0 : _class_static_private_field_spec_get1.call)(A);
(_class_static_private_field_spec_get1 = _class_static_private_field_spec_get(A, A, _fieldFunc)) === null || _class_static_private_field_spec_get1 === void 0 ? void 0 : _class_static_private_field_spec_get1.call(A);
const func = _class_static_private_field_spec_get(A, A, _fieldFunc);
func();
new (_class_static_private_field_spec_get(A, A, _fieldFunc))();
Expand Down
16 changes: 9 additions & 7 deletions crates/swc_ecma_transforms_base/src/fixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,16 @@ impl Fixer<'_> {
}

fn wrap_callee(&mut self, e: &mut Expr) {
if match e {
Expr::Lit(Lit::Num(..) | Lit::Str(..)) => false,
match e {
Expr::Lit(Lit::Num(..) | Lit::Str(..)) => (),
Expr::Cond(..)
| Expr::Bin(..)
| Expr::Lit(..)
| Expr::Unary(..)
| Expr::Object(..)
| Expr::Await(..)
| Expr::Yield(..) => true,
_ => false,
} {
self.wrap(e)
| Expr::Yield(..) => self.wrap(e),
_ => (),
}
}
}
Expand Down Expand Up @@ -400,7 +398,11 @@ impl VisitMut for Fixer<'_> {
fn visit_mut_call_expr(&mut self, node: &mut CallExpr) {
let old = self.visit_call(&mut node.args, &mut node.callee);
if let Callee::Expr(e) = &mut node.callee {
self.wrap_callee(e)
if let Expr::OptChain(_) = &**e {
self.wrap(e)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary -- swc parser and paren_remover won't generate such structure, but it's better to be safe.

} else {
self.wrap_callee(e)
}
}

self.ctx = old;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,21 +455,28 @@ impl<'a> VisitMut for PrivateAccessVisitor<'a> {
let (expr, this) = self.visit_mut_private_get(&mut callee, None);
if let Some(this) = this {
let args = iter::once(this.as_arg()).chain(call.args.take()).collect();
*e = Expr::Call(CallExpr {
let call = OptCall {
span: *span,
callee: OptChainExpr {
span: *span,
optional: *optional,
base: Box::new(OptChainBase::Member(MemberExpr {
span: call.span,
obj: Box::new(expr),
prop: MemberProp::Ident(quote_ident!("call")),
})),
}
.as_callee(),
callee: Box::new(
OptChainExpr {
span: *span,
optional: *optional,
base: Box::new(OptChainBase::Member(MemberExpr {
span: call.span,
obj: Box::new(expr),
prop: MemberProp::Ident(quote_ident!("call")),
})),
}
.into(),
),
args,
type_args: call.type_args.take(),
});
};
*e = Expr::OptChain(OptChainExpr {
span: *span,
optional: false,
base: Box::new(OptChainBase::Call(call)),
})
} else {
call.callee = Box::new(expr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use swc_ecma_transforms_compat::{
es2015::{arrow, block_scoping, classes, function_name, template_literal},
es2016::exponentiation,
es2017::async_to_generator,
es2020::optional_chaining,
es2022::class_properties,
es3::reserved_words,
};
Expand Down Expand Up @@ -6669,3 +6670,41 @@ var _x = {
};
"#
);

test!(
syntax(),
|t| {
let unresolved = Mark::new();
chain!(
class_properties(Some(t.comments.clone()), Default::default()),
optional_chaining(Default::default(), unresolved)
)
},
issue_8003,
"
class Foo {
#priv
search() {
this.#priv?.()
}
}

console.log(new Foo().search())",
r#"
var _priv = new WeakMap();
class Foo {
search() {
var _class_private_field_get1;
(_class_private_field_get1 = _class_private_field_get(this, _priv)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.call(this);
}
constructor(){
_class_private_field_init(this, _priv, {
writable: true,
value: void 0
});
}
}

console.log(new Foo().search());
"#
);