feat(es/modules): Support allowTopLevelThis (#5086)

This commit is contained in:
magic-akari 2022-07-02 12:17:44 +08:00 committed by GitHub
parent f5447cd1b1
commit 9addef6fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 97 additions and 1 deletions

View File

@ -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,
});

View File

@ -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,
});

View File

@ -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);
}
}

View File

@ -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,
});

View File

@ -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(),

View File

@ -0,0 +1 @@
export var v = this;

View File

@ -0,0 +1,3 @@
{
"allowTopLevelThis": false
}

View File

@ -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;
});

View File

@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "v", {
enumerable: true,
get: ()=>v
});
var v = void 0;

View File

@ -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;
});

View File

@ -0,0 +1 @@
export var v = this;

View File

@ -0,0 +1,3 @@
{
"allowTopLevelThis": true
}

View File

@ -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;
});

View File

@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "v", {
enumerable: true,
get: ()=>v
});
var v = this;

View File

@ -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;
});