js-shrink 1.0.14 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/JsShrink.js +52 -8
  2. package/package.json +1 -1
package/JsShrink.js CHANGED
@@ -272,6 +272,12 @@ function findAllLiterals(ast_node, comments, toIncludePropertyKeys=true, minProp
272
272
  let hasFraction = n%1, str2
273
273
  newStr = n.toString()
274
274
  len = newStr.length
275
+ let e = 0, i = len-1
276
+ while(i && newStr[i] === "0") {--i; ++e}
277
+ if(e >= 3){
278
+ newStr = newStr.slice(0, i+1)+"e"+e
279
+ len = newStr.length
280
+ }
275
281
  str2 = n.toExponential().replace("+","")
276
282
  if (str2.length < len && Number(str2) === n) {
277
283
  len = str2.length
@@ -1580,6 +1586,31 @@ function findNextIndexInJs(str, src, comments, start=0, end=src.length, backward
1580
1586
  return -1
1581
1587
  }
1582
1588
  }
1589
+ function nextTokenInJs1(src, start=0, end = src.length-1, skipChar) {
1590
+ var i = start-1
1591
+ while (++i < end) {
1592
+ let c = src[i]
1593
+ if (c === "/") {
1594
+ if (src[i+1] === "/") {
1595
+ let i2 = src.indexOf("\n", i+2)
1596
+ if (i2 < 0) return ""
1597
+ i = i2
1598
+ continue
1599
+ }
1600
+ else if(src[i+1] === "*"){
1601
+ let i2 = src.indexOf("*/", i+2)
1602
+ if (i2 < 0) return ""
1603
+ i = i2+1
1604
+ continue
1605
+ }
1606
+ }
1607
+ if (c.trim() !== "") {
1608
+ if (skipChar && c === skipChar) continue
1609
+ return [c, i]
1610
+ }
1611
+ }
1612
+ return
1613
+ }
1583
1614
  function getExcludeRanges(src, ast) {
1584
1615
  var ranges = []
1585
1616
  var funcNodes = new Set
@@ -1643,6 +1674,16 @@ function forceArrowFunctions(ast, src, ms, comments) {
1643
1674
  if (n.parent.type == "Property" && (n.parent.kind != "init" || n.parent.method) || n.parent.type == "MethodDefinition") return
1644
1675
  if (isFunctionExpression) {
1645
1676
  if (usesPrivateFunctionName(n)) return
1677
+ if (n.parent.type === "CallExpression" && n.parent.callee === n) {
1678
+ let c = nextTokenInJs1(src, n.end)?.[0]
1679
+ if (c === "(") {
1680
+ ms.prependRight(n.start, "(")
1681
+ ms.appendLeft(n.end, ")")
1682
+ if (n.parent.parent.type === "ExpressionStatement") {
1683
+ ms.prependRight(n.start, ";")
1684
+ }
1685
+ }
1686
+ }
1646
1687
  }
1647
1688
  var hasParams = n.params.length
1648
1689
  var init_start = n.start
@@ -1723,7 +1764,12 @@ function forceArrowFunctions(ast, src, ms, comments) {
1723
1764
  }
1724
1765
  }
1725
1766
  for (const func of funcs) {
1726
- ms.move(func.start, func.end, targetIndex)
1767
+ if (func.start === targetIndex) {
1768
+ targetIndex = func.end
1769
+ }
1770
+ else {
1771
+ ms.move(func.start, func.end, targetIndex)
1772
+ }
1727
1773
  }
1728
1774
  }
1729
1775
  }
@@ -1864,7 +1910,7 @@ function inlineClassObjectProperties(src, options) {
1864
1910
  }
1865
1911
  var isVoided = node.type == "ExpressionStatement"
1866
1912
  || node.type == "SequenceExpression" && (
1867
- node.expression[node.expression.length-1] !== node || node.parent.type == "ExpressionStatement"
1913
+ node.expressions[node.expressions.length-1] !== node || node.parent.type == "ExpressionStatement"
1868
1914
  )
1869
1915
  return !isVoided
1870
1916
  }
@@ -3975,7 +4021,7 @@ function Shrink(src, options) {
3975
4021
  }
3976
4022
 
3977
4023
  let caseDelta = node._caseDelta
3978
- var diff = name - id - caseDelta
4024
+ var diff = name.length - id.length - caseDelta
3979
4025
  if(diff < 0 || diff == 0 && !_TO_REPLACE_ON_0_GAIN) return
3980
4026
 
3981
4027
  if (needsPropertyKeyBrackets) {
@@ -4026,11 +4072,9 @@ function Shrink(src, options) {
4026
4072
  // Fix: "object.[A]" => "object[A]"
4027
4073
  else if(node.type == "MemberExpression" && node.computed == false && !node.optional && stringLiterals_nodesMap.has(node.property)){
4028
4074
  // remove "."
4029
- var curSrc = source(node)
4030
- var i = curSrc.lastIndexOf(".")
4031
- if(i > 0){
4032
- var newSrc = curSrc.slice(0, i) + curSrc.slice(i+1)
4033
- update(newSrc, node)
4075
+ let [c, i] = nextTokenInJs1(src, node.object.end, node.property.start, ')')??""
4076
+ if (c === ".") {
4077
+ edit.remove(i, i+1)
4034
4078
  }
4035
4079
  }
4036
4080
  else if(_TO_SHRINK_ALL_UNDECLARED_GLOBALS && (undeclared_global_binding = undeclared_globals_nodesMap.get(node))){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-shrink",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Supplemental javascript minifier",
5
5
  "repository": "https://github.com/brandon942/js-shrink",
6
6
  "author": "brandon942",