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): Support more statements in seqential inliner #6248

Merged
merged 5 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions crates/swc/tests/tsc-references/for-of24.2.minified.js
@@ -1,3 +1,2 @@
//// [for-of24.ts]
var x;
for (var v of x);
for (var v of void 0);
3 changes: 1 addition & 2 deletions crates/swc/tests/tsc-references/for-of29.2.minified.js
@@ -1,3 +1,2 @@
//// [for-of29.ts]
var iterableWithOptionalIterator;
for (var v of iterableWithOptionalIterator);
for (var v of void 0);
5 changes: 2 additions & 3 deletions crates/swc/tests/tsc-references/for-of37.2.minified.js
@@ -1,8 +1,7 @@
//// [for-of37.ts]
var map = new Map([
for (var v of new Map([
[
"",
!0
]
]);
for (var v of map);
]));
5 changes: 2 additions & 3 deletions crates/swc/tests/tsc-references/for-of38.2.minified.js
@@ -1,8 +1,7 @@
//// [for-of38.ts]
var map = new Map([
for (var [k, v] of new Map([
[
"",
!0
]
]);
for (var [k, v] of map);
]));
5 changes: 2 additions & 3 deletions crates/swc/tests/tsc-references/for-of39.2.minified.js
@@ -1,5 +1,5 @@
//// [for-of39.ts]
var map = new Map([
for (var [k, v] of new Map([
[
"",
!0
Expand All @@ -8,5 +8,4 @@ var map = new Map([
"",
0
]
]);
for (var [k, v] of map);
]));
5 changes: 2 additions & 3 deletions crates/swc/tests/tsc-references/for-of40.2.minified.js
@@ -1,8 +1,7 @@
//// [for-of40.ts]
var map = new Map([
for (var [k = "", v = !1] of new Map([
[
"",
!0
]
]);
for (var [k = "", v = !1] of map);
]));
5 changes: 2 additions & 3 deletions crates/swc/tests/tsc-references/for-of44.2.minified.js
@@ -1,5 +1,5 @@
//// [for-of44.ts]
var array = [
for (var [num, strBoolSym] of [
[
0,
""
Expand All @@ -12,5 +12,4 @@ var array = [
1,
Symbol()
]
];
for (var [num, strBoolSym] of array);
]);
6 changes: 3 additions & 3 deletions crates/swc/tests/tsc-references/for-of45.2.minified.js
@@ -1,8 +1,8 @@
//// [for-of45.ts]
var k, v, map = new Map([
var k, v;
for ([k = "", v = !1] of new Map([
[
"",
!0
]
]);
for ([k = "", v = !1] of map);
]));
6 changes: 3 additions & 3 deletions crates/swc/tests/tsc-references/for-of46.2.minified.js
@@ -1,8 +1,8 @@
//// [for-of46.ts]
var k, v, map = new Map([
var k, v;
for ([k = !1, v = ""] of new Map([
[
"",
!0
]
]);
for ([k = !1, v = ""] of map);
]));
6 changes: 3 additions & 3 deletions crates/swc/tests/tsc-references/for-of49.2.minified.js
@@ -1,8 +1,8 @@
//// [for-of49.ts]
var k, v, map = new Map([
var k, v;
for ([k, ...[v]] of new Map([
[
"",
!0
]
]);
for ([k, ...[v]] of map);
]));
5 changes: 2 additions & 3 deletions crates/swc/tests/tsc-references/for-of50.2.minified.js
@@ -1,8 +1,7 @@
//// [for-of50.ts]
var map = new Map([
for (const [k, v] of new Map([
[
"",
!0
]
]);
for (const [k, v] of map);
]));
3 changes: 1 addition & 2 deletions crates/swc/tests/tsc-references/for-of57.2.minified.js
@@ -1,3 +1,2 @@
//// [for-of57.ts]
var iter;
for (let num of iter);
for (let num of void 0);
Expand Up @@ -7,8 +7,7 @@ import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
return logger.log(funcDescription + " completed in " + (end - start) + " msec"), result;
}, stringToLiteral = function(value, length) {
var result = "", addChar = function(index) {
var ch = value.charCodeAt(index);
switch(ch){
switch(value.charCodeAt(index)){
case 0x09:
result += "\\t";
break;
Expand Down
@@ -1,12 +1,6 @@
//// [stringLiteralsWithSwitchStatements03.ts]
var x, z;
switch(x){
var z;
switch(void 0){
case randBool() ? "foo" : "baz":
case randBool(), "bar":
case "bar":
case "baz":
case "foo":
case "bar":
case z || "baz":
case "baz":
}
32 changes: 31 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Expand Up @@ -581,9 +581,31 @@ where
Stmt::Return(ReturnStmt { arg: Some(arg), .. }) => {
vec![Mergable::Expr(arg)]
}

Stmt::If(s) if options.sequences() => {
vec![Mergable::Expr(&mut s.test)]
}

Stmt::Switch(s) if options.sequences() => {
vec![Mergable::Expr(&mut s.discriminant)]
}

Stmt::For(s) if options.sequences() => {
if let Some(VarDeclOrExpr::Expr(e)) = &mut s.init {
vec![Mergable::Expr(e)]
} else {
return None;
}
}

Stmt::ForOf(s) if options.sequences() => {
vec![Mergable::Expr(&mut s.right)]
}

Stmt::ForIn(s) if options.sequences() => {
vec![Mergable::Expr(&mut s.right)]
}

Stmt::Throw(s) if options.sequences() => {
vec![Mergable::Expr(&mut s.arg)]
}
Expand Down Expand Up @@ -624,7 +646,15 @@ where
for stmt in stmts.iter_mut() {
let is_end = matches!(
stmt.as_stmt(),
Some(Stmt::If(..) | Stmt::Throw(..) | Stmt::Return(..))
Some(
Stmt::If(..)
| Stmt::Throw(..)
| Stmt::Return(..)
| Stmt::Switch(..)
| Stmt::For(..)
| Stmt::ForIn(..)
| Stmt::ForOf(..)
) | None
);
let can_skip = match stmt.as_stmt() {
Some(Stmt::Decl(Decl::Fn(..))) => true,
Expand Down
6 changes: 2 additions & 4 deletions crates/swc_ecma_minifier/tests/benches-full/echarts.js
Expand Up @@ -2428,8 +2428,7 @@
var frameIdx, isAdditive = null != this._additiveTrack, valueKey = isAdditive ? 'additiveValue' : 'value', keyframes = this.keyframes, kfsNum = this.keyframes.length, propName = this.propName, arrDim = this.arrDim, isValueColor = this.isValueColor;
if (percent < 0) frameIdx = 0;
else if (percent < this._lastFramePercent) {
var start = Math.min(this._lastFrame + 1, kfsNum - 1);
for(frameIdx = start; frameIdx >= 0 && !(keyframes[frameIdx].percent <= percent); frameIdx--);
for(frameIdx = Math.min(this._lastFrame + 1, kfsNum - 1); frameIdx >= 0 && !(keyframes[frameIdx].percent <= percent); frameIdx--);
frameIdx = Math.min(frameIdx, kfsNum - 2);
} else {
for(frameIdx = this._lastFrame; frameIdx < kfsNum && !(keyframes[frameIdx].percent > percent); frameIdx++);
Expand Down Expand Up @@ -28477,8 +28476,7 @@
if (offsets) {
var lastFrame = this._lastFrame;
if (t < this._lastFramePercent) {
var start = Math.min(lastFrame + 1, len - 1);
for(frame = start; frame >= 0 && !(offsets[frame] <= t); frame--);
for(frame = Math.min(lastFrame + 1, len - 1); frame >= 0 && !(offsets[frame] <= t); frame--);
frame = Math.min(frame, len - 2);
} else {
for(frame = lastFrame; frame < len && !(offsets[frame] > t); frame++);
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_ecma_minifier/tests/benches-full/terser.js
Expand Up @@ -417,8 +417,7 @@
return ret;
}(function(ch, i) {
if (is_big_int) return !1;
var code = ch.charCodeAt(0);
switch(code){
switch(ch.charCodeAt(0)){
case 95:
return numeric_separator = !0;
case 98:
Expand Down
27 changes: 12 additions & 15 deletions crates/swc_ecma_minifier/tests/benches-full/three.js
Expand Up @@ -12078,21 +12078,18 @@
return;
}
var path = new ShapePath();
if (glyph.o) for(var outline = glyph._cachedOutline || (glyph._cachedOutline = glyph.o.split(' ')), i = 0, l = outline.length; i < l;){
var action = outline[i++];
switch(action){
case 'm':
x = outline[i++] * scale + offsetX, y = outline[i++] * scale + offsetY, path.moveTo(x, y);
break;
case 'l':
x = outline[i++] * scale + offsetX, y = outline[i++] * scale + offsetY, path.lineTo(x, y);
break;
case 'q':
cpx = outline[i++] * scale + offsetX, cpy = outline[i++] * scale + offsetY, cpx1 = outline[i++] * scale + offsetX, cpy1 = outline[i++] * scale + offsetY, path.quadraticCurveTo(cpx1, cpy1, cpx, cpy);
break;
case 'b':
cpx = outline[i++] * scale + offsetX, cpy = outline[i++] * scale + offsetY, cpx1 = outline[i++] * scale + offsetX, cpy1 = outline[i++] * scale + offsetY, cpx2 = outline[i++] * scale + offsetX, cpy2 = outline[i++] * scale + offsetY, path.bezierCurveTo(cpx1, cpy1, cpx2, cpy2, cpx, cpy);
}
if (glyph.o) for(var outline = glyph._cachedOutline || (glyph._cachedOutline = glyph.o.split(' ')), i = 0, l = outline.length; i < l;)switch(outline[i++]){
case 'm':
x = outline[i++] * scale + offsetX, y = outline[i++] * scale + offsetY, path.moveTo(x, y);
break;
case 'l':
x = outline[i++] * scale + offsetX, y = outline[i++] * scale + offsetY, path.lineTo(x, y);
break;
case 'q':
cpx = outline[i++] * scale + offsetX, cpy = outline[i++] * scale + offsetY, cpx1 = outline[i++] * scale + offsetX, cpy1 = outline[i++] * scale + offsetY, path.quadraticCurveTo(cpx1, cpy1, cpx, cpy);
break;
case 'b':
cpx = outline[i++] * scale + offsetX, cpy = outline[i++] * scale + offsetY, cpx1 = outline[i++] * scale + offsetX, cpy1 = outline[i++] * scale + offsetY, cpx2 = outline[i++] * scale + offsetX, cpy2 = outline[i++] * scale + offsetY, path.bezierCurveTo(cpx1, cpy1, cpx2, cpy2, cpx, cpy);
}
return {
offsetX: glyph.ha * scale,
Expand Down
Expand Up @@ -10558,8 +10558,7 @@
return programMapTable;
}
}, parsePesType = function(packet, programMapTable) {
var type = programMapTable[parsePid(packet)];
switch(type){
switch(programMapTable[parsePid(packet)]){
case streamTypes.H264_STREAM_TYPE:
return "video";
case streamTypes.ADTS_STREAM_TYPE:
Expand Down Expand Up @@ -10715,8 +10714,7 @@
}
}, inspectAac_ = function(bytes) {
for(var packet, endLoop = !1, audioCount = 0, sampleRate = null, timestamp = null, frameSize = 0, byteIndex = 0; bytes.length - byteIndex >= 3;){
var type = probe.aac.parseType(bytes, byteIndex);
switch(type){
switch(probe.aac.parseType(bytes, byteIndex)){
case "timed-metadata":
if (bytes.length - byteIndex < 10 || (frameSize = probe.aac.parseId3TagSize(bytes, byteIndex)) > bytes.length) {
endLoop = !0;
Expand Down Expand Up @@ -10757,15 +10755,12 @@
pid: null,
table: null
}, result = {};
for(var pid in parsePsi_(bytes, pmt), pmt.table)if (pmt.table.hasOwnProperty(pid)) {
var type = pmt.table[pid];
switch(type){
case streamTypes.H264_STREAM_TYPE:
result.video = [], parseVideoPes_(bytes, pmt, result), 0 === result.video.length && delete result.video;
break;
case streamTypes.ADTS_STREAM_TYPE:
result.audio = [], parseAudioPes_(bytes, pmt, result), 0 === result.audio.length && delete result.audio;
}
for(var pid in parsePsi_(bytes, pmt), pmt.table)if (pmt.table.hasOwnProperty(pid)) switch(pmt.table[pid]){
case streamTypes.H264_STREAM_TYPE:
result.video = [], parseVideoPes_(bytes, pmt, result), 0 === result.video.length && delete result.video;
break;
case streamTypes.ADTS_STREAM_TYPE:
result.audio = [], parseAudioPes_(bytes, pmt, result), 0 === result.audio.length && delete result.audio;
}
return result;
}, tsInspector = {
Expand Down
Expand Up @@ -8241,8 +8241,8 @@
return t.length < 2 ? "0" + t : t;
}
l.get = function(e) {
var t, r, n = e.substring(0, 3).toLowerCase();
switch(n){
var t, r;
switch(e.substring(0, 3).toLowerCase()){
case "hsl":
t = l.get.hsl(e), r = "hsl";
break;
Expand Down Expand Up @@ -9406,10 +9406,10 @@
stickToLeft: 3,
stickToRight: 4
}; t = e.charAt(r++);){
var o = "." === t, a = !o && /\d/.test(t), u = o ? r - 1 == 0 ? i.stickToLeft : i.stickToRight : a ? i.level : i.alphabet;
switch(u){
var o = "." === t, a = !o && /\d/.test(t);
switch(o ? r - 1 == 0 ? i.stickToLeft : i.stickToRight : a ? i.level : i.alphabet){
case i.alphabet:
l || n.levels.push(0), n.text += t;
u || n.levels.push(0), n.text += t;
break;
case i.level:
n.levels.push(parseInt(t));
Expand All @@ -9420,7 +9420,7 @@
case i.stickToRight:
n.stickToRight = !0;
}
var l = a;
var u = a;
}
return n;
}
Expand Down