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

**Related issue:**

 - Closes #7685
This commit is contained in:
Austaras 2023-08-31 13:54:18 +08:00 committed by GitHub
parent 15d3900f60
commit 2db10e9fd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 49 additions and 34 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,3 @@
var Infinity, NaN1;
Infinity.toString();
(1 / 0).toString();
NaN.toString();

View File

@ -1,3 +1,3 @@
var t, r;
t.toString();
var t, i;
Infinity.toString();
NaN.toString();

View File

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

View File

@ -0,0 +1,6 @@
var NaN = 1;
{
let NaN = 1;
console.log(NaN);
}
console.log(NaN);

View File

@ -0,0 +1,5 @@
var NaN__2 = 1;
{
let NaN__3 = 1;
console.log(NaN__3);
}console.log(NaN);

View File

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