-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (34 loc) · 902 Bytes
/
index.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
import { tokTypes } from 'acorn';
function extractPath(parser) {
const pathStr = [];
do {
const { input, start, end } = parser;
pathStr.push(input.substring(start, end));
parser.next.call(parser);
} while (!parser.eat.call(parser, tokTypes.parenR));
return pathStr.join('');
}
function existentialPlugin(parser, options) {
parser.extend(
'parseMaybeAssign',
function(nextMethod) {
return function() {
const { input, start, end } = this;
const isTry = input.substring(start, end) === 'try';
if (isTry) {
const node = this.startNode();
this.next();
this.eat(tokTypes.parenL);
node.path = extractPath(this);
return this.finishNode(node, 'TryExpression');
} else {
return nextMethod.apply(this, arguments);
}
}
}
)
}
export default function(acorn, plugins) {
plugins.existential_operator = existentialPlugin;
return acorn;
}