Skip to content

Commit

Permalink
feat: test for remove unused imporrts
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 26, 2019
1 parent 9ddd31f commit 9d6e382
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
3 changes: 2 additions & 1 deletion cmd/refactor.go
Expand Up @@ -23,7 +23,8 @@ var refactorCmd = &cobra.Command{
app.Analysis()

app2 := NewRemoveUnusedImportApp(path)
app2.Analysis()
results := app2.Analysis()
app2.Refactoring(results)
}

if dependence != "" && rename != "" {
Expand Down
8 changes: 6 additions & 2 deletions core/domain/refactor/base/JavaRefactorListener.go
Expand Up @@ -7,7 +7,7 @@ import (
"unicode"
)

var node *models2.JFullIdentifier;
var node models2.JFullIdentifier;

type JavaRefactorListener struct {
BaseJavaParserListener
Expand Down Expand Up @@ -203,6 +203,10 @@ func isUppercaseText(text string) bool {
return !strings.Contains(text, ".") && unicode.IsUpper([]rune(text)[0])
}

func (s *JavaRefactorListener) InitNode(identifier *models2.JFullIdentifier) {
func (s *JavaRefactorListener) InitNode(identifier models2.JFullIdentifier) {
node = identifier
}

func (s *JavaRefactorListener) GetNodeInfo() models2.JFullIdentifier {
return node
}
4 changes: 2 additions & 2 deletions core/domain/refactor/base/models/JFullIdentifier.go
Expand Up @@ -34,8 +34,8 @@ type JFullIdentifier struct {
Type string
}

func NewJFullIdentifier() *JFullIdentifier {
identifier := &JFullIdentifier{"", "", ""}
func NewJFullIdentifier() JFullIdentifier {
identifier := *&JFullIdentifier{"", "", ""}
methods = nil
fields = make(map[string]JField)
imports = nil
Expand Down
2 changes: 1 addition & 1 deletion core/domain/refactor/base/models/JMoveStruct.go
Expand Up @@ -8,7 +8,7 @@ type JImport struct {
}

type JMoveStruct struct {
*JFullIdentifier
JFullIdentifier

Path string
Deps []JImport
Expand Down
15 changes: 7 additions & 8 deletions core/domain/refactor/move_class/move_class_app.go
Expand Up @@ -2,11 +2,11 @@ package move_class

import (
"bufio"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
base2 "github.com/phodal/coca/core/domain/refactor/base"
models2 "github.com/phodal/coca/core/domain/refactor/base/models"
utils2 "github.com/phodal/coca/core/support"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -35,7 +35,6 @@ func NewMoveClassApp(config string, pPath string) *MoveClassApp {
func (j *MoveClassApp) Analysis() {
// TODO: 使用 Deps.json 来移动包
files := utils2.GetJavaFiles(configPath)
fmt.Println(files)
for index := range files {
file := files[index]

Expand Down Expand Up @@ -86,11 +85,11 @@ func parseRename() {
}
}

func updatePackageInfo(structs []models2.JMoveStruct, originImport string, newImport string) {
func updatePackageInfo(structs []models2.JMoveStruct, originImport string, newImport string) {
var originNode models2.JMoveStruct
for index := range nodes {
node := nodes[index]
if originImport == node.Pkg + "." + node.Name {
if originImport == node.Pkg+"."+node.Name {
originNode = node
}
}
Expand All @@ -100,9 +99,9 @@ func updatePackageInfo(structs []models2.JMoveStruct, originImport string, newIm
}
path := buildJavaPath(configPath + newImport)
split := strings.Split(newImport, ".")
pkg := strings.Join(split[:len(split) - 1], ".")
pkg := strings.Join(split[:len(split)-1], ".")
fmt.Println(pkg)
updateFile(path, originNode.GetPkgInfo().StartLine, "package " + pkg + ";")
updateFile(path, originNode.GetPkgInfo().StartLine, "package "+pkg+";")
}

func updateImportSide(originImport string, newImport string) {
Expand All @@ -127,7 +126,7 @@ func updateFile(path string, lineNum int, newImp string) {

for i := range lines {
if i == lineNum {
lines[i - 1] = newImp
lines[i-1] = newImp
}
}
output := strings.Join(lines, "\n")
Expand Down
22 changes: 15 additions & 7 deletions core/domain/refactor/unused/remove_unused_import.go
Expand Up @@ -19,17 +19,16 @@ var configPath string
type RemoveUnusedImportApp struct {
}

var nodes []models2.JMoveStruct

func NewRemoveUnusedImportApp(pPath string) *RemoveUnusedImportApp {
configPath = pPath

nodes = nil
return &RemoveUnusedImportApp{}
}

func (j *RemoveUnusedImportApp) Analysis() {
func (j *RemoveUnusedImportApp) Analysis() []models2.JFullIdentifier {
files := support.GetJavaFiles(configPath)

var nodes []models2.JFullIdentifier = nil
for index := range files {
file := files[index]

Expand All @@ -46,13 +45,22 @@ func (j *RemoveUnusedImportApp) Analysis() {

antlr.NewParseTreeWalker().Walk(listener, context)

nodes = append(nodes, listener.GetNodeInfo())
}

return nodes
}

func (j *RemoveUnusedImportApp) Refactoring(resultNodes []models2.JFullIdentifier) {
for _, node := range resultNodes {
if node.Name != "" {
handleNode(node)
errorLines := BuildErrorLines(node)
removeImportByLines(currentFile, errorLines)
}
}
}

func handleNode(node *models2.JFullIdentifier) {
func BuildErrorLines(node models2.JFullIdentifier) []int {
var fields = node.GetFields()
var imports = node.GetImports()

Expand All @@ -74,7 +82,7 @@ func handleNode(node *models2.JFullIdentifier) {
}
}

removeImportByLines(currentFile, errorLines)
return errorLines
}

func removeImportByLines(file string, errorLines []int) {
Expand Down
6 changes: 4 additions & 2 deletions core/domain/refactor/unused/remove_unused_import_test.go
Expand Up @@ -11,8 +11,10 @@ func TestRemoveUnusedImportApp_Analysis(t *testing.T) {

codePath := "../../../../_fixtures/refactor/unused"
app := NewRemoveUnusedImportApp(codePath)
app.Analysis()
results := app.Analysis()

g.Expect(len(results)).To(Equal(1))

g.Expect(true).To(Equal(true))
errorLines := BuildErrorLines(results[0])
g.Expect(errorLines).To(Equal([]int{3,4,5}))
}

0 comments on commit 9d6e382

Please sign in to comment.