first_in_statement.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {
  2. AST_Binary,
  3. AST_Conditional,
  4. AST_Dot,
  5. AST_Object,
  6. AST_Sequence,
  7. AST_Statement,
  8. AST_Sub,
  9. AST_UnaryPostfix,
  10. AST_PrefixedTemplateString
  11. } from "../ast.js";
  12. // return true if the node at the top of the stack (that means the
  13. // innermost node in the current output) is lexically the first in
  14. // a statement.
  15. function first_in_statement(stack) {
  16. let node = stack.parent(-1);
  17. for (let i = 0, p; p = stack.parent(i); i++) {
  18. if (p instanceof AST_Statement && p.body === node)
  19. return true;
  20. if ((p instanceof AST_Sequence && p.expressions[0] === node) ||
  21. (p.TYPE === "Call" && p.expression === node) ||
  22. (p instanceof AST_PrefixedTemplateString && p.prefix === node) ||
  23. (p instanceof AST_Dot && p.expression === node) ||
  24. (p instanceof AST_Sub && p.expression === node) ||
  25. (p instanceof AST_Conditional && p.condition === node) ||
  26. (p instanceof AST_Binary && p.left === node) ||
  27. (p instanceof AST_UnaryPostfix && p.expression === node)
  28. ) {
  29. node = p;
  30. } else {
  31. return false;
  32. }
  33. }
  34. }
  35. // Returns whether the leftmost item in the expression is an object
  36. function left_is_object(node) {
  37. if (node instanceof AST_Object) return true;
  38. if (node instanceof AST_Sequence) return left_is_object(node.expressions[0]);
  39. if (node.TYPE === "Call") return left_is_object(node.expression);
  40. if (node instanceof AST_PrefixedTemplateString) return left_is_object(node.prefix);
  41. if (node instanceof AST_Dot || node instanceof AST_Sub) return left_is_object(node.expression);
  42. if (node instanceof AST_Conditional) return left_is_object(node.condition);
  43. if (node instanceof AST_Binary) return left_is_object(node.left);
  44. if (node instanceof AST_UnaryPostfix) return left_is_object(node.expression);
  45. return false;
  46. }
  47. export { first_in_statement, left_is_object };