Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
17 changes: 14 additions & 3 deletions cmd/internal/migrations/v3/view_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ import (
)

func MigrateViewBind(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
// Replace .Bind() with arguments, not the Bind() from the binding package
reViewBind := regexp.MustCompile(`\.Bind\(([^)]+)\)`)
reViewBind := regexp.MustCompile(`(\w+)\.Bind\(([^)]+)\)`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The regular expression ([^)]+) for capturing arguments is not robust for cases where arguments themselves contain parentheses, such as in a function call. For example, a call like c.Bind(someFunc(a)) will not be matched by this regex, and thus will not be migrated. This can lead to incomplete migrations.

A more robust approach would be to use a non-greedy match for the arguments. This can handle simple nested parentheses. To also ensure multi-line arguments are supported (as . does not match newlines by default), the (?s) flag should be used.

I suggest updating the regex to be more resilient.

Suggested change
reViewBind := regexp.MustCompile(`(\w+)\.Bind\(([^)]+)\)`)
reViewBind := regexp.MustCompile(`(?s)(\w+)\.Bind\((.*?)\)`)


changed, err := internal.ChangeFileContent(cwd, func(content string) string {
return reViewBind.ReplaceAllString(content, ".ViewBind($1)")
orig := content
return reViewBind.ReplaceAllStringFunc(content, func(match string) string {
parts := reViewBind.FindStringSubmatch(match)
if len(parts) != 3 {
return match
}
ident := parts[1]
args := parts[2]
if isFiberCtx(orig, ident) {
return ident + ".ViewBind(" + args + ")"
}
return match
})
})
if err != nil {
return fmt.Errorf("failed to migrate ViewBind calls: %w", err)
Expand Down
22 changes: 22 additions & 0 deletions cmd/internal/migrations/v3/view_bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,25 @@ func handler(c fiber.Ctx) error {
assert.NotContains(t, content, "c.Bind(")
assert.Contains(t, buf.String(), "Migrating view binding helpers")
}

func Test_MigrateViewBind_NoChange(t *testing.T) {
t.Parallel()

dir, err := os.MkdirTemp("", "mvbtest")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

file := writeTempFile(t, dir, `package main
func handler() {
queries.Bind(results, &resultSlice)
}`)

var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateViewBind(cmd, dir, nil, nil))

content := readFile(t, file)
assert.Contains(t, content, "queries.Bind(")
assert.NotContains(t, content, "ViewBind(")
assert.Empty(t, buf.String())
}
Loading