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

Commit 6bf447d

Browse files
Denys Smirnovdennwc
authored andcommitted
remove some noise from imports
Signed-off-by: Denys Smirnov <[email protected]>
1 parent cd4ffca commit 6bf447d

15 files changed

+126
-275
lines changed

driver/normalizer/normalizer.go

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

33
import (
44
"github.com/bblfsh/sdk/v3/uast"
5+
"github.com/bblfsh/sdk/v3/uast/nodes"
56
"github.com/bblfsh/sdk/v3/uast/role"
67
. "github.com/bblfsh/sdk/v3/uast/transformer"
78
"github.com/bblfsh/sdk/v3/uast/transformer/positioner"
@@ -364,50 +365,47 @@ var Normalizers = []Mapping{
364365
},
365366
Obj{
366367
"names": Each("vals", UASTType(uast.RuntimeImport{},
367-
Obj{
368-
"Path": Var("name"),
369-
})),
368+
Obj{"Path": Var("name")},
369+
)),
370370
},
371371
), role.Import, role.Declaration, role.Statement),
372372

373+
// pre-process aliases: remove ones with no alias (useless)
374+
Map(Obj{
375+
uast.KeyType: String("alias"),
376+
uast.KeyPos: Any(),
377+
"name": Check(OfKind(nodes.KindString), Var("name")),
378+
"asname": Is(nil),
379+
}, UASTType(uast.Identifier{}, Obj{
380+
uast.KeyPos: UASTType(uast.Positions{}, nil),
381+
"Name": Var("name"),
382+
})),
383+
373384
// FIXME: aliases doesn't have a position (can't be currently fixed by the tokenizer
374-
// because they don't even have a line in the native AST)
385+
// because they don't even have a line in the native AST)
375386
MapSemantic("alias", uast.Alias{}, MapObj(
376387
Obj{
377-
"name": Var("name"),
378-
"asname": Cases("case_alias",
379-
Check(Is(nil), Var("nilalias")),
380-
Check(Not(Is(nil)), Var("alias")),
388+
"name": Var("name"),
389+
"asname": Var("alias"),
390+
},
391+
Obj{
392+
"Name": UASTType(uast.Identifier{}, Obj{
393+
"Name": Var("name"),
394+
}),
395+
"Node": UASTType(uast.Identifier{},
396+
Obj{"Name": Var("alias")},
381397
),
382398
},
383-
CasesObj("case_alias",
384-
Obj{
385-
"Name": UASTType(uast.Identifier{}, Obj{
386-
"Name": Var("name"),
387-
}),
388-
},
389-
Objs{
390-
{"Node": Obj{}},
391-
{
392-
"Node": UASTType(uast.Identifier{},
393-
Obj{
394-
"Name": Var("alias"),
395-
}),
396-
}},
397-
))),
399+
)),
398400

