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