Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit eb2c3b8

Browse files
committed
feat(compiler): add compat fixer
1 parent d8bcf0a commit eb2c3b8

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

compiler/src/compat_fixer.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2020-2021 postUI Lab. All rights reserved. MIT license.
2+
3+
use crate::resolve::is_call_expr_by_name;
4+
5+
use swc_common::DUMMY_SP;
6+
use swc_ecma_ast::*;
7+
use swc_ecma_utils::quote_ident;
8+
use swc_ecma_visit::{noop_fold_type, Fold};
9+
10+
pub fn compat_fixer_fold() -> impl Fold {
11+
CompatFixer {}
12+
}
13+
14+
struct CompatFixer {}
15+
16+
impl Fold for CompatFixer {
17+
noop_fold_type!();
18+
19+
// - `require("regenerator-runtime")` -> `__ALEPH.require("regenerator-runtime")`
20+
fn fold_call_expr(&mut self, call: CallExpr) -> CallExpr {
21+
if is_call_expr_by_name(&call, "require") {
22+
let ok = match call.args.first() {
23+
Some(ExprOrSpread { expr, .. }) => match expr.as_ref() {
24+
Expr::Lit(lit) => match lit {
25+
Lit::Str(_) => true,
26+
_ => false,
27+
},
28+
_ => false,
29+
},
30+
_ => false,
31+
};
32+
if ok {
33+
return CallExpr {
34+
span: DUMMY_SP,
35+
callee: ExprOrSuper::Expr(Box::new(Expr::Member(MemberExpr {
36+
span: DUMMY_SP,
37+
obj: ExprOrSuper::Expr(Box::new(Expr::Ident(quote_ident!("__ALEPH")))),
38+
prop: Box::new(Expr::Ident(quote_ident!("require"))),
39+
computed: false,
40+
}))),
41+
args: call.args,
42+
type_args: None,
43+
};
44+
}
45+
}
46+
call
47+
}
48+
}

compiler/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
extern crate lazy_static;
55

66
mod aleph;
7+
mod compat_fixer;
78
mod error;
89
mod fast_refresh;
910
mod import_map;

compiler/src/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ pub fn is_remote_url(url: &str) -> bool {
686686
return url.starts_with("https://") || url.starts_with("http://");
687687
}
688688

689-
fn is_call_expr_by_name(call: &CallExpr, name: &str) -> bool {
689+
pub fn is_call_expr_by_name(call: &CallExpr, name: &str) -> bool {
690690
let callee = match &call.callee {
691691
ExprOrSuper::Super(_) => return false,
692692
ExprOrSuper::Expr(callee) => callee.as_ref(),

compiler/src/swc.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
22
// Copyright 2020-2021 postUI Lab. All rights reserved. MIT license.
33

4+
use crate::compat_fixer::compat_fixer_fold;
45
use crate::error::{DiagnosticBuffer, ErrorBuffer};
56
use crate::fast_refresh::fast_refresh_fold;
67
use crate::jsx::aleph_jsx_fold;
@@ -126,12 +127,12 @@ impl ParsedModule {
126127
) -> Result<(String, Option<String>), anyhow::Error> {
127128
swc_common::GLOBALS.set(&Globals::new(), || {
128129
let specifier_is_remote = resolver.borrow_mut().specifier_is_remote;
129-
let ts = match self.source_type {
130+
let is_ts = match self.source_type {
130131
SourceType::TypeScript => true,
131132
SourceType::TSX => true,
132133
_ => false,
133134
};
134-
let jsx = match self.source_type {
135+
let is_jsx = match self.source_type {
135136
SourceType::JSX => true,
136137
SourceType::TSX => true,
137138
_ => false,
@@ -144,8 +145,8 @@ impl ParsedModule {
144145
let root_mark = Mark::fresh(Mark::root());
145146
let mut passes = chain!(
146147
aleph_resolve_fold(resolver.clone()),
147-
Optional::new(aleph_jsx_fold, jsx),
148-
Optional::new(aleph_jsx_builtin_resolve_fold, jsx),
148+
Optional::new(aleph_jsx_fold, is_jsx),
149+
Optional::new(aleph_jsx_builtin_resolve_fold, is_jsx),
149150
Optional::new(
150151
fast_refresh_fold(
151152
"$RefreshReg$",
@@ -167,21 +168,22 @@ impl ParsedModule {
167168
..Default::default()
168169
},
169170
),
170-
jsx
171+
is_jsx
171172
),
172173
decorators::decorators(decorators::Config {
173174
legacy: true,
174175
emit_metadata: false
175176
}),
176177
Optional::new(es2020(), options.target < JscTarget::Es2020),
177-
Optional::new(strip(), ts),
178+
Optional::new(strip(), is_ts),
178179
Optional::new(es2018(), options.target < JscTarget::Es2018),
179180
Optional::new(es2017(), options.target < JscTarget::Es2017),
180181
Optional::new(es2016(), options.target < JscTarget::Es2016),
181182
Optional::new(
182183
es2015(root_mark, Default::default()),
183184
options.target < JscTarget::Es2015
184185
),
186+
Optional::new(compat_fixer_fold(), options.target < JscTarget::Es2015),
185187
Optional::new(
186188
helpers::inject_helpers(),
187189
options.target < JscTarget::Es2020

0 commit comments

Comments
 (0)