399401
// Star imports
400402
MapSemantic("ImportFrom", uast.RuntimeImport{}, MapObj(
401403
Fields{
402404
{Name: "names", Op: Arr(
403405
Obj{
404-
uast.KeyType: String("uast:Alias"),
405-
uast.KeyPos: Var("pos"),
406-
"Name": Obj{
407-
uast.KeyType: String("uast:Identifier"),
408-
"Name": String("*"),
409-
},
410-
"Node": Obj{},
406+
uast.KeyType: String("uast:Identifier"),
407+
uast.KeyPos: Any(),
408+
"Name": String("*"),
411409
},
412410
)},
413411
{Name: "level", Op: Var("level")},

fixtures/bench_ethopian_multiplication.py.sem.uast

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,15 @@
6868
},
6969
All: false,
7070
Names: [
71-
{ '@type': "uast:Alias",
71+
{ '@type': "uast:Identifier",
7272
'@pos': { '@type': "uast:Positions",
7373
},
74-
Name: { '@type': "uast:Identifier",
75-
Name: "izip",
76-
},
77-
Node: {},
74+
Name: "izip",
7875
},
79-
{ '@type': "uast:Alias",
76+
{ '@type': "uast:Identifier",
8077
'@pos': { '@type': "uast:Positions",
8178
},
82-
Name: { '@type': "uast:Identifier",
83-
Name: "takewhile",
84-
},
85-
Node: {},
79+
Name: "takewhile",
8680
},
8781
],
8882
Path: { '@type': "uast:Identifier",

fixtures/bench_javaobs.py.sem.uast

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,10 @@
9595
{ '@type': "uast:RuntimeImport",
9696
All: false,
9797
Names: ~,
98-
Path: { '@type': "uast:Alias",
98+
Path: { '@type': "uast:Identifier",
9999
'@pos': { '@type': "uast:Positions",
100100
},
101-
Name: { '@type': "uast:Identifier",
102-
Name: "collections",
103-
},
104-
Node: {},
101+
Name: "collections",
105102
},
106103
Target: ~,
107104
},
@@ -156,13 +153,10 @@
156153
{ '@type': "uast:RuntimeImport",
157154
All: false,
158155
Names: ~,
159-
Path: { '@type': "uast:Alias",
156+
Path: { '@type': "uast:Identifier",
160157
'@pos': { '@type': "uast:Positions",
161158
},
162-
Name: { '@type': "uast:Identifier",
163-
Name: "logging",
164-
},
165-
Node: {},
159+
Name: "logging",
166160
},
167161
Target: ~,
168162
},
@@ -185,13 +179,10 @@
185179
{ '@type': "uast:RuntimeImport",
186180
All: false,
187181
Names: ~,
188-
Path: { '@type': "uast:Alias",
182+
Path: { '@type': "uast:Identifier",
189183
'@pos': { '@type': "uast:Positions",
190184
},
191-
Name: { '@type': "uast:Identifier",
192-
Name: "os",
193-
},
194-
Node: {},
185+
Name: "os",
195186
},
196187
Target: ~,
197188
},
@@ -214,13 +205,10 @@
214205
{ '@type': "uast:RuntimeImport",
215206
All: false,
216207
Names: ~,
217-
Path: { '@type': "uast:Alias",
208+
Path: { '@type': "uast:Identifier",
218209
'@pos': { '@type': "uast:Positions",
219210
},
220-
Name: { '@type': "uast:Identifier",
221-
Name: "struct",
222-
},
223-
Node: {},
211+
Name: "struct",
224212
},
225213
Target: ~,
226214
},
@@ -243,13 +231,10 @@
243231
{ '@type': "uast:RuntimeImport",
244232
All: false,
245233
Names: ~,
246-
Path: { '@type': "uast:Alias",
234+
Path: { '@type': "uast:Identifier",
247235
'@pos': { '@type': "uast:Positions",
248236
},
249-
Name: { '@type': "uast:Identifier",
250-
Name: "sys",
251-
},
252-
Node: {},
237+
Name: "sys",
253238
},
254239
Target: ~,
255240
},
@@ -342,13 +327,10 @@
342327
},
343328
All: false,
344329
Names: [
345-
{ '@type': "uast:Alias",
330+
{ '@type': "uast:Identifier",
346331
'@pos': { '@type': "uast:Positions",
347332
},
348-
Name: { '@type': "uast:Identifier",
349-
Name: "BytesIO",
350-
},
351-
Node: {},
333+
Name: "BytesIO",
352334
},
353335
],
354336
Path: { '@type': "uast:Identifier",

fixtures/bench_pangram.py.sem.uast

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,20 @@
1919
{ '@type': "uast:RuntimeImport",
2020
All: false,
2121
Names: ~,
22-
Path: { '@type': "uast:Alias",
22+
Path: { '@type': "uast:Identifier",
2323
'@pos': { '@type': "uast:Positions",
2424
},
25-
Name: { '@type': "uast:Identifier",
26-
Name: "string",
27-
},
28-
Node: {},
25+
Name: "string",
2926
},
3027
Target: ~,
3128
},
3229
{ '@type': "uast:RuntimeImport",
3330
All: false,
3431
Names: ~,
35-
Path: { '@type': "uast:Alias",
32+
Path: { '@type': "uast:Identifier",
3633
'@pos': { '@type': "uast:Positions",
3734
},
38-
Name: { '@type': "uast:Identifier",
39-
Name: "sys",
40-
},
41-
Node: {},
35+
Name: "sys",
4236
},
4337
Target: ~,
4438
},

fixtures/issue30.py.sem.uast

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
{ '@type': "uast:RuntimeImport",
2020
All: false,
2121
Names: ~,
22-
Path: { '@type': "uast:Alias",
22+
Path: { '@type': "uast:Identifier",
2323
'@pos': { '@type': "uast:Positions",
2424
},
25-
Name: { '@type': "uast:Identifier",
26-
Name: "sys",
27-
},
28-
Node: {},
25+
Name: "sys",
2926
},
3027
Target: ~,
3128
},

fixtures/issue62.py.sem.uast

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
},
1919
All: false,
2020
Names: [
21-
{ '@type': "uast:Alias",
21+
{ '@type': "uast:Identifier",
2222
'@pos': { '@type': "uast:Positions",
2323
},
24-
Name: { '@type': "uast:Identifier",
25-
Name: "path",
26-
},
27-
Node: {},
24+
Name: "path",
2825
},
2926
],
3027
Path: { '@type': "uast:Identifier",
@@ -48,13 +45,10 @@
4845
{ '@type': "uast:RuntimeImport",
4946
All: false,
5047
Names: ~,
51-
Path: { '@type': "uast:Alias",
48+
Path: { '@type': "uast:Identifier",
5249
'@pos': { '@type': "uast:Positions",
5350
},
54-
Name: { '@type': "uast:Identifier",
55-
Name: "sys",
56-
},
57-
Node: {},
51+
Name: "sys",
5852
},
5953
Target: ~,
6054
},

fixtures/issue62_b.py.sem.uast

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
},
1919
All: false,
2020
Names: [
21-
{ '@type': "uast:Alias",
21+
{ '@type': "uast:Identifier",
2222
'@pos': { '@type': "uast:Positions",
2323
},
24-
Name: { '@type': "uast:Identifier",
25-
Name: "Counter",
26-
},
27-
Node: {},
24+
Name: "Counter",
2825
},
2926
],
3027
Path: { '@type': "uast:Identifier",
@@ -42,13 +39,10 @@
4239
},
4340
All: false,
4441
Names: [
45-
{ '@type': "uast:Alias",
42+
{ '@type': "uast:Identifier",
4643
'@pos': { '@type': "uast:Positions",
4744
},
48-
Name: { '@type': "uast:Identifier",
49-
Name: "SIMPLE_IDENTIFIER",
50-
},
51-
Node: {},
45+
Name: "SIMPLE_IDENTIFIER",
5246
},
5347
],
5448
Path: { '@type': "uast:Identifier",
@@ -66,13 +60,10 @@
6660
},
6761
All: false,
6862
Names: [
69-
{ '@type': "uast:Alias",
63+
{ '@type': "uast:Identifier",
7064
'@pos': { '@type': "uast:Positions",
7165
},
72-
Name: { '@type': "uast:Identifier",
73-
Name: "Repo2Base",
74-
},
75-
Node: {},
66+
Name: "Repo2Base",
7667
},
7768
],
7869
Path: { '@type': "uast:Identifier",

fixtures/issue94.py.sem.uast

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
{ '@type': "uast:RuntimeImport",
2020
All: false,
2121
Names: ~,
22-
Path: { '@type': "uast:Alias",
22+
Path: { '@type': "uast:Identifier",
2323
'@pos': { '@type': "uast:Positions",
2424
},
25-
Name: { '@type': "uast:Identifier",
26-
Name: "lib1",
27-
},
28-
Node: {},
25+
Name: "lib1",
2926
},
3027
Target: ~,
3128
},
@@ -48,13 +45,10 @@
4845
{ '@type': "uast:RuntimeImport",
4946
All: false,
5047
Names: ~,
51-
Path: { '@type': "uast:Alias",
48+
Path: { '@type': "uast:Identifier",
5249
'@pos': { '@type': "uast:Positions",
5350
},
54-
Name: { '@type': "uast:Identifier",
55-
Name: "lib2.lib21",
56-
},
57-
Node: {},
51+
Name: "lib2.lib21",
5852
},
5953
Target: ~,
6054
},
@@ -107,13 +101,10 @@
107101
},
108102
All: false,
109103
Names: [
110-
{ '@type': "uast:Alias",
104+
{ '@type': "uast:Identifier",
111105
'@pos': { '@type': "uast:Positions",
112106
},
113-
Name: { '@type': "uast:Identifier",
114-
Name: "lib41",
115-
},
116-
Node: {},
107+
Name: "lib41",
117108
},
118109
],
119110
Path: { '@type': "uast:Identifier",
@@ -131,13 +122,10 @@
131122
},
132123
All: false,
133124
Names: [
134-
{ '@type': "uast:Alias",
125+
{ '@type': "uast:Identifier",
135126
'@pos': { '@type': "uast:Positions",
136127
},
137-
Name: { '@type': "uast:Identifier",
138-
Name: "lib511",
139-
},
140-
Node: {},
128+
Name: "lib511",
141129
},
142130
],
143131
Path: { '@type': "uast:Identifier",

0 commit comments

Comments
 (0)