-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathacorn-walk.node.txt
62 lines (47 loc) · 3.44 KB
/
acorn-walk.node.txt
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
51
52
53
54
55
56
57
58
59
60
61
62
┏━━━━━━━━━━━━━━━━┓
┃ ACORN-WALK ┃
┗━━━━━━━━━━━━━━━━┛
ALTERNATIVES ==> # - acorn-walk (preferred with acorn)
# - eslint-visitor-keys (preferred with espree)
VERSION ==> #See Acorn
AST FORMAT ==> #ESTree
NODE.type #Include abstract types: 'Expression', 'Statement', 'Pattern', 'Function'
#Add: 'ForInit' (first part of "for in|of")
#Do not include: 'ExportSpecifier', 'ClassBody', 'SwitchCase'
┌────────────────┐
│ TRAVERSING │
└────────────────┘
WALK.simple(NODE, OBJ) #Walk through AST tree and call OBJ.TYPE(CHILD_NODE) for each CHILD_NODE whose type is TYPE.
#Start with leaves, end with root.
WALK.ancestors(NODE, OBJ) #Same but instead call OBJ.TYPE(CHILD_NODE, ANCESTOR_NODE_ARR)
#ANCESTOR_NODE_ARR starts with ancestor and ends with CHILD_NODE.
WALK.full[Ancestors](NODE, FUNC) #Same as two previous except using FUNC(CHILD_NODE, ...) for any CHILD_NODE
WALK.recursive(NODE) #Same except does not trigger any callbacks. Only useful when BASE_OBJ is overridden.
┌─────────────┐
│ FINDING │
└─────────────┘
WALK.findNodeAt #Returns { node NODE2 } with first NODE2 where:
(NODE[, START_OFFSET[, END_OFFSET # - NODE2.start === START_OFFSET
[,'TYP'|FUNC('TYP',NODE)->BOOL]]])# - NODE2.end === END_OFFSET
[->OBJ] # - 'TYPE'|FUNC()->true
#OFFSET is file-wise (not line-wise).
#Returns undefined if none found
WALK.findNodeAround(NODE, OFFSET,
['TYPE'|FUNC('TYPE', NODE)->BOOL])
->OBJ #Same with NODE2.start <= OFFSET <= NODE.end
WALK.findNodeAfter(NODE, OFFSET,
['TYPE'|FUNC('TYPE', NODE)->BOOL])
->OBJ #Same with NODE2.start >= OFFSET
WALK.findNodeBefore(NODE, OFFSET,
['TYPE'|FUNC('TYPE', NODE)->BOOL])
->OBJ #Same with OFFSET <= NODE.end
┌───────────────────┐
│ CUSTOMIZATION │
└───────────────────┘
WALK.*(..., STATE_OBJ) #Every function can pass a STATE_OBJ that is passed as last argument to callbacks
WALK.*(..., BASE_OBJ) #Every function can pass a BASE_OBJ to customize the walker.
#BASE_OBJ is { TYPE(NODE, STATE_OBJ, FUNC), ... }
# - must call FUNC(NODE, STATE_OBJ[, 'TYPE']) on children
# - 'TYPE' defaults to NODE.type
WALK.base #Default BASE_OBJ
make(OBJ)->OBJ #Same as { ...WALK.base, ...OBJ }