未验证 提交 9d6e382f 编写于 作者: P Phodal Huang

feat: test for remove unused imporrts

上级 9ddd31ff
...@@ -23,7 +23,8 @@ var refactorCmd = &cobra.Command{ ...@@ -23,7 +23,8 @@ var refactorCmd = &cobra.Command{
app.Analysis() app.Analysis()
app2 := NewRemoveUnusedImportApp(path) app2 := NewRemoveUnusedImportApp(path)
app2.Analysis() results := app2.Analysis()
app2.Refactoring(results)
} }
if dependence != "" && rename != "" { if dependence != "" && rename != "" {
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
"unicode" "unicode"
) )
var node *models2.JFullIdentifier; var node models2.JFullIdentifier;
type JavaRefactorListener struct { type JavaRefactorListener struct {
BaseJavaParserListener BaseJavaParserListener
...@@ -203,6 +203,10 @@ func isUppercaseText(text string) bool { ...@@ -203,6 +203,10 @@ func isUppercaseText(text string) bool {
return !strings.Contains(text, ".") && unicode.IsUpper([]rune(text)[0]) 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 node = identifier
} }
func (s *JavaRefactorListener) GetNodeInfo() models2.JFullIdentifier {
return node
}
...@@ -34,8 +34,8 @@ type JFullIdentifier struct { ...@@ -34,8 +34,8 @@ type JFullIdentifier struct {
Type string Type string
} }
func NewJFullIdentifier() *JFullIdentifier { func NewJFullIdentifier() JFullIdentifier {
identifier := &JFullIdentifier{"", "", ""} identifier := *&JFullIdentifier{"", "", ""}
methods = nil methods = nil
fields = make(map[string]JField) fields = make(map[string]JField)
imports = nil imports = nil
......
...@@ -8,7 +8,7 @@ type JImport struct { ...@@ -8,7 +8,7 @@ type JImport struct {
} }
type JMoveStruct struct { type JMoveStruct struct {
*JFullIdentifier JFullIdentifier
Path string Path string
Deps []JImport Deps []JImport
......
...@@ -2,11 +2,11 @@ package move_class ...@@ -2,11 +2,11 @@ package move_class
import ( import (
"bufio" "bufio"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
base2 "github.com/phodal/coca/core/domain/refactor/base" base2 "github.com/phodal/coca/core/domain/refactor/base"
models2 "github.com/phodal/coca/core/domain/refactor/base/models" models2 "github.com/phodal/coca/core/domain/refactor/base/models"
utils2 "github.com/phodal/coca/core/support" utils2 "github.com/phodal/coca/core/support"
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
...@@ -35,7 +35,6 @@ func NewMoveClassApp(config string, pPath string) *MoveClassApp { ...@@ -35,7 +35,6 @@ func NewMoveClassApp(config string, pPath string) *MoveClassApp {
func (j *MoveClassApp) Analysis() { func (j *MoveClassApp) Analysis() {
// TODO: 使用 Deps.json 来移动包 // TODO: 使用 Deps.json 来移动包
files := utils2.GetJavaFiles(configPath) files := utils2.GetJavaFiles(configPath)
fmt.Println(files)
for index := range files { for index := range files {
file := files[index] file := files[index]
...@@ -86,11 +85,11 @@ func parseRename() { ...@@ -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 var originNode models2.JMoveStruct
for index := range nodes { for index := range nodes {
node := nodes[index] node := nodes[index]
if originImport == node.Pkg + "." + node.Name { if originImport == node.Pkg+"."+node.Name {
originNode = node originNode = node
} }
} }
...@@ -100,9 +99,9 @@ func updatePackageInfo(structs []models2.JMoveStruct, originImport string, newIm ...@@ -100,9 +99,9 @@ func updatePackageInfo(structs []models2.JMoveStruct, originImport string, newIm
} }
path := buildJavaPath(configPath + newImport) path := buildJavaPath(configPath + newImport)
split := strings.Split(newImport, ".") split := strings.Split(newImport, ".")
pkg := strings.Join(split[:len(split) - 1], ".") pkg := strings.Join(split[:len(split)-1], ".")
fmt.Println(pkg) fmt.Println(pkg)
updateFile(path, originNode.GetPkgInfo().StartLine, "package " + pkg + ";") updateFile(path, originNode.GetPkgInfo().StartLine, "package "+pkg+";")
} }
func updateImportSide(originImport string, newImport string) { func updateImportSide(originImport string, newImport string) {
...@@ -127,7 +126,7 @@ func updateFile(path string, lineNum int, newImp string) { ...@@ -127,7 +126,7 @@ func updateFile(path string, lineNum int, newImp string) {
for i := range lines { for i := range lines {
if i == lineNum { if i == lineNum {
lines[i - 1] = newImp lines[i-1] = newImp
} }
} }
output := strings.Join(lines, "\n") output := strings.Join(lines, "\n")
......
...@@ -19,17 +19,16 @@ var configPath string ...@@ -19,17 +19,16 @@ var configPath string
type RemoveUnusedImportApp struct { type RemoveUnusedImportApp struct {
} }
var nodes []models2.JMoveStruct
func NewRemoveUnusedImportApp(pPath string) *RemoveUnusedImportApp { func NewRemoveUnusedImportApp(pPath string) *RemoveUnusedImportApp {
configPath = pPath configPath = pPath
nodes = nil
return &RemoveUnusedImportApp{} return &RemoveUnusedImportApp{}
} }
func (j *RemoveUnusedImportApp) Analysis() { func (j *RemoveUnusedImportApp) Analysis() []models2.JFullIdentifier {
files := support.GetJavaFiles(configPath) files := support.GetJavaFiles(configPath)
var nodes []models2.JFullIdentifier = nil
for index := range files { for index := range files {
file := files[index] file := files[index]
...@@ -46,13 +45,22 @@ func (j *RemoveUnusedImportApp) Analysis() { ...@@ -46,13 +45,22 @@ func (j *RemoveUnusedImportApp) Analysis() {
antlr.NewParseTreeWalker().Walk(listener, context) 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 != "" { 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 fields = node.GetFields()
var imports = node.GetImports() var imports = node.GetImports()
...@@ -74,7 +82,7 @@ func handleNode(node *models2.JFullIdentifier) { ...@@ -74,7 +82,7 @@ func handleNode(node *models2.JFullIdentifier) {
} }
} }
removeImportByLines(currentFile, errorLines) return errorLines
} }
func removeImportByLines(file string, errorLines []int) { func removeImportByLines(file string, errorLines []int) {
......
...@@ -11,8 +11,10 @@ func TestRemoveUnusedImportApp_Analysis(t *testing.T) { ...@@ -11,8 +11,10 @@ func TestRemoveUnusedImportApp_Analysis(t *testing.T) {
codePath := "../../../../_fixtures/refactor/unused" codePath := "../../../../_fixtures/refactor/unused"
app := NewRemoveUnusedImportApp(codePath) 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}))
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册