Skip to content

Commit a923381

Browse files
committed
Add new syntax support to walker.
1 parent 1a62362 commit a923381

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var test = require('tape')
22
var acorn = require('../')
3+
var walk = require('../walk')
34
var baseAcorn = require('acorn')
45

56
test('parses object spread syntax', function (t) {
@@ -85,3 +86,20 @@ test('supports dynamic import() with sourceType: module', function (t) {
8586
})
8687
t.end()
8788
})
89+
90+
test('walk supports plugin syntax', function (t) {
91+
var ast = acorn.parse(
92+
'async function* a() { try { await import(xyz); } catch { for await (x of null) {} } yield import.meta.url }',
93+
{ sourceType: 'module' }
94+
)
95+
t.plan(2)
96+
walk.simple(ast, {
97+
Import () {
98+
t.pass('import()')
99+
},
100+
MetaProperty () {
101+
t.pass('import.meta')
102+
}
103+
})
104+
t.end()
105+
})

walk.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,63 @@
1+
var xtend = require('xtend')
12
var walk = require('acorn/dist/walk')
23

3-
module.exports = walk
4+
var base = xtend(walk.base, {
5+
Import: function () {},
6+
// pending https://github.com/acornjs/acorn/pull/705
7+
CatchClause: function (node, st, c) {
8+
if (node.param) c(node.param, st, 'Pattern')
9+
c(node.body, st, 'ScopeBody')
10+
}
11+
})
12+
13+
function simple (node, visitors, baseVisitor, state, override) {
14+
return walk.simple(node, visitors, baseVisitor || base, state, override)
15+
}
16+
17+
function ancestor (node, visitors, baseVisitor, state) {
18+
return walk.ancestor(node, visitors, baseVisitor || base, state)
19+
}
20+
21+
function recursive (node, state, funcs, baseVisitor, override) {
22+
return walk.recursive(node, state, funcs, baseVisitor || base, override)
23+
}
24+
25+
function full (node, callback, baseVisitor, state, override) {
26+
return walk.full(node, callback, baseVisitor || base, state, override)
27+
}
28+
29+
function fullAncestor (node, callback, baseVisitor, state) {
30+
return walk.fullAncestor(node, callback, baseVisitor || base, state)
31+
}
32+
33+
function findNodeAt (node, start, end, test, baseVisitor, state) {
34+
return walk.findNodeAt(node, start, end, test, baseVisitor || base, state)
35+
}
36+
37+
function findNodeAround (node, pos, test, baseVisitor, state) {
38+
return walk.findNodeAround(node, pos, test, baseVisitor || base, state)
39+
}
40+
41+
function findNodeAfter (node, pos, test, baseVisitor, state) {
42+
return walk.findNodeAfter(node, pos, test, baseVisitor || base, state)
43+
}
44+
45+
function findNodeBefore (node, pos, test, baseVisitor, state) {
46+
return walk.findNodeBefore(node, pos, test, baseVisitor || base, state)
47+
}
48+
49+
function make (funcs, baseVisitor) {
50+
return walk.make(funcs, baseVisitor || base)
51+
}
52+
53+
exports.simple = simple
54+
exports.ancestor = ancestor
55+
exports.recursive = recursive
56+
exports.full = full
57+
exports.fullAncestor = fullAncestor
58+
exports.findNodeAt = findNodeAt
59+
exports.findNodeAround = findNodeAround
60+
exports.findNodeAfter = findNodeAfter
61+
exports.findNodeBefore = findNodeBefore
62+
exports.make = make
63+
exports.base = base

0 commit comments

Comments
 (0)