context.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.types = exports.TokContext = void 0;
  6. var _types = require("./types");
  7. var _whitespace = require("../util/whitespace");
  8. class TokContext {
  9. constructor(token, isExpr, preserveSpace, override) {
  10. this.token = token;
  11. this.isExpr = !!isExpr;
  12. this.preserveSpace = !!preserveSpace;
  13. this.override = override;
  14. }
  15. }
  16. exports.TokContext = TokContext;
  17. const types = {
  18. braceStatement: new TokContext("{", false),
  19. braceExpression: new TokContext("{", true),
  20. templateQuasi: new TokContext("${", false),
  21. parenStatement: new TokContext("(", false),
  22. parenExpression: new TokContext("(", true),
  23. template: new TokContext("`", true, true, p => p.readTmplToken()),
  24. functionExpression: new TokContext("function", true),
  25. functionStatement: new TokContext("function", false)
  26. };
  27. exports.types = types;
  28. _types.types.parenR.updateContext = _types.types.braceR.updateContext = function () {
  29. if (this.state.context.length === 1) {
  30. this.state.exprAllowed = true;
  31. return;
  32. }
  33. let out = this.state.context.pop();
  34. if (out === types.braceStatement && this.curContext().token === "function") {
  35. out = this.state.context.pop();
  36. }
  37. this.state.exprAllowed = !out.isExpr;
  38. };
  39. _types.types.name.updateContext = function (prevType) {
  40. let allowed = false;
  41. if (prevType !== _types.types.dot) {
  42. if (this.state.value === "of" && !this.state.exprAllowed || this.state.value === "yield" && this.scope.inGenerator) {
  43. allowed = true;
  44. }
  45. }
  46. this.state.exprAllowed = allowed;
  47. if (this.state.isIterator) {
  48. this.state.isIterator = false;
  49. }
  50. };
  51. _types.types.braceL.updateContext = function (prevType) {
  52. this.state.context.push(this.braceIsBlock(prevType) ? types.braceStatement : types.braceExpression);
  53. this.state.exprAllowed = true;
  54. };
  55. _types.types.dollarBraceL.updateContext = function () {
  56. this.state.context.push(types.templateQuasi);
  57. this.state.exprAllowed = true;
  58. };
  59. _types.types.parenL.updateContext = function (prevType) {
  60. const statementParens = prevType === _types.types._if || prevType === _types.types._for || prevType === _types.types._with || prevType === _types.types._while;
  61. this.state.context.push(statementParens ? types.parenStatement : types.parenExpression);
  62. this.state.exprAllowed = true;
  63. };
  64. _types.types.incDec.updateContext = function () {};
  65. _types.types._function.updateContext = _types.types._class.updateContext = function (prevType) {
  66. if (prevType.beforeExpr && prevType !== _types.types.semi && prevType !== _types.types._else && !(prevType === _types.types._return && _whitespace.lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) && !((prevType === _types.types.colon || prevType === _types.types.braceL) && this.curContext() === types.b_stat)) {
  67. this.state.context.push(types.functionExpression);
  68. } else {
  69. this.state.context.push(types.functionStatement);
  70. }
  71. this.state.exprAllowed = false;
  72. };
  73. _types.types.backQuote.updateContext = function () {
  74. if (this.curContext() === types.template) {
  75. this.state.context.pop();
  76. } else {
  77. this.state.context.push(types.template);
  78. }
  79. this.state.exprAllowed = false;
  80. };