index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Generated by `npm run build`, do not edit! */
  2. "use strict"
  3. var acorn = require("acorn")
  4. if (false) {
  5. throw new Error(("acorn-private-class-elements requires acorn@^6.1.0, not " + (acorn.version)))
  6. }
  7. var tt = acorn.tokTypes
  8. var TokenType = acorn.TokenType
  9. module.exports = function(Parser) {
  10. // Only load this plugin once.
  11. if (Parser.prototype.parsePrivateName) {
  12. return Parser
  13. }
  14. // Make sure `Parser` comes from the same acorn as our `tt`,
  15. // otherwise the comparisons fail.
  16. var cur = Parser
  17. while (cur && cur !== acorn.Parser) {
  18. cur = cur.__proto__
  19. }
  20. if (cur !== acorn.Parser) {
  21. throw new Error("acorn-private-class-elements does not support mixing different acorn copies")
  22. }
  23. Parser = /*@__PURE__*/(function (Parser) {
  24. function Parser_ () {
  25. Parser.apply(this, arguments);
  26. }
  27. if ( Parser ) Parser_.__proto__ = Parser;
  28. Parser_.prototype = Object.create( Parser && Parser.prototype );
  29. Parser_.prototype.constructor = Parser_;
  30. Parser_.prototype._branch = function _branch () {
  31. this.__branch = this.__branch || new Parser({ecmaVersion: this.options.ecmaVersion}, this.input)
  32. this.__branch.end = this.end
  33. this.__branch.pos = this.pos
  34. this.__branch.type = this.type
  35. this.__branch.value = this.value
  36. this.__branch.containsEsc = this.containsEsc
  37. return this.__branch
  38. };
  39. Parser_.prototype.parsePrivateClassElementName = function parsePrivateClassElementName (element) {
  40. element.computed = false
  41. element.key = this.parsePrivateName()
  42. if (element.key.name == "constructor") { this.raise(element.key.start, "Classes may not have a private element named constructor") }
  43. var accept = {get: "set", set: "get"}[element.kind]
  44. var privateBoundNames = this._privateBoundNamesStack[this._privateBoundNamesStack.length - 1]
  45. if (Object.prototype.hasOwnProperty.call(privateBoundNames, element.key.name) && privateBoundNames[element.key.name] !== accept) {
  46. this.raise(element.start, "Duplicate private element")
  47. }
  48. privateBoundNames[element.key.name] = element.kind || true
  49. delete this._unresolvedPrivateNamesStack[this._unresolvedPrivateNamesStack.length - 1][element.key.name]
  50. return element.key
  51. };
  52. Parser_.prototype.parsePrivateName = function parsePrivateName () {
  53. var node = this.startNode()
  54. node.name = this.value
  55. this.next()
  56. this.finishNode(node, "PrivateName")
  57. if (this.options.allowReserved == "never") { this.checkUnreserved(node) }
  58. return node
  59. };
  60. // Parse # token
  61. Parser_.prototype.getTokenFromCode = function getTokenFromCode (code) {
  62. if (code === 35) {
  63. ++this.pos
  64. var word = this.readWord1()
  65. return this.finishToken(this.privateNameToken, word)
  66. }
  67. return Parser.prototype.getTokenFromCode.call(this, code)
  68. };
  69. // Manage stacks and check for undeclared private names
  70. Parser_.prototype.parseClass = function parseClass (node, isStatement) {
  71. this._privateBoundNamesStack = this._privateBoundNamesStack || []
  72. var privateBoundNames = Object.create(this._privateBoundNamesStack[this._privateBoundNamesStack.length - 1] || null)
  73. this._privateBoundNamesStack.push(privateBoundNames)
  74. this._unresolvedPrivateNamesStack = this._unresolvedPrivateNamesStack || []
  75. var unresolvedPrivateNames = Object.create(null)
  76. this._unresolvedPrivateNamesStack.push(unresolvedPrivateNames)
  77. var _return = Parser.prototype.parseClass.call(this, node, isStatement)
  78. this._privateBoundNamesStack.pop()
  79. this._unresolvedPrivateNamesStack.pop()
  80. if (!this._unresolvedPrivateNamesStack.length) {
  81. var names = Object.keys(unresolvedPrivateNames)
  82. if (names.length) {
  83. names.sort(function (n1, n2) { return unresolvedPrivateNames[n1] - unresolvedPrivateNames[n2]; })
  84. this.raise(unresolvedPrivateNames[names[0]], "Usage of undeclared private name")
  85. }
  86. } else { Object.assign(this._unresolvedPrivateNamesStack[this._unresolvedPrivateNamesStack.length - 1], unresolvedPrivateNames) }
  87. return _return
  88. };
  89. // Parse private element access
  90. Parser_.prototype.parseSubscript = function parseSubscript (base, startPos, startLoc, noCalls, maybeAsyncArrow) {
  91. if (!this.eat(tt.dot)) {
  92. return Parser.prototype.parseSubscript.call(this, base, startPos, startLoc, noCalls, maybeAsyncArrow)
  93. }
  94. var node = this.startNodeAt(startPos, startLoc)
  95. node.object = base
  96. node.computed = false
  97. if (this.type == this.privateNameToken) {
  98. node.property = this.parsePrivateName()
  99. if (!this._privateBoundNamesStack.length || !this._privateBoundNamesStack[this._privateBoundNamesStack.length - 1][node.property.name]) {
  100. this._unresolvedPrivateNamesStack[this._unresolvedPrivateNamesStack.length - 1][node.property.name] = node.property.start
  101. }
  102. } else {
  103. node.property = this.parseIdent(true)
  104. }
  105. return this.finishNode(node, "MemberExpression")
  106. };
  107. // Prohibit delete of private class elements
  108. Parser_.prototype.parseMaybeUnary = function parseMaybeUnary (refDestructuringErrors, sawUnary) {
  109. var _return = Parser.prototype.parseMaybeUnary.call(this, refDestructuringErrors, sawUnary)
  110. if (_return.operator == "delete") {
  111. if (_return.argument.type == "MemberExpression" && _return.argument.property.type == "PrivateName") {
  112. this.raise(_return.start, "Private elements may not be deleted")
  113. }
  114. }
  115. return _return
  116. };
  117. return Parser_;
  118. }(Parser))
  119. Parser.prototype.privateNameToken = new TokenType("privateName")
  120. return Parser
  121. }