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

Commit

Permalink
split import paths
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <denys@sourced.tech>
  • Loading branch information
Denys Smirnov authored and dennwc committed May 23, 2019
1 parent eec17a9 commit 57cc13f
Show file tree
Hide file tree
Showing 32 changed files with 1,781 additions and 596 deletions.
45 changes: 43 additions & 2 deletions driver/normalizer/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var Normalizers = []Mapping{
Obj{
"Comment": Is(nil),
"Doc": Is(nil),
"Path": Var("path"),
"Path": pathSplit{Var("path")},
"Name": Cases("case",
// case 1: no alias for the import
Is(nil),
Expand Down Expand Up @@ -163,7 +163,7 @@ var Normalizers = []Mapping{
"Comment": Is(nil),
"Doc": Is(nil),
"Name": Var("alias"),
"Path": Var("path"),
"Path": pathSplit{Var("path")},
},
// ->
Obj{
Expand Down Expand Up @@ -489,3 +489,44 @@ func (op fieldSplit) Check(st *State, n nodes.Node) (bool, error) {
func (op fieldSplit) Construct(st *State, n nodes.Node) (nodes.Node, error) {
return st.MustGetVar(op.vr) // TODO: join nodes back on reverse
}

// pathSplit splits the Go imports path and constructs a QualifiedIdentifier from it.
type pathSplit struct {
path Op
}

func (pathSplit) Kinds() nodes.Kind {
return nodes.KindObject
}

func (op pathSplit) Check(st *State, n nodes.Node) (bool, error) {
obj, ok := n.(nodes.Object)
if !ok {
return false, ErrUnexpectedType.New(nodes.String(""), n)
}
var path uast.String
if err := uast.NodeAs(obj, &path); err != nil {
return false, err
}
var idents []uast.Identifier
for _, name := range strings.Split(path.Value, "/") {
idents = append(idents, uast.Identifier{Name: name})
}
var out interface{}
if len(idents) == 1 {
name := idents[0]
name.GenNode = path.GenNode
out = name
} else {
out = uast.QualifiedIdentifier{GenNode: path.GenNode, Names: idents}
}
nd, err := uast.ToNode(out)
if err != nil {
return false, err
}
return op.path.Check(st, nd)
}

func (op pathSplit) Construct(st *State, n nodes.Node) (nodes.Node, error) {
return op.path.Construct(st, n) // TODO: join nodes back on reverse
}
10 changes: 4 additions & 6 deletions fixtures/bench_accumulator_factory.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -2470,7 +2469,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -2483,8 +2482,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_binary_search.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -1452,7 +1451,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -1465,8 +1464,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_ethiopian_multiplication.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -1907,7 +1906,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -1920,8 +1919,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_fibonacci.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -685,7 +684,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -698,8 +697,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_fizzbuzz.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -1319,7 +1318,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -1332,8 +1331,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_gcd.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -3892,7 +3891,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -3905,8 +3904,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_happy_numbers.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -83,8 +83,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -2061,7 +2060,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -2074,8 +2073,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
10 changes: 4 additions & 6 deletions fixtures/bench_hundred_doors.go.sem.uast
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -156,8 +156,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down Expand Up @@ -1842,7 +1841,7 @@
},
All: true,
Names: [],
Path: { '@type': "uast:String",
Path: { '@type': "uast:Identifier",
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 22,
Expand All @@ -1855,8 +1854,7 @@
col: 13,
},
},
Format: "",
Value: "fmt",
Name: "fmt",
},
Target: ~,
},
Expand Down
Loading

0 comments on commit 57cc13f

Please sign in to comment.