BVB Source Codes

reactide Show lval.js Source code

Return Download reactide: download lval.js Source code - Download reactide Source code - Type:.js
  1. import {types as tt} from "./tokentype"
  2. import {Parser} from "./state"
  3. import {has} from "./util"
  4.  
  5. const pp = Parser.prototype
  6.  
  7. // Convert existing expression atom to assignable pattern
  8. // if possible.
  9.  
  10. pp.toAssignable = function(node, isBinding) {
  11.   if (this.options.ecmaVersion >= 6 && node) {
  12.     switch (node.type) {
  13.     case "Identifier":
  14.     case "ObjectPattern":
  15.     case "ArrayPattern":
  16.       break
  17.  
  18.     case "ObjectExpression":
  19.       node.type = "ObjectPattern"
  20.       for (let i = 0; i < node.properties.length; i++) {
  21.         let prop = node.properties[i]
  22.         if (prop.kind !== "init") this.raise(prop.key.start, "Object pattern can't contain getter or setter")
  23.         this.toAssignable(prop.value, isBinding)
  24.       }
  25.       break
  26.  
  27.     case "ArrayExpression":
  28.       node.type = "ArrayPattern"
  29.       this.toAssignableList(node.elements, isBinding)
  30.       break
  31.  
  32.     case "AssignmentExpression":
  33.       if (node.operator === "=") {
  34.         node.type = "AssignmentPattern"
  35.         delete node.operator
  36.         // falls through to AssignmentPattern
  37.       } else {
  38.         this.raise(node.left.end, "Only '=' operator can be used for specifying default value.")
  39.         break
  40.       }
  41.  
  42.     case "AssignmentPattern":
  43.       if (node.right.type === "YieldExpression")
  44.         this.raise(node.right.start, "Yield expression cannot be a default value")
  45.       break
  46.  
  47.     case "ParenthesizedExpression":
  48.       node.expression = this.toAssignable(node.expression, isBinding)
  49.       break
  50.  
  51.     case "MemberExpression":
  52.       if (!isBinding) break
  53.  
  54.     default:
  55.       this.raise(node.start, "Assigning to rvalue")
  56.     }
  57.   }
  58.   return node
  59. }
  60.  
  61. // Convert list of expression atoms to binding list.
  62.  
  63. pp.toAssignableList = function(exprList, isBinding) {
  64.   let end = exprList.length
  65.   if (end) {
  66.     let last = exprList[end - 1]
  67.     if (last && last.type == "RestElement") {
  68.       --end
  69.     } else if (last && last.type == "SpreadElement") {
  70.       last.type = "RestElement"
  71.       let arg = last.argument
  72.       this.toAssignable(arg, isBinding)
  73.       if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern")
  74.         this.unexpected(arg.start)
  75.       --end
  76.     }
  77.  
  78.     if (isBinding && last && last.type === "RestElement" && last.argument.type !== "Identifier")
  79.       this.unexpected(last.argument.start)
  80.   }
  81.   for (let i = 0; i < end; i++) {
  82.     let elt = exprList[i]
  83.     if (elt) this.toAssignable(elt, isBinding)
  84.   }
  85.   return exprList
  86. }
  87.  
  88. // Parses spread element.
  89.  
  90. pp.parseSpread = function(refDestructuringErrors) {
  91.   let node = this.startNode()
  92.   this.next()
  93.   node.argument = this.parseMaybeAssign(false, refDestructuringErrors)
  94.   return this.finishNode(node, "SpreadElement")
  95. }
  96.  
  97. pp.parseRest = function(allowNonIdent) {
  98.   let node = this.startNode()
  99.   this.next()
  100.  
  101.   // RestElement inside of a function parameter must be an identifier
  102.   if (allowNonIdent) node.argument = this.type === tt.name ? this.parseIdent() : this.unexpected()
  103.   else node.argument = this.type === tt.name || this.type === tt.bracketL ? this.parseBindingAtom() : this.unexpected()
  104.  
  105.   return this.finishNode(node, "RestElement")
  106. }
  107.  
  108. // Parses lvalue (assignable) atom.
  109.  
  110. pp.parseBindingAtom = function() {
  111.   if (this.options.ecmaVersion < 6) return this.parseIdent()
  112.   switch (this.type) {
  113.   case tt.name:
  114.     return this.parseIdent()
  115.  
  116.   case tt.bracketL:
  117.     let node = this.startNode()
  118.     this.next()
  119.     node.elements = this.parseBindingList(tt.bracketR, true, true)
  120.     return this.finishNode(node, "ArrayPattern")
  121.  
  122.   case tt.braceL:
  123.     return this.parseObj(true)
  124.  
  125.   default:
  126.     this.unexpected()
  127.   }
  128. }
  129.  
  130. pp.parseBindingList = function(close, allowEmpty, allowTrailingComma, allowNonIdent) {
  131.   let elts = [], first = true
  132.   while (!this.eat(close)) {
  133.     if (first) first = false
  134.     else this.expect(tt.comma)
  135.     if (allowEmpty && this.type === tt.comma) {
  136.       elts.push(null)
  137.     } else if (allowTrailingComma && this.afterTrailingComma(close)) {
  138.       break
  139.     } else if (this.type === tt.ellipsis) {
  140.       let rest = this.parseRest(allowNonIdent)
  141.       this.parseBindingListItem(rest)
  142.       elts.push(rest)
  143.       if (this.type === tt.comma) this.raise(this.start, "Comma is not permitted after the rest element")
  144.       this.expect(close)
  145.       break
  146.     } else {
  147.       let elem = this.parseMaybeDefault(this.start, this.startLoc)
  148.       this.parseBindingListItem(elem)
  149.       elts.push(elem)
  150.     }
  151.   }
  152.   return elts
  153. }
  154.  
  155. pp.parseBindingListItem = function(param) {
  156.   return param
  157. }
  158.  
  159. // Parses assignment pattern around given atom if possible.
  160.  
  161. pp.parseMaybeDefault = function(startPos, startLoc, left) {
  162.   left = left || this.parseBindingAtom()
  163.   if (this.options.ecmaVersion < 6 || !this.eat(tt.eq)) return left
  164.   let node = this.startNodeAt(startPos, startLoc)
  165.   node.left = left
  166.   node.right = this.parseMaybeAssign()
  167.   return this.finishNode(node, "AssignmentPattern")
  168. }
  169.  
  170. // Verify that a node is an lval 鈥 something that can be assigned
  171. // to.
  172.  
  173. pp.checkLVal = function(expr, isBinding, checkClashes) {
  174.   switch (expr.type) {
  175.   case "Identifier":
  176.     if (this.strict && this.reservedWordsStrictBind.test(expr.name))
  177.       this.raiseRecoverable(expr.start, (isBinding ? "Binding " : "Assigning to ") + expr.name + " in strict mode")
  178.     if (checkClashes) {
  179.       if (has(checkClashes, expr.name))
  180.         this.raiseRecoverable(expr.start, "Argument name clash")
  181.       checkClashes[expr.name] = true
  182.     }
  183.     break
  184.  
  185.   case "MemberExpression":
  186.     if (isBinding) this.raiseRecoverable(expr.start, (isBinding ? "Binding" : "Assigning to") + " member expression")
  187.     break
  188.  
  189.   case "ObjectPattern":
  190.     for (let i = 0; i < expr.properties.length; i++)
  191.       this.checkLVal(expr.properties[i].value, isBinding, checkClashes)
  192.     break
  193.  
  194.   case "ArrayPattern":
  195.     for (let i = 0; i < expr.elements.length; i++) {
  196.       let elem = expr.elements[i]
  197.       if (elem) this.checkLVal(elem, isBinding, checkClashes)
  198.     }
  199.     break
  200.  
  201.   case "AssignmentPattern":
  202.     this.checkLVal(expr.left, isBinding, checkClashes)
  203.     break
  204.  
  205.   case "RestElement":
  206.     this.checkLVal(expr.argument, isBinding, checkClashes)
  207.     break
  208.  
  209.   case "ParenthesizedExpression":
  210.     this.checkLVal(expr.expression, isBinding, checkClashes)
  211.     break
  212.  
  213.   default:
  214.     this.raise(expr.start, (isBinding ? "Binding" : "Assigning to") + " rvalue")
  215.   }
  216. }
  217.  
downloadlval.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top