fix(es/modules): Handle this in class methods (#5065)

This commit is contained in:
magic-akari 2022-06-29 19:27:06 +08:00 committed by GitHub
parent 73874486b2
commit ef543b1b22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 203 additions and 18 deletions

View File

@ -160,7 +160,7 @@ impl VisitMut for Amd {
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
import_map,
lazy_record: Default::default(),
top_level: true,
is_global_this: true,
});
// ====================

View File

@ -136,7 +136,7 @@ impl VisitMut for Cjs {
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
import_map,
lazy_record,
top_level: true,
is_global_this: true,
});
*n = stmts;

View File

@ -35,7 +35,7 @@ pub(crate) struct ModuleRefRewriter {
pub lazy_record: AHashSet<Id>,
pub top_level: bool,
pub is_global_this: bool,
}
impl VisitMut for ModuleRefRewriter {
@ -67,7 +67,7 @@ impl VisitMut for ModuleRefRewriter {
}
Expr::This(ThisExpr { span }) => {
if self.top_level {
if self.is_global_this {
*n = *undefined(*span);
}
}
@ -99,38 +99,34 @@ impl VisitMut for ModuleRefRewriter {
}
fn visit_mut_function(&mut self, n: &mut Function) {
n.params.visit_mut_with(self);
self.visit_mut_with_non_top_level(&mut n.body);
self.visit_mut_with_non_global_this(n);
}
fn visit_mut_constructor(&mut self, n: &mut Constructor) {
n.params.visit_mut_with(self);
self.visit_mut_with_non_top_level(&mut n.body);
self.visit_mut_with_non_global_this(n);
}
fn visit_mut_class_prop(&mut self, n: &mut ClassProp) {
n.key.visit_mut_with(self);
self.visit_mut_with_non_top_level(&mut n.value);
self.visit_mut_with_non_global_this(&mut n.value);
}
fn visit_mut_static_block(&mut self, n: &mut StaticBlock) {
self.visit_mut_with_non_top_level(&mut n.body);
self.visit_mut_with_non_global_this(n);
}
}
impl ModuleRefRewriter {
fn visit_mut_with_non_top_level<T>(&mut self, n: &mut T)
fn visit_mut_with_non_global_this<T>(&mut self, n: &mut T)
where
T: VisitMutWith<Self>,
{
let top_level = self.top_level;
let top_level = self.is_global_this;
self.top_level = false;
n.visit_mut_with(self);
self.top_level = top_level;
self.is_global_this = false;
n.visit_mut_children_with(self);
self.is_global_this = top_level;
}
fn map_module_ref_ident(&mut self, ref_ident: &Ident) -> Option<Expr> {

View File

@ -140,7 +140,7 @@ impl VisitMut for Umd {
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
import_map,
lazy_record: Default::default(),
top_level: true,
is_global_this: true,
});
// ====================

View File

@ -0,0 +1,6 @@
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
}

View File

@ -0,0 +1,11 @@
define([
"require"
], function(require) {
"use strict";
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
}
});

View File

@ -0,0 +1,7 @@
"use strict";
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
}

View File

@ -0,0 +1,13 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
class Foo {
bar = 5;
getThing(a, b = this.bar) {
return a + b;
}
}
});

View File

@ -0,0 +1,28 @@
export class Foo {
[this] = this;
[this](a, b = this.x) {
return a + b;
}
static [this] = this;
static [this](a, b = this.x) {
return a + b;
}
}
export function foo(a = this) {
console.log(a);
}
export default {
[this]: this,
[this](a, b = this.x) {
return a + b;
},
[this]: function (a, b = this.x) {
return a + b;
},
};

View File

@ -0,0 +1,42 @@
define([
"require",
"exports"
], function(require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
Foo: ()=>Foo,
default: ()=>_default,
foo: ()=>foo
});
class Foo {
[void 0] = this;
[void 0](a, b = this.x) {
return a + b;
}
static [void 0] = this;
static [void 0](a, b = this.x) {
return a + b;
}
}
function foo(a = this) {
console.log(a);
}
var _default = {
[void 0]: void 0,
[void 0] (a, b = this.x) {
return a + b;
},
[void 0]: function(a, b = this.x) {
return a + b;
}
};
});

View File

@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
Foo: ()=>Foo,
default: ()=>_default,
foo: ()=>foo
});
class Foo {
[void 0] = this;
[void 0](a, b = this.x) {
return a + b;
}
static [void 0] = this;
static [void 0](a, b = this.x) {
return a + b;
}
}
function foo(a = this) {
console.log(a);
}
var _default = {
[void 0]: void 0,
[void 0] (a, b = this.x) {
return a + b;
},
[void 0]: function(a, b = this.x) {
return a + b;
}
};

View File

@ -0,0 +1,45 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
get: all[name],
enumerable: true
});
}
_export(exports, {
Foo: ()=>Foo,
default: ()=>_default,
foo: ()=>foo
});
class Foo {
[void 0] = this;
[void 0](a, b = this.x) {
return a + b;
}
static [void 0] = this;
static [void 0](a, b = this.x) {
return a + b;
}
}
function foo(a = this) {
console.log(a);
}
var _default = {
[void 0]: void 0,
[void 0] (a, b = this.x) {
return a + b;
},
[void 0]: function(a, b = this.x) {
return a + b;
}
};
});