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

Commit e6bf9a6

Browse files
Denys Smirnovdennwc
authored andcommitted
properly convert imports
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 6bf447d commit e6bf9a6

15 files changed

+329
-785
lines changed

driver/fixtures/fixtures_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var Suite = &fixtures.Suite{
2828
"BoolLiteral",
2929
"Bytes",
3030
"FunctionDef",
31+
"Import",
3132
"ImportFrom",
3233
"Name",
3334
"NoopLine",

driver/normalizer/normalizer.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,37 @@ var Normalizers = []Mapping{
359359
funcDefMap("FunctionDef", false),
360360
funcDefMap("AsyncFunctionDef", true),
361361

362-
AnnotateType("Import", MapObj(
362+
// import statements may have multiple paths
363+
// if there is only one path, we emit RuntimeImport directly
364+
MapSemantic("Import", uast.RuntimeImport{}, MapObj(
365+
Fields{
366+
// FIXME: change this once we've a way to store other nodes on semantic objects
367+
// See: https://github.com/bblfsh/sdk/issues/361
368+
// See: https://github.com/bblfsh/python-driver/issues/178
369+
{Name: "noops_previous", Drop: true, Op: Any()},
370+
{Name: "noops_sameline", Drop: true, Op: Any()},
371+
{Name: "names", Op: One(Var("name"))},
372+
},
363373
Obj{
364-
"names": Each("vals", Var("name")),
374+
"Path": Var("name"),
375+
},
376+
)),
377+
// for grouped statement, we emit a Group with multiple RuntimeImports
378+
MapSemantic("Import", uast.Group{}, MapObj(
379+
Fields{
380+
// FIXME: change this once we've a way to store other nodes on semantic objects
381+
// See: https://github.com/bblfsh/sdk/issues/361
382+
// See: https://github.com/bblfsh/python-driver/issues/178
383+
{Name: "noops_previous", Drop: true, Op: Any()},
384+
{Name: "noops_sameline", Drop: true, Op: Any()},
385+
{Name: "names", Op: Each("vals", Var("name"))},
365386
},
366387
Obj{
367-
"names": Each("vals", UASTType(uast.RuntimeImport{},
388+
"Nodes": Each("vals", UASTType(uast.RuntimeImport{},
368389
Obj{"Path": Var("name")},
369390
)),
370391
},
371-
), role.Import, role.Declaration, role.Statement),
392+
)),
372393

373394
// pre-process aliases: remove ones with no alias (useless)
374395
Map(Obj{

fixtures/bench_javaobs.py.sem.uast

Lines changed: 38 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -79,167 +79,90 @@
7979
},
8080
},
8181
},
82-
{ '@type': "python:Import",
83-
'@token': "import",
84-
'@role': [Declaration, Import, Statement],
82+
{ '@type': "uast:RuntimeImport",
8583
'@pos': { '@type': "uast:Positions",
8684
start: { '@type': "uast:Position",
8785
offset: 1206,
8886
line: 37,
8987
col: 1,
9088
},
9189
},
92-
names: { '@type': "python:ImportFrom.names",
93-
'@role': [Identifier, Import, Incomplete, Pathname],
94-
'name_list': [
95-
{ '@type': "uast:RuntimeImport",
96-
All: false,
97-
Names: ~,
98-
Path: { '@type': "uast:Identifier",
99-
'@pos': { '@type': "uast:Positions",
100-
},
101-
Name: "collections",
102-
},
103-
Target: ~,
104-
},
105-
],
106-
},
107-
'noops_previous': { '@type': "python:PreviousNoops",
108-
'@role': [Noop],
90+
All: false,
91+
Names: ~,
92+
Path: { '@type': "uast:Identifier",
10993
'@pos': { '@type': "uast:Positions",
110-
start: { '@type': "uast:Position",
111-
offset: 1186,
112-
line: 35,
113-
col: 1,
114-
},
115-
end: { '@type': "uast:Position",
116-
offset: 1204,
117-
line: 36,
118-
col: 18,
119-
},
12094
},
121-
lines: [
122-
{ '@type': "uast:Comment",
123-
'@role': [Noop],
124-
'@pos': { '@type': "uast:Positions",
125-
start: { '@type': "uast:Position",
126-
offset: 1187,
127-
line: 36,
128-
col: 1,
129-
},
130-
},
131-
Block: false,
132-
Prefix: " ",
133-
Suffix: "\n",
134-
Tab: "",
135-
Text: "Standard library",
136-
},
137-
],
95+
Name: "collections",
13896
},
97+
Target: ~,
13998
},
140-
{ '@type': "python:Import",
141-
'@token': "import",
142-
'@role': [Declaration, Import, Statement],
99+
{ '@type': "uast:RuntimeImport",
143100
'@pos': { '@type': "uast:Positions",
144101
start: { '@type': "uast:Position",
145102
offset: 1225,
146103
line: 38,
147104
col: 1,
148105
},
149106
},
150-
names: { '@type': "python:ImportFrom.names",
151-
'@role': [Identifier, Import, Incomplete, Pathname],
152-
'name_list': [
153-
{ '@type': "uast:RuntimeImport",
154-
All: false,
155-
Names: ~,
156-
Path: { '@type': "uast:Identifier",
157-
'@pos': { '@type': "uast:Positions",
158-
},
159-
Name: "logging",
160-
},
161-
Target: ~,
162-
},
163-
],
107+
All: false,
108+
Names: ~,
109+
Path: { '@type': "uast:Identifier",
110+
'@pos': { '@type': "uast:Positions",
111+
},
112+
Name: "logging",
164113
},
114+
Target: ~,
165115
},
166-
{ '@type': "python:Import",
167-
'@token': "import",
168-
'@role': [Declaration, Import, Statement],
116+
{ '@type': "uast:RuntimeImport",
169117
'@pos': { '@type': "uast:Positions",
170118
start: { '@type': "uast:Position",
171119
offset: 1240,
172120
line: 39,
173121
col: 1,
174122
},
175123
},
176-
names: { '@type': "python:ImportFrom.names",
177-
'@role': [Identifier, Import, Incomplete, Pathname],
178-
'name_list': [
179-
{ '@type': "uast:RuntimeImport",
180-
All: false,
181-
Names: ~,
182-
Path: { '@type': "uast:Identifier",
183-
'@pos': { '@type': "uast:Positions",
184-
},
185-
Name: "os",
186-
},
187-
Target: ~,
188-
},
189-
],
124+
All: false,
125+
Names: ~,
126+
Path: { '@type': "uast:Identifier",
127+
'@pos': { '@type': "uast:Positions",
128+
},
129+
Name: "os",
190130
},
131+
Target: ~,
191132
},
192-
{ '@type': "python:Import",
193-
'@token': "import",
194-
'@role': [Declaration, Import, Statement],
133+
{ '@type': "uast:RuntimeImport",
195134
'@pos': { '@type': "uast:Positions",
196135
start: { '@type': "uast:Position",
197136
offset: 1250,
198137
line: 40,
199138
col: 1,
200139
},
201140
},
202-
names: { '@type': "python:ImportFrom.names",
203-
'@role': [Identifier, Import, Incomplete, Pathname],
204-
'name_list': [
205-
{ '@type': "uast:RuntimeImport",
206-
All: false,
207-
Names: ~,
208-
Path: { '@type': "uast:Identifier",
209-
'@pos': { '@type': "uast:Positions",
210-
},
211-
Name: "struct",
212-
},
213-
Target: ~,
214-
},
215-
],
141+
All: false,
142+
Names: ~,
143+
Path: { '@type': "uast:Identifier",
144+
'@pos': { '@type': "uast:Positions",
145+
},
146+
Name: "struct",
216147
},
148+
Target: ~,
217149
},
218-
{ '@type': "python:Import",
219-
'@token': "import",
220-
'@role': [Declaration, Import, Statement],
150+
{ '@type': "uast:RuntimeImport",
221151
'@pos': { '@type': "uast:Positions",
222152
start: { '@type': "uast:Position",
223153
offset: 1264,
224154
line: 41,
225155
col: 1,
226156
},
227157
},
228-
names: { '@type': "python:ImportFrom.names",
229-
'@role': [Identifier, Import, Incomplete, Pathname],
230-
'name_list': [
231-
{ '@type': "uast:RuntimeImport",
232-
All: false,
233-
Names: ~,
234-
Path: { '@type': "uast:Identifier",
235-
'@pos': { '@type': "uast:Positions",
236-
},
237-
Name: "sys",
238-
},
239-
Target: ~,
240-
},
241-
],
158+
All: false,
159+
Names: ~,
160+
Path: { '@type': "uast:Identifier",
161+
'@pos': { '@type': "uast:Positions",
162+
},
163+
Name: "sys",
242164
},
165+
Target: ~,
243166
},
244167
{ '@type': "python:Try",
245168
'@token': "try",

fixtures/bench_pangram.py.sem.uast

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,36 @@
33
'@pos': { '@type': "uast:Positions",
44
},
55
body: [
6-
{ '@type': "python:Import",
7-
'@token': "import",
8-
'@role': [Declaration, Import, Statement],
6+
{ '@type': "uast:Group",
97
'@pos': { '@type': "uast:Positions",
108
start: { '@type': "uast:Position",
119
offset: 0,
1210
line: 1,
1311
col: 1,
1412
},
1513
},
16-
names: { '@type': "python:ImportFrom.names",
17-
'@role': [Identifier, Import, Incomplete, Pathname],
18-
'name_list': [
19-
{ '@type': "uast:RuntimeImport",
20-
All: false,
21-
Names: ~,
22-
Path: { '@type': "uast:Identifier",
23-
'@pos': { '@type': "uast:Positions",
24-
},
25-
Name: "string",
14+
Nodes: [
15+
{ '@type': "uast:RuntimeImport",
16+
All: false,
17+
Names: ~,
18+
Path: { '@type': "uast:Identifier",
19+
'@pos': { '@type': "uast:Positions",
2620
},
27-
Target: ~,
21+
Name: "string",
2822
},
29-
{ '@type': "uast:RuntimeImport",
30-
All: false,
31-
Names: ~,
32-
Path: { '@type': "uast:Identifier",
33-
'@pos': { '@type': "uast:Positions",
34-
},
35-
Name: "sys",
23+
Target: ~,
24+
},
25+
{ '@type': "uast:RuntimeImport",
26+
All: false,
27+
Names: ~,
28+
Path: { '@type': "uast:Identifier",
29+
'@pos': { '@type': "uast:Positions",
3630
},
37-
Target: ~,
31+
Name: "sys",
3832
},
39-
],
40-
},
33+
Target: ~,
34+
},
35+
],
4136
},
4237
{ '@type': "python:If",
4338
'@token': "if",

fixtures/issue30.py.sem.uast

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,22 @@
33
'@pos': { '@type': "uast:Positions",
44
},
55
body: [
6-
{ '@type': "python:Import",
7-
'@token': "import",
8-
'@role': [Declaration, Import, Statement],
6+
{ '@type': "uast:RuntimeImport",
97
'@pos': { '@type': "uast:Positions",
108
start: { '@type': "uast:Position",
119
offset: 0,
1210
line: 1,
1311
col: 1,
1412
},
1513
},
16-
names: { '@type': "python:ImportFrom.names",
17-
'@role': [Identifier, Import, Incomplete, Pathname],
18-
'name_list': [
19-
{ '@type': "uast:RuntimeImport",
20-
All: false,
21-
Names: ~,
22-
Path: { '@type': "uast:Identifier",
23-
'@pos': { '@type': "uast:Positions",
24-
},
25-
Name: "sys",
26-
},
27-
Target: ~,
28-
},
29-
],
14+
All: false,
15+
Names: ~,
16+
Path: { '@type': "uast:Identifier",
17+
'@pos': { '@type': "uast:Positions",
18+
},
19+
Name: "sys",
3020
},
21+
Target: ~,
3122
},
3223
{ '@type': "python:Expr",
3324
'@role': [Expression],

0 commit comments

Comments
 (0)