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

feat: test for remove unused imporrts

上级 9ddd31ff
......@@ -23,7 +23,8 @@ var refactorCmd = &cobra.Command{
app.Analysis()
app2 := NewRemoveUnusedImportApp(path)
app2.Analysis()
results := app2.Analysis()
app2.Refactoring(results)
}
if dependence != "" && rename != "" {
......
......@@ -7,7 +7,7 @@ import (
"unicode"
)
var node *models2.JFullIdentifier;
var node models2.JFullIdentifier;
type JavaRefactorListener struct {
BaseJavaParserListener
......@@ -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
}
......@@ -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
......
......@@ -8,7 +8,7 @@ type JImport struct {
}
type JMoveStruct struct {
*JFullIdentifier
JFullIdentifier
Path string
Deps []JImport
......
......@@ -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"
......@@ -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]
......@@ -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
}
}
......@@ -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) {
......@@ -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")
......
......@@ -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]
......@@ -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()
......@@ -74,7 +82,7 @@ func handleNode(node *models2.JFullIdentifier) {
}
}
removeImportByLines(currentFile, errorLines)
return errorLines
}
func removeImportByLines(file string, errorLines []int) {
......
......@@ -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}))
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册