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

Commit eec78c8

Browse files
committed
driver: ToNode configuration
1 parent 5c1136f commit eec78c8

File tree

8 files changed

+76
-930
lines changed

8 files changed

+76
-930
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.yarnrc
2+
.yarn
23
.cache
34
.sdk
5+
.config
46
build

ANNOTATION.md

-3
This file was deleted.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# javascript-driver ![Driver Status](https://img.shields.io/badge/status-planning-e08dd1.svg) [![Build Status](https://travis-ci.org/bblfsh/javascript-driver.svg?branch=master)](https://travis-ci.org/bblfsh/javascript-driver) ![Native Version](https://img.shields.io/badge/javascript%20version-6.10.3--r1-aa93ea.svg) ![Go Version](https://img.shields.io/badge/go%20version-1.8-63afbf.svg)
1+
# javascript-driver ![Driver Status](https://img.shields.io/badge/status-alpha-db975c.svg) [![Build Status](https://travis-ci.org/bblfsh/javascript-driver.svg?branch=master)](https://travis-ci.org/bblfsh/javascript-driver) ![Native Version](https://img.shields.io/badge/javascript%20version-6.10.3--r1-aa93ea.svg) ![Go Version](https://img.shields.io/badge/go%20version-1.8-63afbf.svg)
22

