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

feat(es/minifier): Make name mangler understand block scoping #6670

Merged
merged 20 commits into from
Jan 6, 2023
6 changes: 3 additions & 3 deletions crates/swc/tests/fixture/issues-2xxx/2352/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
async function test(listings) {
for (const listing of listings){}
for (const listing1 of activeAndOpen){
const listing11 = await foo(listing1);
console.log(listing11);
for (const listing of activeAndOpen){
const listing1 = await foo(listing);
console.log(listing1);
}
}
44 changes: 22 additions & 22 deletions crates/swc/tests/fixture/next.js/server/render/1/output/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export async function renderToHTML(req, res, pathname, query, renderOpts) {
props[SERVER_PROPS_ID] = true;
}
if (getServerSideProps && !isFallback) {
let data1;
let data;
let canAccessRes = true;
let resOrProxy = res;
if (process.env.NODE_ENV !== "production") {
Expand All @@ -412,7 +412,7 @@ export async function renderToHTML(req, res, pathname, query, renderOpts) {
});
}
try {
data1 = await getServerSideProps({
data = await getServerSideProps({
req: req,
res: resOrProxy,
query,
Expand All @@ -437,45 +437,45 @@ export async function renderToHTML(req, res, pathname, query, renderOpts) {
}
throw serverSidePropsError;
}
if (data1 == null) {
if (data == null) {
throw new Error(GSSP_NO_RETURNED_VALUE);
}
const invalidKeys1 = Object.keys(data1).filter((key)=>key !== "props" && key !== "redirect" && key !== "notFound");
if (data1.unstable_notFound) {
const invalidKeys = Object.keys(data).filter((key)=>key !== "props" && key !== "redirect" && key !== "notFound");
if (data.unstable_notFound) {
throw new Error(`unstable_notFound has been renamed to notFound, please update the field to continue. Page: ${pathname}`);
}
if (data1.unstable_redirect) {
if (data.unstable_redirect) {
throw new Error(`unstable_redirect has been renamed to redirect, please update the field to continue. Page: ${pathname}`);
}
if (invalidKeys1.length) {
throw new Error(invalidKeysMsg("getServerSideProps", invalidKeys1));
if (invalidKeys.length) {
throw new Error(invalidKeysMsg("getServerSideProps", invalidKeys));
}
if ("notFound" in data1 && data1.notFound) {
if ("notFound" in data && data.notFound) {
if (pathname === "/404") {
throw new Error(`The /404 page can not return notFound in "getStaticProps", please remove it to continue!`);
}
renderOpts.isNotFound = true;
return null;
}
if ("redirect" in data1 && typeof data1.redirect === "object") {
checkRedirectValues(data1.redirect, req, "getServerSideProps");
data1.props = {
__N_REDIRECT: data1.redirect.destination,
__N_REDIRECT_STATUS: getRedirectStatus(data1.redirect)
if ("redirect" in data && typeof data.redirect === "object") {
checkRedirectValues(data.redirect, req, "getServerSideProps");
data.props = {
__N_REDIRECT: data.redirect.destination,
__N_REDIRECT_STATUS: getRedirectStatus(data.redirect)
};
if (typeof data1.redirect.basePath !== "undefined") {
data1.props.__N_REDIRECT_BASE_PATH = data1.redirect.basePath;
if (typeof data.redirect.basePath !== "undefined") {
data.props.__N_REDIRECT_BASE_PATH = data.redirect.basePath;
}
renderOpts.isRedirect = true;
}
if (data1.props instanceof Promise) {
data1.props = await data1.props;
if (data.props instanceof Promise) {
data.props = await data.props;
}
if ((dev || isBuildTimeSSG) && !isSerializableProps(pathname, "getServerSideProps", data1.props)) {
if ((dev || isBuildTimeSSG) && !isSerializableProps(pathname, "getServerSideProps", data.props)) {
// this fn should throw an error instead of ever returning `false`
throw new Error("invariant: getServerSideProps did not return valid props. Please report this.");
}
props.pageProps = Object.assign({}, props.pageProps, data1.props);
props.pageProps = Object.assign({}, props.pageProps, data.props);
renderOpts.pageData = props;
}
if (!isSSG && // we only show this warning for legacy pages
Expand Down Expand Up @@ -545,12 +545,12 @@ export async function renderToHTML(req, res, pathname, query, renderOpts) {
throw new Error(`'router' and 'Component' can not be returned in getInitialProps from _app.js https://nextjs.org/docs/messages/cant-override-next-props`);
}
const { App: EnhancedApp , Component: EnhancedComponent } = enhanceComponents(options, App, Component);
const html1 = ReactDOMServer.renderToString(/*#__PURE__*/ React.createElement(AppContainer, null, /*#__PURE__*/ React.createElement(EnhancedApp, _extends({
const html = ReactDOMServer.renderToString(/*#__PURE__*/ React.createElement(AppContainer, null, /*#__PURE__*/ React.createElement(EnhancedApp, _extends({
Component: EnhancedComponent,
router: router
}, props))));
return {
html: html1,
html,
head
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const a = 2;
class C {
static{
const a1 = 1;
a1;
const a = 1;
a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ function f() {
const b1 = 22;
class C1 {
static{
var a11 = 111;
var a1 = 111;
var a2 = 111;
const b11 = 222;
const b1 = 222;
const b2 = 222;
}
}
}
class C2 {
static{
var a11 = 111;
var a21 = 111;
const b11 = 222;
const b21 = 222;
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const a = 1;
const b = 2;
class C {
static{
const a1 = 11;
a1;
const a = 11;
a;
b;
}
static{
const a2 = 11;
a2;
const a = 11;
a;
b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const a = 1;
const b = 2;
class C {
static{
const a1 = 11;
a1;
const a = 11;
a;
b;
}
static{
const a2 = 11;
a2;
const a = 11;
a;
b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ try {} catch (e) {
e.toUpperCase(); // e string
}
}
try {} catch (e1) {
var isString1 = typeof e1 === "string";
e1 = 1;
try {} catch (e) {
var isString1 = typeof e === "string";
e = 1;
if (isString1) {
e1.toUpperCase(); // e any/unknown
e.toUpperCase(); // e any/unknown
}
if (typeof e1 === "string") {
e1.toUpperCase(); // e string
if (typeof e === "string") {
e.toUpperCase(); // e string
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,123 +9,123 @@
]] = [];
const bb = b;
}{
let a1 = 1;
let b1;
[{ [a1]: b1 } = [
let a = 1;
let b;
[{ [a]: b } = [
9,
a1 = 0
a = 0
]] = [];
const bb1 = b1;
const bb = b;
}{
let a2 = 0;
let b2;
[{ [a2 = 1]: b2 } = [
let a = 0;
let b;
[{ [a = 1]: b } = [
9,
a2
a
]] = [
[
9,
8
]
];
const bb2 = b2;
const bb = b;
}{
let a3 = 1;
let b3;
[{ [a3]: b3 } = [
a3 = 0,
let a = 1;
let b;
[{ [a]: b } = [
a = 0,
9
]] = [
[
8,
9
]
];
const bb3 = b3;
const bb = b;
}// same as above but on left of a binary expression
{
let a4 = 0;
let b4;
[{ [a4 = 1]: b4 } = [
let a = 0;
let b;
[{ [a = 1]: b } = [
9,
a4
a
]] = [], f();
const bb4 = b4;
const bb = b;
}{
let a5 = 1;
let b5;
[{ [a5]: b5 } = [
let a = 1;
let b;
[{ [a]: b } = [
9,
a5 = 0
a = 0
]] = [], f();
const bb5 = b5;
const bb = b;
}{
let a6 = 0;
let b6;
[{ [a6 = 1]: b6 } = [
let a = 0;
let b;
[{ [a = 1]: b } = [
9,
a6
a
]] = [
[
9,
8
]
], f();
const bb6 = b6;
const bb = b;
}{
let a7 = 1;
let b7;
[{ [a7]: b7 } = [
a7 = 0,
let a = 1;
let b;
[{ [a]: b } = [
a = 0,
9
]] = [
[
8,
9
]
], f();
const bb7 = b7;
const bb = b;
}// same as above but on right of a binary expression
{
let a8 = 0;
let b8;
f(), [{ [a8 = 1]: b8 } = [
let a = 0;
let b;
f(), [{ [a = 1]: b } = [
9,
a8
a
]] = [];
const bb8 = b8;
const bb = b;
}{
let a9 = 1;
let b9;
f(), [{ [a9]: b9 } = [
let a = 1;
let b;
f(), [{ [a]: b } = [
9,
a9 = 0
a = 0
]] = [];
const bb9 = b9;
const bb = b;
}{
let a10 = 0;
let b10;
f(), [{ [a10 = 1]: b10 } = [
let a = 0;
let b;
f(), [{ [a = 1]: b } = [
9,
a10
a
]] = [
[
9,
8
]
];
const bb10 = b10;
const bb = b;
}{
let a11 = 1;
let b11;
f(), [{ [a11]: b11 } = [
a11 = 0,
let a = 1;
let b;
f(), [{ [a]: b } = [
a = 0,
9
]] = [
[
8,
9
]
];
const bb11 = b11;
const bb = b;
}