Skip to content

Commit

Permalink
fix(es): Fix codegen & minifier (#2006)
Browse files Browse the repository at this point in the history
swc_ecma_codegen:
 - Emit a semicolon before `!`.
 - Emit a semicolon before `[`. (#2007)

swc_ecma_minifier:
 - Disable buggy passes.
 - `iife`: Don't inline async functions. (#2007)
  • Loading branch information
kdy1 authored Aug 4, 2021
1 parent 68608db commit 48bc26d
Show file tree
Hide file tree
Showing 39 changed files with 5,773 additions and 5,578 deletions.
6 changes: 3 additions & 3 deletions 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/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_codegen"
repository = "https://github.com/swc-project/swc.git"
version = "0.66.0"
version = "0.66.1"

[dependencies]
bitflags = "1"
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/codegen/src/text_writer/semicolon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<W: WriteJs> WriteJs for OmitTrailingSemi<W> {
self.commit_pending_semi()?;
}

"/" | "{" | "(" => {
"[" | "!" | "/" | "{" | "(" => {
self.commit_pending_semi()?;
}

Expand Down
2 changes: 2 additions & 0 deletions ecmascript/codegen/tests/fixture/semi/pending/004/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo;
!foo
2 changes: 2 additions & 0 deletions ecmascript/codegen/tests/fixture/semi/pending/004/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo;
!foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo;!foo
2 changes: 2 additions & 0 deletions ecmascript/codegen/tests/fixture/semi/pending/005/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo()
!unary
2 changes: 2 additions & 0 deletions ecmascript/codegen/tests/fixture/semi/pending/005/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo();
!unary;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo();!unary
7 changes: 7 additions & 0 deletions ecmascript/codegen/tests/fixture/semi/pending/006/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
const a = {};

for (let b in a)
a[b] = a[b].trim();

["foo",].forEach(() => { })
8 changes: 8 additions & 0 deletions ecmascript/codegen/tests/fixture/semi/pending/006/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
const a = {
};
for(let b in a)a[b] = a[b].trim();
[
"foo",
].forEach(()=>{
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";const a={};for(let b in a)a[b]=a[b].trim();["foo",].forEach(()=>{})
2 changes: 1 addition & 1 deletion ecmascript/minifier/src/compress/optimize/conditionals.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::Optimizer;
use crate::compress::optimize::bools::negate_cost;
use crate::compress::optimize::Ctx;
use crate::compress::optimize::DISABLE_BUGGY_PASSES;
use crate::debug::dump;
use crate::util::make_bool;
use crate::util::SpanExt;
use crate::DISABLE_BUGGY_PASSES;
use std::mem::swap;
use swc_common::EqIgnoreSpan;
use swc_common::Spanned;
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/minifier/src/compress/optimize/evaluate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Optimizer;
use super::DISABLE_BUGGY_PASSES;
use crate::compress::optimize::is_pure_undefined_or_null;
use crate::DISABLE_BUGGY_PASSES;
use std::f64;
use std::num::FpCategory;
use swc_atoms::js_word;
Expand Down
4 changes: 4 additions & 0 deletions ecmascript/minifier/src/compress/optimize/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::Optimizer;
use crate::{
compress::optimize::util::is_directive,
util::{sort::is_sorted_by, MoudleItemExt},
DISABLE_BUGGY_PASSES,
};
use std::cmp::Ordering;
use swc_ecma_ast::*;
Expand All @@ -28,6 +29,9 @@ impl Optimizer<'_> {
if !self.options.join_vars {
return;
}
if DISABLE_BUGGY_PASSES {
return;
}

// Sort by position

Expand Down
15 changes: 15 additions & 0 deletions ecmascript/minifier/src/compress/optimize/iife.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,16 @@ impl Optimizer<'_> {

match callee {
Expr::Arrow(f) => {
if f.is_async {
log::trace!("iife: [x] Cannot inline async fn");
return;
}

if f.is_generator {
log::trace!("iife: [x] Cannot inline generator");
return;
}

if self.ctx.in_top_level() && !self.ctx.in_call_arg && self.options.negate_iife {
match &f.body {
BlockStmtOrExpr::BlockStmt(body) => {
Expand Down Expand Up @@ -431,6 +441,11 @@ impl Optimizer<'_> {
}
}

if f.function.is_async {
log::trace!("iife: [x] Cannot inline async fn");
return;
}

if f.function.is_generator {
log::trace!("iife: [x] Cannot inline generator");
return;
Expand Down
2 changes: 0 additions & 2 deletions ecmascript/minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ mod unsafes;
mod unused;
mod util;

const DISABLE_BUGGY_PASSES: bool = true;

#[derive(Debug, Default)]
pub(super) struct OptimizerState {
vars_for_inlining: FxHashMap<Id, Box<Expr>>,
Expand Down
2 changes: 2 additions & 0 deletions ecmascript/minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mod pass;
pub mod timing;
mod util;

const DISABLE_BUGGY_PASSES: bool = true;

#[inline]
pub fn optimize(
mut m: Module,
Expand Down
6 changes: 5 additions & 1 deletion ecmascript/minifier/src/pass/postcompress.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::option::CompressOptions;
use crate::{option::CompressOptions, DISABLE_BUGGY_PASSES};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::MapWithMut;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
Expand All @@ -16,6 +16,10 @@ impl PostcompressOptimizer<'_> {
if !self.options.bools {
return;
}
// This is buggy
if DISABLE_BUGGY_PASSES {
return;
}

// Note: `||` is not handled because of precedence.
match e {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { m } from "../index.f66dda46.js";
const value$1 = "it works", value = "it works";
function AliasOutside() {
return m`<div><p>Inside: ${value}</p><p>Outside: ${value$1}</p></div>`;
}
const value$1 = "it works", value = "it works";
export default AliasOutside;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { m } from "../index.f66dda46.js";
const jpg = "/assets/img.2dae108d.jpg";
function Files() {
return m`<div style="padding: 2rem;"><h1>Files</h1><p> jpg: ${jpg}<br/><img src=${jpg} alt="" height="320"/></p></div>`;
}
const jpg = "/assets/img.2dae108d.jpg";
export default Files;
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { s as style, m } from "../index.f66dda46.js";
function Environment() {
return m`<table><thead><tr><th>Name ${foo}</th><th>Value</th></tr></thead><tbody>${Object.keys(process.env).sort().map((key)=>{
return m`<tr key=${key}><td>${key}</td><td>${String(process.env[key])}</td></tr>`;
})}</tbody></table>`;
}
const process = {
browser: !0,
env: {
Expand All @@ -14,4 +9,9 @@ const process = {
NODE_ENV: "production"
}
}, foo = 42;
function Environment() {
return m`<table><thead><tr><th>Name ${foo}</th><th>Value</th></tr></thead><tbody>${Object.keys(process.env).sort().map((key)=>{
return m`<tr key=${key}><td>${key}</td><td>${String(process.env[key])}</td></tr>`;
})}</tbody></table>`;
}
export { Environment };
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { s as style, y, m } from "../index.f66dda46.js";
const styles = {
about: "about_migxty"
};
function About({ query , title , }) {
return y(()=>(console.log("Mounted About: ", title), ()=>{
console.log("Unmounting About: ", title);
})
, []), m`<section class=${styles.about}><h1>${title || "About"}</h1><p>My name is Jason.</p><pre>${JSON.stringify(query)}</pre></section>`;
}
const styles = {
about: "about_migxty"
};
export default About;
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { a as l, y, m } from "../index.f66dda46.js";
const json = {
foo: 42,
bar: "bar"
};
function JSONView() {
const [fetched, setFetched, ] = l(null);
return y(()=>{
Expand All @@ -7,8 +11,4 @@ function JSONView() {
);
}, []), m`<div><p>import: ${JSON.stringify(json)}</p><p>fetch: ${JSON.stringify(fetched)}</p></div>`;
}
const json = {
foo: 42,
bar: "bar"
};
export { JSONView };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const obj = {}
for (let key in obj) {
obj[key] = obj[key].trim();
}

let arr = ["foo",]
arr.forEach(() => { })
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const obj = {
};
for(let key in obj)obj[key] = obj[key].trim();
let arr = [
"foo",
];
arr.forEach(()=>{
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function func() {
(async () => {
await this.foo();
this.bar();
})()
}

func()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function func() {
(async ()=>{
await this.foo(), this.bar();
})();
}
func();
Loading

0 comments on commit 48bc26d

Please sign in to comment.