-
-
Notifications
You must be signed in to change notification settings - Fork 827
Expand file tree
/
Copy pathutil.rs
More file actions
176 lines (163 loc) · 5.11 KB
/
Copy pathutil.rs
File metadata and controls
176 lines (163 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::sync::LazyLock;
use rspack_core::DependencyRange;
use rspack_error::{Diagnostic, Error, Severity};
use rspack_regex::RspackRegex;
use swc_atoms::Atom;
use swc_experimental_ecma_ast::{Expr, Lit, MemberExpr, OptChainBase};
use super::JavascriptParser;
static DEFAULT_CONTEXT_REGEXP: LazyLock<RspackRegex> =
LazyLock::new(|| RspackRegex::new(r"^\.\/.*$").expect("reg failed"));
pub mod expr_name {
pub const MODULE: &str = "module";
pub const MODULE_HOT: &str = "module.hot";
pub const MODULE_HOT_ACCEPT: &str = "module.hot.accept";
pub const MODULE_HOT_DECLINE: &str = "module.hot.decline";
pub const MODULE_REQUIRE: &str = "module.require";
pub const REQUIRE: &str = "require";
pub const REQUIRE_RESOLVE: &str = "require.resolve";
pub const REQUIRE_RESOLVE_WEAK: &str = "require.resolveWeak";
pub const IMPORT_META_PREFIX: &str = "import.meta.";
pub const IMPORT_META: &str = "import.meta";
pub const IMPORT_META_FILENAME: &str = "import.meta.filename";
pub const IMPORT_META_DIRNAME: &str = "import.meta.dirname";
pub const IMPORT_META_URL: &str = "import.meta.url";
pub const IMPORT_META_RESOLVE: &str = "import.meta.resolve";
pub const IMPORT_META_VERSION: &str = "import.meta.webpack";
pub const IMPORT_META_MAIN: &str = "import.meta.main";
pub const IMPORT_META_RSPACK_RSC: &str = "import.meta.rspackRsc";
pub const IMPORT_META_HOT: &str = "import.meta.webpackHot";
pub const IMPORT_META_HOT_ACCEPT: &str = "import.meta.webpackHot.accept";
pub const IMPORT_META_HOT_DECLINE: &str = "import.meta.webpackHot.decline";
pub const IMPORT_META_HOT_ALIAS: &str = "import.meta.hot";
pub const IMPORT_META_HOT_ALIAS_ACCEPT: &str = "import.meta.hot.accept";
pub const IMPORT_META_HOT_ALIAS_DECLINE: &str = "import.meta.hot.decline";
pub const IMPORT_META_CONTEXT: &str = "import.meta.webpackContext";
pub const IMPORT_META_GLOB: &str = "import.meta.glob";
}
pub fn parse_order_string(x: &str) -> Option<i32> {
match x {
"true" => Some(0),
"false" => None,
_ => x.parse::<i32>().ok(),
}
}
pub fn static_string_from_expr(expr: &Expr) -> Option<String> {
expr
.as_lit()
.and_then(|lit| {
if let Lit::Str(str) = lit {
return Some(str.value.to_string_lossy().to_string());
}
None
})
.or_else(|| {
if let Some(tpl) = expr.as_tpl()
&& tpl.exprs.is_empty()
&& tpl.quasis.len() == 1
&& let Some(el) = tpl.quasis.first()
{
return Some(el.raw.to_string());
}
None
})
}
pub fn create_traceable_error(
title: String,
message: String,
source: String,
span: DependencyRange,
) -> Error {
Error::from_string(
Some(source),
span.start as usize,
span.end as usize,
title,
message,
)
}
#[inline]
pub fn default_context_reg_exp() -> RspackRegex {
DEFAULT_CONTEXT_REGEXP.clone()
}
pub fn context_reg_exp(
expr: &str,
flags: &str,
error_span: Option<DependencyRange>,
parser: &mut JavascriptParser,
) -> Option<RspackRegex> {
if expr.is_empty() {
return None;
}
let regexp = RspackRegex::with_flags(expr, flags).expect("reg failed");
clean_regexp_in_context_module(regexp, error_span, parser)
}
pub fn clean_regexp_in_context_module(
regexp: RspackRegex,
error_span: Option<DependencyRange>,
parser: &mut JavascriptParser,
) -> Option<RspackRegex> {
if regexp.sticky() || regexp.global() {
if let Some(error_span) = error_span {
let mut error = create_traceable_error(
"Critical dependency".into(),
"Contexts can't use RegExps with the 'g' or 'y' flags".to_string(),
parser.source.to_string(),
error_span,
);
error.severity = Severity::Warning;
parser.add_warning(Diagnostic::from(error));
} else {
let mut err = Error::warning("Contexts can't use RegExps with the 'g' or 'y' flags".into());
err.code = Some("Critical dependency".into());
parser.add_warning(err.into());
}
None
} else {
Some(regexp)
}
}
pub fn get_non_optional_part<'a>(members: &'a [Atom], members_optionals: &[bool]) -> &'a [Atom] {
let mut i = 0;
while i < members.len() && matches!(members_optionals.get(i), Some(false)) {
i += 1;
}
if i != members.len() {
&members[0..i]
} else {
members
}
}
pub fn get_non_optional_member_chain_from_expr<'a>(
mut expr: &'a Expr<'a>,
mut count: i32,
) -> &'a Expr<'a> {
while count != 0 {
if let Expr::Member(member) = expr {
expr = &member.obj;
count -= 1;
} else if let Expr::OptChain(opt_chain) = expr {
expr = match &opt_chain.base {
OptChainBase::Member(member) => &member.obj,
OptChainBase::Call(call) if call.callee.as_member().is_some() => {
let member = call
.callee
.as_member()
.expect("`call.callee` is `MemberExpr` in `if_guard`");
&member.obj
}
_ => unreachable!(),
};
count -= 1;
} else {
unreachable!()
}
}
expr
}
pub fn get_non_optional_member_chain_from_member<'a>(
member: &'a MemberExpr<'a>,
mut count: i32,
) -> &'a Expr<'a> {
count -= 1;
get_non_optional_member_chain_from_expr(&member.obj, count)
}