-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.js
50 lines (43 loc) · 1.41 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const assert = require('assert').strict;
const prune = require('./support/prune');
const parse = require('../lib/parse');
describe('parse', () => {
it('should get a node', () => {
const { ast } = parse('a.b.c');
assert.deepEqual(prune(ast), {
type: 'root',
value: '',
nodes: [
{ type: 'ident', value: 'a' },
{ type: 'separator', value: '.' },
{ type: 'ident', value: 'b' },
{ type: 'separator', value: '.' },
{ type: 'ident', value: 'c' }
],
output: 'a.b.c'
});
});
it('should get "loc" from node', () => {
const { ast } = parse('a.b.c');
assert.deepEqual(prune(ast.nodes[0].loc), {
start: { index: 0, line: 1, col: 0 },
end: { index: 1, line: 1, col: 1 }
});
});
it('should get "range" from node.loc', () => {
const { ast } = parse('a.b.c');
assert.deepEqual(ast.nodes[0].loc.range, [0, 1]);
assert.deepEqual(ast.nodes[1].loc.range, [1, 2]);
assert.deepEqual(ast.nodes[2].loc.range, [2, 3]);
});
it('should "slice" a range the given input', () => {
const input = 'a.b.c';
const { ast } = parse(input);
assert.equal(ast.nodes[0].loc.slice(input), 'a');
assert.equal(ast.nodes[1].loc.slice(input), '.');
assert.equal(ast.nodes[2].loc.slice(input), 'b');
assert.equal(ast.nodes[3].loc.slice(input), '.');
assert.equal(ast.nodes[4].loc.slice(input), 'c');
});
});