context.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.types = exports.TokContext = undefined;
  6. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  7. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  8. var _types = require("./types");
  9. var _whitespace = require("../util/whitespace");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. // The algorithm used to determine whether a regexp can appear at a
  12. // given point in the program is loosely based on sweet.js' approach.
  13. // See https://github.com/mozilla/sweet.js/wiki/design
  14. var TokContext = exports.TokContext = function TokContext(token, isExpr, preserveSpace, override) {
  15. (0, _classCallCheck3.default)(this, TokContext);
  16. this.token = token;
  17. this.isExpr = !!isExpr;
  18. this.preserveSpace = !!preserveSpace;
  19. this.override = override;
  20. };
  21. var types = exports.types = {
  22. braceStatement: new TokContext("{", false),
  23. braceExpression: new TokContext("{", true),
  24. templateQuasi: new TokContext("${", true),
  25. parenStatement: new TokContext("(", false),
  26. parenExpression: new TokContext("(", true),
  27. template: new TokContext("`", true, true, function (p) {
  28. return p.readTmplToken();
  29. }),
  30. functionExpression: new TokContext("function", true)
  31. };
  32. // Token-specific context update code
  33. _types.types.parenR.updateContext = _types.types.braceR.updateContext = function () {
  34. if (this.state.context.length === 1) {
  35. this.state.exprAllowed = true;
  36. return;
  37. }
  38. var out = this.state.context.pop();
  39. if (out === types.braceStatement && this.curContext() === types.functionExpression) {
  40. this.state.context.pop();
  41. this.state.exprAllowed = false;
  42. } else if (out === types.templateQuasi) {
  43. this.state.exprAllowed = true;
  44. } else {
  45. this.state.exprAllowed = !out.isExpr;
  46. }
  47. };
  48. _types.types.name.updateContext = function (prevType) {
  49. this.state.exprAllowed = false;
  50. if (prevType === _types.types._let || prevType === _types.types._const || prevType === _types.types._var) {
  51. if (_whitespace.lineBreak.test(this.input.slice(this.state.end))) {
  52. this.state.exprAllowed = true;
  53. }
  54. }
  55. };
  56. _types.types.braceL.updateContext = function (prevType) {
  57. this.state.context.push(this.braceIsBlock(prevType) ? types.braceStatement : types.braceExpression);
  58. this.state.exprAllowed = true;
  59. };
  60. _types.types.dollarBraceL.updateContext = function () {
  61. this.state.context.push(types.templateQuasi);
  62. this.state.exprAllowed = true;
  63. };
  64. _types.types.parenL.updateContext = function (prevType) {
  65. var statementParens = prevType === _types.types._if || prevType === _types.types._for || prevType === _types.types._with || prevType === _types.types._while;
  66. this.state.context.push(statementParens ? types.parenStatement : types.parenExpression);
  67. this.state.exprAllowed = true;
  68. };
  69. _types.types.incDec.updateContext = function () {
  70. // tokExprAllowed stays unchanged
  71. };
  72. _types.types._function.updateContext = function () {
  73. if (this.curContext() !== types.braceStatement) {
  74. this.state.context.push(types.functionExpression);
  75. }
  76. this.state.exprAllowed = false;
  77. };
  78. _types.types.backQuote.updateContext = function () {
  79. if (this.curContext() === types.template) {
  80. this.state.context.pop();
  81. } else {
  82. this.state.context.push(types.template);
  83. }
  84. this.state.exprAllowed = false;
  85. };