fix(es/compat): Handle super in a private method (#5613)

This commit is contained in:
magic-akari 2022-08-24 23:19:41 +08:00 committed by GitHub
parent 8d44b71b6c
commit 35d3558535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 1 deletions

View File

@ -0,0 +1,14 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es2021",
"minify": {
"compress": false,
"mangle": false
},
"loose": false
},
"minify": false
}

View File

@ -0,0 +1,21 @@
class Base {
superMethod() {
return 'good';
}
}
class Sub extends Base {
superMethod() {
return 'bad';
}
#privateMethod() {
return super.superMethod();
}
publicMethod() {
return this.#privateMethod();
}
}
(new Sub()).publicMethod().toEqual('good');

View File

@ -0,0 +1,26 @@
import _class_private_method_get from "@swc/helpers/src/_class_private_method_get.mjs";
import _class_private_method_init from "@swc/helpers/src/_class_private_method_init.mjs";
import _get from "@swc/helpers/src/_get.mjs";
import _get_prototype_of from "@swc/helpers/src/_get_prototype_of.mjs";
class Base {
superMethod() {
return 'good';
}
}
var _privateMethod = new WeakSet();
class Sub extends Base {
superMethod() {
return 'bad';
}
publicMethod() {
return _class_private_method_get(this, _privateMethod, privateMethod).call(this);
}
constructor(...args){
super(...args);
_class_private_method_init(this, _privateMethod);
}
}
function privateMethod() {
return _get(_get_prototype_of(Sub.prototype), "superMethod", this).call(this);
}
new Sub().publicMethod().toEqual('good');

View File

@ -742,7 +742,7 @@ impl<C: Comments> ClassProperties<C> {
constructor = Some(c);
}
ClassMember::PrivateMethod(method) => {
ClassMember::PrivateMethod(mut method) => {
let is_static = method.is_static;
let prop_span = method.span;
@ -875,6 +875,20 @@ impl<C: Comments> ClassProperties<C> {
})
};
method.function.visit_mut_with(&mut SuperFieldAccessFolder {
class_name: &class_ident,
vars: &mut vars,
constructor_this_mark: None,
is_static,
folding_constructor: false,
in_injected_define_property_call: false,
in_nested_scope: false,
this_alias_mark: None,
constant_super: self.c.constant_super,
super_class: &super_ident,
in_pat: false,
});
private_method_fn_decls.push(Stmt::Decl(Decl::Fn(FnDecl {
ident: fn_name,
function: method.function,