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

Fix parsing ast with array #213

Merged
merged 2 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions uast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ func (c *ObjectToNode) toNodes(obj interface{}) ([]*Node, error) {

// This property -> List elements will be added as the current node Children
children, err := c.sliceToNodeSlice(k, ov)
// List of non-nodes
if ErrUnexpectedObject.Is(err) {
err = c.addProperty(n, k, ov)
}
if err != nil {
return nil, err
}
Expand Down
21 changes: 21 additions & 0 deletions uast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,27 @@ func TestOnToNode(t *testing.T) {
require.Len(n.Children, 18)
}

func TestToNodeWithArray(t *testing.T) {
require := require.New(t)

ast := map[string]interface{}{}
astJSON := `{"flags": ["a", "b"], "kind": "VariableDeclarationList"}`
err := json.Unmarshal([]byte(astJSON), &ast)
require.NoError(err)

c := &ObjectToNode{
TopLevelIsRootNode: true,
InternalTypeKey: "kind",
}

n, err := c.ToNode(ast)
require.NoError(err)
require.NotNil(n)
require.Equal(n.InternalType, "VariableDeclarationList")
require.Len(n.Properties, 1)
require.Equal(n.Properties["flags"], "[a b]")
}

func findChildWithInternalType(n *Node, internalType string) *Node {
for _, child := range n.Children {
if child.InternalType == internalType {
Expand Down