fix(es/minifier): Fix analysis of unary expressions (#3286)

swc_ecma_minifier:
 - Mark update as usage and assignment. (Closes #3265)
This commit is contained in:
Donny/강동윤 2022-01-17 14:28:35 +09:00 committed by GitHub
parent a282146f7f
commit b55ae4b312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 6 deletions

View File

@ -448,9 +448,14 @@ where
e.visit_children_with(self);
if let Expr::Ident(i) = e {
if self.ctx.in_update_arg {
self.report_usage(i, true);
self.report_usage(i, false);
} else {
self.report_usage(i, self.ctx.in_update_arg || self.ctx.in_assign_lhs);
}
}
}
fn visit_fn_decl(&mut self, n: &FnDecl) {
self.declare_decl(&n.ident, true, None, true);

View File

@ -0,0 +1,6 @@
var n = 0;
n = 2000; // this gets removed in the output
console.log(n++);
export default n;

View File

@ -0,0 +1,3 @@
var n = 0;
n = 2000, console.log(n++);
export default n;

View File

@ -0,0 +1,26 @@
// real life example taken from https://github.com/nodeca/pako/blob/master/lib/zlib/adler32.js#L26
const adler32 = (adler, buf, len, pos) => {
let s1 = (adler & 0xffff) | 0,
s2 = ((adler >>> 16) & 0xffff) | 0,
n = 0;
while (len !== 0) {
// Set limit ~ twice less than 5552, to keep
// s2 in 31-bits, because we force signed ints.
// in other case %= will fail.
n = len > 2000 ? 2000 : len;
len -= n;
do {
s1 = (s1 + buf[pos++]) | 0;
s2 = (s2 + s1) | 0;
} while (--n);
s1 %= 65521;
s2 %= 65521;
}
return (s1 | (s2 << 16)) | 0;
};
export default adler32;

View File

@ -0,0 +1,10 @@
export default ((adler, buf, len, pos)=>{
let s1 = 65535 & adler | 0, s2 = adler >>> 16 & 65535 | 0, n = 0;
for(; 0 !== len;){
len -= n = len > 2000 ? 2000 : len;
do s2 = s2 + (s1 = s1 + buf[pos++] | 0) | 0;
while (--n)
s1 %= 65521, s2 %= 65521;
}
return s1 | s2 << 16 | 0;
});

View File

@ -606,7 +606,7 @@
});
if ("string" == typeof value && value) {
for(classes = (value || "").match(core_rnotwhite) || []; i < len; i++)if (cur = 1 === (elem = this[i]).nodeType && (elem.className ? (" " + elem.className + " ").replace(rclass, " ") : " ")) {
for(; clazz = classes[j1++];)0 > cur.indexOf(" " + clazz + " ") && (cur += clazz + " ");
for(j1 = 0; clazz = classes[j1++];)0 > cur.indexOf(" " + clazz + " ") && (cur += clazz + " ");
elem.className = jQuery.trim(cur);
}
}
@ -619,7 +619,7 @@
});
if (proceed) {
for(classes = (value || "").match(core_rnotwhite) || []; i < len; i++)if (cur = 1 === (elem = this[i]).nodeType && (elem.className ? (" " + elem.className + " ").replace(rclass, " ") : "")) {
for(; clazz = classes[j2++];)for(; cur.indexOf(" " + clazz + " ") >= 0;)cur = cur.replace(" " + clazz + " ", " ");
for(j2 = 0; clazz = classes[j2++];)for(; cur.indexOf(" " + clazz + " ") >= 0;)cur = cur.replace(" " + clazz + " ", " ");
elem.className = value ? jQuery.trim(cur) : "";
}
}
@ -887,7 +887,7 @@
event = jQuery.event.fix(event);
var i, ret, handleObj, matched, j, handlerQueue = [], args = core_slice.call(arguments), handlers = (jQuery._data(this, "events") || {})[event.type] || [], special = jQuery.event.special[event.type] || {};
if (args[0] = event, event.delegateTarget = this, !special.preDispatch || !1 !== special.preDispatch.call(this, event)) {
for(handlerQueue = jQuery.event.handlers.call(this, event, handlers); (matched = handlerQueue[i++]) && !event.isPropagationStopped();)for(event.currentTarget = matched.elem; (handleObj = matched.handlers[j++]) && !event.isImmediatePropagationStopped();)(!event.namespace_re || event.namespace_re.test(handleObj.namespace)) && (event.handleObj = handleObj, event.data = handleObj.data, undefined1 !== (ret = ((jQuery.event.special[handleObj.origType] || {}).handle || handleObj.handler).apply(matched.elem, args)) && !1 === (event.result = ret) && (event.preventDefault(), event.stopPropagation()));
for(handlerQueue = jQuery.event.handlers.call(this, event, handlers), i = 0; (matched = handlerQueue[i++]) && !event.isPropagationStopped();)for(event.currentTarget = matched.elem, j = 0; (handleObj = matched.handlers[j++]) && !event.isImmediatePropagationStopped();)(!event.namespace_re || event.namespace_re.test(handleObj.namespace)) && (event.handleObj = handleObj, event.data = handleObj.data, undefined1 !== (ret = ((jQuery.event.special[handleObj.origType] || {}).handle || handleObj.handler).apply(matched.elem, args)) && !1 === (event.result = ret) && (event.preventDefault(), event.stopPropagation()));
return special.postDispatch && special.postDispatch.call(this, event), event.result;
}
},
@ -1669,7 +1669,7 @@
var elem, j, matcher, setMatched = [], matchedCount = 0, i = "0", unmatched = seed && [], outermost = null != expandContext, contextBackup = outermostContext, elems = seed || byElement && Expr.find.TAG("*", expandContext && context.parentNode || context), dirrunsUnique = dirruns += null == contextBackup ? 1 : Math.random() || 0.1;
for(outermost && (outermostContext = context !== document && context, cachedruns = matcherCachedRuns); null != (elem = elems[i]); i++){
if (byElement && elem) {
for(; matcher = elementMatchers[j++];)if (matcher(elem, context, xml)) {
for(j = 0; matcher = elementMatchers[j++];)if (matcher(elem, context, xml)) {
results.push(elem);
break;
}
@ -1678,7 +1678,7 @@
bySet && ((elem = !matcher && elem) && matchedCount--, seed && unmatched.push(elem));
}
if (matchedCount += i, bySet && i !== matchedCount) {
for(; matcher = setMatchers[j++];)matcher(unmatched, setMatched, context, xml);
for(j = 0; matcher = setMatchers[j++];)matcher(unmatched, setMatched, context, xml);
if (seed) {
if (matchedCount > 0) for(; i--;)unmatched[i] || setMatched[i] || (setMatched[i] = pop.call(results));
setMatched = condense(setMatched);