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

Commit 765e9de

Browse files
Denys Smirnovdennwc
authored andcommitted
simplify relative import normalization; add more cases with full imports
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 6622d6d commit 765e9de

10 files changed

+17
-53
lines changed

driver/normalizer/normalizer.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@ var Normalizers = []Mapping{
444444
// FIXME: no position for the module (path) in the native AST, only when the import starts
445445
numLevel: Var("level"),
446446
path: Var("module"),
447-
joined: Var("joined"),
448447
prefix: "../",
449448
},
450449
}),
@@ -468,7 +467,6 @@ var Normalizers = []Mapping{
468467
"Name": OpPrependPath{
469468
numLevel: Var("level"),
470469
path: Var("module"),
471-
joined: Var("joined"),
472470
prefix: "../",
473471
},
474472
}),

driver/normalizer/util.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func num2dots(n nodes.Value, prefix string) nodes.Value {
1919
type OpPrependPath struct {
2020
numLevel Op
2121
path Op
22-
joined Op
2322
prefix string
2423
}
2524

@@ -58,16 +57,7 @@ func (op OpPrependPath) Construct(st *State, n nodes.Node) (nodes.Node, error) {
5857
if err != nil || path == nil {
5958
return n, err
6059
}
61-
62-
joined := string(prependVal.(nodes.String)) + string(path.(nodes.String))
63-
op.joined = String(joined)
64-
65-
v, err := op.joined.Construct(st, n)
66-
if err != nil {
67-
return nil, err
68-
}
69-
70-
return v, nil
60+
return prependVal.(nodes.String) + path.(nodes.String), nil
7161
}
7262

7363
type OpLevelDotsNumConv struct {

fixtures/u2_import_relativepath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .a import b
2-
from ..c import e
2+
from ..c.d import e
33
from ... import f

fixtures/u2_import_relativepath.py.native

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
{
2222
'ast_type': "ImportFrom",
2323
'col_offset': 1,
24-
'end_col_offset': 9,
25-
'end_lineno': 2,
2624
level: 2,
2725
lineno: 2,
28-
module: "c",
26+
module: "c.d",
2927
names: [
3028
{
3129
asname: ~,

fixtures/u2_import_relativepath.py.sem.uast

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
line: 2,
3737
col: 1,
3838
},
39-
end: { '@type': "uast:Position",
40-
offset: 25,
41-
line: 2,
42-
col: 9,
43-
},
4439
},
4540
All: false,
4641
Names: [
@@ -51,14 +46,14 @@
5146
},
5247
],
5348
Path: { '@type': "uast:Identifier",
54-
Name: "../../c",
49+
Name: "../../c.d",
5550
},
5651
Target: ~,
5752
},
5853
{ '@type': "uast:RuntimeImport",
5954
'@pos': { '@type': "uast:Positions",
6055
start: { '@type': "uast:Position",
61-
offset: 35,
56+
offset: 37,
6257
line: 3,
6358
col: 1,
6459
},

fixtures/u2_import_relativepath.py.uast

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,13 @@
5050
line: 2,
5151
col: 1,
5252
},
53-
end: { '@type': "uast:Position",
54-
offset: 25,
55-
line: 2,
56-
col: 9,
57-
},
5853
},
5954
level: { '@type': "ImportFrom.level",
6055
'@token': "..",
6156
'@role': [Import, Incomplete],
6257
},
6358
module: { '@type': "ImportFrom.module",
64-
'@token': "c",
59+
'@token': "c.d",
6560
'@role': [Identifier, Import, Pathname],
6661
},
6762
names: { '@type': "ImportFrom.names",
@@ -85,7 +80,7 @@
8580
'@role': [Declaration, Import, Statement],
8681
'@pos': { '@type': "uast:Positions",
8782
start: { '@type': "uast:Position",
88-
offset: 35,
83+
offset: 37,
8984
line: 3,
9085
col: 1,
9186
},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import x.y as z
2-
from a import b as c
2+
from a.b import c as d

fixtures/u2_import_subsymbol_alias.py.native

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
{
1818
'ast_type': "ImportFrom",
1919
'col_offset': 1,
20-
'end_col_offset': 7,
21-
'end_lineno': 2,
2220
level: 0,
2321
lineno: 2,
24-
module: "a",
22+
module: "a.b",
2523
names: [
2624
{
27-
asname: "c",
25+
asname: "d",
2826
'ast_type': "alias",
29-
name: "b",
27+
name: "c",
3028
},
3129
],
3230
},

fixtures/u2_import_subsymbol_alias.py.sem.uast

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,22 @@
3232
line: 2,
3333
col: 1,
3434
},
35-
end: { '@type': "uast:Position",
36-
offset: 22,
37-
line: 2,
38-
col: 7,
39-
},
4035
},
4136
All: false,
4237
Names: [
4338
{ '@type': "uast:Alias",
4439
'@pos': { '@type': "uast:Positions",
4540
},
4641
Name: { '@type': "uast:Identifier",
47-
Name: "c",
42+
Name: "d",
4843
},
4944
Node: { '@type': "uast:Identifier",
50-
Name: "b",
45+
Name: "c",
5146
},
5247
},
5348
],
5449
Path: { '@type': "uast:Identifier",
55-
Name: "a",
50+
Name: "a.b",
5651
},
5752
Target: ~,
5853
},

fixtures/u2_import_subsymbol_alias.py.uast

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,25 @@
3737
line: 2,
3838
col: 1,
3939
},
40-
end: { '@type': "uast:Position",
41-
offset: 22,
42-
line: 2,
43-
col: 7,
44-
},
4540
},
4641
level: { '@type': "ImportFrom.level",
4742
'@token': "",
4843
'@role': [Import, Incomplete],
4944
},
5045
module: { '@type': "ImportFrom.module",
51-
'@token': "a",
46+
'@token': "a.b",
5247
'@role': [Identifier, Import, Pathname],
5348
},
5449
names: { '@type': "ImportFrom.names",
5550
'@role': [Identifier, Import, Incomplete, Pathname],
5651
'name_list': [
5752
{ '@type': "alias",
58-
'@token': "b",
53+
'@token': "c",
5954
'@role': [Identifier, Import, Pathname],
6055
'@pos': { '@type': "uast:Positions",
6156
},
6257
asname: { '@type': "alias.asname",
63-
'@token': "c",
58+
'@token': "d",
6459
'@role': [Alias, Identifier, Import, Pathname],
6560
},
6661
},

0 commit comments

Comments
 (0)