Skip to content

Commit 82bd807

Browse files
authoredJan 4, 2024
fix(es/resolver): Resolve top-level undefined, NaN, and Infinity correctly (#8471)
**Description:** For following code ```js var NaN = 1 console.log(NaN) ``` Result would be |Envirnoment|Result| |-|-| |Non strict script(browser, nodejs repl)|NaN| |Non strict script(nodejs script)|1| |Strict script(browser, nodejs repl)|runtime error| |Strict script(nodejs script)|1| |ESM|1| So SWC choose to behave like browser in script mode and confirm to esm standard. **Related issue:** - Closes #8465
1 parent ce76159 commit 82bd807

File tree

8 files changed

+23
-9
lines changed

8 files changed

+23
-9
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function Infinity() {
2+
console.log("xxx");
3+
}
4+
export default Infinity;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function() {
2+
console.log("xxx");
3+
}

‎crates/swc_ecma_minifier/tests/terser_exec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ fn terser_exec(input: PathBuf) {
112112
eprintln!("Optimizing");
113113

114114
let output = run(cm.clone(), &handler, &input, &config);
115-
let output_module = match output {
115+
let output_program = match output {
116116
Some(v) => v,
117117
None => return Err(()),
118118
};
119119

120-
let actual = print(cm, &[output_module], false, false);
120+
let actual = print(cm, &[output_program], false, false);
121121
let actual_stdout = stdout_of(&actual, Duration::from_secs(5)).unwrap();
122122

123123
if let Some(expected_src) = expected_src {

‎crates/swc_ecma_transforms_base/src/resolver/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub fn resolver(
148148
current: Scope::new(ScopeKind::Fn, top_level_mark, None),
149149
ident_type: IdentType::Ref,
150150
in_type: false,
151+
is_module: false,
151152
in_ts_module: false,
152153
decl_kind: DeclKind::Lexical,
153154
strict_mode: false,
@@ -204,6 +205,7 @@ struct Resolver<'a> {
204205
current: Scope<'a>,
205206
ident_type: IdentType,
206207
in_type: bool,
208+
is_module: bool,
207209
in_ts_module: bool,
208210
decl_kind: DeclKind,
209211
strict_mode: bool,
@@ -225,6 +227,7 @@ impl<'a> Resolver<'a> {
225227
current,
226228
ident_type: IdentType::Ref,
227229
in_type: false,
230+
is_module: false,
228231
in_ts_module: false,
229232
config,
230233
decl_kind: DeclKind::Lexical,
@@ -245,6 +248,7 @@ impl<'a> Resolver<'a> {
245248
ident_type: IdentType::Ref,
246249
config: self.config,
247250
in_type: self.in_type,
251+
is_module: self.is_module,
248252
in_ts_module: self.in_ts_module,
249253
decl_kind: self.decl_kind,
250254
strict_mode: self.strict_mode,
@@ -306,7 +310,9 @@ impl<'a> Resolver<'a> {
306310
return match &**sym {
307311
// https://tc39.es/ecma262/multipage/global-object.html#sec-value-properties-of-the-global-object-infinity
308312
// non configurable global value
309-
"undefined" | "NaN" | "Infinity" if mark == self.config.top_level_mark => {
313+
"undefined" | "NaN" | "Infinity"
314+
if mark == self.config.top_level_mark && !self.is_module =>
315+
{
310316
Some(self.config.unresolved_mark)
311317
}
312318
_ => Some(mark),
@@ -1032,6 +1038,7 @@ impl<'a> VisitMut for Resolver<'a> {
10321038

10331039
fn visit_mut_module(&mut self, module: &mut Module) {
10341040
self.strict_mode = true;
1041+
self.is_module = true;
10351042
module.visit_mut_children_with(self)
10361043
}
10371044

‎crates/swc_ecma_transforms_base/tests/resolver/issues/7685/output.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ var NaN__2 = 1;
22
{
33
let NaN__3 = 1;
44
console.log(NaN__3);
5-
}console.log(NaN);
5+
}console.log(NaN__2);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var NaN__2;
2-
console.log(NaN.toString());
2+
console.log(NaN__2.toString());
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var NaN__2 = 5;
2-
console.log(NaN.toString());
2+
console.log(NaN__2.toString());

‎crates/swc_node_bundler/tests/pass/resolve-name-fix/output/entry.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4570,7 +4570,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) {
45704570
var Math1 = global.Math;
45714571
var RangeError1 = global.RangeError;
45724572
// eslint-disable-next-line no-shadow-restricted-names
4573-
global.Infinity;
4573+
var Infinity1 = global.Infinity;
45744574
var BaseBuffer = $ArrayBuffer;
45754575
var abs = Math1.abs;
45764576
var pow = Math1.pow;
@@ -4595,7 +4595,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) {
45954595
var e, m, c;
45964596
value = abs(value);
45974597
// eslint-disable-next-line no-self-compare
4598-
if (value != value || value === Infinity) {
4598+
if (value != value || value === Infinity1) {
45994599
// eslint-disable-next-line no-self-compare
46004600
m = value != value ? 1 : 0;
46014601
e = eMax;
@@ -4645,7 +4645,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) {
46454645
nBits += mLen;
46464646
for(; nBits > 0; m = m * 256 + buffer[i], i--, nBits -= 8);
46474647
if (e === 0) e = 1 - eBias;
4648-
else if (e === eMax) return m ? NaN : s ? -Infinity : Infinity;
4648+
else if (e === eMax) return m ? NaN : s ? -Infinity1 : Infinity1;
46494649
else {
46504650
m = m + pow(2, mLen);
46514651
e = e - eBias;

0 commit comments

Comments
 (0)
Please sign in to comment.