Skip to content

Commit 757d7d9

Browse files
committed
quoted comma separated string array helper
1 parent ebcb111 commit 757d7d9

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

internal/helpers/strings.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package helpers
22

3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
38
func StringArraysEqual(a []string, b []string) bool {
49
if len(a) != len(b) {
510
return false
@@ -11,3 +16,14 @@ func StringArraysEqual(a []string, b []string) bool {
1116
}
1217
return true
1318
}
19+
20+
func StringArrayToQuotedCommaSeparatedString(a []string) string {
21+
sb := strings.Builder{}
22+
for i, str := range a {
23+
if i > 0 {
24+
sb.WriteString(", ")
25+
}
26+
sb.WriteString(fmt.Sprintf("%q", str))
27+
}
28+
return sb.String()
29+
}

internal/resolver/resolver.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,7 @@ func prettyPrintPath(prefix string, key string, value logger.Path) string {
152152
}
153153

154154
func prettyPrintStringArray(prefix string, key string, value []string) string {
155-
quoted := make([]string, len(value))
156-
for i, v := range value {
157-
quoted[i] = fmt.Sprintf("%q", v)
158-
}
159-
return fmt.Sprintf("%s %q: [%s],", prefix, key, strings.Join(quoted, ","))
155+
return fmt.Sprintf("%s %q: [%s],", prefix, key, helpers.StringArrayToQuotedCommaSeparatedString(value))
160156
}
161157

162158
func prettyPrintTSTarget(prefix string, key string, value *config.TSTarget) string {
@@ -1600,13 +1596,9 @@ func (r resolverQuery) loadAsMainField(dirInfo *dirInfo, path string, extensionO
16001596
fmt.Sprintf("The %q field here was ignored. Main fields must be configured explicitly when using the \"neutral\" platform.",
16011597
field)))
16021598
} else {
1603-
quoted := make([]string, len(mainFieldKeys))
1604-
for i, key := range mainFieldKeys {
1605-
quoted[i] = fmt.Sprintf("%q", key)
1606-
}
16071599
r.debugMeta.notes = append(r.debugMeta.notes, tracker.MsgData(keyRange,
16081600
fmt.Sprintf("The %q field here was ignored because the list of main fields to use is currently set to [%s].",
1609-
field, strings.Join(quoted, ", "))))
1601+
field, helpers.StringArrayToQuotedCommaSeparatedString(mainFieldKeys))))
16101602
}
16111603
break
16121604
}
@@ -2045,14 +2037,6 @@ func (r resolverQuery) finalizeImportsExportsResult(
20452037
fmt.Sprintf("Importing the directory %q is not supported:", resolvedPath))}
20462038

20472039
case pjStatusUndefinedNoConditionsMatch:
2048-
prettyPrintConditions := func(conditions []string) string {
2049-
quoted := make([]string, len(conditions))
2050-
for i, condition := range conditions {
2051-
quoted[i] = fmt.Sprintf("%q", condition)
2052-
}
2053-
return strings.Join(quoted, ", ")
2054-
}
2055-
20562040
keys := make([]string, 0, len(conditions))
20572041
for key := range conditions {
20582042
keys = append(keys, key)
@@ -2071,8 +2055,8 @@ func (r resolverQuery) finalizeImportsExportsResult(
20712055

20722056
tracker.MsgData(debug.token,
20732057
fmt.Sprintf("None of the conditions provided (%s) match any of the currently active conditions (%s):",
2074-
prettyPrintConditions(unmatchedConditions),
2075-
prettyPrintConditions(keys),
2058+
helpers.StringArrayToQuotedCommaSeparatedString(unmatchedConditions),
2059+
helpers.StringArrayToQuotedCommaSeparatedString(keys),
20762060
)),
20772061
}
20782062

pkg/api/api_impl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ func validateFeatures(log logger.Log, target Target, engines []Engine) (config.T
364364
case 3:
365365
text = fmt.Sprintf("%s%d.%d.%d", engine.String(), version[0], version[1], version[2])
366366
}
367-
targets = append(targets, fmt.Sprintf("%q", text))
367+
targets = append(targets, text)
368368
}
369369

370370
sort.Strings(targets)
371-
targetEnv := strings.Join(targets, ", ")
371+
targetEnv := helpers.StringArrayToQuotedCommaSeparatedString(targets)
372372

373373
return targetFromAPI, compat.UnsupportedJSFeatures(constraints), compat.UnsupportedCSSFeatures(constraints), targetEnv
374374
}

0 commit comments

Comments
 (0)