Skip to content

Commit

Permalink
fix(es/minifier): Check for conditional usages while inlining (#2459)
Browse files Browse the repository at this point in the history
swc_ecma_minifier:
 - `hoist_props`: Check for `used_in_cond` before inlining.
  • Loading branch information
kdy1 authored Oct 18, 2021
1 parent 9ba68c6 commit 06ca25f
Show file tree
Hide file tree
Showing 27 changed files with 193 additions and 112 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "src/lists/*.json"]
license = "Apache-2.0/MIT"
name = "swc_ecma_minifier"
repository = "https://github.com/swc-project/swc.git"
version = "0.42.0"
version = "0.42.1"

[features]
debug = ["backtrace"]
Expand Down
1 change: 1 addition & 0 deletions ecmascript/minifier/src/compress/optimize/hoist_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ where
&& v.has_property_access
&& v.is_fn_local
&& !v.used_in_loop
&& !v.used_in_cond
})
})
.unwrap_or(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2723,8 +2723,8 @@
};
},
23140: function(module, __unused_webpack_exports, __webpack_require__) {
var wellKnownSymbol = __webpack_require__(81019), create = __webpack_require__(18255), UNSCOPABLES = wellKnownSymbol("unscopables"), ArrayPrototype = Array.prototype;
void 0 == ArrayPrototype[UNSCOPABLES] && __webpack_require__(94770).f(ArrayPrototype, UNSCOPABLES, {
var wellKnownSymbol = __webpack_require__(81019), create = __webpack_require__(18255), definePropertyModule = __webpack_require__(94770), UNSCOPABLES = wellKnownSymbol("unscopables"), ArrayPrototype = Array.prototype;
void 0 == ArrayPrototype[UNSCOPABLES] && definePropertyModule.f(ArrayPrototype, UNSCOPABLES, {
configurable: !0,
value: create(null)
}), module.exports = function(key) {
Expand Down Expand Up @@ -5790,8 +5790,8 @@
},
23172: function(__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
"use strict";
var isCallable = __webpack_require__(67106), isObject = __webpack_require__(39817), getPrototypeOf = __webpack_require__(39311), HAS_INSTANCE = __webpack_require__(81019)("hasInstance"), FunctionPrototype = Function.prototype;
HAS_INSTANCE in FunctionPrototype || __webpack_require__(94770).f(FunctionPrototype, HAS_INSTANCE, {
var isCallable = __webpack_require__(67106), isObject = __webpack_require__(39817), definePropertyModule = __webpack_require__(94770), getPrototypeOf = __webpack_require__(39311), wellKnownSymbol = __webpack_require__(81019), HAS_INSTANCE = wellKnownSymbol("hasInstance"), FunctionPrototype = Function.prototype;
HAS_INSTANCE in FunctionPrototype || definePropertyModule.f(FunctionPrototype, HAS_INSTANCE, {
value: function(O) {
if (!isCallable(this) || !isObject(O)) return !1;
if (!isObject(this.prototype)) return O instanceof this;
Expand Down Expand Up @@ -8418,14 +8418,14 @@
},
74445: function(module, __unused_webpack_exports, __webpack_require__) {
"use strict";
var InternalWeakMap, global = __webpack_require__(19514), redefineAll = __webpack_require__(59855), collection = __webpack_require__(6807), collectionWeak = __webpack_require__(85653), isObject = __webpack_require__(39817), enforceIternalState = __webpack_require__(44670).enforce, NATIVE_WEAK_MAP = __webpack_require__(83165), IS_IE11 = !global.ActiveXObject && "ActiveXObject" in global, isExtensible = Object.isExtensible, wrapper = function(init) {
var InternalWeakMap, global = __webpack_require__(19514), redefineAll = __webpack_require__(59855), InternalMetadataModule = __webpack_require__(19322), collection = __webpack_require__(6807), collectionWeak = __webpack_require__(85653), isObject = __webpack_require__(39817), enforceIternalState = __webpack_require__(44670).enforce, NATIVE_WEAK_MAP = __webpack_require__(83165), IS_IE11 = !global.ActiveXObject && "ActiveXObject" in global, isExtensible = Object.isExtensible, wrapper = function(init) {
return function() {
return init(this, arguments.length ? arguments[0] : void 0);
};
};
}, $WeakMap = module.exports = collection("WeakMap", wrapper, collectionWeak);
if (NATIVE_WEAK_MAP && IS_IE11) {
InternalWeakMap = collectionWeak.getConstructor(wrapper, "WeakMap", !0), __webpack_require__(19322).enable();
var WeakMapPrototype = (module.exports = collection("WeakMap", wrapper, collectionWeak)).prototype, nativeDelete = WeakMapPrototype.delete, nativeHas = WeakMapPrototype.has, nativeGet = WeakMapPrototype.get, nativeSet = WeakMapPrototype.set;
InternalWeakMap = collectionWeak.getConstructor(wrapper, "WeakMap", !0), InternalMetadataModule.enable();
var WeakMapPrototype = $WeakMap.prototype, nativeDelete = WeakMapPrototype.delete, nativeHas = WeakMapPrototype.has, nativeGet = WeakMapPrototype.get, nativeSet = WeakMapPrototype.set;
redefineAll(WeakMapPrototype, {
"delete": function(key) {
if (isObject(key) && !isExtensible(key)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@



const CONST_1 = 'const1';
const CONST_2 = 'const2';

function useHook1() {
const [v1, v1_set] = useState(undefined);

useEffect(() => {
if (
GLOBALS.get(CONST_1) &&
GLOBALS.get(CONST_2)
) {
v1_set(true);
} else {
v1_set(false);
}
}, []);

return v1;
}


function useHook2() {
const [a1, a1_set] = useState({});
useEffect(() => {
a1_set(
JSON.parse(GLOBALS.get(CONST1) || '{}') ,
);
}, []);
return a1;
}



export function HeaderCTA() {
const varB = useHook2();
const varA = useHook1();

// Loading...
if (varA === undefined) {
return null;
}

if (varA) {
return (
use(varB.field)
);
}

return (
pure()
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function HeaderCTA() {
const varB = function() {
const [a1, a1_set] = useState({
});
return useEffect(()=>{
a1_set(JSON.parse(GLOBALS.get(CONST1) || "{}"));
}, []), a1;
}(), varA = function() {
const [v1, v1_set] = useState(void 0);
return useEffect(()=>{
GLOBALS.get("const1") && GLOBALS.get("const2") ? v1_set(!0) : v1_set(!1);
}, []), v1;
}();
return void 0 === varA ? null : varA ? use(varB.field) : pure();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ var isServerSide = "undefined" == typeof document, META = "M", TITLE = "T", LINK
var newMeta = metaQueue.find(function(m) {
return m.keyword === oldMeta.keyword && (m.charset || m[m.keyword] === oldMeta[m.keyword]);
});
newMeta ? changeOrCreateMetaTag(newMeta) : document.head.removeChild(document.head.querySelectorAll(oldMeta.charset ? "meta[" + oldMeta.keyword + "]" : "meta[" + oldMeta.keyword + "=\"" + oldMeta[oldMeta.keyword] + "\"]")[0]);
if (newMeta) changeOrCreateMetaTag(newMeta);
else {
var result = document.head.querySelectorAll(oldMeta.charset ? "meta[" + oldMeta.keyword + "]" : "meta[" + oldMeta.keyword + "=\"" + oldMeta[oldMeta.keyword] + "\"]");
document.head.removeChild(result[0]);
}
}
}
},
Expand Down Expand Up @@ -69,14 +73,15 @@ var isServerSide = "undefined" == typeof document, META = "M", TITLE = "T", LINK
}
};
}, defaultDispatcher = createDispatcher(), DispatcherContext = D(defaultDispatcher), useLang = function(language) {
isServerSide && F(DispatcherContext)._setLang(language), y(function() {
var dispatcher = F(DispatcherContext);
isServerSide && dispatcher._setLang(language), y(function() {
document.getElementsByTagName("html")[0].setAttribute("lang", language);
}, [
language,
]);
}, useLink = function(options) {
var hasMounted = s(!1), node = s(), originalOptions = s();
isServerSide && !hasMounted.current && F(DispatcherContext)._addToQueue(LINK, options), y(function() {
var dispatcher = F(DispatcherContext), hasMounted = s(!1), node = s(), originalOptions = s();
isServerSide && !hasMounted.current && dispatcher._addToQueue(LINK, options), y(function() {
hasMounted.current && Object.keys(options).forEach(function(key) {
node.current.setAttribute(key, options[key]);
});
Expand Down
7 changes: 4 additions & 3 deletions ecmascript/minifier/tests/projects/output/jquery-1.9.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@
queue: function(type, data) {
var setter = 2;
return ("string" != typeof type && (data = type, type = "fx", setter--), arguments.length < setter) ? jQuery.queue(this[0], type) : data === undefined ? this : this.each(function() {
jQuery._queueHooks(this, type), "fx" === type && "inprogress" !== jQuery.queue(this, type, data)[0] && jQuery.dequeue(this, type);
var queue = jQuery.queue(this, type, data);
jQuery._queueHooks(this, type), "fx" === type && "inprogress" !== queue[0] && jQuery.dequeue(this, type);
});
},
dequeue: function(type) {
Expand Down Expand Up @@ -659,8 +660,8 @@
val: function(value) {
var ret, hooks, isFunction, elem = this[0];
return arguments.length ? (isFunction = jQuery.isFunction(value), this.each(function(i) {
var val;
1 === this.nodeType && (null == (val = isFunction ? value.call(this, i, jQuery(this).val()) : value) ? val = "" : "number" == typeof val ? val += "" : jQuery.isArray(val) && (val = jQuery.map(val, function(value) {
var val, self = jQuery(this);
1 === this.nodeType && (null == (val = isFunction ? value.call(this, i, self.val()) : value) ? val = "" : "number" == typeof val ? val += "" : jQuery.isArray(val) && (val = jQuery.map(val, function(value) {
return null == value ? "" : value + "";
})), (hooks = jQuery.valHooks[this.type] || jQuery.valHooks[this.nodeName.toLowerCase()]) && "set" in hooks && undefined !== hooks.set(this, val, "value") || (this.value = val));
})) : elem ? (hooks = jQuery.valHooks[elem.type] || jQuery.valHooks[elem.nodeName.toLowerCase()]) && "get" in hooks && undefined !== (ret = hooks.get(elem, "value")) ? ret : "string" == typeof (ret = elem.value) ? ret.replace(rreturn, "") : null == ret ? "" : ret : void 0;
Expand Down
14 changes: 8 additions & 6 deletions ecmascript/minifier/tests/projects/output/jquery.mobile-1.4.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,8 @@
})), this._headerCloseButton = btn;
},
close: function() {
this._isCloseable && (this._isCloseable = !1, $.mobile.hashListeningEnabled && $.mobile.navigate.history.activeIndex > 0 ? $.mobile.back() : $.mobile.pageContainer.pagecontainer("back"));
var hist = $.mobile.navigate.history;
this._isCloseable && (this._isCloseable = !1, $.mobile.hashListeningEnabled && hist.activeIndex > 0 ? $.mobile.back() : $.mobile.pageContainer.pagecontainer("back"));
}
});
})(jQuery, this), $12 = jQuery, rInitialLetter = /([A-Z])/g, iconposClass = function(iconpos) {
Expand Down Expand Up @@ -2370,7 +2371,8 @@
_updateAll: function() {
var self = this;
this._getInputSet().each(function() {
(this.checked || "checkbox" === self.inputtype) && $(this).trigger("change");
var $this = $(this);
(this.checked || "checkbox" === self.inputtype) && $this.trigger("change");
}).checkboxradio("refresh");
},
_reset: function() {
Expand Down Expand Up @@ -4423,10 +4425,10 @@
}
},
_getFilterableItems: function() {
var children = this.options.children, items = children ? $.isFunction(children) ? children() : children.nodeName ? $(children) : children.jquery ? children : this.element.find(children) : {
var elem = this.element, children = this.options.children, items = children ? $.isFunction(children) ? children() : children.nodeName ? $(children) : children.jquery ? children : this.element.find(children) : {
length: 0
};
return 0 === items.length && (items = this.element.children()), items;
return 0 === items.length && (items = elem.children()), items;
},
_filterItems: function(val) {
var idx, callback, length, show = [], hide = [], opts = this.options, filterItems = this._getFilterableItems();
Expand Down Expand Up @@ -4704,8 +4706,8 @@
}), this._focusable(this.tabs), this._hoverable(this.tabs);
},
_setupHeightStyle: function(heightStyle) {
var maxHeight;
"fill" === heightStyle ? (maxHeight = this.element.parent().height(), maxHeight -= this.element.outerHeight() - this.element.height(), this.element.siblings(":visible").each(function() {
var maxHeight, parent = this.element.parent();
"fill" === heightStyle ? (maxHeight = parent.height(), maxHeight -= this.element.outerHeight() - this.element.height(), this.element.siblings(":visible").each(function() {
var elem = $(this), position = elem.css("position");
"absolute" !== position && "fixed" !== position && (maxHeight -= elem.outerHeight(!0));
}), this.element.children().not(this.panels).each(function() {
Expand Down
4 changes: 2 additions & 2 deletions ecmascript/minifier/tests/projects/output/mootools-1.4.5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2721,13 +2721,13 @@ Elements.prototype = {
}), listener = listeners[uid] || {
forms: [],
fns: []
}, forms = listener.forms;
}, forms = listener.forms, fns = listener.fns;
if (-1 == forms.indexOf(form)) {
forms.push(form);
var _fn = function(event) {
bubbleUp(self, match, fn, event, target);
};
form.addEvent(type, _fn), listener.fns.push(_fn), listeners[uid] = listener, self.store(_key + type + "listeners", listeners);
form.addEvent(type, _fn), fns.push(_fn), listeners[uid] = listener, self.store(_key + type + "listeners", listeners);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ecmascript/minifier/tests/projects/output/yui-3.12.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ var YUI = function() {
this.Array || this._attach([
"yui-base"
]);
var len, loader, handleBoot, i, Y = this, G_ENV = YUI.Env, mods = G_ENV.mods, Env = Y.Env, used = Env._used, aliases = G_ENV.aliases, queue = G_ENV._loaderQueue, firstArg = args[0], config = Y.config, boot = config.bootstrap, missing = [], r = [], ret = !0, fetchCSS = config.fetchCSS, process = function(names, skip) {
var len, loader, handleBoot, i, Y = this, G_ENV = YUI.Env, mods = G_ENV.mods, Env = Y.Env, used = Env._used, aliases = G_ENV.aliases, queue = G_ENV._loaderQueue, firstArg = args[0], YArray = Y.Array, config = Y.config, boot = config.bootstrap, missing = [], r = [], ret = !0, fetchCSS = config.fetchCSS, process = function(names, skip) {
var name, len, m, req, use, i = 0, a = [];
if (names.length) {
if (aliases) {
Expand All @@ -258,7 +258,7 @@ var YUI = function() {
}
return (mods.loader || mods["loader-base"]) && !Y.Loader && Y._attach([
"loader" + (mods.loader ? "" : "-base")
]), boot && Y.Loader && args.length && ((loader = getLoader(Y)).require(args), loader.ignoreRegistered = !0, loader._boot = !0, loader.calculate(null, fetchCSS ? null : "js"), args = loader.sorted, loader._boot = !1), process(args), (len = missing.length) && (len = (missing = Y.Array.dedupe(missing)).length), boot && len && Y.Loader ? (Y._loading = !0, (loader = getLoader(Y)).onEnd = handleLoader, loader.context = Y, loader.data = args, loader.ignoreRegistered = !1, loader.require(missing), loader.insert(null, fetchCSS ? null : "js")) : boot && len && Y.Get && !Env.bootstrapped ? (Y._loading = !0, handleBoot = function() {
]), boot && Y.Loader && args.length && ((loader = getLoader(Y)).require(args), loader.ignoreRegistered = !0, loader._boot = !0, loader.calculate(null, fetchCSS ? null : "js"), args = loader.sorted, loader._boot = !1), process(args), (len = missing.length) && (len = (missing = YArray.dedupe(missing)).length), boot && len && Y.Loader ? (Y._loading = !0, (loader = getLoader(Y)).onEnd = handleLoader, loader.context = Y, loader.data = args, loader.ignoreRegistered = !1, loader.require(missing), loader.insert(null, fetchCSS ? null : "js")) : boot && len && Y.Get && !Env.bootstrapped ? (Y._loading = !0, handleBoot = function() {
Y._loading = !1, queue.running = !1, Env.bootstrapped = !0, G_ENV._bootstrapping = !1, Y._attach([
"loader"
]) && Y._use(args, callback);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
try {
for(var _iterator = v[Symbol.iterator](); !(_iteratorNormalCompletion = _iterator.next().done); _iteratorNormalCompletion = !0);
for(var _step, _iterator = v[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0)var v = _step.value;
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
var map = new Map([
[
"",
!0
]
]), _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
try {
for(var _step, _iterator = new Map([
[
"",
!0
]
])[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0);
for(var _step, _iterator = map[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0);
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ function _slicedToArray(arr, i) {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})();
}
var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
var map = new Map([
[
"",
!0
]
]), _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
try {
for(var _step, _iterator = new Map([
[
"",
!0
]
])[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0)var _value = _slicedToArray(_step.value, 2);
for(var _step, _iterator = map[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0)var _value = _slicedToArray(_step.value, 2);
_value[0], _value[1];
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ function _slicedToArray(arr, i) {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})();
}
var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
var map = new Map([
[
"",
!0
],
[
"",
0
]
]), _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
try {
for(var _step, _iterator = new Map([
[
"",
!0
],
[
"",
0
]
])[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0)var _value = _slicedToArray(_step.value, 2);
for(var _step, _iterator = map[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0)var _value = _slicedToArray(_step.value, 2);
_value[0], _value[1];
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
Expand Down
Loading

0 comments on commit 06ca25f

Please sign in to comment.