-
Notifications
You must be signed in to change notification settings - Fork 11
/
ast-helper.js
45 lines (40 loc) · 1.1 KB
/
ast-helper.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
'use strict';
const R = require('ramda');
// :: Node -> String
const getName = R.ifElse(
R.propEq('type', 'MemberExpression'),
R.path(['property', 'name']),
R.prop('name')
);
// :: String -> Node -> Boolean
const isRamdaMethod = name => R.either(
R.whereEq({
type: 'Identifier',
name
}),
R.where({
type: R.equals('MemberExpression'),
object: R.whereEq({ type: 'Identifier', name: 'R' }),
property: R.either(
R.whereEq({ type: 'Identifier', name }),
R.whereEq({ type: 'Literal', value: name })
)
})
);
// :: { name :: String, arguments :: [Node] -> Boolean }
// -> Object
// -> Boolean
const isCalling = pattern => R.where({
type: R.equals('CallExpression'),
callee: isRamdaMethod(pattern.name),
arguments: pattern.arguments || R.T
});
// :: Node -> Boolean
const isBooleanLiteral = R.both(
R.propEq('type', 'Literal'),
R.propSatisfies(R.is(Boolean), 'value')
);
exports.isRamdaMethod = isRamdaMethod;
exports.isCalling = isCalling;
exports.getName = getName;
exports.isBooleanLiteral = isBooleanLiteral;