Fix parsing of existing imports with empty line

main
Jonas Letzbor 2024-04-26 20:57:06 +02:00
parent 4a4eb2fd99
commit ae30ef8295
Signed by: RPJosh
GPG Key ID: 43ACB900522EA740
2 changed files with 12 additions and 3 deletions

View File

@ -457,7 +457,7 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo
} }
// Regex to find an import statement // Regex to find an import statement
reg := regexp.MustCompile(`"([^"]+)"`) importRegex := regexp.MustCompile(`"([^"]+)"`)
var importStart, importEnd int var importStart, importEnd int
importFound := true importFound := true
@ -481,7 +481,7 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo
continue continue
} else { } else {
// Extract imported package // Extract imported package
matches := reg.FindStringSubmatch(lineContent) matches := importRegex.FindStringSubmatch(lineContent)
if len(matches) >= 2 { if len(matches) >= 2 {
// We can only import ONE package without () // We can only import ONE package without ()
if _, exists := imports[matches[1]]; !exists { if _, exists := imports[matches[1]]; !exists {
@ -505,7 +505,13 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo
break break
} }
matches := reg.FindStringSubmatch(lineContent) // Ignore any empty import lines. It's used as a seperator between std packages
// and "external" modules
if lineContent == "" {
continue
}
matches := importRegex.FindStringSubmatch(lineContent)
if len(matches) >= 2 { if len(matches) >= 2 {
if _, exists := imports[matches[1]]; !exists { if _, exists := imports[matches[1]]; !exists {
imports[matches[1]] = true imports[matches[1]] = true

View File

@ -328,6 +328,8 @@ package olaf
import ( import (
"time" "time"
"database/sql" "database/sql"
"git.anything"
) )
type SomeRandomTab struct { type SomeRandomTab struct {
@ -349,6 +351,7 @@ package olaf
import ( import (
"database/sql" "database/sql"
"git.anything"
"sql.NullString" "sql.NullString"
"time" "time"
) )