-
Notifications
You must be signed in to change notification settings - Fork 6
/
to-be-captured.js
52 lines (45 loc) · 1.69 KB
/
to-be-captured.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
51
52
'use strict';
const { Syntax } = require('estraverse');
const { getParentNode, getCurrentKey } = require('../controller-utils');
const caputuringTargetTypes = [
// Syntax.Property,
Syntax.ObjectExpression,
Syntax.ArrayExpression,
// Syntax.ConditionalExpression,
Syntax.Identifier,
Syntax.MemberExpression,
Syntax.CallExpression,
Syntax.UnaryExpression,
Syntax.BinaryExpression,
Syntax.LogicalExpression,
Syntax.AssignmentExpression,
Syntax.NewExpression,
Syntax.UpdateExpression,
Syntax.YieldExpression,
Syntax.AwaitExpression,
Syntax.TemplateLiteral,
Syntax.TaggedTemplateExpression
];
const isCaputuringTargetType = (currentNode) => {
return caputuringTargetTypes.indexOf(currentNode.type) !== -1;
};
const isCalleeOfParent = (parentNode, currentKey) => {
return (parentNode.type === Syntax.CallExpression || parentNode.type === Syntax.NewExpression) && currentKey === 'callee';
};
const isChildOfTaggedTemplateExpression = (parentNode) => {
return parentNode.type === Syntax.TaggedTemplateExpression;
};
const isYieldOrAwaitArgument = (parentNode, currentKey) => {
// capture the yielded/await result, not the promise
return (parentNode.type === Syntax.YieldExpression || parentNode.type === Syntax.AwaitExpression) && currentKey === 'argument';
};
const toBeCaptured = (controller) => {
const currentNode = controller.current();
const parentNode = getParentNode(controller);
const currentKey = getCurrentKey(controller);
return isCaputuringTargetType(currentNode) &&
!isYieldOrAwaitArgument(parentNode, currentKey) &&
!isCalleeOfParent(parentNode, currentKey) &&
!isChildOfTaggedTemplateExpression(parentNode);
};
module.exports = toBeCaptured;