Skip to content
Closed
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
23 changes: 23 additions & 0 deletions cmd/internal/migrations/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,26 @@
return nil
}

func addImport(content, pkg string) string {
importStmt := fmt.Sprintf("\"%s\"", pkg)

Check failure on line 525 in cmd/internal/migrations/v3/common.go

View workflow job for this annotation

GitHub Actions / lint

sprintfQuotedString: use %q instead of "%s" for quoted strings (gocritic)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix lint: use %q instead of "%s" when building a quoted string.

golangci-lint/gocritic is right here. Use %q to build a quoted import literal.

Apply this diff:

-	importStmt := fmt.Sprintf("\"%s\"", pkg)
+	importStmt := fmt.Sprintf("%q", pkg)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
importStmt := fmt.Sprintf("\"%s\"", pkg)
importStmt := fmt.Sprintf("%q", pkg)
🧰 Tools
🪛 GitHub Check: lint

[failure] 525-525:
sprintfQuotedString: use %q instead of "%s" for quoted strings (gocritic)

🪛 GitHub Actions: golangci-lint

[error] 525-525: golangci-lint (gocritic): sprintfQuotedString should use %q instead of "%s" for quoted strings.

🤖 Prompt for AI Agents
In cmd/internal/migrations/v3/common.go around line 525, the code builds a
quoted import string using fmt.Sprintf("\"%s\"", pkg); replace that with
fmt.Sprintf("%q", pkg) to use the correct formatting verb for quoted strings and
satisfy the linter; update the call accordingly so the resulting string remains
the same but uses %q.

if strings.Contains(content, importStmt) {
return content
}

reBlock := regexp.MustCompile(`(?m)^import\s*\(([^)]*)\)`)
if reBlock.MatchString(content) {
return reBlock.ReplaceAllString(content, fmt.Sprintf("import (\n$1\t%s\n)", importStmt))
}

reSingle := regexp.MustCompile(`(?m)^import\s+([^\n]+)`) // matches single import line
if reSingle.MatchString(content) {
return reSingle.ReplaceAllString(content, fmt.Sprintf("import (\n\t$1\n\t%s\n)", importStmt))
}

rePkg := regexp.MustCompile(`(?m)^package\s+\w+`)
return rePkg.ReplaceAllString(content, fmt.Sprintf("$0\n\nimport (\n\t%s\n)", importStmt))
}

// MigrateUtilsImport replaces old Fiber utils imports and updates removed helpers
func MigrateUtilsImport(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
reImport := regexp.MustCompile(`(?m)^(\s*(?:\w+\s+)?)"github\.com/gofiber/fiber/v\d+/utils"$`)
Expand Down Expand Up @@ -553,6 +573,9 @@
content = strings.ReplaceAll(content, "utils.GetBytes", "utils.CopyBytes")
content = strings.ReplaceAll(content, "utils.ImmutableString", "string")
content = strings.ReplaceAll(content, "utils.AssertEqual", "assert.Equal")
if strings.Contains(content, "assert.Equal") {
content = addImport(content, "github.com/stretchr/testify/assert")
}

return content
})
Expand Down
4 changes: 4 additions & 0 deletions cmd/internal/migrations/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ var (
_ = utils.GetString([]byte("a"))
_ = utils.GetBytes("a")
_ = utils.ImmutableString([]byte("b"))
_ = utils.AssertEqual("a", "a")
)`)

var buf bytes.Buffer
Expand All @@ -583,6 +584,7 @@ var (

content := readFile(t, file)
assert.Contains(t, content, "github.com/gofiber/utils/v2\"")
assert.Contains(t, content, "github.com/stretchr/testify/assert")
assert.NotContains(t, content, "fiber/v3/utils")
assert.NotContains(t, content, "TrimBytes(")
assert.NotContains(t, content, "TrimRightBytes(")
Expand All @@ -591,6 +593,7 @@ var (
assert.NotContains(t, content, "GetString(")
assert.NotContains(t, content, "GetBytes(")
assert.NotContains(t, content, "ImmutableString(")
assert.NotContains(t, content, "utils.AssertEqual")
assert.Contains(t, content, "utils.Trim(")
assert.Contains(t, content, "utils.TrimRight(")
assert.Contains(t, content, "utils.TrimLeft(")
Expand All @@ -602,6 +605,7 @@ var (
assert.Contains(t, content, "utils.ToString(")
assert.Contains(t, content, "utils.CopyBytes(")
assert.Contains(t, content, "string([]byte(\"b\"))")
assert.Contains(t, content, "assert.Equal")
assert.Contains(t, buf.String(), "Migrating utils imports")
}

Expand Down
Loading