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

Commit

Permalink
simplify relative import normalization; add more cases with full imports
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <[email protected]>
  • Loading branch information
Denys Smirnov authored and dennwc committed May 23, 2019
1 parent 6622d6d commit 765e9de
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 53 deletions.
2 changes: 0 additions & 2 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ var Normalizers = []Mapping{
// FIXME: no position for the module (path) in the native AST, only when the import starts
numLevel: Var("level"),
path: Var("module"),
joined: Var("joined"),
prefix: "../",
},
}),
Expand All @@ -468,7 +467,6 @@ var Normalizers = []Mapping{
"Name": OpPrependPath{
numLevel: Var("level"),
path: Var("module"),
joined: Var("joined"),
prefix: "../",
},
}),
Expand Down
12 changes: 1 addition & 11 deletions driver/normalizer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func num2dots(n nodes.Value, prefix string) nodes.Value {
type OpPrependPath struct {
numLevel Op
path Op
joined Op
prefix string
}

Expand Down Expand Up @@ -58,16 +57,7 @@ func (op OpPrependPath) Construct(st *State, n nodes.Node) (nodes.Node, error) {
if err != nil || path == nil {
return n, err
}

joined := string(prependVal.(nodes.String)) + string(path.(nodes.String))
op.joined = String(joined)

v, err := op.joined.Construct(st, n)
if err != nil {
return nil, err
}

return v, nil
return prependVal.(nodes.String) + path.(nodes.String), nil
}

type OpLevelDotsNumConv struct {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/u2_import_relativepath.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .a import b
from ..c import e
from ..c.d import e
from ... import f
4 changes: 1 addition & 3 deletions fixtures/u2_import_relativepath.py.native
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
{
'ast_type': "ImportFrom",
'col_offset': 1,
'end_col_offset': 9,
'end_lineno': 2,
level: 2,
lineno: 2,
module: "c",
module: "c.d",
names: [
{
asname: ~,
Expand Down
9 changes: 2 additions & 7 deletions fixtures/u2_import_relativepath.py.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
line: 2,
col: 1,
},
end: { '@type': "uast:Position",
offset: 25,
line: 2,
col: 9,
},
},
All: false,
Names: [
Expand All @@ -51,14 +46,14 @@
},
],
Path: { '@type': "uast:Identifier",
Name: "../../c",
Name: "../../c.d",
},
Target: ~,
},
{ '@type': "uast:RuntimeImport",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 35,
offset: 37,
line: 3,
col: 1,
},
Expand Down
9 changes: 2 additions & 7 deletions fixtures/u2_import_relativepath.py.uast
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,13 @@
line: 2,
col: 1,
},
end: { '@type': "uast:Position",
offset: 25,
line: 2,
col: 9,
},
},
level: { '@type': "ImportFrom.level",
'@token': "..",
'@role': [Import, Incomplete],
},
module: { '@type': "ImportFrom.module",
'@token': "c",
'@token': "c.d",
'@role': [Identifier, Import, Pathname],
},
names: { '@type': "ImportFrom.names",
Expand All @@ -85,7 +80,7 @@
'@role': [Declaration, Import, Statement],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 35,
offset: 37,
line: 3,
col: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion fixtures/u2_import_subsymbol_alias.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import x.y as z
from a import b as c
from a.b import c as d
8 changes: 3 additions & 5 deletions fixtures/u2_import_subsymbol_alias.py.native
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
{
'ast_type': "ImportFrom",
'col_offset': 1,
'end_col_offset': 7,
'end_lineno': 2,
level: 0,
lineno: 2,
module: "a",
module: "a.b",
names: [
{
asname: "c",
asname: "d",
'ast_type': "alias",
name: "b",
name: "c",
},
],
},
Expand Down
11 changes: 3 additions & 8 deletions fixtures/u2_import_subsymbol_alias.py.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,22 @@
line: 2,
col: 1,
},
end: { '@type': "uast:Position",
offset: 22,
line: 2,
col: 7,
},
},
All: false,
Names: [
{ '@type': "uast:Alias",
'@pos': { '@type': "uast:Positions",
},
Name: { '@type': "uast:Identifier",
Name: "c",
Name: "d",
},
Node: { '@type': "uast:Identifier",
Name: "b",
Name: "c",
},
},
],
Path: { '@type': "uast:Identifier",
Name: "a",
Name: "a.b",
},
Target: ~,
},
Expand Down
11 changes: 3 additions & 8 deletions fixtures/u2_import_subsymbol_alias.py.uast
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,25 @@
line: 2,
col: 1,
},
end: { '@type': "uast:Position",
offset: 22,
line: 2,
col: 7,
},
},
level: { '@type': "ImportFrom.level",
'@token': "",
'@role': [Import, Incomplete],
},
module: { '@type': "ImportFrom.module",
'@token': "a",
'@token': "a.b",
'@role': [Identifier, Import, Pathname],
},
names: { '@type': "ImportFrom.names",
'@role': [Identifier, Import, Incomplete, Pathname],
'name_list': [
{ '@type': "alias",
'@token': "b",
'@token': "c",
'@role': [Identifier, Import, Pathname],
'@pos': { '@type': "uast:Positions",
},
asname: { '@type': "alias.asname",
'@token': "c",
'@token': "d",
'@role': [Alias, Identifier, Import, Pathname],
},
},
Expand Down

0 comments on commit 765e9de

Please sign in to comment.