feat(es/codegen): Optimize number and bigint literal (#5223)

This commit is contained in:
Austaras 2022-07-18 11:27:03 +08:00 committed by GitHub
parent 6218363012
commit 3f0856db2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 7 deletions

View File

@ -1 +1 @@
import a from"@swc/helpers/src/_instanceof.mjs";import{Color as b,rgbConvert as c,Rgb as d}from"./color.js";var e=Math.PI/180,f=180/Math.PI,g=-1.78277*.29227-.1347134789;export default function h(b,e,h,i){return 1===arguments.length?function(b){if(a(b,Cubehelix))return new Cubehelix(b.h,b.s,b.l,b.opacity);a(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)}})) import a from"@swc/helpers/src/_instanceof.mjs";import{Color as b,rgbConvert as c,Rgb as d}from"./color.js";var e=Math.PI/180,f=180/Math.PI,g=-1.78277*.29227-.1347134789;export default function h(b,e,h,i){return 1===arguments.length?function(b){if(a(b,Cubehelix))return new Cubehelix(b.h,b.s,b.l,b.opacity);a(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)- -.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*(-.14861*f+1.78277*g)),255*(b+c*(-.29227*f+ -.90649*g)),255*(b+c*(1.97294*f)),this.opacity)}}))

View File

@ -661,7 +661,14 @@ where
self.emit_leading_comments_of_span(v.span, false)?; self.emit_leading_comments_of_span(v.span, false)?;
if self.cfg.minify { if self.cfg.minify {
self.wr.write_lit(v.span, &v.value.to_string())?; let value = if v.value >= 10000000000000000_i64.into() {
format!("0x{}", v.value.to_str_radix(16))
} else if v.value <= (-10000000000000000_i64).into() {
format!("-0x{}", (-v.value.clone()).to_str_radix(16))
} else {
v.value.to_string()
};
self.wr.write_lit(v.span, &value)?;
self.wr.write_lit(v.span, "n")?; self.wr.write_lit(v.span, "n")?;
} else { } else {
match &v.raw { match &v.raw {
@ -3623,7 +3630,7 @@ fn minify_number(num: f64) -> String {
let mut original = printed.clone(); let mut original = printed.clone();
if num.fract() == 0.0 && num.is_sign_positive() && num <= (0x7fffffffffffffff_i64 as f64) { if num.fract() == 0.0 && (i64::MIN as f64) <= num && num <= (i64::MAX as f64) {
let hex = format!("{:#x}", num as i64); let hex = format!("{:#x}", num as i64);
if hex.len() < printed.len() { if hex.len() < printed.len() {
@ -3635,6 +3642,10 @@ fn minify_number(num: f64) -> String {
original.replace_range(0..1, ""); original.replace_range(0..1, "");
} }
if original.starts_with("-0.") {
original.replace_range(1..2, "");
}
if original.starts_with(".000") { if original.starts_with(".000") {
let mut cnt = 3; let mut cnt = 3;

View File

@ -12,3 +12,4 @@ const n11 = 0b101n;
const n12 = 0b101n; const n12 = 0b101n;
const n13 = 0o13n; const n13 = 0o13n;
const n14 = 0b101_101n; const n14 = 0b101_101n;
const n15 = 10000000000000000n;

View File

@ -12,3 +12,4 @@ const n11 = 0b101n;
const n12 = 0b101n; const n12 = 0b101n;
const n13 = 0o13n; const n13 = 0o13n;
const n14 = 0b101_101n; const n14 = 0b101_101n;
const n15 = 10000000000000000n;

View File

@ -1 +1 @@
const n1=9007199254740991n;const n2=-9007199254740991n;const n3=+9007199254740991n;const n4=100000000n;const n5=0n;const n6=+0n;const n7=-0n;const n8=16n;const n9=16n;const n10=16n;const n11=5n;const n12=5n;const n13=11n;const n14=45n const n1=9007199254740991n;const n2=-9007199254740991n;const n3=+9007199254740991n;const n4=100000000n;const n5=0n;const n6=+0n;const n7=-0n;const n8=16n;const n9=16n;const n10=16n;const n11=5n;const n12=5n;const n13=11n;const n14=45n;const n15=0x2386f26fc10000n

File diff suppressed because one or more lines are too long