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

Commit a1781b7

Browse files
author
Juanjo Alvarez
committed
Fix name and path inverter for RuntimeImport
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 9ad5956 commit a1781b7

19 files changed

+849
-486
lines changed

driver/normalizer/normalizer.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package normalizer
22

33
import (
44
"gopkg.in/bblfsh/sdk.v2/uast"
5+
"gopkg.in/bblfsh/sdk.v2/uast/role"
56
. "gopkg.in/bblfsh/sdk.v2/uast/transformer"
67
)
78

@@ -30,8 +31,8 @@ func funcDefMap(typ string, async bool) Mapping {
3031
"name": Var("name"),
3132
// Arguments should be converted by the uast.Arguments normalization
3233
"args": Obj{
33-
"args": Var("arguments"),
34-
uast.KeyPos: Var("_pos"),
34+
"args": Var("arguments"),
35+
uast.KeyPos: Var("_pos"),
3536
uast.KeyType: Var("_type"),
3637
},
3738
//"decorator_list": Var("_decorators"),
@@ -105,7 +106,7 @@ var Normalizers = []Mapping{
105106

106107
MapSemantic("alias", uast.Alias{}, MapObj(
107108
Obj{
108-
"name": Var("name"),
109+
"name": Var("name"),
109110
"asname": Var("aliased"),
110111
},
111112
Obj{
@@ -123,19 +124,6 @@ var Normalizers = []Mapping{
123124
Obj{"Name": Var("name")},
124125
)),
125126

126-
// FIXME: check that the identifiers are in the right order
127-
// FIXME: check that those are uast:Identifiers
128-
//MapSemantic("QualifiedIdentifier", uast.QualifiedIdentifier{}, MapObj(
129-
// Obj{"identifiers": Var("identifiers")},
130-
// Obj{"Names": Var("identifiers")},
131-
//)),
132-
//MapSemantic("QualifiedIdentifier", uast.QualifiedIdentifier{}, MapObj(
133-
// Obj{"identifiers": Each("ids", Var("ident"))},
134-
// Obj{"Names": Each("ids", UASTType(uast.Identifier{}, Obj{
135-
// "Name": Var("ident"),
136-
// }))},
137-
//)),
138-
139127
MapSemantic("NoopLine", uast.Comment{}, MapObj(
140128
Obj{"noop_line": CommentText([2]string{}, "comm")},
141129
CommentNode(false, "comm", nil),
@@ -149,7 +137,7 @@ var Normalizers = []Mapping{
149137
MapSemantic("arg", uast.Argument{}, MapObj(
150138
Obj{
151139
uast.KeyToken: Var("name"),
152-
"default": Var("init"),
140+
"default": Var("init"),
153141
},
154142
Obj{
155143
"Name": UASTType(uast.Identifier{}, Obj{
@@ -173,7 +161,7 @@ var Normalizers = []Mapping{
173161
MapSemantic("kwonly_arg", uast.Argument{}, MapObj(
174162
Obj{
175163
uast.KeyToken: Var("name"),
176-
"default": Var("init"),
164+
"default": Var("init"),
177165
},
178166
Obj{
179167
"Name": UASTType(uast.Identifier{}, Obj{
@@ -210,29 +198,42 @@ var Normalizers = []Mapping{
210198
funcDefMap("FunctionDef", false),
211199
funcDefMap("AsyncFunctionDef", true),
212200

201+
AnnotateType("Import", MapObj(
202+
Obj{
203+
"names": Each("vals", Var("name")),
204+
},
205+
Obj{
206+
"names": Each("vals", UASTType(uast.RuntimeImport{},
207+
Obj{
208+
"Path": Var("name"),
209+
})),
210+
},
211+
), role.Import, role.Declaration, role.Statement),
213212

214213
// FIXME: what to do with levels? convert to ../../... in Path?
215214
// FIXME: "import * from x": check the * and set "All" to true
215+
//MapSemantic("ImportFrom", uast.RuntimeImport{}, MapObj(
216+
// Obj{
217+
// "names": Check(Is(Arr(Any))),
218+
// "module": Var("module"),
219+
// },
220+
// Obj{
221+
// "All": Bool(true),
222+
// "Path": UASTType(uast.Identifier{}, Obj{
223+
// "Name": Var("module"),
224+
// }),
225+
// },
226+
//)),
216227
MapSemantic("ImportFrom", uast.RuntimeImport{}, MapObj(
217228
Obj{
218229
"names": Var("names"),
219230
"module": Var("module"),
220231
},
221232
Obj{
222233
"Names": Var("names"),
223-
"Path": UASTType(uast.String{}, Obj{
224-
"Value": Var("module"),
225-
"Format": String(""),
234+
"Path": UASTType(uast.Identifier{}, Obj{
235+
"Name": Var("module"),
226236
}),
227237
},
228238
)),
229-
230-
MapSemantic("Import", uast.RuntimeImport{}, MapObj(
231-
Obj{
232-
"names": Var("names"),
233-
},
234-
Obj{
235-
"Names": Var("names"),
236-
},
237-
)),
238239
}

fixtures/issue30.py.sem.uast

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,36 @@
33
'@pos': { '@type': "uast:Positions",
44
},
55
body: [
6-
{ '@type': "uast:RuntimeImport",
6+
{ '@type': "Import",
7+
'@token': "import",
8+
'@role': [Declaration, Import, Statement],
79
'@pos': { '@type': "uast:Positions",
810
start: { '@type': "uast:Position",
911
offset: 0,
1012
line: 1,
1113
col: 1,
1214
},
1315
},
14-
All: false,
15-
Names: [
16-
{ '@type': "uast:Alias",
17-
'@pos': { '@type': "uast:Positions",
18-
},
19-
Name: { '@type': "uast:Identifier",
20-
Name: "sys",
21-
},
22-
Node: { '@type': "uast:Identifier",
23-
Name: ~,
16+
names: { '@type': "ImportFrom.names",
17+
'@role': [Identifier, Import, Incomplete, Pathname],
18+
'name_list': [
19+
{ '@type': "uast:RuntimeImport",
20+
All: false,
21+
Names: ~,
22+
Path: { '@type': "uast:Alias",
23+
'@pos': { '@type': "uast:Positions",
24+
},
25+
Name: { '@type': "uast:Identifier",
26+
Name: "sys",
27+
},
28+
Node: { '@type': "uast:Identifier",
29+
Name: ~,
30+
},
31+
},
32+
Target: ~,
2433
},
25-
},
26-
],
27-
Path: ~,
28-
Target: ~,
34+
],
35+
},
2936
},
3037
{ '@type': "Expr",
3138
'@role': [Expression],

fixtures/issue62.py.sem.uast

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,59 +29,88 @@
2929
},
3030
},
3131
],
32-
Path: { '@type': "uast:String",
33-
Format: "",
34-
Value: "os",
32+
Path: { '@type': "uast:Identifier",
33+
Name: "os",
3534
},
3635
Target: ~,
3736
},
38-
{ '@type': "uast:RuntimeImport",
37+
{ '@type': "Import",
38+
'@token': "import",
39+
'@role': [Declaration, Import, Statement],
3940
'@pos': { '@type': "uast:Positions",
4041
start: { '@type': "uast:Position",
4142
offset: 20,
4243
line: 2,
4344
col: 1,
4445
},
4546
},
46-
All: false,
47-
Names: [
48-
{ '@type': "uast:Alias",
49-
'@pos': { '@type': "uast:Positions",
50-
},
51-
Name: { '@type': "uast:Identifier",
52-
Name: "sys",
47+
names: { '@type': "ImportFrom.names",
48+
'@role': [Identifier, Import, Incomplete, Pathname],
49+
'name_list': [
50+
{ '@type': "uast:RuntimeImport",
51+
All: false,
52+
Names: ~,
53+
Path: { '@type': "uast:Alias",
54+
'@pos': { '@type': "uast:Positions",
55+
},
56+
Name: { '@type': "uast:Identifier",
57+
Name: "sys",
58+
},
59+
Node: { '@type': "uast:Identifier",
60+
Name: ~,
61+
},
62+
},
63+
Target: ~,
5364
},
54-
Node: { '@type': "uast:Identifier",
55-
Name: ~,
56-
},
57-
},
58-
],
59-
Path: ~,
60-
Target: ~,
65+
],
66+
},
6167
},
62-
{ '@type': "uast:RuntimeImport",
68+
{ '@type': "Import",
69+
'@token': "import",
70+
'@role': [Declaration, Import, Statement],
6371
'@pos': { '@type': "uast:Positions",
6472
start: { '@type': "uast:Position",
6573
offset: 32,
6674
line: 4,
6775
col: 1,
6876
},
6977
},
70-
All: false,
71-
Names: [
72-
{ '@type': "uast:Alias",
73-
'@pos': { '@type': "uast:Positions",
78+
names: { '@type': "ImportFrom.names",
79+
'@role': [Identifier, Import, Incomplete, Pathname],
80+
'name_list': [
81+
{ '@type': "uast:RuntimeImport",
82+
All: false,
83+
Names: ~,
84+
Path: { '@type': "uast:Alias",
85+
'@pos': { '@type': "uast:Positions",
86+
},
87+
Name: { '@type': "uast:Identifier",
88+
Name: "numpy",
89+
},
90+
Node: { '@type': "uast:Identifier",
91+
Name: "np",
92+
},
93+
},
94+
Target: ~,
7495
},
75-
Name: { '@type': "uast:Identifier",
76-
Name: "numpy",
96+
],
97+
},
98+
'noops_previous': { '@type': "PreviousNoops",
99+
'@role': [Noop],
100+
'@pos': { '@type': "uast:Positions",
101+
start: { '@type': "uast:Position",
102+
offset: 31,
103+
line: 3,
104+
col: 1,
77105
},
78-
Node: { '@type': "uast:Identifier",
79-
Name: "np",
106+
end: { '@type': "uast:Position",
107+
offset: 31,
108+
line: 3,
109+
col: 1,
80110
},
81111
},
82-
],
83-
Path: ~,
84-
Target: ~,
112+
lines: [],
113+
},
85114
},
86115
],
87116
}

fixtures/issue62_b.py.sem.uast

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
},
3030
},
3131
],
32-
Path: { '@type': "uast:String",
33-
Format: "",
34-
Value: "collections",
32+
Path: { '@type': "uast:Identifier",
33+
Name: "collections",
3534
},
3635
Target: ~,
3736
},
@@ -56,9 +55,8 @@
5655
},
5756
},
5857
],
59-
Path: { '@type': "uast:String",
60-
Format: "",
61-
Value: "ast2vec.bblfsh_roles",
58+
Path: { '@type': "uast:Identifier",
59+
Name: "ast2vec.bblfsh_roles",
6260
},
6361
Target: ~,
6462
},
@@ -83,9 +81,8 @@
8381
},
8482
},
8583
],
86-
Path: { '@type': "uast:String",
87-
Format: "",
88-
Value: "ast2vec.repo2.base",
84+
Path: { '@type': "uast:Identifier",
85+
Name: "ast2vec.repo2.base",
8986
},
9087
Target: ~,
9188
},

0 commit comments

Comments
 (0)