Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Coca
提交
9d6e382f
C
Coca
项目概览
Phodal
/
Coca
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coca
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
9d6e382f
编写于
12月 26, 2019
作者:
P
Phodal Huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: test for remove unused imporrts
上级
9ddd31ff
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
37 addition
and
23 deletion
+37
-23
cmd/refactor.go
cmd/refactor.go
+2
-1
core/domain/refactor/base/JavaRefactorListener.go
core/domain/refactor/base/JavaRefactorListener.go
+6
-2
core/domain/refactor/base/models/JFullIdentifier.go
core/domain/refactor/base/models/JFullIdentifier.go
+2
-2
core/domain/refactor/base/models/JMoveStruct.go
core/domain/refactor/base/models/JMoveStruct.go
+1
-1
core/domain/refactor/move_class/move_class_app.go
core/domain/refactor/move_class/move_class_app.go
+7
-8
core/domain/refactor/unused/remove_unused_import.go
core/domain/refactor/unused/remove_unused_import.go
+15
-7
core/domain/refactor/unused/remove_unused_import_test.go
core/domain/refactor/unused/remove_unused_import_test.go
+4
-2
未找到文件。
cmd/refactor.go
浏览文件 @
9d6e382f
...
...
@@ -23,7 +23,8 @@ var refactorCmd = &cobra.Command{
app
.
Analysis
()
app2
:=
NewRemoveUnusedImportApp
(
path
)
app2
.
Analysis
()
results
:=
app2
.
Analysis
()
app2
.
Refactoring
(
results
)
}
if
dependence
!=
""
&&
rename
!=
""
{
...
...
core/domain/refactor/base/JavaRefactorListener.go
浏览文件 @
9d6e382f
...
...
@@ -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
}
core/domain/refactor/base/models/JFullIdentifier.go
浏览文件 @
9d6e382f
...
...
@@ -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
...
...
core/domain/refactor/base/models/JMoveStruct.go
浏览文件 @
9d6e382f
...
...
@@ -8,7 +8,7 @@ type JImport struct {
}
type
JMoveStruct
struct
{
*
JFullIdentifier
JFullIdentifier
Path
string
Deps
[]
JImport
...
...
core/domain/refactor/move_class/move_class_app.go
浏览文件 @
9d6e382f
...
...
@@ -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
]
...
...
@@ -90,7 +89,7 @@ func updatePackageInfo(structs []models2.JMoveStruct, originImport string, newIm
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
"
)
...
...
core/domain/refactor/unused/remove_unused_import.go
浏览文件 @
9d6e382f
...
...
@@ -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) {
}
}
re
moveImportByLines
(
currentFile
,
errorLines
)
re
turn
errorLines
}
func
removeImportByLines
(
file
string
,
errorLines
[]
int
)
{
...
...
core/domain/refactor/unused/remove_unused_import_test.go
浏览文件 @
9d6e382f
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录