Skip to content

Commit

Permalink
fix: remove unnecessary space after number
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Mar 29, 2023
1 parent b2b8978 commit 9f46e3b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -348,6 +349,7 @@ type printer struct {
exportDefaultStart int
arrowExprStart int
forOfInitStart int
prevNumStart int

prevOpEnd int
prevNumEnd int
Expand Down Expand Up @@ -514,6 +516,9 @@ func (p *printer) printNumber(value float64, level js_ast.L) {
} else {
if !math.Signbit(value) {
p.printSpaceBeforeIdentifier()

p.prevNumStart = len(p.js)

p.printNonNegativeFloat(absValue)

// Remember the end of the latest number
Expand All @@ -528,6 +533,9 @@ func (p *printer) printNumber(value float64, level js_ast.L) {
p.print(")")
} else {
p.printSpaceBeforeOperator(js_ast.UnOpNeg)

p.prevNumStart = len(p.js)

p.print("-")
p.printNonNegativeFloat(absValue)

Expand Down Expand Up @@ -1415,6 +1423,7 @@ func (p *printer) printUndefined(loc logger.Loc, level js_ast.L) {
p.printSpaceBeforeIdentifier()
p.addSourceMapping(loc)
p.print("void 0")
p.prevNumStart = len(p.js) - 1
p.prevNumEnd = len(p.js)
}
}
Expand Down Expand Up @@ -2297,7 +2306,11 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
if p.canPrintIdentifier(e.Name) {
if e.OptionalChain != js_ast.OptionalChainStart && p.prevNumEnd == len(p.js) {
// "1.toString" is a syntax error, so print "1 .toString" instead
p.print(" ")
num := p.js[p.prevNumStart:p.prevNumEnd]
ok, _ := regexp.Match("[xa-f.]", num)
if !ok {
p.print(" ")
}
}
if e.OptionalChain == js_ast.OptionalChainStart {
p.print("?.")
Expand Down
5 changes: 5 additions & 0 deletions internal/js_printer/js_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ func TestNumber(t *testing.T) {
expectPrintedMinify(t, "x = 0xFFFF_FFFF_FFFF_F000", "x=0xfffffffffffff000;")
expectPrintedMinify(t, "x = 0xFFFF_FFFF_FFFF_F800", "x=1844674407370955e4;")
expectPrintedMinify(t, "x = 0xFFFF_FFFF_FFFF_FFFF", "x=18446744073709552e3;")

expectPrintedMinify(t, "(2).toFixed(0)", "2 .toFixed(0);")
expectPrintedMinify(t, "(0.2).toFixed(0)", ".2.toFixed(0);")
expectPrintedMinify(t, "(0.00000002).toFixed(0)", "2e-8.toFixed(0);")
expectPrintedMinify(t, "(1000000000000000128).toFixed(0)", "0xde0b6b3a7640080.toFixed(0);")
}

func TestArray(t *testing.T) {
Expand Down

0 comments on commit 9f46e3b

Please sign in to comment.