Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/resolver): Correctly resolve global value #7893

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
console.log(Infinity);
if (Infinity > 4) console.log("Infinity");
}{
let NaN1 = 3;
let NaN = 3;
console.log(NaN);
if (NaN < 4) console.log("NaN");
}{
let undefined = 3;
console.log(undefined);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var a = 0;
function n(n) {
function f(n) {
function f(n) {
if ((false <= NaN) & (this >> 1 >= 0)) {
a++;
var n = 0;
function a(a) {
function f(a) {
function f(a) {
if ((false <= a) & (this >> 1 >= 0)) {
n++;
}
}
var o = f(NaN, a++);
var o = f(NaN, n++);
}
var n = f(-3, -1);
var a = f(-3, -1);
}
n(false);
console.log(a);
a(false);
console.log(n);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var n = "FAIL";
(function() {
function o() {
function o(o) {
(true << NaN) - 0 / 0 || (n = "PASS");
(true << o) - 0 / 0 || (n = "PASS");
}
o([]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var c = "FAIL";
(function() {
function n(n, a) {
function o() {
function n(n, o) {
function a() {
switch(n){
case n:
break;
case ((c = "PASS"), NaN):
case ((c = "PASS"), o):
break;
}
}
o();
a();
}
n(0 / 0);
})();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var c = "FAIL";
(function() {
function n(n, a) {
function o() {
function n(n, o) {
function a() {
switch(n){
case n:
break;
case ((c = "PASS"), NaN):
case ((c = "PASS"), o):
break;
}
}
o();
a();
}
n(0 / 0);
})();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var Infinity, NaN1;
Infinity.toString();
(1 / 0).toString();
NaN.toString();
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var t, r;
t.toString();
var t, i;
Infinity.toString();
NaN.toString();
18 changes: 11 additions & 7 deletions crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_hash::FxHashSet;
use swc_atoms::{js_word, JsWord};
use swc_atoms::JsWord;
use swc_common::{
collections::{AHashMap, AHashSet},
Mark, Span, SyntaxContext,
Expand Down Expand Up @@ -269,11 +269,6 @@ impl<'a> Resolver<'a> {
}

fn mark_for_ref_inner(&self, sym: &JsWord, stop_an_fn_scope: bool) -> Option<Mark> {
// NaN always points the globals
if *sym == js_word!("NaN") {
return Some(self.config.unresolved_mark);
}

if self.config.handle_types && self.in_type {
let mut mark = self.current.mark;
let mut scope = Some(&self.current);
Expand Down Expand Up @@ -307,7 +302,16 @@ impl<'a> Resolver<'a> {
if mark == Mark::root() {
return None;
}
return Some(mark);

return match &**sym {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#value_properties
"undefined" | "NaN" | "Infinity" | "globalThis"
if mark == self.config.top_level_mark =>
{
Some(self.config.unresolved_mark)
}
_ => Some(mark),
};
}

if cur.kind == ScopeKind::Fn && stop_an_fn_scope {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var NaN = 1;
{
let NaN = 1;
console.log(NaN);
}
console.log(NaN);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var NaN__2 = 1;
{
let NaN__3 = 1;
console.log(NaN__3);
}console.log(NaN);
Original file line number Diff line number Diff line change
Expand Up @@ -4569,8 +4569,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) {
var $DataView = global[DATA_VIEW];
var Math1 = global.Math;
var RangeError1 = global.RangeError;
// eslint-disable-next-line no-shadow-restricted-names
var Infinity1 = global.Infinity;
global.Infinity;
var BaseBuffer = $ArrayBuffer;
var abs = Math1.abs;
var pow = Math1.pow;
Expand All @@ -4595,7 +4594,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) {
var e, m, c;
value = abs(value);
// eslint-disable-next-line no-self-compare
if (value != value || value === Infinity1) {
if (value != value || value === Infinity) {
// eslint-disable-next-line no-self-compare
m = value != value ? 1 : 0;
e = eMax;
Expand Down Expand Up @@ -4645,7 +4644,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) {
nBits += mLen;
for(; nBits > 0; m = m * 256 + buffer[i], i--, nBits -= 8);
if (e === 0) e = 1 - eBias;
else if (e === eMax) return m ? NaN : s ? -Infinity1 : Infinity1;
else if (e === eMax) return m ? NaN : s ? -Infinity : Infinity;
else {
m = m + pow(2, mLen);
e = e - eBias;
Expand Down