mirror of
https://github.com/swc-project/swc.git
synced 2024-11-27 13:38:33 +03:00
feat(es/codegen): Improve compression of numbers (#3425)
This commit is contained in:
parent
73efd72cbe
commit
e8b64a9871
@ -1 +1 @@
|
||||
class a{#a;constructor(b){this.#a=b} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1000)}run(){this.#b()}}export{a as default}
|
||||
class a{#a;constructor(b){this.#a=b} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1e3)}run(){this.#b()}}export{a as default}
|
||||
|
@ -1 +1 @@
|
||||
import*as a from"@swc/helpers";import{Color as b,rgbConvert as c,Rgb as d}from"./color.js";var e=Math.PI/180,f=180/Math.PI,g=-0.5210501878999999-0.1347134789;export default function h(b,e,h,i){return 1===arguments.length?(function(b){if(a._instanceof(b,Cubehelix))return new Cubehelix(b.h,b.s,b.l,b.opacity);a._instanceof(b,d)||(b=c(b));var e=b.r/255,h=b.g/255,i=b.b/255,j=(g*i+ -1.7884503806*e-3.5172982438*h)/(g+ -1.7884503806-3.5172982438),k=i-j,l=-((1.97294*(h-j)- -0.29227*k)/0.90649),m=Math.sqrt(l*l+k*k)/(1.97294*j*(1-j)),n=m?Math.atan2(l,k)*f-120:NaN;return new Cubehelix(n<0?n+360:n,m,j,b.opacity)})(b):new Cubehelix(b,e,h,null==i?1:i)};export function Cubehelix(a,b,c,d){this.h=+a,this.s=+b,this.l=+c,this.opacity=+d}!function(a,b,c){a.prototype=b.prototype=c,c.constructor=a}(Cubehelix,h,function(a,b){var c=Object.create(a.prototype);for(var d in b)c[d]=b[d];return c}(b,{brighter:function(a){return a=null==a?1.4285714285714286:Math.pow(1.4285714285714286,a),new Cubehelix(this.h,this.s,this.l*a,this.opacity)},darker:function(a){return a=null==a?0.7:Math.pow(0.7,a),new Cubehelix(this.h,this.s,this.l*a,this.opacity)},rgb:function(){var a=isNaN(this.h)?0:(this.h+120)*e,b=+this.l,c=isNaN(this.s)?0:this.s*b*(1-b),f=Math.cos(a),g=Math.sin(a);return new d(255*(b+c*(-0.14861*f+1.78277*g)),255*(b+c*(-0.29227*f+ -0.90649*g)),255*(b+c*(1.97294*f)),this.opacity)}}))
|
||||
import*as a from"@swc/helpers";import{Color as b,rgbConvert as c,Rgb as d}from"./color.js";var e=Math.PI/180,f=180/Math.PI,g=-0.5210501878999999-.1347134789;export default function h(b,e,h,i){return 1===arguments.length?(function(b){if(a._instanceof(b,Cubehelix))return new Cubehelix(b.h,b.s,b.l,b.opacity);a._instanceof(b,d)||(b=c(b));var e=b.r/255,h=b.g/255,i=b.b/255,j=(g*i+ -1.7884503806*e-3.5172982438*h)/(g+ -1.7884503806-3.5172982438),k=i-j,l=-((1.97294*(h-j)- -0.29227*k)/.90649),m=Math.sqrt(l*l+k*k)/(1.97294*j*(1-j)),n=m?Math.atan2(l,k)*f-120:NaN;return new Cubehelix(n<0?n+360:n,m,j,b.opacity)})(b):new Cubehelix(b,e,h,null==i?1:i)};export function Cubehelix(a,b,c,d){this.h=+a,this.s=+b,this.l=+c,this.opacity=+d}!function(a,b,c){a.prototype=b.prototype=c,c.constructor=a}(Cubehelix,h,function(a,b){var c=Object.create(a.prototype);for(var d in b)c[d]=b[d];return c}(b,{brighter:function(a){return a=null==a?1.4285714285714286:Math.pow(1.4285714285714286,a),new Cubehelix(this.h,this.s,this.l*a,this.opacity)},darker:function(a){return a=null==a?.7:Math.pow(.7,a),new Cubehelix(this.h,this.s,this.l*a,this.opacity)},rgb:function(){var a=isNaN(this.h)?0:(this.h+120)*e,b=+this.l,c=isNaN(this.s)?0:this.s*b*(1-b),f=Math.cos(a),g=Math.sin(a);return new d(255*(b+c*(-0.14861*f+1.78277*g)),255*(b+c*(-0.29227*f+ -0.90649*g)),255*(b+c*(1.97294*f)),this.opacity)}}))
|
||||
|
@ -530,19 +530,65 @@ where
|
||||
self.wr.write_str_lit(num.span, "-")?;
|
||||
}
|
||||
self.wr.write_str_lit(num.span, "Infinity")?;
|
||||
} else if num.value.is_sign_negative() && num.value == 0.0 {
|
||||
self.wr.write_str_lit(num.span, "-0")?;
|
||||
} else {
|
||||
let mut s = num.value.to_string();
|
||||
if self.cfg.minify && !s.contains('.') && !s.contains('e') && s.ends_with("0000") {
|
||||
let cnt = s.as_bytes().iter().rev().filter(|&&v| v == b'0').count() - 1;
|
||||
let mut printed = num.value.to_string();
|
||||
|
||||
s.truncate(s.len() - cnt);
|
||||
s.push('e');
|
||||
s.push_str(&cnt.to_string());
|
||||
if self.cfg.minify {
|
||||
let mut original = printed.clone();
|
||||
|
||||
if num.value.fract() == 0.0 {
|
||||
let mut hex = String::new();
|
||||
|
||||
hex.push_str(&format!("{:#x}", num.value as i64));
|
||||
|
||||
if hex.len() < printed.len() {
|
||||
printed = hex;
|
||||
}
|
||||
}
|
||||
|
||||
if original.starts_with("0.") {
|
||||
original.replace_range(0..1, "");
|
||||
}
|
||||
|
||||
if original.starts_with(".000") {
|
||||
let mut cnt = 3;
|
||||
|
||||
for &v in original.as_bytes().iter().skip(4) {
|
||||
if v == b'0' {
|
||||
cnt += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
original.replace_range(0..cnt + 1, "");
|
||||
|
||||
let remain_len = original.len();
|
||||
|
||||
original.push_str("e-");
|
||||
original.push_str(&(remain_len + cnt).to_string());
|
||||
} else if original.ends_with("000") {
|
||||
let mut cnt = 3;
|
||||
|
||||
for &v in original.as_bytes().iter().rev().skip(3) {
|
||||
if v == b'0' {
|
||||
cnt += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
original.truncate(original.len() - cnt);
|
||||
original.push('e');
|
||||
original.push_str(&cnt.to_string());
|
||||
}
|
||||
|
||||
if original.len() < printed.len() {
|
||||
printed = original;
|
||||
}
|
||||
}
|
||||
|
||||
self.wr.write_str_lit(num.span, &s)?;
|
||||
self.wr.write_str_lit(num.span, &printed)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1 +1 @@
|
||||
var millisecondsPerWeek=7*24*60*60*1000;console.log(millisecondsPerWeek)
|
||||
var millisecondsPerWeek=7*24*60*60*1e3;console.log(millisecondsPerWeek)
|
||||
|
63
crates/swc_ecma_codegen/tests/fixture/number/input.js
Normal file
63
crates/swc_ecma_codegen/tests/fixture/number/input.js
Normal file
@ -0,0 +1,63 @@
|
||||
const exp = 1000;
|
||||
const Exp = 1000000000000;
|
||||
const negativeExp = 0.00000001;
|
||||
const huge = 1000000000001;
|
||||
const big = 100000000001;
|
||||
const fractional = 100.2300200;
|
||||
const numeric_separators = 1_000_000_000_000;
|
||||
const one = 1_000;
|
||||
const two = 1_000_000;
|
||||
const three = -1_000_000;
|
||||
const bin = 0b0101_0101;
|
||||
const oct = 0o0123_4567;
|
||||
const hex = 0xDEAD_BEEF;
|
||||
const fractional2 = 1_000.000_100;
|
||||
const identifier = _1000;
|
||||
const negate_identifier = -_1000;
|
||||
const foo = 0.1;
|
||||
const foo1 = +0.1;
|
||||
const foo2 = -0.1;
|
||||
const foo3 = 1050;
|
||||
const foo4 = 100500;
|
||||
const foo5 = 10005000;
|
||||
const foo6 = 1000050000;
|
||||
const foo7 = 0.1;
|
||||
const foo8 = 0.01;
|
||||
const foo9 = 0.001;
|
||||
const foo10 = 0.0001;
|
||||
const foo11 = 0.00001;
|
||||
const foo12 = 0.000001;
|
||||
const foo13 = 0.000024;
|
||||
const foo14 = -0.000024;
|
||||
const foo15 = 1e1;
|
||||
const foo16 = 1e2;
|
||||
const foo17 = 1e3;
|
||||
const foo18 = 1e4;
|
||||
const foo19 = 1e5;
|
||||
const foo20 = 1e-1;
|
||||
const foo21 = 1e-2;
|
||||
const foo22 = 1e-3;
|
||||
const foo23 = 1e-4;
|
||||
const foo24 = 1e-5;
|
||||
const foo25 = -1e-1;
|
||||
const foo26 = -1e-2;
|
||||
const foo27 = -1e-3;
|
||||
const foo28 = -1e-4;
|
||||
const foo29 = -1e-5;
|
||||
const foo30 = 100e-5;
|
||||
const foo31 = 321e-5;
|
||||
const foo32 = 321e-6;
|
||||
const foo33 = 1E-1;
|
||||
const foo34 = 1E-5;
|
||||
const foo35 = 000000;
|
||||
const foo36 = -000000;
|
||||
const foo37 = +000000;
|
||||
const foo38 = 0.00001543;
|
||||
const foo39 = 0.00001543000;
|
||||
const foo40 = (0.00001543000);
|
||||
const foo41 = { 1000050000: "foo" };
|
||||
const foo42 = 1000000003242;
|
||||
const foo43 = -1000000003242;
|
||||
const foo43 = 0.0;
|
||||
const foo44 = -0.0;
|
||||
const foo45 = +0.0;
|
65
crates/swc_ecma_codegen/tests/fixture/number/output.js
Normal file
65
crates/swc_ecma_codegen/tests/fixture/number/output.js
Normal file
@ -0,0 +1,65 @@
|
||||
const exp = 1000;
|
||||
const Exp = 1000000000000;
|
||||
const negativeExp = 0.00000001;
|
||||
const huge = 1000000000001;
|
||||
const big = 100000000001;
|
||||
const fractional = 100.23002;
|
||||
const numeric_separators = 1000000000000;
|
||||
const one = 1000;
|
||||
const two = 1000000;
|
||||
const three = -1000000;
|
||||
const bin = 85;
|
||||
const oct = 342391;
|
||||
const hex = 3735928559;
|
||||
const fractional2 = 1000.0001;
|
||||
const identifier = _1000;
|
||||
const negate_identifier = -_1000;
|
||||
const foo = 0.1;
|
||||
const foo1 = +0.1;
|
||||
const foo2 = -0.1;
|
||||
const foo3 = 1050;
|
||||
const foo4 = 100500;
|
||||
const foo5 = 10005000;
|
||||
const foo6 = 1000050000;
|
||||
const foo7 = 0.1;
|
||||
const foo8 = 0.01;
|
||||
const foo9 = 0.001;
|
||||
const foo10 = 0.0001;
|
||||
const foo11 = 0.00001;
|
||||
const foo12 = 0.000001;
|
||||
const foo13 = 0.000024;
|
||||
const foo14 = -0.000024;
|
||||
const foo15 = 10;
|
||||
const foo16 = 100;
|
||||
const foo17 = 1000;
|
||||
const foo18 = 10000;
|
||||
const foo19 = 100000;
|
||||
const foo20 = 0.1;
|
||||
const foo21 = 0.01;
|
||||
const foo22 = 0.001;
|
||||
const foo23 = 0.0001;
|
||||
const foo24 = 0.00001;
|
||||
const foo25 = -0.1;
|
||||
const foo26 = -0.01;
|
||||
const foo27 = -0.001;
|
||||
const foo28 = -0.0001;
|
||||
const foo29 = -0.00001;
|
||||
const foo30 = 0.001;
|
||||
const foo31 = 0.00321;
|
||||
const foo32 = 0.000321;
|
||||
const foo33 = 0.1;
|
||||
const foo34 = 0.00001;
|
||||
const foo35 = 0;
|
||||
const foo36 = -0;
|
||||
const foo37 = +0;
|
||||
const foo38 = 0.00001543;
|
||||
const foo39 = 0.00001543;
|
||||
const foo40 = (0.00001543);
|
||||
const foo41 = {
|
||||
1000050000: "foo"
|
||||
};
|
||||
const foo42 = 1000000003242;
|
||||
const foo43 = -1000000003242;
|
||||
const foo43 = 0;
|
||||
const foo44 = -0;
|
||||
const foo45 = +0;
|
1
crates/swc_ecma_codegen/tests/fixture/number/output.min.js
vendored
Normal file
1
crates/swc_ecma_codegen/tests/fixture/number/output.min.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
const exp=1e3;const Exp=1e12;const negativeExp=1e-8;const huge=0xe8d4a51001;const big=100000000001;const fractional=100.23002;const numeric_separators=1e12;const one=1e3;const two=1e6;const three=-1e6;const bin=85;const oct=342391;const hex=3735928559;const fractional2=1000.0001;const identifier=_1000;const negate_identifier=-_1000;const foo=.1;const foo1=+.1;const foo2=-.1;const foo3=1050;const foo4=100500;const foo5=10005e3;const foo6=100005e4;const foo7=.1;const foo8=.01;const foo9=.001;const foo10=1e-4;const foo11=1e-5;const foo12=1e-6;const foo13=24e-6;const foo14=-24e-6;const foo15=10;const foo16=100;const foo17=1e3;const foo18=1e4;const foo19=1e5;const foo20=.1;const foo21=.01;const foo22=.001;const foo23=1e-4;const foo24=1e-5;const foo25=-.1;const foo26=-.01;const foo27=-.001;const foo28=-1e-4;const foo29=-1e-5;const foo30=.001;const foo31=.00321;const foo32=321e-6;const foo33=.1;const foo34=1e-5;const foo35=0;const foo36=-0;const foo37=+0;const foo38=1543e-8;const foo39=1543e-8;const foo40=(1543e-8);const foo41={100005e4:"foo"};const foo42=0xe8d4a51caa;const foo43=-0xe8d4a51caa;const foo43=0;const foo44=-0;const foo45=+0
|
@ -1 +1 @@
|
||||
import define,{extend}from"./define.js";import{Color,rgbConvert,Rgb,darker,brighter}from"./color.js";import{deg2rad,rad2deg}from"./math.js";var A=-0.14861,B=1.78277,C=-0.29227,D=-0.90649,E=1.97294,ED=E*D,EB=E*B,BC_DA=B*C-D*A;function cubehelixConvert(o){if(o instanceof Cubehelix)return new Cubehelix(o.h,o.s,o.l,o.opacity);o instanceof Rgb||(o=rgbConvert(o));var r=o.r/255,g=o.g/255,b=o.b/255,l=(BC_DA*b+ED*r-EB*g)/(BC_DA+ED-EB),bl=b-l,k=(E*(g-l)-C*bl)/D,s=Math.sqrt(k*k+bl*bl)/(E*l*(1-l)),h=s?Math.atan2(k,bl)*rad2deg-120:NaN;return new Cubehelix(h<0?h+360:h,s,l,o.opacity)}export default function cubehelix(h,s,l,opacity){return 1===arguments.length?cubehelixConvert(h):new Cubehelix(h,s,l,null==opacity?1:opacity)};export function Cubehelix(h,s,l,opacity){this.h=+h,this.s=+s,this.l=+l,this.opacity=+opacity}define(Cubehelix,cubehelix,extend(Color,{brighter:function(k){return k=null==k?brighter:Math.pow(brighter,k),new Cubehelix(this.h,this.s,this.l*k,this.opacity)},darker:function(k){return k=null==k?darker:Math.pow(darker,k),new Cubehelix(this.h,this.s,this.l*k,this.opacity)},rgb:function(){var h=isNaN(this.h)?0:(this.h+120)*deg2rad,l=+this.l,a=isNaN(this.s)?0:this.s*l*(1-l),cosh=Math.cos(h),sinh=Math.sin(h);return new Rgb(255*(l+a*(A*cosh+B*sinh)),255*(l+a*(C*cosh+D*sinh)),255*(l+a*(E*cosh)),this.opacity)}}))
|
||||
import define,{extend}from"./define.js";import{Color,rgbConvert,Rgb,darker,brighter}from"./color.js";import{deg2rad,rad2deg}from"./math.js";var A=-.14861,B=1.78277,C=-.29227,D=-.90649,E=1.97294,ED=E*D,EB=E*B,BC_DA=B*C-D*A;function cubehelixConvert(o){if(o instanceof Cubehelix)return new Cubehelix(o.h,o.s,o.l,o.opacity);o instanceof Rgb||(o=rgbConvert(o));var r=o.r/255,g=o.g/255,b=o.b/255,l=(BC_DA*b+ED*r-EB*g)/(BC_DA+ED-EB),bl=b-l,k=(E*(g-l)-C*bl)/D,s=Math.sqrt(k*k+bl*bl)/(E*l*(1-l)),h=s?Math.atan2(k,bl)*rad2deg-120:NaN;return new Cubehelix(h<0?h+360:h,s,l,o.opacity)}export default function cubehelix(h,s,l,opacity){return 1===arguments.length?cubehelixConvert(h):new Cubehelix(h,s,l,null==opacity?1:opacity)};export function Cubehelix(h,s,l,opacity){this.h=+h,this.s=+s,this.l=+l,this.opacity=+opacity}define(Cubehelix,cubehelix,extend(Color,{brighter:function(k){return k=null==k?brighter:Math.pow(brighter,k),new Cubehelix(this.h,this.s,this.l*k,this.opacity)},darker:function(k){return k=null==k?darker:Math.pow(darker,k),new Cubehelix(this.h,this.s,this.l*k,this.opacity)},rgb:function(){var h=isNaN(this.h)?0:(this.h+120)*deg2rad,l=+this.l,a=isNaN(this.s)?0:this.s*l*(1-l),cosh=Math.cos(h),sinh=Math.sin(h);return new Rgb(255*(l+a*(A*cosh+B*sinh)),255*(l+a*(C*cosh+D*sinh)),255*(l+a*(E*cosh)),this.opacity)}}))
|
||||
|
@ -1 +1 @@
|
||||
new a(...0.5)
|
||||
new a(....5)
|
||||
|
@ -1 +1 @@
|
||||
0.000000000149241783
|
||||
149241783e-18
|
||||
|
@ -1 +1 @@
|
||||
10e19,10e20
|
||||
1e20,1e21
|
||||
|
@ -1 +1 @@
|
||||
0.14
|
||||
.14
|
||||
|
@ -1 +1 @@
|
||||
export default function(c,e){try{if("string"==typeof c&&c.length>0)return(function(a){if((a=String(a)).length>100)throw new Error("Value exceeds the maximum length of 100 characters.");const b=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(a);if(!b)return NaN;const c=parseFloat(b[1]),d=(b[2]||"ms").toLowerCase();switch(d){case"years":case"year":case"yrs":case"yr":case"y":return 3155760e4*c;case"weeks":case"week":case"w":return 6048e5*c;case"days":case"day":case"d":return 8640e4*c;case"hours":case"hour":case"hrs":case"hr":case"h":return 360e4*c;case"minutes":case"minute":case"mins":case"min":case"m":return 60e3*c;case"seconds":case"second":case"secs":case"sec":case"s":return 1000*c;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return c;default:throw new Error(`The unit ${d} was matched, but no matching case exists.`)}})(c);if("number"==typeof c&&isFinite(c))return e?.long?b(c):a(c);throw new Error("Value is not a string or number.")}catch(f){const g=d(f)?`${f.message}. value=${JSON.stringify(c)}`:"An unknown error has occured.";throw new Error(g)}};function a(a){const b=Math.abs(a);return b>=8640e4?`${Math.round(a/8640e4)}d`:b>=360e4?`${Math.round(a/360e4)}h`:b>=60e3?`${Math.round(a/60e3)}m`:b>=1000?`${Math.round(a/1000)}s`:`${a}ms`}function b(a){const b=Math.abs(a);return b>=8640e4?c(a,b,8640e4,"day"):b>=360e4?c(a,b,360e4,"hour"):b>=60e3?c(a,b,60e3,"minute"):b>=1000?c(a,b,1000,"second"):`${a} ms`}function c(a,b,c,d){return`${Math.round(a/c)} ${d}${b>=1.5*c?"s":""}`}function d(a){return"object"==typeof a&&null!==a&&"message"in a}
|
||||
export default function(c,e){try{if("string"==typeof c&&c.length>0)return(function(a){if((a=String(a)).length>100)throw new Error("Value exceeds the maximum length of 100 characters.");const b=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(a);if(!b)return NaN;const c=parseFloat(b[1]),d=(b[2]||"ms").toLowerCase();switch(d){case"years":case"year":case"yrs":case"yr":case"y":return 315576e5*c;case"weeks":case"week":case"w":return 6048e5*c;case"days":case"day":case"d":return 864e5*c;case"hours":case"hour":case"hrs":case"hr":case"h":return 36e5*c;case"minutes":case"minute":case"mins":case"min":case"m":return 6e4*c;case"seconds":case"second":case"secs":case"sec":case"s":return 1e3*c;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return c;default:throw new Error(`The unit ${d} was matched, but no matching case exists.`)}})(c);if("number"==typeof c&&isFinite(c))return e?.long?b(c):a(c);throw new Error("Value is not a string or number.")}catch(f){const g=d(f)?`${f.message}. value=${JSON.stringify(c)}`:"An unknown error has occured.";throw new Error(g)}};function a(a){const b=Math.abs(a);return b>=864e5?`${Math.round(a/864e5)}d`:b>=36e5?`${Math.round(a/36e5)}h`:b>=6e4?`${Math.round(a/6e4)}m`:b>=1e3?`${Math.round(a/1e3)}s`:`${a}ms`}function b(a){const b=Math.abs(a);return b>=864e5?c(a,b,864e5,"day"):b>=36e5?c(a,b,36e5,"hour"):b>=6e4?c(a,b,6e4,"minute"):b>=1e3?c(a,b,1e3,"second"):`${a} ms`}function c(a,b,c,d){return`${Math.round(a/c)} ${d}${b>=1.5*c?"s":""}`}function d(a){return"object"==typeof a&&null!==a&&"message"in a}
|
||||
|
Loading…
Reference in New Issue
Block a user