mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 22:56:11 +03:00
feat(es/modules): Support allowTopLevelThis
(#5086)
This commit is contained in:
parent
f5447cd1b1
commit
9addef6fc8
@ -163,6 +163,7 @@ impl VisitMut for Amd {
|
||||
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
|
||||
import_map,
|
||||
lazy_record: Default::default(),
|
||||
allow_top_level_this: self.config.allow_top_level_this,
|
||||
is_global_this: true,
|
||||
});
|
||||
|
||||
|
@ -139,6 +139,7 @@ impl VisitMut for Cjs {
|
||||
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
|
||||
import_map,
|
||||
lazy_record,
|
||||
allow_top_level_this: self.config.allow_top_level_this,
|
||||
is_global_this: true,
|
||||
});
|
||||
|
||||
|
@ -35,6 +35,8 @@ pub(crate) struct ModuleRefRewriter {
|
||||
|
||||
pub lazy_record: AHashSet<Id>,
|
||||
|
||||
pub allow_top_level_this: bool,
|
||||
|
||||
pub is_global_this: bool,
|
||||
}
|
||||
|
||||
@ -67,7 +69,7 @@ impl VisitMut for ModuleRefRewriter {
|
||||
}
|
||||
|
||||
Expr::This(ThisExpr { span }) => {
|
||||
if self.is_global_this {
|
||||
if !self.allow_top_level_this && self.is_global_this {
|
||||
*n = *undefined(*span);
|
||||
}
|
||||
}
|
||||
|
@ -143,6 +143,7 @@ impl VisitMut for Umd {
|
||||
stmts.visit_mut_children_with(&mut ModuleRefRewriter {
|
||||
import_map,
|
||||
lazy_record: Default::default(),
|
||||
allow_top_level_this: self.config.config.allow_top_level_this,
|
||||
is_global_this: true,
|
||||
});
|
||||
|
||||
|
@ -14,6 +14,8 @@ use swc_ecma_utils::{
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||
pub struct Config {
|
||||
#[serde(default)]
|
||||
pub allow_top_level_this: bool,
|
||||
#[serde(default)]
|
||||
pub strict: bool,
|
||||
#[serde(default = "default_strict_mode")]
|
||||
@ -34,6 +36,7 @@ pub struct Config {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
allow_top_level_this: false,
|
||||
strict: false,
|
||||
strict_mode: default_strict_mode(),
|
||||
lazy: Lazy::default(),
|
||||
|
@ -0,0 +1 @@
|
||||
export var v = this;
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"allowTopLevelThis": false
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
define([
|
||||
"require",
|
||||
"exports"
|
||||
], function(require, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "v", {
|
||||
enumerable: true,
|
||||
get: ()=>v
|
||||
});
|
||||
var v = void 0;
|
||||
});
|
@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "v", {
|
||||
enumerable: true,
|
||||
get: ()=>v
|
||||
});
|
||||
var v = void 0;
|
@ -0,0 +1,17 @@
|
||||
(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
|
||||
});
|
||||
Object.defineProperty(exports, "v", {
|
||||
enumerable: true,
|
||||
get: ()=>v
|
||||
});
|
||||
var v = void 0;
|
||||
});
|
@ -0,0 +1 @@
|
||||
export var v = this;
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"allowTopLevelThis": true
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
define([
|
||||
"require",
|
||||
"exports"
|
||||
], function(require, exports) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "v", {
|
||||
enumerable: true,
|
||||
get: ()=>v
|
||||
});
|
||||
var v = this;
|
||||
});
|
@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "v", {
|
||||
enumerable: true,
|
||||
get: ()=>v
|
||||
});
|
||||
var v = this;
|
@ -0,0 +1,17 @@
|
||||
(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
|
||||
});
|
||||
Object.defineProperty(exports, "v", {
|
||||
enumerable: true,
|
||||
get: ()=>v
|
||||
});
|
||||
var v = this;
|
||||
});
|
Loading…
Reference in New Issue
Block a user