Skip to content

Commit

Permalink
Refactor to switch-case; decrease indentation (#122)
Browse files Browse the repository at this point in the history
* Refactor to switch-case; decrease indentation

* Update file.go

---------

Co-authored-by: Vyacheslav Pryimak <vyacheslavpryimak@gmail.com>
  • Loading branch information
alexandear and incu6us committed Sep 5, 2023
1 parent 0d5a4c9 commit 062693f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 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: 29 additions & 32 deletions reviser/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -458,49 +457,47 @@ func (f *SourceFile) parseImports(file *ast.File) (map[string]*commentsMetadata,
shouldUseAliasForVersionSuffix := f.shouldUseAliasForVersionSuffix

var packageImports map[string]string
var err error

if shouldRemoveUnusedImports || shouldUseAliasForVersionSuffix {
packageImports, err = astutil.LoadPackageDependencies(filepath.Dir(f.filePath), astutil.ParseBuildTag(file))
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) || 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 062693f

Please sign in to comment.