Skip to content

Commit 1a739b7

Browse files
authored
fix(es/minifier): Fix undefined judgement (#9146)
**Description:** by symbol name -> by whether it points to global undefined or not. The updated test results all involve variables with `undefined` ident (although they are indeed uninitialized). **Related issue:** - Closes: #8567
1 parent 2481d73 commit 1a739b7

File tree

9 files changed

+80
-85
lines changed

9 files changed

+80
-85
lines changed

crates/swc/tests/tsc-references/fixSignatureCaching.2.minified.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
376376
return impl.prepareDetectionCache(this._cache, this.ua, this.maxPhoneWidth), this._cache.tablet;
377377
},
378378
userAgent: function() {
379-
return undefined === this._cache.userAgent && (this._cache.userAgent = impl.findMatch(impl.mobileDetectRules.uas, this.ua)), this._cache.userAgent;
379+
return this._cache.userAgent === undefined && (this._cache.userAgent = impl.findMatch(impl.mobileDetectRules.uas, this.ua)), this._cache.userAgent;
380380
},
381381
userAgents: function() {
382-
return undefined === this._cache.userAgents && (this._cache.userAgents = impl.findMatches(impl.mobileDetectRules.uas, this.ua)), this._cache.userAgents;
382+
return this._cache.userAgents === undefined && (this._cache.userAgents = impl.findMatches(impl.mobileDetectRules.uas, this.ua)), this._cache.userAgents;
383383
},
384384
os: function() {
385-
return undefined === this._cache.os && (this._cache.os = impl.detectOS(this.ua)), this._cache.os;
385+
return this._cache.os === undefined && (this._cache.os = impl.detectOS(this.ua)), this._cache.os;
386386
},
387387
version: function(key) {
388388
return impl.getVersion(key, this.ua);
@@ -400,7 +400,7 @@ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
400400
return MobileDetect.isPhoneSized(maxPhoneWidth || this.maxPhoneWidth);
401401
},
402402
mobileGrade: function() {
403-
return undefined === this._cache.grade && (this._cache.grade = impl.mobileGrade(this)), this._cache.grade;
403+
return this._cache.grade === undefined && (this._cache.grade = impl.mobileGrade(this)), this._cache.grade;
404404
}
405405
}, 'undefined' != typeof window && window.screen ? MobileDetect.isPhoneSized = function(maxPhoneWidth) {
406406
return maxPhoneWidth < 0 ? undefined : impl.getDeviceSmallerSide() <= maxPhoneWidth;

crates/swc_ecma_minifier/src/compress/util/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,13 @@ pub(crate) fn negate_cost(
378378

379379
pub(crate) fn is_pure_undefined(expr_ctx: &ExprCtx, e: &Expr) -> bool {
380380
match e {
381-
Expr::Ident(Ident { sym, .. }) if &**sym == "undefined" => true,
382-
383381
Expr::Unary(UnaryExpr {
384382
op: UnaryOp::Void,
385383
arg,
386384
..
387385
}) if !arg.may_have_side_effects(expr_ctx) => true,
388386

389-
_ => false,
387+
_ => e.is_undefined(expr_ctx),
390388
}
391389
}
392390

crates/swc_ecma_minifier/tests/benches-full/lodash.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@
723723
return hasOwnProperty.call(data, key) ? data[key] : undefined;
724724
}, Hash.prototype.has = function(key) {
725725
var data = this.__data__;
726-
return nativeCreate ? undefined !== data[key] : hasOwnProperty.call(data, key);
726+
return nativeCreate ? data[key] !== undefined : hasOwnProperty.call(data, key);
727727
}, Hash.prototype.set = function(key, value) {
728728
var data = this.__data__;
729729
return this.size += this.has(key) ? 0 : 1, data[key] = nativeCreate && undefined === value ? HASH_UNDEFINED : value, this;
@@ -1546,7 +1546,7 @@
15461546
}
15471547
(value = source[5]) && (partials = data[5], data[5] = partials ? composeArgsRight(partials, value, source[6]) : value, data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : source[6]), (value = source[7]) && (data[7] = value), 128 & srcBitmask && (data[8] = null == data[8] ? source[8] : nativeMin(data[8], source[8])), null == data[9] && (data[9] = source[9]), data[0] = source[0], data[1] = newBitmask;
15481548
}
1549-
}(newData, data), func = newData[0], bitmask = newData[1], thisArg = newData[2], partials = newData[3], holders = newData[4], (arity = newData[9] = undefined === newData[9] ? isBindKey ? 0 : func.length : nativeMax(newData[9] - length, 0)) || !(24 & bitmask) || (bitmask &= -25), bitmask && 1 != bitmask) 8 == bitmask || 16 == bitmask ? (func1 = func, bitmask1 = bitmask, arity1 = arity, Ctor = createCtor(func1), result = function wrapper() {
1549+
}(newData, data), func = newData[0], bitmask = newData[1], thisArg = newData[2], partials = newData[3], holders = newData[4], (arity = newData[9] = newData[9] === undefined ? isBindKey ? 0 : func.length : nativeMax(newData[9] - length, 0)) || !(24 & bitmask) || (bitmask &= -25), bitmask && 1 != bitmask) 8 == bitmask || 16 == bitmask ? (func1 = func, bitmask1 = bitmask, arity1 = arity, Ctor = createCtor(func1), result = function wrapper() {
15501550
for(var length = arguments.length, args = Array1(length), index = length, placeholder = getHolder(wrapper); index--;)args[index] = arguments[index];
15511551
var holders = length < 3 && args[0] !== placeholder && args[length - 1] !== placeholder ? [] : replaceHolders(args, placeholder);
15521552
return (length -= holders.length) < arity1 ? createRecurry(func1, bitmask1, createHybrid, wrapper.placeholder, undefined, args, holders, undefined, undefined, arity1 - length) : apply(this && this !== root && this instanceof wrapper ? Ctor : func1, this, args);
@@ -3061,7 +3061,7 @@
30613061
}, lodash.prototype.commit = function() {
30623062
return new LodashWrapper(this.value(), this.__chain__);
30633063
}, lodash.prototype.next = function() {
3064-
undefined === this.__values__ && (this.__values__ = toArray(this.value()));
3064+
this.__values__ === undefined && (this.__values__ = toArray(this.value()));
30653065
var done = this.__index__ >= this.__values__.length, value = done ? undefined : this.__values__[this.__index__++];
30663066
return {
30673067
done: done,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const templatePath = () => {
2+
const undefined = "lol1";
3+
return undefined;
4+
};
5+
console.log(templatePath());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("lol1");

crates/swc_ecma_minifier/tests/full/feedback-mapbox/785-e1932cc99ac3bb67/output.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc_ecma_minifier/tests/projects/output/angular-1.2.5.js

+18-27
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
if (!equals(o1[key], o2[key])) return !1;
172172
keySet[key] = !0;
173173
}
174-
for(key in o2)if (!keySet.hasOwnProperty(key) && "$" !== key.charAt(0) && undefined !== o2[key] && !isFunction(o2[key])) return !1;
174+
for(key in o2)if (!keySet.hasOwnProperty(key) && "$" !== key.charAt(0) && o2[key] !== undefined && !isFunction(o2[key])) return !1;
175175
return !0;
176176
}
177177
}
@@ -199,7 +199,7 @@
199199
return "string" == typeof key && "$" === key.charAt(0) ? val = undefined : isWindow(value) ? val = "$WINDOW" : value && document1 === value ? val = "$DOCUMENT" : isScope(value) && (val = "$SCOPE"), val;
200200
}
201201
function toJson(obj, pretty) {
202-
if (void 0 !== obj) return JSON.stringify(obj, toJsonReplacer, pretty ? " " : null);
202+
return void 0 === obj ? undefined : JSON.stringify(obj, toJsonReplacer, pretty ? " " : null);
203203
}
204204
function fromJson(json) {
205205
return isString(json) ? JSON.parse(json) : json;
@@ -440,7 +440,7 @@
440440
for(var names = isArray(name) ? name : [
441441
name
442442
]; element.length;){
443-
for(var i = 0, ii = names.length; i < ii; i++)if (undefined !== (value = element.data(names[i]))) return value;
443+
for(var i = 0, ii = names.length; i < ii; i++)if ((value = element.data(names[i])) !== undefined) return value;
444444
element = element.parent();
445445
}
446446
}
@@ -939,7 +939,7 @@
939939
var cookieLength, cookieArray, cookie, i, index;
940940
if (name) undefined === value ? rawDocument.cookie = escape(name) + "=;path=" + cookiePath + ";expires=Thu, 01 Jan 1970 00:00:00 GMT" : isString(value) && (cookieLength = (rawDocument.cookie = escape(name) + "=" + escape(value) + ";path=" + cookiePath).length + 1) > 4096 && $log.warn("Cookie '" + name + "' possibly not set or overflowed because it was too large (" + cookieLength + " > 4096 bytes)!");
941941
else {
942-
if (rawDocument.cookie !== lastCookieString) for(i = 0, cookieArray = (lastCookieString = rawDocument.cookie).split("; "), lastCookies = {}; i < cookieArray.length; i++)(index = (cookie = cookieArray[i]).indexOf("=")) > 0 && undefined === lastCookies[name = unescape(cookie.substring(0, index))] && (lastCookies[name] = unescape(cookie.substring(index + 1)));
942+
if (rawDocument.cookie !== lastCookieString) for(i = 0, cookieArray = (lastCookieString = rawDocument.cookie).split("; "), lastCookies = {}; i < cookieArray.length; i++)(index = (cookie = cookieArray[i]).indexOf("=")) > 0 && lastCookies[name = unescape(cookie.substring(0, index))] === undefined && (lastCookies[name] = unescape(cookie.substring(index + 1)));
943943
return lastCookies;
944944
}
945945
}, self.defer = function(fn, delay) {
@@ -1847,7 +1847,7 @@
18471847
this.$$url = encodePath(this.$$path) + (search ? "?" + search : "") + hash, this.$$absUrl = appBaseNoFile + this.$$url.substr(1);
18481848
}, this.$$rewrite = function(url) {
18491849
var appUrl, prevAppUrl;
1850-
return undefined !== (appUrl = beginsWith(appBase, url)) ? (prevAppUrl = appUrl, undefined !== (appUrl = beginsWith(basePrefix, appUrl))) ? appBaseNoFile + (beginsWith("/", appUrl) || appUrl) : appBase + prevAppUrl : undefined !== (appUrl = beginsWith(appBaseNoFile, url)) ? appBaseNoFile + appUrl : appBaseNoFile == url + "/" ? appBaseNoFile : void 0;
1850+
return (appUrl = beginsWith(appBase, url)) !== undefined ? (prevAppUrl = appUrl, (appUrl = beginsWith(basePrefix, appUrl)) !== undefined) ? appBaseNoFile + (beginsWith("/", appUrl) || appUrl) : appBase + prevAppUrl : (appUrl = beginsWith(appBaseNoFile, url)) !== undefined ? appBaseNoFile + appUrl : appBaseNoFile == url + "/" ? appBaseNoFile : void 0;
18511851
};
18521852
}
18531853
function LocationHashbangUrl(appBase, hashPrefix) {
@@ -2246,7 +2246,7 @@
22462246
promise.then(function(val) {
22472247
promise.$$v = val;
22482248
});
2249-
}(obj), undefined === obj.$$v && (obj.$$v = {}), obj = obj.$$v);
2249+
}(obj), obj.$$v === undefined && (obj.$$v = {}), obj = obj.$$v);
22502250
}
22512251
return obj[key = ensureSafeMemberName(element.shift(), fullExp)] = setValue, setValue;
22522252
}
@@ -2403,9 +2403,9 @@
24032403
var parser = this, indexFn = this.expression();
24042404
return this.consume("]"), extend(function(self, locals) {
24052405
var v, p, o = obj(self, locals), i = indexFn(self, locals);
2406-
if (o) return (v = ensureSafeObject(o[i], parser.text)) && v.then && parser.options.unwrapPromises && (p = v, "$$v" in v || (p.$$v = undefined, p.then(function(val) {
2406+
return o ? ((v = ensureSafeObject(o[i], parser.text)) && v.then && parser.options.unwrapPromises && (p = v, "$$v" in v || (p.$$v = undefined, p.then(function(val) {
24072407
p.$$v = val;
2408-
})), v = v.$$v), v;
2408+
})), v = v.$$v), v) : undefined;
24092409
}, {
24102410
assign: function(self, value, locals) {
24112411
var key = indexFn(self, locals);
@@ -3488,44 +3488,38 @@
34883488
number: function(scope, element, attr, ctrl, $sniffer, $browser) {
34893489
if (textInputType(scope, element, attr, ctrl, $sniffer, $browser), ctrl.$parsers.push(function(value) {
34903490
var empty = ctrl.$isEmpty(value);
3491-
if (empty || NUMBER_REGEXP.test(value)) return ctrl.$setValidity("number", !0), "" === value ? null : empty ? value : parseFloat(value);
3492-
ctrl.$setValidity("number", !1);
3491+
return empty || NUMBER_REGEXP.test(value) ? (ctrl.$setValidity("number", !0), "" === value ? null : empty ? value : parseFloat(value)) : (ctrl.$setValidity("number", !1), undefined);
34933492
}), ctrl.$formatters.push(function(value) {
34943493
return ctrl.$isEmpty(value) ? "" : "" + value;
34953494
}), attr.min) {
34963495
var minValidator = function(value) {
34973496
var min = parseFloat(attr.min);
3498-
if (ctrl.$isEmpty(value) || !(value < min)) return ctrl.$setValidity("min", !0), value;
3499-
ctrl.$setValidity("min", !1);
3497+
return !ctrl.$isEmpty(value) && value < min ? (ctrl.$setValidity("min", !1), undefined) : (ctrl.$setValidity("min", !0), value);
35003498
};
35013499
ctrl.$parsers.push(minValidator), ctrl.$formatters.push(minValidator);
35023500
}
35033501
if (attr.max) {
35043502
var maxValidator = function(value) {
35053503
var max = parseFloat(attr.max);
3506-
if (ctrl.$isEmpty(value) || !(value > max)) return ctrl.$setValidity("max", !0), value;
3507-
ctrl.$setValidity("max", !1);
3504+
return !ctrl.$isEmpty(value) && value > max ? (ctrl.$setValidity("max", !1), undefined) : (ctrl.$setValidity("max", !0), value);
35083505
};
35093506
ctrl.$parsers.push(maxValidator), ctrl.$formatters.push(maxValidator);
35103507
}
35113508
ctrl.$formatters.push(function(value) {
3512-
if (ctrl.$isEmpty(value) || isNumber(value)) return ctrl.$setValidity("number", !0), value;
3513-
ctrl.$setValidity("number", !1);
3509+
return ctrl.$isEmpty(value) || isNumber(value) ? (ctrl.$setValidity("number", !0), value) : (ctrl.$setValidity("number", !1), undefined);
35143510
});
35153511
},
35163512
url: function(scope, element, attr, ctrl, $sniffer, $browser) {
35173513
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
35183514
var urlValidator = function(value) {
3519-
if (ctrl.$isEmpty(value) || URL_REGEXP.test(value)) return ctrl.$setValidity("url", !0), value;
3520-
ctrl.$setValidity("url", !1);
3515+
return ctrl.$isEmpty(value) || URL_REGEXP.test(value) ? (ctrl.$setValidity("url", !0), value) : (ctrl.$setValidity("url", !1), undefined);
35213516
};
35223517
ctrl.$formatters.push(urlValidator), ctrl.$parsers.push(urlValidator);
35233518
},
35243519
email: function(scope, element, attr, ctrl, $sniffer, $browser) {
35253520
textInputType(scope, element, attr, ctrl, $sniffer, $browser);
35263521
var emailValidator = function(value) {
3527-
if (ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value)) return ctrl.$setValidity("email", !0), value;
3528-
ctrl.$setValidity("email", !1);
3522+
return ctrl.$isEmpty(value) || EMAIL_REGEXP.test(value) ? (ctrl.$setValidity("email", !0), value) : (ctrl.$setValidity("email", !1), undefined);
35293523
};
35303524
ctrl.$formatters.push(emailValidator), ctrl.$parsers.push(emailValidator);
35313525
},
@@ -3591,8 +3585,7 @@
35913585
element.val(ctrl.$isEmpty(ctrl.$viewValue) ? "" : ctrl.$viewValue);
35923586
};
35933587
var patternValidator, match, pattern = attr.ngPattern, validate = function(regexp, value) {
3594-
if (ctrl.$isEmpty(value) || regexp.test(value)) return ctrl.$setValidity("pattern", !0), value;
3595-
ctrl.$setValidity("pattern", !1);
3588+
return ctrl.$isEmpty(value) || regexp.test(value) ? (ctrl.$setValidity("pattern", !0), value) : (ctrl.$setValidity("pattern", !1), undefined);
35963589
};
35973590
if (pattern && ((match = pattern.match(/^\/(.*)\/([gim]*)$/)) ? (pattern = new RegExp(match[1], match[2]), patternValidator = function(value) {
35983591
return validate(pattern, value);
@@ -3602,15 +3595,13 @@
36023595
return validate(patternObj, value);
36033596
}, ctrl.$formatters.push(patternValidator), ctrl.$parsers.push(patternValidator)), attr.ngMinlength) {
36043597
var minlength = int(attr.ngMinlength), minLengthValidator = function(value) {
3605-
if (ctrl.$isEmpty(value) || !(value.length < minlength)) return ctrl.$setValidity("minlength", !0), value;
3606-
ctrl.$setValidity("minlength", !1);
3598+
return !ctrl.$isEmpty(value) && value.length < minlength ? (ctrl.$setValidity("minlength", !1), undefined) : (ctrl.$setValidity("minlength", !0), value);
36073599
};
36083600
ctrl.$parsers.push(minLengthValidator), ctrl.$formatters.push(minLengthValidator);
36093601
}
36103602
if (attr.ngMaxlength) {
36113603
var maxlength = int(attr.ngMaxlength), maxLengthValidator = function(value) {
3612-
if (ctrl.$isEmpty(value) || !(value.length > maxlength)) return ctrl.$setValidity("maxlength", !0), value;
3613-
ctrl.$setValidity("maxlength", !1);
3604+
return !ctrl.$isEmpty(value) && value.length > maxlength ? (ctrl.$setValidity("maxlength", !1), undefined) : (ctrl.$setValidity("maxlength", !0), value);
36143605
};
36153606
ctrl.$parsers.push(maxLengthValidator), ctrl.$formatters.push(maxLengthValidator);
36163607
}
@@ -3720,7 +3711,7 @@
37203711
}), list;
37213712
}
37223713
}), ctrl.$formatters.push(function(value) {
3723-
if (isArray(value)) return value.join(", ");
3714+
return isArray(value) ? value.join(", ") : undefined;
37243715
}), ctrl.$isEmpty = function(value) {
37253716
return !value || !value.length;
37263717
};

0 commit comments

Comments
 (0)