-
Notifications
You must be signed in to change notification settings - Fork 6
/
to-be-skipped.js
62 lines (49 loc) · 2.59 KB
/
to-be-skipped.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
53
54
55
56
57
58
59
60
61
62
'use strict';
const { Syntax } = require('estraverse');
const supportedNodeTypes = require('./supported-node-types');
const isLeftHandSideOfAssignment = (parentNode, currentKey) => {
// Do not instrument left due to 'Invalid left-hand side in assignment'
return parentNode.type === Syntax.AssignmentExpression && currentKey === 'left';
};
const isChildOfObjectLiteral = (parentNode) => {
return parentNode.type === Syntax.Property && parentNode.kind === 'init';
};
const isObjectLiteralKey = (parentNode, currentKey) => {
return isChildOfObjectLiteral(parentNode) && currentKey === 'key';
};
const isObjectLiteralValue = (parentNode, currentKey) => {
return isChildOfObjectLiteral(parentNode) && currentKey === 'value';
};
const isNonComputedObjectLiteralKey = (parentNode, currentKey) => {
// Do not instrument non-computed Object literal key
return isObjectLiteralKey(parentNode, currentKey) && !parentNode.computed;
};
const isShorthandedValueOfObjectLiteral = (parentNode, currentKey) => {
// Do not instrument shorthanded Object literal value
return isObjectLiteralValue(parentNode, currentKey) && parentNode.shorthand;
};
const isUpdateExpression = (parentNode) => {
// Just wrap UpdateExpression, not digging in.
return parentNode.type === Syntax.UpdateExpression;
};
const isCallExpressionWithNonComputedMemberExpression = (currentNode, parentNode, currentKey) => {
// Do not instrument non-computed property of MemberExpression within CallExpression.
return currentNode.type === Syntax.Identifier && parentNode.type === Syntax.MemberExpression && !parentNode.computed && currentKey === 'property';
};
const isTypeOfOrDeleteUnaryExpression = (currentNode, parentNode, currentKey) => {
// 'typeof Identifier' or 'delete Identifier' is not instrumented
return currentNode.type === Syntax.Identifier && parentNode.type === Syntax.UnaryExpression && (parentNode.operator === 'typeof' || parentNode.operator === 'delete') && currentKey === 'argument';
};
const isSupportedNodeType = (node) => {
return supportedNodeTypes.indexOf(node.type) !== -1;
};
const toBeSkipped = ({ currentNode, parentNode, currentKey }) => {
return !isSupportedNodeType(currentNode) ||
isLeftHandSideOfAssignment(parentNode, currentKey) ||
isNonComputedObjectLiteralKey(parentNode, currentKey) ||
isShorthandedValueOfObjectLiteral(parentNode, currentKey) ||
isUpdateExpression(parentNode) ||
isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentKey) ||
isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentKey);
};
module.exports = toBeSkipped;