to-be-skipped.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. var estraverse = require('estraverse');
  3. var syntax = estraverse.Syntax;
  4. var supportedNodeTypes = require('./supported-node-types');
  5. function isLeftHandSideOfAssignment(parentNode, currentKey) {
  6. // Do not instrument left due to 'Invalid left-hand side in assignment'
  7. return parentNode.type === syntax.AssignmentExpression && currentKey === 'left';
  8. }
  9. function isChildOfObjectLiteral (parentNode) {
  10. return parentNode.type === syntax.Property && parentNode.kind === 'init';
  11. }
  12. function isObjectLiteralKey (parentNode, currentKey) {
  13. return isChildOfObjectLiteral(parentNode) && currentKey === 'key';
  14. }
  15. function isObjectLiteralValue (parentNode, currentKey) {
  16. return isChildOfObjectLiteral(parentNode) && currentKey === 'value';
  17. }
  18. function isNonComputedObjectLiteralKey(parentNode, currentKey) {
  19. // Do not instrument non-computed Object literal key
  20. return isObjectLiteralKey(parentNode, currentKey) && !parentNode.computed;
  21. }
  22. function isShorthandedValueOfObjectLiteral(parentNode, currentKey) {
  23. // Do not instrument shorthanded Object literal value
  24. return isObjectLiteralValue(parentNode, currentKey) && parentNode.shorthand;
  25. }
  26. function isUpdateExpression(parentNode) {
  27. // Just wrap UpdateExpression, not digging in.
  28. return parentNode.type === syntax.UpdateExpression;
  29. }
  30. function isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentKey) {
  31. // Do not instrument non-computed property of MemberExpression within CallExpression.
  32. return currentNode.type === syntax.Identifier && parentNode.type === syntax.MemberExpression && !parentNode.computed && currentKey === 'property';
  33. }
  34. function isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentKey) {
  35. // 'typeof Identifier' or 'delete Identifier' is not instrumented
  36. return currentNode.type === syntax.Identifier && parentNode.type === syntax.UnaryExpression && (parentNode.operator === 'typeof' || parentNode.operator === 'delete') && currentKey === 'argument';
  37. }
  38. function isSupportedNodeType (node) {
  39. return supportedNodeTypes.indexOf(node.type) !== -1;
  40. }
  41. module.exports = function toBeSkipped (currentNode, parentNode, currentKey) {
  42. return !isSupportedNodeType(currentNode) ||
  43. isLeftHandSideOfAssignment(parentNode, currentKey) ||
  44. isNonComputedObjectLiteralKey(parentNode, currentKey) ||
  45. isShorthandedValueOfObjectLiteral(parentNode, currentKey) ||
  46. isUpdateExpression(parentNode) ||
  47. isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentKey) ||
  48. isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentKey);
  49. };