Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 0f7d3d0

Browse files
Juanjo Alvarezdennwc
authored andcommitted
Comment fixes
- Extract the actual comment text. - Remove //, /*..*/ and /**..*/ from comments/javadoc. Signed-off-by: Juanjo Alvarez <juanjo@sourced.tech>
1 parent dce5e95 commit 0f7d3d0

15 files changed

Lines changed: 385 additions & 21 deletions

driver/normalizer/tonode.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package normalizer
22

33
import (
4+
"strings"
5+
46
"gopkg.in/bblfsh/sdk.v1/uast"
57
)
68

@@ -24,11 +26,34 @@ var ToNode = &uast.ObjectToNode{
2426
"keyword": true, // Modifier
2527
"primitiveTypeCode": true, // ?
2628
},
29+
2730
SyntheticTokens: map[string]string{
2831
"PackageDeclaration": "package",
2932
"IfStatement": "if",
3033
"NullLiteral": "null",
3134
},
35+
36+
Modifier: func(n map[string]interface{}) error {
37+
// Remove //, /*...*/ and /**..*/ from comment nodes
38+
if t, ok := n["internalClass"]; ok {
39+
switch t {
40+
case "LineComment":
41+
if text, ok := n["text"].(string); ok && strings.HasPrefix(text, "//") {
42+
n["text"] = text[2:]
43+
}
44+
case "BlockComment":
45+
if text, ok := n["text"].(string); ok && strings.HasPrefix(text, "/*") {
46+
n["text"] = text[2 : len(text)-2]
47+
}
48+
case "Javadoc":
49+
if text, ok := n["text"].(string); ok && strings.HasPrefix(text, "/**") {
50+
n["text"] = text[3 : len(text)-2]
51+
}
52+
}
53+
}
54+
55+
return nil
56+
},
3257
//TODO: add names of children (e.g. elseStatement) as
3358
// children node properties.
3459
}

fixtures/block_comment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
/* block comment before */
22
class A { /* block comment inline */ }
33
/* block comment after */
4+
5+
/*
6+
* This is a multiline
7+
* block comment
8+
* end
9+
*/

fixtures/block_comment.java.native

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"internalClass": "BlockComment",
1313
"startColumn": 1,
1414
"startLine": 1,
15-
"startPosition": 0
15+
"startPosition": 0,
16+
"text": "/* block comment before */"
1617
},
1718
{
1819
"endColumn": 37,
@@ -21,7 +22,8 @@
2122
"internalClass": "BlockComment",
2223
"startColumn": 11,
2324
"startLine": 2,
24-
"startPosition": 37
25+
"startPosition": 37,
26+
"text": "/* block comment inline */"
2527
},
2628
{
2729
"endColumn": 26,
@@ -30,7 +32,18 @@
3032
"internalClass": "BlockComment",
3133
"startColumn": 1,
3234
"startLine": 3,
33-
"startPosition": 66
35+
"startPosition": 66,
36+
"text": "/* block comment after */"
37+
},
38+
{
39+
"endColumn": 4,
40+
"endLine": 9,
41+
"endPosition": 146,
42+
"internalClass": "BlockComment",
43+
"startColumn": 1,
44+
"startLine": 5,
45+
"startPosition": 93,
46+
"text": "/*\n* This is a multiline\n* block comment\n* end\n*/"
3447
}
3548
],
3649
"internalClass": "CompilationUnit",

fixtures/block_comment.java.uast

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CompilationUnit {
1919
. . . }
2020
. . . Properties: {
2121
. . . . internalRole: comments
22+
. . . . text: block comment before
2223
. . . }
2324
. . }
2425
. . 1: TypeDeclaration {
@@ -71,6 +72,7 @@ CompilationUnit {
7172
. . . }
7273
. . . Properties: {
7374
. . . . internalRole: comments
75+
. . . . text: block comment inline
7476
. . . }
7577
. . }
7678
. . 3: BlockComment {
@@ -87,6 +89,28 @@ CompilationUnit {
8789
. . . }
8890
. . . Properties: {
8991
. . . . internalRole: comments
92+
. . . . text: block comment after
93+
. . . }
94+
. . }
95+
. . 4: BlockComment {
96+
. . . Roles: Comment
97+
. . . StartPosition: {
98+
. . . . Offset: 93
99+
. . . . Line: 5
100+
. . . . Col: 1
101+
. . . }
102+
. . . EndPosition: {
103+
. . . . Offset: 146
104+
. . . . Line: 9
105+
. . . . Col: 4
106+
. . . }
107+
. . . Properties: {
108+
. . . . internalRole: comments
109+
. . . . text:
110+
* This is a multiline
111+
* block comment
112+
* end
113+
90114
. . . }
91115
. . }
92116
. }

