Skip to content

Commit

Permalink
fix: kebab-case in asset names
Browse files Browse the repository at this point in the history
with multiple uppercase characters in a row, closes #241
  • Loading branch information
aragon999 authored and shyim committed Oct 18, 2023
1 parent d5224ab commit 2fc1bdd
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion extension/asset_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func createConfigFromPath(entryPointName string, extensionRoot string) Extension
Views: []string{
"Resources/views",
},
TechnicalName: strings.ReplaceAll(ToSnakeCase(entryPointName), "_", "-"),
TechnicalName: esbuild.ToKebabCase(entryPointName),
Administration: ExtensionAssetConfigAdmin{
Path: "Resources/app/administration/src",
EntryFilePath: entryFilePathAdmin,
Expand Down
12 changes: 0 additions & 12 deletions extension/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,9 @@ package extension
import (
"os"
"path/filepath"
"regexp"
"strings"
)

var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)

func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}

func PlatformPath(projectRoot, component, path string) string {
if _, err := os.Stat(filepath.Join(projectRoot, "src", "Core", "composer.json")); err == nil {
return filepath.Join(projectRoot, "src", component, path)
Expand Down
2 changes: 1 addition & 1 deletion internal/esbuild/esbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Context(ctx context.Context, options AssetCompileOptions) (api.BuildContext
}

func CompileExtensionAsset(ctx context.Context, options AssetCompileOptions) (*AssetCompileResult, error) {
technicalName := strings.ReplaceAll(toSnakeCase(options.Name), "_", "-")
technicalName := ToKebabCase(options.Name)
jsFile := filepath.Join(options.Path, options.OutputDir, "js", technicalName+".js")
cssFile := filepath.Join(options.Path, options.OutputDir, "css", technicalName+".css")

Expand Down
4 changes: 2 additions & 2 deletions internal/esbuild/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
var matchLetter = regexp.MustCompile(`[A-Z]`)

// @see https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php#L31

Check notice on line 10 in internal/esbuild/util.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Comment of exported element starts with the incorrect name

Comment should have the following format 'ToKebabCase ...' (with an optional leading article)
func toSnakeCase(str string) string {
func ToKebabCase(str string) string {
converted := matchLetter.ReplaceAllStringFunc(str, func(match string) string {
return "_" + strings.ToLower(match)
return "-" + strings.ToLower(match)
})
return converted[1:]
}
9 changes: 5 additions & 4 deletions internal/esbuild/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSnakeCase(t *testing.T) {
assert.Equal(t, "foo_bar", toSnakeCase("FooBar"))
assert.Equal(t, "frosh_tools", toSnakeCase("FroshTools"))
assert.Equal(t, "my_module_name_s_w6", toSnakeCase("MyModuleNameSW6"))
func TestKebabCase(t *testing.T) {
assert.Equal(t, "foo-bar", ToKebabCase("FooBar"))
assert.Equal(t, "f-o-o-bar-baz", ToKebabCase("FOOBarBaz"))
assert.Equal(t, "frosh-tools", ToKebabCase("FroshTools"))
assert.Equal(t, "my-module-name-s-w6", ToKebabCase("MyModuleNameSW6"))
}

0 comments on commit 2fc1bdd

Please sign in to comment.