to-be-captured.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var estraverse = require('estraverse');
  3. var syntax = estraverse.Syntax;
  4. var caputuringTargetTypes = [
  5. // syntax.Property,
  6. syntax.ObjectExpression,
  7. syntax.ArrayExpression,
  8. // syntax.ConditionalExpression,
  9. syntax.Identifier,
  10. syntax.MemberExpression,
  11. syntax.CallExpression,
  12. syntax.UnaryExpression,
  13. syntax.BinaryExpression,
  14. syntax.LogicalExpression,
  15. syntax.AssignmentExpression,
  16. syntax.NewExpression,
  17. syntax.UpdateExpression,
  18. syntax.YieldExpression,
  19. syntax.AwaitExpression,
  20. syntax.TemplateLiteral,
  21. syntax.TaggedTemplateExpression
  22. ];
  23. function isCaputuringTargetType (currentNode) {
  24. return caputuringTargetTypes.indexOf(currentNode.type) !== -1;
  25. }
  26. function isCalleeOfParent(parentNode, currentKey) {
  27. return (parentNode.type === syntax.CallExpression || parentNode.type === syntax.NewExpression) && currentKey === 'callee';
  28. }
  29. function isChildOfTaggedTemplateExpression(parentNode) {
  30. return parentNode.type === syntax.TaggedTemplateExpression;
  31. }
  32. function isYieldOrAwaitArgument(parentNode, currentKey) {
  33. // capture the yielded/await result, not the promise
  34. return (parentNode.type === syntax.YieldExpression || parentNode.type === syntax.AwaitExpression) && currentKey === 'argument';
  35. }
  36. module.exports = function toBeCaptured (currentNode, parentNode, currentKey) {
  37. return isCaputuringTargetType(currentNode) &&
  38. !isYieldOrAwaitArgument(parentNode, currentKey) &&
  39. !isCalleeOfParent(parentNode, currentKey) &&
  40. !isChildOfTaggedTemplateExpression(parentNode);
  41. };