fixtures/empty.java.native

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
{
22
"status": "ok",
3-
"language": "java",
3+
"language": "",
44
"errors": [],
5-
"ast": {
6-
"CompilationUnit": {
7-
"internalClass": "CompilationUnit"
8-
}
9-
}
5+
"ast": null
106
}

fixtures/empty.java.uast

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
Status: ok
2-
Language: java
2+
Language:
33
Errors:
44
UAST:
5-
CompilationUnit {
6-
. Roles: File
7-
}
85

fixtures/javadoc.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** This is a javadoc comment */
2+
3+
class A { /** javadoc comment inline */ }
4+
5+
/**
6+
* This is a multiline JavaDoc
7+
* comment
8+
* */

fixtures/javadoc.java.native

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"status": "ok",
3+
"language": "java",
4+
"errors": [],
5+
"ast": {
6+
"CompilationUnit": {
7+
"comments": [
8+
{
9+
"endColumn": 40,
10+
"endLine": 3,
11+
"endPosition": 73,
12+
"internalClass": "Javadoc",
13+
"startColumn": 11,
14+
"startLine": 3,
15+
"startPosition": 44,
16+
"text": "/** javadoc comment inline */"
17+
},
18+
{
19+
"endColumn": 6,
20+
"endLine": 8,
21+
"endPosition": 128,
22+
"internalClass": "Javadoc",
23+
"startColumn": 1,
24+
"startLine": 5,
25+
"startPosition": 77,
26+
"text": "/**\n* This is a multiline JavaDoc\n* comment\n* */"
27+
}
28+
],
29+
"internalClass": "CompilationUnit",
30+
"types": [
31+
{
32+
"endColumn": 42,
33+
"endLine": 3,
34+
"endPosition": 75,
35+
"interface": "false",
36+
"internalClass": "TypeDeclaration",
37+
"javadoc": {
38+
"internalClass": "Javadoc",
39+
"tags": [
40+
{
41+
"fragments": [
42+
{
43+
"endColumn": 31,
44+
"endLine": 1,
45+
"endPosition": 30,
46+
"internalClass": "TextElement",
47+
"startColumn": 5,
48+
"startLine": 1,
49+
"startPosition": 4,
50+
"text": "This is a javadoc comment "
51+
}
52+
],
53+
"internalClass": "TagElement"
54+
}
55+
]
56+
},
57+
"name": {
58+
"endColumn": 8,
59+
"endLine": 3,
60+
"endPosition": 41,
61+
"identifier": "A",
62+
"internalClass": "SimpleName",
63+
"startColumn": 7,
64+
"startLine": 3,
65+
"startPosition": 40
66+
},
67+
"startColumn": 1,
68+
"startLine": 1,
69+
"startPosition": 0
70+
}
71+
]
72+
}
73+
}
74+
}

