Skip to content

Commit 5088c19

Browse files
authored
refactor: use strings.Builder (#4290)
Signed-off-by: zhoujiaweii <zjwustc@outlook.com>
1 parent 755da31 commit 5088c19

12 files changed

Lines changed: 60 additions & 52 deletions

File tree

internal/ast/ast.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ast
66

77
import (
88
"sort"
9+
"strings"
910

1011
"github.com/evanw/esbuild/internal/helpers"
1112
"github.com/evanw/esbuild/internal/logger"
@@ -811,15 +812,16 @@ func (minifier NameMinifier) NumberToMinifiedName(i int) string {
811812
n_tail := len(minifier.tail)
812813

813814
j := i % n_head
814-
name := minifier.head[j : j+1]
815+
var name strings.Builder
816+
name.WriteString(minifier.head[j : j+1])
815817
i = i / n_head
816818

817819
for i > 0 {
818820
i--
819821
j := i % n_tail
820-
name += minifier.tail[j : j+1]
822+
name.WriteString(minifier.tail[j : j+1])
821823
i = i / n_tail
822824
}
823825

824-
return name
826+
return name.String()
825827
}

internal/bundler_tests/bundler_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func es(version int) compat.JSFeature {
3434

3535
func assertLog(t *testing.T, msgs []logger.Msg, expected string) {
3636
t.Helper()
37-
text := ""
37+
var text strings.Builder
3838
for _, msg := range msgs {
39-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
39+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
4040
}
41-
test.AssertEqualWithDiff(t, text, expected)
41+
test.AssertEqualWithDiff(t, text.String(), expected)
4242
}
4343

4444
func hasErrors(msgs []logger.Msg) bool {
@@ -283,14 +283,14 @@ func (s *suite) updateSnapshots() {
283283
keys = append(keys, key)
284284
}
285285
sort.Strings(keys)
286-
contents := ""
286+
var contents strings.Builder
287287
for i, key := range keys {
288288
if i > 0 {
289-
contents += snapshotSplitter
289+
contents.WriteString(snapshotSplitter)
290290
}
291-
contents += fmt.Sprintf("%s\n%s", key, s.generatedSnapshots[key])
291+
contents.WriteString(fmt.Sprintf("%s\n%s", key, s.generatedSnapshots[key]))
292292
}
293-
if err := ioutil.WriteFile(s.path, []byte(contents), 0644); err != nil {
293+
if err := ioutil.WriteFile(s.path, []byte(contents.String()), 0644); err != nil {
294294
panic(err)
295295
}
296296
}

internal/css_lexer/css_lexer_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package css_lexer
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/evanw/esbuild/internal/logger"
@@ -20,11 +21,11 @@ func lexToken(contents string) (T, string) {
2021
func lexerError(contents string) string {
2122
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
2223
Tokenize(log, test.SourceForTest(contents), Options{})
23-
text := ""
24+
var text strings.Builder
2425
for _, msg := range log.Done() {
25-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
26+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
2627
}
27-
return text
28+
return text.String()
2829
}
2930

3031
func TestTokens(t *testing.T) {

internal/css_parser/css_parser_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package css_parser
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/evanw/esbuild/internal/ast"
@@ -19,11 +20,11 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st
1920
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
2021
tree := Parse(log, test.SourceForTest(contents), OptionsFromConfig(loader, &options))
2122
msgs := log.Done()
22-
text := ""
23+
var text strings.Builder
2324
for _, msg := range msgs {
24-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
25+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
2526
}
26-
test.AssertEqualWithDiff(t, text, expectedLog)
27+
test.AssertEqualWithDiff(t, text.String(), expectedLog)
2728
symbols := ast.NewSymbolMap(1)
2829
symbols.SymbolsForSource[0] = tree.Symbols
2930
result := css_printer.Print(tree, symbols, css_printer.Options{

internal/css_printer/css_printer_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package css_printer
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/evanw/esbuild/internal/ast"
@@ -19,13 +20,13 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st
1920
MinifyWhitespace: options.MinifyWhitespace,
2021
}))
2122
msgs := log.Done()
22-
text := ""
23+
var text strings.Builder
2324
for _, msg := range msgs {
2425
if msg.Kind == logger.Error {
25-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
26+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
2627
}
2728
}
28-
test.AssertEqualWithDiff(t, text, "")
29+
test.AssertEqualWithDiff(t, text.String(), "")
2930
symbols := ast.NewSymbolMap(1)
3031
symbols.SymbolsForSource[0] = tree.Symbols
3132
result := Print(tree, symbols, options)

internal/js_lexer/js_lexer_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ func expectLexerError(t *testing.T, contents string, expected string) {
5353
NewLexer(log, test.SourceForTest(contents), config.TSOptions{})
5454
}()
5555
msgs := log.Done()
56-
text := ""
56+
var text strings.Builder
5757
for _, msg := range msgs {
58-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
58+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
5959
}
60-
test.AssertEqual(t, text, expected)
60+
test.AssertEqual(t, text.String(), expected)
6161
})
6262
}
6363

@@ -437,11 +437,11 @@ func expectLexerErrorString(t *testing.T, contents string, expected string) {
437437
lexer.StringLiteral()
438438
}()
439439
msgs := log.Done()
440-
text := ""
440+
var text strings.Builder
441441
for _, msg := range msgs {
442-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
442+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
443443
}
444-
test.AssertEqual(t, text, expected)
444+
test.AssertEqual(t, text.String(), expected)
445445
})
446446
}
447447

