react / wstein / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / lval.js
80550 viewsimport {types as tt} from "./tokentype"1import {Parser} from "./state"2import {reservedWords} from "./identifier"3import {has} from "./util"45const pp = Parser.prototype67// Convert existing expression atom to assignable pattern8// if possible.910pp.toAssignable = function(node, isBinding) {11if (this.options.ecmaVersion >= 6 && node) {12switch (node.type) {13case "Identifier":14case "ObjectPattern":15case "ArrayPattern":16case "AssignmentPattern":17break1819case "ObjectExpression":20node.type = "ObjectPattern"21for (let i = 0; i < node.properties.length; i++) {22let prop = node.properties[i]23if (prop.kind !== "init") this.raise(prop.key.start, "Object pattern can't contain getter or setter")24this.toAssignable(prop.value, isBinding)25}26break2728case "ArrayExpression":29node.type = "ArrayPattern"30this.toAssignableList(node.elements, isBinding)31break3233case "AssignmentExpression":34if (node.operator === "=") {35node.type = "AssignmentPattern"36} else {37this.raise(node.left.end, "Only '=' operator can be used for specifying default value.")38}39break4041case "ParenthesizedExpression":42node.expression = this.toAssignable(node.expression, isBinding)43break4445case "MemberExpression":46if (!isBinding) break4748default:49this.raise(node.start, "Assigning to rvalue")50}51}52return node53}5455// Convert list of expression atoms to binding list.5657pp.toAssignableList = function(exprList, isBinding) {58let end = exprList.length59if (end) {60let last = exprList[end - 1]61if (last && last.type == "RestElement") {62--end63} else if (last && last.type == "SpreadElement") {64last.type = "RestElement"65let arg = last.argument66this.toAssignable(arg, isBinding)67if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern")68this.unexpected(arg.start)69--end70}71}72for (let i = 0; i < end; i++) {73let elt = exprList[i]74if (elt) this.toAssignable(elt, isBinding)75}76return exprList77}7879// Parses spread element.8081pp.parseSpread = function(refShorthandDefaultPos) {82let node = this.startNode()83this.next()84node.argument = this.parseMaybeAssign(refShorthandDefaultPos)85return this.finishNode(node, "SpreadElement")86}8788pp.parseRest = function() {89let node = this.startNode()90this.next()91node.argument = this.type === tt.name || this.type === tt.bracketL ? this.parseBindingAtom() : this.unexpected()92return this.finishNode(node, "RestElement")93}9495// Parses lvalue (assignable) atom.9697pp.parseBindingAtom = function() {98if (this.options.ecmaVersion < 6) return this.parseIdent()99switch (this.type) {100case tt.name:101return this.parseIdent()102103case tt.bracketL:104let node = this.startNode()105this.next()106node.elements = this.parseBindingList(tt.bracketR, true, true)107return this.finishNode(node, "ArrayPattern")108109case tt.braceL:110return this.parseObj(true)111112default:113this.unexpected()114}115}116117pp.parseBindingList = function(close, allowEmpty, allowTrailingComma) {118let elts = [], first = true119while (!this.eat(close)) {120if (first) first = false121else this.expect(tt.comma)122if (allowEmpty && this.type === tt.comma) {123elts.push(null)124} else if (allowTrailingComma && this.afterTrailingComma(close)) {125break126} else if (this.type === tt.ellipsis) {127let rest = this.parseRest()128this.parseBindingListItem(rest)129elts.push(rest)130this.expect(close)131break132} else {133let elem = this.parseMaybeDefault(this.start, this.startLoc)134this.parseBindingListItem(elem)135elts.push(elem)136}137}138return elts139}140141pp.parseBindingListItem = function(param) {142return param143}144145// Parses assignment pattern around given atom if possible.146147pp.parseMaybeDefault = function(startPos, startLoc, left) {148if (Array.isArray(startPos)){149if (this.options.locations && noCalls === undefined) {150// shift arguments to left by one151left = startLoc152// flatten startPos153startLoc = startPos[1]154startPos = startPos[0]155}156}157left = left || this.parseBindingAtom()158if (!this.eat(tt.eq)) return left159let node = this.startNodeAt(startPos, startLoc)160node.operator = "="161node.left = left162node.right = this.parseMaybeAssign()163return this.finishNode(node, "AssignmentPattern")164}165166// Verify that a node is an lval — something that can be assigned167// to.168169pp.checkLVal = function(expr, isBinding, checkClashes) {170switch (expr.type) {171case "Identifier":172if (this.strict && (reservedWords.strictBind(expr.name) || reservedWords.strict(expr.name)))173this.raise(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode")174if (checkClashes) {175if (has(checkClashes, expr.name))176this.raise(expr.start, "Argument name clash in strict mode")177checkClashes[expr.name] = true178}179break180181case "MemberExpression":182if (isBinding) this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression")183break184185case "ObjectPattern":186for (let i = 0; i < expr.properties.length; i++)187this.checkLVal(expr.properties[i].value, isBinding, checkClashes)188break189190case "ArrayPattern":191for (let i = 0; i < expr.elements.length; i++) {192let elem = expr.elements[i]193if (elem) this.checkLVal(elem, isBinding, checkClashes)194}195break196197case "AssignmentPattern":198this.checkLVal(expr.left, isBinding, checkClashes)199break200201case "RestElement":202this.checkLVal(expr.argument, isBinding, checkClashes)203break204205case "ParenthesizedExpression":206this.checkLVal(expr.expression, isBinding, checkClashes)207break208209default:210this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " rvalue")211}212}213214215