Skip to content

Commit

Permalink
quoted comma separated string array helper
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Apr 12, 2022
1 parent ebcb111 commit 757d7d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
16 changes: 16 additions & 0 deletions internal/helpers/strings.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package helpers

import (
"fmt"
"strings"
)

func StringArraysEqual(a []string, b []string) bool {
if len(a) != len(b) {
return false
Expand All @@ -11,3 +16,14 @@ func StringArraysEqual(a []string, b []string) bool {
}
return true
}

func StringArrayToQuotedCommaSeparatedString(a []string) string {
sb := strings.Builder{}
for i, str := range a {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%q", str))
}
return sb.String()
}
24 changes: 4 additions & 20 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ func prettyPrintPath(prefix string, key string, value logger.Path) string {
}

func prettyPrintStringArray(prefix string, key string, value []string) string {
quoted := make([]string, len(value))
for i, v := range value {
quoted[i] = fmt.Sprintf("%q", v)
}
return fmt.Sprintf("%s %q: [%s],", prefix, key, strings.Join(quoted, ","))
return fmt.Sprintf("%s %q: [%s],", prefix, key, helpers.StringArrayToQuotedCommaSeparatedString(value))
}

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

case pjStatusUndefinedNoConditionsMatch:
prettyPrintConditions := func(conditions []string) string {
quoted := make([]string, len(conditions))
for i, condition := range conditions {
quoted[i] = fmt.Sprintf("%q", condition)
}
return strings.Join(quoted, ", ")
}

keys := make([]string, 0, len(conditions))
for key := range conditions {
keys = append(keys, key)
Expand All @@ -2071,8 +2055,8 @@ func (r resolverQuery) finalizeImportsExportsResult(

tracker.MsgData(debug.token,
fmt.Sprintf("None of the conditions provided (%s) match any of the currently active conditions (%s):",
prettyPrintConditions(unmatchedConditions),
prettyPrintConditions(keys),
helpers.StringArrayToQuotedCommaSeparatedString(unmatchedConditions),
helpers.StringArrayToQuotedCommaSeparatedString(keys),
)),
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,11 @@ func validateFeatures(log logger.Log, target Target, engines []Engine) (config.T
case 3:
text = fmt.Sprintf("%s%d.%d.%d", engine.String(), version[0], version[1], version[2])
}
targets = append(targets, fmt.Sprintf("%q", text))
targets = append(targets, text)
}

sort.Strings(targets)
targetEnv := strings.Join(targets, ", ")
targetEnv := helpers.StringArrayToQuotedCommaSeparatedString(targets)

return targetFromAPI, compat.UnsupportedJSFeatures(constraints), compat.UnsupportedCSSFeatures(constraints), targetEnv
}
Expand Down

0 comments on commit 757d7d9

Please sign in to comment.