Skip to content

Commit

Permalink
Refactor to switch-case; decrease indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Aug 18, 2023
1 parent 8ec65d1 commit 48a7bc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,12 @@ func resultPostProcess(hasChange bool, deprecatedMessagesCh chan string, originF
printDeprecations(deprecatedMessagesCh)
return
}
if hasChange && *listFileName && output != "write" {
switch {
case hasChange && *listFileName && output != "write":
fmt.Println(originFilePath)
} else if output == "stdout" || originFilePath == reviser.StandardInput {
case output == "stdout" || originFilePath == reviser.StandardInput:
fmt.Print(string(formattedOutput))
} else if output == "file" || output == "write" {
case output == "file" || output == "write":
if !hasChange {
printDeprecations(deprecatedMessagesCh)
return
Expand All @@ -360,7 +361,7 @@ func resultPostProcess(hasChange bool, deprecatedMessagesCh chan string, originF
if *listFileName {
fmt.Println(originFilePath)
}
} else {
default:
log.Fatalf(`invalid output %q specified`, output)
}

Expand Down
61 changes: 31 additions & 30 deletions reviser/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,49 +457,50 @@ func (f *SourceFile) parseImports(file *ast.File) (map[string]*commentsMetadata,
shouldUseAliasForVersionSuffix := f.shouldUseAliasForVersionSuffix

var packageImports map[string]string
var err error

if shouldRemoveUnusedImports || shouldUseAliasForVersionSuffix {
var err error
packageImports, err = astutil.LoadPackageDependencies(path.Dir(f.filePath), astutil.ParseBuildTag(file))
if err != nil {
return nil, err
}
}

for _, decl := range file.Decls {
switch decl.(type) {
case *ast.GenDecl:
dd := decl.(*ast.GenDecl)
if isSingleCgoImport(dd) {
dd, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if isSingleCgoImport(dd) {
continue
}
if dd.Tok != token.IMPORT {
continue
}
for _, spec := range dd.Specs {
importSpec := spec.(*ast.ImportSpec)

if shouldRemoveUnusedImports && !astutil.UsesImport(
file, packageImports, strings.Trim(importSpec.Path.Value, `"`),
) {
continue
}
if dd.Tok == token.IMPORT {
for _, spec := range dd.Specs {
var importSpecStr string
importSpec := spec.(*ast.ImportSpec)

if shouldRemoveUnusedImports && !astutil.UsesImport(
file, packageImports, strings.Trim(importSpec.Path.Value, `"`),
) {
continue
}

if importSpec.Name != nil {
importSpecStr = strings.Join([]string{importSpec.Name.String(), importSpec.Path.Value}, " ")
} else {
if shouldUseAliasForVersionSuffix {
importSpecStr = setAliasForVersionedImportSpec(importSpec, packageImports)
} else {
importSpecStr = importSpec.Path.Value
}
}

importsWithMetadata[importSpecStr] = &commentsMetadata{
Doc: importSpec.Doc,
Comment: importSpec.Comment,
}

var importSpecStr string
if importSpec.Name != nil {
importSpecStr = strings.Join([]string{importSpec.Name.String(), importSpec.Path.Value}, " ")
} else {
if shouldUseAliasForVersionSuffix {
importSpecStr = setAliasForVersionedImportSpec(importSpec, packageImports)
} else {
importSpecStr = importSpec.Path.Value
}
}

importsWithMetadata[importSpecStr] = &commentsMetadata{
Doc: importSpec.Doc,
Comment: importSpec.Comment,
}
}
}

Expand Down

0 comments on commit 48a7bc5

Please sign in to comment.