fixtures/javadoc.java.uast

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
Status: ok
2+
Language: java
3+
Errors:
4+
UAST:
5+
CompilationUnit {
6+
. Roles: File
7+
. Children: {
8+
. . 0: TypeDeclaration {
9+
. . . Roles: Visibility,Package,Declaration,Type
10+
. . . StartPosition: {
11+
. . . . Offset: 0
12+
. . . . Line: 1
13+
. . . . Col: 1
14+
. . . }
15+
. . . EndPosition: {
16+
. . . . Offset: 75
17+
. . . . Line: 3
18+
. . . . Col: 42
19+
. . . }
20+
. . . Properties: {
21+
. . . . interface: false
22+
. . . . internalRole: types
23+
. . . }
24+
. . . Children: {
25+
. . . . 0: Javadoc {
26+
. . . . . Roles: Documentation,Comment
27+
. . . . . Properties: {
28+
. . . . . . internalRole: javadoc
29+
. . . . . }
30+
. . . . . Children: {
31+
. . . . . . 0: TagElement {
32+
. . . . . . . Roles: Documentation,Incomplete
33+
. . . . . . . Properties: {
34+
. . . . . . . . internalRole: tags
35+
. . . . . . . }
36+
. . . . . . . Children: {
37+
. . . . . . . . 0: TextElement {
38+
. . . . . . . . . Roles: Documentation,Incomplete
39+
. . . . . . . . . StartPosition: {
40+
. . . . . . . . . . Offset: 4
41+
. . . . . . . . . . Line: 1
42+
. . . . . . . . . . Col: 5
43+
. . . . . . . . . }
44+
. . . . . . . . . EndPosition: {
45+
. . . . . . . . . . Offset: 30
46+
. . . . . . . . . . Line: 1
47+
. . . . . . . . . . Col: 31
48+
. . . . . . . . . }
49+
. . . . . . . . . Properties: {
50+
. . . . . . . . . . internalRole: fragments
51+
. . . . . . . . . . text: This is a javadoc comment
52+
. . . . . . . . . }
53+
. . . . . . . . }
54+
. . . . . . . }
55+
. . . . . . }
56+
. . . . . }
57+
. . . . }
58+
. . . . 1: SimpleName {
59+
. . . . . Roles: Expression,Identifier
60+
. . . . . TOKEN "A"
61+
. . . . . StartPosition: {
62+
. . . . . . Offset: 40
63+
. . . . . . Line: 3
64+
. . . . . . Col: 7
65+
. . . . . }
66+
. . . . . EndPosition: {
67+
. . . . . . Offset: 41
68+
. . . . . . Line: 3
69+
. . . . . . Col: 8
70+
. . . . . }
71+
. . . . . Properties: {
72+
. . . . . . internalRole: name
73+
. . . . . }
74+
. . . . }
75+
. . . }
76+
. . }
77+
. . 1: Javadoc {
78+
. . . Roles: Documentation,Comment
79+
. . . StartPosition: {
80+
. . . . Offset: 44
81+
. . . . Line: 3
82+
. . . . Col: 11
83+
. . . }
84+
. . . EndPosition: {
85+
. . . . Offset: 73
86+
. . . . Line: 3
87+
. . . . Col: 40
88+
. . . }
89+
. . . Properties: {
90+
. . . . internalRole: comments
91+
. . . . text: javadoc comment inline
92+
. . . }
93+
. . }
94+
. . 2: Javadoc {
95+
. . . Roles: Documentation,Comment
96+
. . . StartPosition: {
97+
. . . . Offset: 77
98+
. . . . Line: 5
99+
. . . . Col: 1
100+
. . . }
101+
. . . EndPosition: {
102+
. . . . Offset: 128
103+
. . . . Line: 8
104+
. . . . Col: 6
105+
. . . }
106+
. . . Properties: {
107+
. . . . internalRole: comments
108+
. . . . text:
109+
* This is a multiline JavaDoc
110+
* comment
111+
*
112+
. . . }
113+
. . }
114+
. }
115+
}
116+

fixtures/line_comment.java.native

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"internalClass": "LineComment",
1313
"startColumn": 1,
1414
"startLine": 1,
15-
"startPosition": 0
15+
"startPosition": 0,
16+
"text": "// first comment before"
1617
},
1718
{
1819
"endColumn": 28,
@@ -21,7 +22,8 @@
2122
"internalClass": "LineComment",
2223
"startColumn": 11,
2324
"startLine": 2,
24-
"startPosition": 34
25+
"startPosition": 34,
26+
"text": "// comment inline"
2527
},
2628
{
2729
"endColumn": 17,
@@ -30,7 +32,8 @@
3032
"internalClass": "LineComment",
3133
"startColumn": 1,
3234
"startLine": 4,
33-
"startPosition": 54
35+
"startPosition": 54,
36+
"text": "// comment after"
3437
}
3538
],
3639
"internalClass": "CompilationUnit",

0 commit comments

Comments
 (0)