internal/js_parser/js_parser.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5156,7 +5156,8 @@ func (p *parser) parseJSXTag() (logger.Range, string, js_ast.Expr) {
51565156
tag := js_ast.Expr{Loc: loc, Data: &js_ast.EIdentifier{Ref: p.storeNameInRef(tagName)}}
51575157

51585158
// Parse a member expression chain
5159-
chain := tagName.String
5159+
var chain strings.Builder
5160+
chain.WriteString(tagName.String)
51605161
for p.lexer.Token == js_lexer.TDot {
51615162
p.lexer.NextInsideJSXElement()
51625163
memberRange := p.lexer.Range()
@@ -5171,12 +5172,12 @@ func (p *parser) parseJSXTag() (logger.Range, string, js_ast.Expr) {
51715172
panic(js_lexer.LexerPanic{})
51725173
}
51735174

5174-
chain += "." + member.String
5175+
chain.WriteString("." + member.String)
51755176
tag = js_ast.Expr{Loc: loc, Data: p.dotOrMangledPropParse(tag, member, memberRange.Loc, js_ast.OptionalChainNone, wasOriginallyDot)}
51765177
tagRange.Len = memberRange.Loc.Start + memberRange.Len - tagRange.Loc.Start
51775178
}
51785179

5179-
return tagRange, chain, tag
5180+
return tagRange, chain.String(), tag
51805181
}
51815182

51825183
func (p *parser) parseJSXElement(loc logger.Loc) js_ast.Expr {

internal/js_parser/js_parser_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func expectParseErrorCommon(t *testing.T, contents string, expected string, opti
2323
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
2424
Parse(log, test.SourceForTest(contents), OptionsFromConfig(&options))
2525
msgs := log.Done()
26-
text := ""
26+
var text strings.Builder
2727
for _, msg := range msgs {
28-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
28+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
2929
}
30-
test.AssertEqualWithDiff(t, text, expected)
30+
test.AssertEqualWithDiff(t, text.String(), expected)
3131
})
3232
}
3333

@@ -67,13 +67,13 @@ func expectPrintedCommon(t *testing.T, contents string, expected string, options
6767
options.OmitRuntimeForTests = true
6868
tree, ok := Parse(log, test.SourceForTest(contents), OptionsFromConfig(&options))
6969
msgs := log.Done()
70-
text := ""
70+
var text strings.Builder
7171
for _, msg := range msgs {
7272
if msg.Kind != logger.Warning {
73-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
73+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
7474
}
7575
}
76-
test.AssertEqualWithDiff(t, text, "")
76+
test.AssertEqualWithDiff(t, text.String(), "")
7777
if !ok {
7878
t.Fatal("Parse error")
7979
}

internal/js_parser/json_parser_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package js_parser
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/evanw/esbuild/internal/ast"
@@ -18,11 +19,11 @@ func expectParseErrorJSON(t *testing.T, contents string, expected string) {
1819
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
1920
ParseJSON(log, test.SourceForTest(contents), JSONOptions{})
2021
msgs := log.Done()
21-
text := ""
22+
var text strings.Builder
2223
for _, msg := range msgs {
23-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
24+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
2425
}
25-
test.AssertEqualWithDiff(t, text, expected)
26+
test.AssertEqualWithDiff(t, text.String(), expected)
2627
})
2728
}
2829

@@ -41,11 +42,11 @@ func expectPrintedJSONWithWarning(t *testing.T, contents string, warning string,
4142
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
4243
expr, ok := ParseJSON(log, test.SourceForTest(contents), JSONOptions{})
4344
msgs := log.Done()
44-
text := ""
45+
var text strings.Builder
4546
for _, msg := range msgs {
46-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
47+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
4748
}
48-
test.AssertEqualWithDiff(t, text, warning)
49+
test.AssertEqualWithDiff(t, text.String(), warning)
4950
if !ok {
5051
t.Fatal("Parse error")
5152
}

internal/js_printer/js_printer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st
2020
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
2121
tree, ok := js_parser.Parse(log, test.SourceForTest(contents), js_parser.OptionsFromConfig(&options))
2222
msgs := log.Done()
23-
text := ""
23+
var text strings.Builder
2424
for _, msg := range msgs {
2525
if msg.Kind != logger.Error {
2626
continue
2727
}
28-
text += msg.String(logger.OutputOptions{}, logger.TerminalInfo{})
28+
text.WriteString(msg.String(logger.OutputOptions{}, logger.TerminalInfo{}))
2929
}
30-
test.AssertEqualWithDiff(t, text, "")
30+
test.AssertEqualWithDiff(t, text.String(), "")
3131
if !ok {
3232
t.Fatal("Parse error")
3333
}

0 commit comments

Comments
 (0)