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/minifier): Don't skip unresolved identifiers #6050

Merged
merged 9 commits into from
Oct 5, 2022
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
@@ -1,5 +1,9 @@
//// [classAbstractSingleLineDecl.ts]
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
var A = function A() {
"use strict";
_class_call_check(this, A);
};
abstract;
var B = function B() {
"use strict";
Expand All @@ -10,7 +14,4 @@ var C = function C() {
"use strict";
_class_call_check(this, C);
};
new function A() {
"use strict";
_class_call_check(this, A);
}, new B, new C;
new A, new B, new C;
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import { useProject as o } from "@swr/use-project";
import a from "@swr/use-team";
export default function m() {
var m = e().query.project, u = o(m).data;
return a().teamSlug, useProjectBranches(null == u ? void 0 : u.id).data, r(t, {});
a().teamSlug;
var s = null == u ? void 0 : u.id;
return useProjectBranches(s).data, r(t, {});
}
4 changes: 3 additions & 1 deletion crates/swc/tests/vercel/full/utf8-1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import r from "./on-demand-entries-client";
import { addMessageListener as n, connectHMR as c } from "./error-overlay/websocket";
var o = JSON.parse(document.getElementById("__NEXT_DATA__").textContent);
window.__NEXT_DATA__ = o;
var s = o.assetPrefix, i = o.page, _ = null, u = __webpack_hash__, d = (s = s || "") + (s.endsWith("/") ? "" : "/") + "_next/static/webpack/";
var s = o.assetPrefix, i = o.page;
s = s || "";
var _ = null, u = __webpack_hash__, d = s + (s.endsWith("/") ? "" : "/") + "_next/static/webpack/";
function p() {
return (p = e(function() {
var e, a, r, n, c;
Expand Down
11 changes: 10 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::{
alias::{collect_infects_from, AliasConfig},
compress::{
optimize::{unused::PropertyAccessOpts, util::replace_id_with_expr},
util::{is_directive, is_ident_used_by, replace_expr},
util::{
is_directive, is_global_var_with_pure_property_access, is_ident_used_by, replace_expr,
},
},
mode::Mode,
option::CompressOptions,
Expand Down Expand Up @@ -944,6 +946,13 @@ where

match e {
Expr::Ident(e) => {
if e.span.ctxt == self.expr_ctx.unresolved_ctxt
&& !is_global_var_with_pure_property_access(&e.sym)
{
log_abort!("Undeclared");
return false;
}

if let Some(a) = a {
match a {
Mergable::Var(a) => {
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_minifier/src/compress/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ pub(super) fn is_global_var_with_pure_property_access(s: &str) -> bool {
| "clearTimeout"
| "setInterval"
| "setTimeout"
| "btoa"
| "Boolean"
| "Date"
| "decodeURI"
Expand All @@ -751,7 +752,9 @@ pub(super) fn is_global_var_with_pure_property_access(s: &str) -> bool {
| "encodeURIComponent"
| "escape"
| "eval"
| "Error"
| "EvalError"
| "Function"
| "isFinite"
| "isNaN"
| "JSON"
Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/tests/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ collapse_vars/collapse_vars_repeated/input.js
collapse_vars/collapse_vars_short_circuit/input.js
collapse_vars/collapse_vars_short_circuited_conditions/input.js
collapse_vars/collapse_vars_side_effects_1/input.js
collapse_vars/collapse_vars_side_effects_2/input.js
collapse_vars/collapse_vars_switch/input.js
collapse_vars/collapse_vars_unary/input.js
collapse_vars/cond_branch_1/input.js
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/victory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8121,7 +8121,7 @@
};
},
"../../../node_modules/lodash/isPlainObject.js": function(module1, exports1, __webpack_require__) {
var baseGetTag = __webpack_require__("../../../node_modules/lodash/_baseGetTag.js"), getPrototype = __webpack_require__("../../../node_modules/lodash/_getPrototype.js"), isObjectLike = __webpack_require__("../../../node_modules/lodash/isObjectLike.js"), funcProto = Function.prototype, objectProto = Object.prototype, funcToString = funcProto.toString, hasOwnProperty = objectProto.hasOwnProperty, objectCtorString = funcToString.call(Object);
var baseGetTag = __webpack_require__("../../../node_modules/lodash/_baseGetTag.js"), getPrototype = __webpack_require__("../../../node_modules/lodash/_getPrototype.js"), isObjectLike = __webpack_require__("../../../node_modules/lodash/isObjectLike.js"), objectProto = Object.prototype, funcToString = Function.prototype.toString, hasOwnProperty = objectProto.hasOwnProperty, objectCtorString = funcToString.call(Object);
module1.exports = function(value) {
if (!isObjectLike(value) || '[object Object]' != baseGetTag(value)) return !1;
var proto = getPrototype(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var a = z()

g(a)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var a = z();
g(a);
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@
};
},
function(module1, exports1, __webpack_require__) {
var isFunction = __webpack_require__(36), isMasked = __webpack_require__(105), isObject = __webpack_require__(14), toSource = __webpack_require__(107), reIsHostCtor = /^\[object .+?Constructor\]$/, funcProto = Function.prototype, objectProto = Object.prototype, funcToString = funcProto.toString, hasOwnProperty = objectProto.hasOwnProperty, reIsNative = RegExp("^" + funcToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$");
var isFunction = __webpack_require__(36), isMasked = __webpack_require__(105), isObject = __webpack_require__(14), toSource = __webpack_require__(107), reIsHostCtor = /^\[object .+?Constructor\]$/, objectProto = Object.prototype, funcToString = Function.prototype.toString, hasOwnProperty = objectProto.hasOwnProperty, reIsNative = RegExp("^" + funcToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$");
module1.exports = function(value) {
return !(!isObject(value) || isMasked(value)) && (isFunction(value) ? reIsNative : reIsHostCtor).test(toSource(value));
};
Expand Down Expand Up @@ -2039,7 +2039,7 @@
};
},
function(module1, exports1, __webpack_require__) {
var baseGetTag = __webpack_require__(22), getPrototype = __webpack_require__(50), isObjectLike = __webpack_require__(18), funcProto = Function.prototype, objectProto = Object.prototype, funcToString = funcProto.toString, hasOwnProperty = objectProto.hasOwnProperty, objectCtorString = funcToString.call(Object);
var baseGetTag = __webpack_require__(22), getPrototype = __webpack_require__(50), isObjectLike = __webpack_require__(18), objectProto = Object.prototype, funcToString = Function.prototype.toString, hasOwnProperty = objectProto.hasOwnProperty, objectCtorString = funcToString.call(Object);
module1.exports = function(value) {
if (!isObjectLike(value) || "[object Object]" != baseGetTag(value)) return !1;
var proto = getPrototype(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import { useProject } from "@swr/use-project";
import useTeam from "@swr/use-team";
export default function MyComp() {
var projectName = useRouter().query.project, projectInfo = useProject(projectName).data;
return useTeam().teamSlug, useProjectBranches(null == projectInfo ? void 0 : projectInfo.id).data, _jsx(_Fragment, {});
useTeam().teamSlug;
var projectId = null == projectInfo ? void 0 : projectInfo.id;
return useProjectBranches(projectId).data, _jsx(_Fragment, {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -15119,7 +15119,7 @@
module.exports = baseIsMap;
},
8458: function(module, __unused_webpack_exports, __webpack_require__) {
var isFunction = __webpack_require__(3560), isMasked = __webpack_require__(5346), isObject = __webpack_require__(3218), toSource = __webpack_require__(346), reRegExpChar = /[\\^$.*+?()[\]{}|]/g, reIsHostCtor = /^\[object .+?Constructor\]$/, funcProto = Function.prototype, objectProto = Object.prototype, funcToString = funcProto.toString, hasOwnProperty = objectProto.hasOwnProperty, reIsNative = RegExp('^' + funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&').replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$');
var isFunction = __webpack_require__(3560), isMasked = __webpack_require__(5346), isObject = __webpack_require__(3218), toSource = __webpack_require__(346), reRegExpChar = /[\\^$.*+?()[\]{}|]/g, reIsHostCtor = /^\[object .+?Constructor\]$/, objectProto = Object.prototype, funcToString = Function.prototype.toString, hasOwnProperty = objectProto.hasOwnProperty, reIsNative = RegExp('^' + funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&').replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$');
function baseIsNative(value) {
return !(!isObject(value) || isMasked(value)) && (isFunction(value) ? reIsNative : reIsHostCtor).test(toSource(value));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function setCurrentlyValidatingElement$1(element) {
if (element) {
var owner = element._owner;
setExtraStackFrame(describeUnknownElementTypeFrameInDEV(element.type, element._source, owner ? owner.type : null));
var owner = element._owner, stack = describeUnknownElementTypeFrameInDEV(element.type, element._source, owner ? owner.type : null);
setExtraStackFrame(stack);
} else setExtraStackFrame(null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ function validatePropTypes(element) {
!error$1 || error$1 instanceof Error || (setCurrentlyValidatingElement(element), error("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", componentName || "React class", location, typeSpecName, typeof error$1), setCurrentlyValidatingElement(null)), error$1 instanceof Error && !(error$1.message in loggedTypeFailures) && (loggedTypeFailures[error$1.message] = !0, setCurrentlyValidatingElement(element), error("Failed %s type: %s", location, error$1.message), setCurrentlyValidatingElement(null));
}
}(propTypes, element.props, "prop", name, element);
} else void 0 === type.PropTypes || propTypesMisspellWarningShown || (propTypesMisspellWarningShown = !0, error("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", getComponentName(type) || "Unknown"));
} else if (void 0 !== type.PropTypes && !propTypesMisspellWarningShown) {
propTypesMisspellWarningShown = !0;
var _name = getComponentName(type);
error("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", _name || "Unknown");
}
"function" != typeof type.getDefaultProps || type.getDefaultProps.isReactClassApproved || error("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ function mapIntoArray(children, array, escapedPrefix, nameSoFar, callback) {
return 1;
}
var subtreeCount = 0, nextNamePrefix = "" === nameSoFar ? "." : nameSoFar + SUBSEPARATOR;
if (Array.isArray(children)) for(var i = 0; i < children.length; i++)nextName = nextNamePrefix + getElementKey(child = children[i], i), subtreeCount += mapIntoArray(child, array, escapedPrefix, nextName, callback);
if (Array.isArray(children)) for(var i = 0; i < children.length; i++)child = children[i], nextName = nextNamePrefix + getElementKey(child, i), subtreeCount += mapIntoArray(child, array, escapedPrefix, nextName, callback);
else {
var iteratorFn = getIteratorFn(children);
if ("function" == typeof iteratorFn) {
var child, nextName, step, iterableChildren = children;
iteratorFn === iterableChildren.entries && (didWarnAboutMaps || warn("Using Maps as children is not supported. Use an array of keyed ReactElements instead."), didWarnAboutMaps = !0);
for(var iterator = iteratorFn.call(iterableChildren), ii = 0; !(step = iterator.next()).done;)nextName = nextNamePrefix + getElementKey(child = step.value, ii++), subtreeCount += mapIntoArray(child, array, escapedPrefix, nextName, callback);
for(var iterator = iteratorFn.call(iterableChildren), ii = 0; !(step = iterator.next()).done;)child = step.value, nextName = nextNamePrefix + getElementKey(child, ii++), subtreeCount += mapIntoArray(child, array, escapedPrefix, nextName, callback);
} else if ("object" === type) {
var childrenString = "" + children;
throw Error("Objects are not valid as a React child (found: " + ("[object Object]" === childrenString ? "object with keys {" + Object.keys(children).join(", ") + "}" : childrenString) + "). If you meant to render a collection of children, use an array instead.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
var element = jqLite(element);
if (element.injector()) throw ngMinErr("btstrpd", "App Already Bootstrapped with this Element '{0}'", element[0] === document ? "document" : startingTag(element));
if (element.injector()) {
var tag = element[0] === document ? "document" : startingTag(element);
throw ngMinErr("btstrpd", "App Already Bootstrapped with this Element '{0}'", tag);
}
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/tests/passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ collapse_vars/collapse_vars_properties/input.js
collapse_vars/collapse_vars_regexp/input.js
collapse_vars/collapse_vars_self_reference/input.js
collapse_vars/collapse_vars_seq/input.js
collapse_vars/collapse_vars_side_effects_2/input.js
collapse_vars/collapse_vars_throw/input.js
collapse_vars/collapse_vars_try/input.js
collapse_vars/collapse_vars_unary_2/input.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
console.log(
(function n(o) {
return x(y(n(o)));
})(c)
);
console.log(function n(o) {
var r;
return x((r = o, y(n(r))));
}(c));