3-
javascript driver for [babelfish](https://github.com/bblfsh/server).
3+
JavaScript driver for [babelfish](https://github.com/bblfsh/bblfshd).
44

55

66
Development Environment

driver/normalizer/tonode.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package normalizer
22

3-
import "gopkg.in/bblfsh/sdk.v1/uast"
3+
import (
4+
"gopkg.in/bblfsh/sdk.v1/uast"
5+
)
46

57
// ToNode is an instance of `uast.ObjectToNode`, defining how to transform an
68
// into a UAST (`uast.Node`).
@@ -15,4 +17,34 @@ var ToNode = &uast.ObjectToNode{
1517
EndLineKey: "loc.end.line",
1618
ColumnKey: "loc.start.column",
1719
EndColumnKey: "loc.end.column",
20+
TokenKeys: map[string]bool{
21+
"value": true,
22+
"name": true,
23+
},
24+
// Several maps with properties are returned by the AST, we should use only
25+
// those with the key "type"
26+
IsNode: func(v map[string]interface{}) bool {
27+
_, ok := v["type"].(string)
28+
return ok
29+
},
30+
// The column value from babylon are based on 0, so we always sum one.
31+
Modifier: func(n map[string]interface{}) error {
32+
loc, ok := n["loc"].(map[string]interface{})
33+
if !ok {
34+
return nil
35+
}
36+
37+
for _, key := range []string{"start", "end"} {
38+
p, ok := loc[key].(map[string]interface{})
39+
if !ok {
40+
continue
41+
}
42+
43+
if c, ok := p["column"].(float64); ok {
44+
p["column"] = c + 1
45+
}
46+
}
47+
48+
return nil
49+
},
1850
}

driver/normalizer/tonode_test.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12-
func TestNativeToNoder(t *testing.T) {
12+
func TestNativeToNode(t *testing.T) {
1313
require := require.New(t)
1414

1515
f, err := getFixture("hello.js.json")
@@ -19,6 +19,19 @@ func TestNativeToNoder(t *testing.T) {
1919
require.NotNil(n)
2020
}
2121

22+
func TestNativeToNodeOffset(t *testing.T) {
23+
require := require.New(t)
24+
25+
f, err := getFixture("hello.js.json")
26+
require.NoError(err)
27+
n, err := ToNode.ToNode(f)
28+
require.NoError(err)
29+
require.NotNil(n)
30+
31+
require.Equal(int(n.StartPosition.Col), 1)
32+
require.Equal(int(n.EndPosition.Col), 1)
33+
}
34+
2235
const fixtureDir = "fixtures"
2336

2437
func getFixture(name string) (interface{}, error) {

fixtures/hello.js.native

-209
Original file line numberDiff line numberDiff line change
@@ -137,215 +137,6 @@
137137
"type": "Program"
138138
},
139139
"start": 0,
140-
"tokens": [
141-
{
142-
"end": 7,
143-
"loc": {
144-
"end": {
145-
"column": 7,
146-
"line": 1
147-
},
148-
"start": {
149-
"column": 0,
150-
"line": 1
151-
}
152-
},
153-
"start": 0,
154-
"type": {
155-
"beforeExpr": false,
156-
"binop": null,
157-
"isAssign": false,
158-
"isLoop": false,
159-
"label": "name",
160-
"postfix": false,
161-
"prefix": false,
162-
"rightAssociative": false,
163-
"startsExpr": true
164-
},
165-
"value": "console"
166-
},
167-
{
168-
"end": 8,
169-
"loc": {
170-
"end": {
171-
"column": 8,
172-
"line": 1
173-
},
174-
"start": {
175-
"column": 7,
176-
"line": 1
177-
}
178-
},
179-
"start": 7,
180-
"type": {
181-
"beforeExpr": false,
182-
"binop": null,
183-
"isAssign": false,
184-
"isLoop": false,
185-
"label": ".",
186-
"postfix": false,
187-
"prefix": false,
188-
"rightAssociative": false,
189-
"startsExpr": false,
190-
"updateContext": null
191-
}
192-
},
193-
{
194-
"end": 11,
195-
"loc": {
196-
"end": {
197-
"column": 11,
198-
"line": 1
199-
},
200-
"start": {
201-
"column": 8,
202-
"line": 1
203-
}
204-
},
205-
"start": 8,
206-
"type": {
207-
"beforeExpr": false,
208-
"binop": null,
209-
"isAssign": false,
210-
"isLoop": false,
211-
"label": "name",
212-
"postfix": false,
213-
"prefix": false,
214-
"rightAssociative": false,
215-
"startsExpr": true
216-
},
217-
"value": "log"
218-
},
219-
{
220-
"end": 12,
221-
"loc": {
222-
"end": {
223-
"column": 12,
224-
"line": 1
225-
},
226-
"start": {
227-
"column": 11,
228-
"line": 1
229-
}
230-
},
231-
"start": 11,
232-
"type": {
233-
"beforeExpr": true,
234-
"binop": null,
235-
"isAssign": false,
236-
"isLoop": false,
237-
"label": "(",
238-
"postfix": false,
239-
"prefix": false,
240-
"rightAssociative": false,
241-
"startsExpr": true
242-
}
243-
},
244-
{
245-
"end": 25,
246-
"loc": {
247-
"end": {
248-
"column": 25,
249-
"line": 1
250-
},
251-
"start": {
252-
"column": 12,
253-
"line": 1
254-
}
255-
},
256-
"start": 12,
257-
"type": {
258-
"beforeExpr": false,
259-
"binop": null,
260-
"isAssign": false,
261-
"isLoop": false,
262-
"label": "string",
263-
"postfix": false,
264-
"prefix": false,
265-
"rightAssociative": false,
266-
"startsExpr": true,
267-
"updateContext": null
268-
},
269-
"value": "Hello World"
270-
},
271-
{
272-
"end": 26,
273-
"loc": {
274-
"end": {
275-
"column": 26,
276-
"line": 1
277-
},
278-
"start": {
279-
"column": 25,
280-
"line": 1
281-
}
282-
},
283-
"start": 25,
284-
"type": {
285-
"beforeExpr": false,
286-
"binop": null,
287-
"isAssign": false,
288-
"isLoop": false,
289-
"label": ")",
290-
"postfix": false,
291-
"prefix": false,
292-
"rightAssociative": false,
293-
"startsExpr": false
294-
}
295-
},
296-
{
297-
"end": 27,
298-
"loc": {
299-
"end": {
300-
"column": 27,
301-
"line": 1
302-
},
303-
"start": {
304-
"column": 26,
305-
"line": 1
306-
}
307-
},
308-
"start": 26,
309-
"type": {
310-
"beforeExpr": true,
311-
"binop": null,
312-
"isAssign": false,
313-
"isLoop": false,
314-
"label": ";",
315-
"postfix": false,
316-
"prefix": false,
317-
"rightAssociative": false,
318-
"startsExpr": false,
319-
"updateContext": null
320-
}
321-
},
322-
{
323-
"end": 28,
324-
"loc": {
325-
"end": {
326-
"column": 0,
327-
"line": 2
328-
},
329-
"start": {
330-
"column": 0,
331-
"line": 2
332-
}
333-
},
334-
"start": 28,
335-
"type": {
336-
"beforeExpr": false,
337-
"binop": null,
338-
"isAssign": false,
339-
"isLoop": false,
340-
"label": "eof",
341-
"postfix": false,
342-
"prefix": false,
343-
"rightAssociative": false,
344-
"startsExpr": false,
345-
"updateContext": null
346-
}
347-
}
348-
],
349140
"type": "File"
350141
}
351142
}

0 commit comments

Comments
 (0)