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

fix: remove unnecessary space after number #3026

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(`(?i)[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