expression.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import {LooseParser} from "./state"
  2. import {isDummy} from "./parseutil"
  3. import {tokTypes as tt} from "../index"
  4. const lp = LooseParser.prototype
  5. lp.checkLVal = function(expr) {
  6. if (!expr) return expr
  7. switch (expr.type) {
  8. case "Identifier":
  9. case "MemberExpression":
  10. return expr
  11. case "ParenthesizedExpression":
  12. expr.expression = this.checkLVal(expr.expression)
  13. return expr
  14. default:
  15. return this.dummyIdent()
  16. }
  17. }
  18. lp.parseExpression = function(noIn) {
  19. let start = this.storeCurrentPos()
  20. let expr = this.parseMaybeAssign(noIn)
  21. if (this.tok.type === tt.comma) {
  22. let node = this.startNodeAt(start)
  23. node.expressions = [expr]
  24. while (this.eat(tt.comma)) node.expressions.push(this.parseMaybeAssign(noIn))
  25. return this.finishNode(node, "SequenceExpression")
  26. }
  27. return expr
  28. }
  29. lp.parseParenExpression = function() {
  30. this.pushCx()
  31. this.expect(tt.parenL)
  32. let val = this.parseExpression()
  33. this.popCx()
  34. this.expect(tt.parenR)
  35. return val
  36. }
  37. lp.parseMaybeAssign = function(noIn) {
  38. if (this.toks.isContextual("yield")) {
  39. let node = this.startNode()
  40. this.next()
  41. if (this.semicolon() || this.canInsertSemicolon() || (this.tok.type != tt.star && !this.tok.type.startsExpr)) {
  42. node.delegate = false
  43. node.argument = null
  44. } else {
  45. node.delegate = this.eat(tt.star)
  46. node.argument = this.parseMaybeAssign()
  47. }
  48. return this.finishNode(node, "YieldExpression")
  49. }
  50. let start = this.storeCurrentPos()
  51. let left = this.parseMaybeConditional(noIn)
  52. if (this.tok.type.isAssign) {
  53. let node = this.startNodeAt(start)
  54. node.operator = this.tok.value
  55. node.left = this.tok.type === tt.eq ? this.toAssignable(left) : this.checkLVal(left)
  56. this.next()
  57. node.right = this.parseMaybeAssign(noIn)
  58. return this.finishNode(node, "AssignmentExpression")
  59. }
  60. return left
  61. }
  62. lp.parseMaybeConditional = function(noIn) {
  63. let start = this.storeCurrentPos()
  64. let expr = this.parseExprOps(noIn)
  65. if (this.eat(tt.question)) {
  66. let node = this.startNodeAt(start)
  67. node.test = expr
  68. node.consequent = this.parseMaybeAssign()
  69. node.alternate = this.expect(tt.colon) ? this.parseMaybeAssign(noIn) : this.dummyIdent()
  70. return this.finishNode(node, "ConditionalExpression")
  71. }
  72. return expr
  73. }
  74. lp.parseExprOps = function(noIn) {
  75. let start = this.storeCurrentPos()
  76. let indent = this.curIndent, line = this.curLineStart
  77. return this.parseExprOp(this.parseMaybeUnary(false), start, -1, noIn, indent, line)
  78. }
  79. lp.parseExprOp = function(left, start, minPrec, noIn, indent, line) {
  80. if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) return left
  81. let prec = this.tok.type.binop
  82. if (prec != null && (!noIn || this.tok.type !== tt._in)) {
  83. if (prec > minPrec) {
  84. let node = this.startNodeAt(start)
  85. node.left = left
  86. node.operator = this.tok.value
  87. this.next()
  88. if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) {
  89. node.right = this.dummyIdent()
  90. } else {
  91. let rightStart = this.storeCurrentPos()
  92. node.right = this.parseExprOp(this.parseMaybeUnary(false), rightStart, prec, noIn, indent, line)
  93. }
  94. this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression")
  95. return this.parseExprOp(node, start, minPrec, noIn, indent, line)
  96. }
  97. }
  98. return left
  99. }
  100. lp.parseMaybeUnary = function(sawUnary) {
  101. let start = this.storeCurrentPos(), expr
  102. if (this.options.ecmaVersion >= 8 && this.inAsync && this.toks.isContextual("await")) {
  103. expr = this.parseAwait()
  104. sawUnary = true
  105. } else if (this.tok.type.prefix) {
  106. let node = this.startNode(), update = this.tok.type === tt.incDec
  107. if (!update) sawUnary = true
  108. node.operator = this.tok.value
  109. node.prefix = true
  110. this.next()
  111. node.argument = this.parseMaybeUnary(true)
  112. if (update) node.argument = this.checkLVal(node.argument)
  113. expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
  114. } else if (this.tok.type === tt.ellipsis) {
  115. let node = this.startNode()
  116. this.next()
  117. node.argument = this.parseMaybeUnary(sawUnary)
  118. expr = this.finishNode(node, "SpreadElement")
  119. } else {
  120. expr = this.parseExprSubscripts()
  121. while (this.tok.type.postfix && !this.canInsertSemicolon()) {
  122. let node = this.startNodeAt(start)
  123. node.operator = this.tok.value
  124. node.prefix = false
  125. node.argument = this.checkLVal(expr)
  126. this.next()
  127. expr = this.finishNode(node, "UpdateExpression")
  128. }
  129. }
  130. if (!sawUnary && this.eat(tt.starstar)) {
  131. let node = this.startNodeAt(start)
  132. node.operator = "**"
  133. node.left = expr
  134. node.right = this.parseMaybeUnary(false)
  135. return this.finishNode(node, "BinaryExpression")
  136. }
  137. return expr
  138. }
  139. lp.parseExprSubscripts = function() {
  140. let start = this.storeCurrentPos()
  141. return this.parseSubscripts(this.parseExprAtom(), start, false, this.curIndent, this.curLineStart)
  142. }
  143. lp.parseSubscripts = function(base, start, noCalls, startIndent, line) {
  144. for (;;) {
  145. if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine()) {
  146. if (this.tok.type == tt.dot && this.curIndent == startIndent)
  147. --startIndent
  148. else
  149. return base
  150. }
  151. let maybeAsyncArrow = base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon()
  152. if (this.eat(tt.dot)) {
  153. let node = this.startNodeAt(start)
  154. node.object = base
  155. if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine())
  156. node.property = this.dummyIdent()
  157. else
  158. node.property = this.parsePropertyAccessor() || this.dummyIdent()
  159. node.computed = false
  160. base = this.finishNode(node, "MemberExpression")
  161. } else if (this.tok.type == tt.bracketL) {
  162. this.pushCx()
  163. this.next()
  164. let node = this.startNodeAt(start)
  165. node.object = base
  166. node.property = this.parseExpression()
  167. node.computed = true
  168. this.popCx()
  169. this.expect(tt.bracketR)
  170. base = this.finishNode(node, "MemberExpression")
  171. } else if (!noCalls && this.tok.type == tt.parenL) {
  172. let exprList = this.parseExprList(tt.parenR)
  173. if (maybeAsyncArrow && this.eat(tt.arrow))
  174. return this.parseArrowExpression(this.startNodeAt(start), exprList, true)
  175. let node = this.startNodeAt(start)
  176. node.callee = base
  177. node.arguments = exprList
  178. base = this.finishNode(node, "CallExpression")
  179. } else if (this.tok.type == tt.backQuote) {
  180. let node = this.startNodeAt(start)
  181. node.tag = base
  182. node.quasi = this.parseTemplate()
  183. base = this.finishNode(node, "TaggedTemplateExpression")
  184. } else {
  185. return base
  186. }
  187. }
  188. }
  189. lp.parseExprAtom = function() {
  190. let node
  191. switch (this.tok.type) {
  192. case tt._this:
  193. case tt._super:
  194. let type = this.tok.type === tt._this ? "ThisExpression" : "Super"
  195. node = this.startNode()
  196. this.next()
  197. return this.finishNode(node, type)
  198. case tt.name:
  199. let start = this.storeCurrentPos()
  200. let id = this.parseIdent()
  201. let isAsync = false
  202. if (id.name === "async" && !this.canInsertSemicolon()) {
  203. if (this.eat(tt._function))
  204. return this.parseFunction(this.startNodeAt(start), false, true)
  205. if (this.tok.type === tt.name) {
  206. id = this.parseIdent()
  207. isAsync = true
  208. }
  209. }
  210. return this.eat(tt.arrow) ? this.parseArrowExpression(this.startNodeAt(start), [id], isAsync) : id
  211. case tt.regexp:
  212. node = this.startNode()
  213. let val = this.tok.value
  214. node.regex = {pattern: val.pattern, flags: val.flags}
  215. node.value = val.value
  216. node.raw = this.input.slice(this.tok.start, this.tok.end)
  217. this.next()
  218. return this.finishNode(node, "Literal")
  219. case tt.num: case tt.string:
  220. node = this.startNode()
  221. node.value = this.tok.value
  222. node.raw = this.input.slice(this.tok.start, this.tok.end)
  223. this.next()
  224. return this.finishNode(node, "Literal")
  225. case tt._null: case tt._true: case tt._false:
  226. node = this.startNode()
  227. node.value = this.tok.type === tt._null ? null : this.tok.type === tt._true
  228. node.raw = this.tok.type.keyword
  229. this.next()
  230. return this.finishNode(node, "Literal")
  231. case tt.parenL:
  232. let parenStart = this.storeCurrentPos()
  233. this.next()
  234. let inner = this.parseExpression()
  235. this.expect(tt.parenR)
  236. if (this.eat(tt.arrow)) {
  237. // (a,)=>a // SequenceExpression makes dummy in the last hole. Drop the dummy.
  238. let params = inner.expressions || [inner]
  239. if (params.length && isDummy(params[params.length - 1]))
  240. params.pop()
  241. return this.parseArrowExpression(this.startNodeAt(parenStart), params)
  242. }
  243. if (this.options.preserveParens) {
  244. let par = this.startNodeAt(parenStart)
  245. par.expression = inner
  246. inner = this.finishNode(par, "ParenthesizedExpression")
  247. }
  248. return inner
  249. case tt.bracketL:
  250. node = this.startNode()
  251. node.elements = this.parseExprList(tt.bracketR, true)
  252. return this.finishNode(node, "ArrayExpression")
  253. case tt.braceL:
  254. return this.parseObj()
  255. case tt._class:
  256. return this.parseClass(false)
  257. case tt._function:
  258. node = this.startNode()
  259. this.next()
  260. return this.parseFunction(node, false)
  261. case tt._new:
  262. return this.parseNew()
  263. case tt.backQuote:
  264. return this.parseTemplate()
  265. default:
  266. return this.dummyIdent()
  267. }
  268. }
  269. lp.parseNew = function() {
  270. let node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart
  271. let meta = this.parseIdent(true)
  272. if (this.options.ecmaVersion >= 6 && this.eat(tt.dot)) {
  273. node.meta = meta
  274. node.property = this.parseIdent(true)
  275. return this.finishNode(node, "MetaProperty")
  276. }
  277. let start = this.storeCurrentPos()
  278. node.callee = this.parseSubscripts(this.parseExprAtom(), start, true, startIndent, line)
  279. if (this.tok.type == tt.parenL) {
  280. node.arguments = this.parseExprList(tt.parenR)
  281. } else {
  282. node.arguments = []
  283. }
  284. return this.finishNode(node, "NewExpression")
  285. }
  286. lp.parseTemplateElement = function() {
  287. let elem = this.startNode()
  288. elem.value = {
  289. raw: this.input.slice(this.tok.start, this.tok.end).replace(/\r\n?/g, '\n'),
  290. cooked: this.tok.value
  291. }
  292. this.next()
  293. elem.tail = this.tok.type === tt.backQuote
  294. return this.finishNode(elem, "TemplateElement")
  295. }
  296. lp.parseTemplate = function() {
  297. let node = this.startNode()
  298. this.next()
  299. node.expressions = []
  300. let curElt = this.parseTemplateElement()
  301. node.quasis = [curElt]
  302. while (!curElt.tail) {
  303. this.next()
  304. node.expressions.push(this.parseExpression())
  305. if (this.expect(tt.braceR)) {
  306. curElt = this.parseTemplateElement()
  307. } else {
  308. curElt = this.startNode()
  309. curElt.value = {cooked: '', raw: ''}
  310. curElt.tail = true
  311. this.finishNode(curElt, "TemplateElement")
  312. }
  313. node.quasis.push(curElt)
  314. }
  315. this.expect(tt.backQuote)
  316. return this.finishNode(node, "TemplateLiteral")
  317. }
  318. lp.parseObj = function() {
  319. let node = this.startNode()
  320. node.properties = []
  321. this.pushCx()
  322. let indent = this.curIndent + 1, line = this.curLineStart
  323. this.eat(tt.braceL)
  324. if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
  325. while (!this.closes(tt.braceR, indent, line)) {
  326. let prop = this.startNode(), isGenerator, isAsync, start
  327. if (this.options.ecmaVersion >= 6) {
  328. start = this.storeCurrentPos()
  329. prop.method = false
  330. prop.shorthand = false
  331. isGenerator = this.eat(tt.star)
  332. }
  333. this.parsePropertyName(prop)
  334. if (!prop.computed &&
  335. prop.key.type === "Identifier" && prop.key.name === "async" && this.tok.type !== tt.parenL &&
  336. this.tok.type !== tt.colon && !this.canInsertSemicolon()) {
  337. this.parsePropertyName(prop)
  338. isAsync = true
  339. } else {
  340. isAsync = false
  341. }
  342. if (isDummy(prop.key)) { if (isDummy(this.parseMaybeAssign())) this.next(); this.eat(tt.comma); continue }
  343. if (this.eat(tt.colon)) {
  344. prop.kind = "init"
  345. prop.value = this.parseMaybeAssign()
  346. } else if (this.options.ecmaVersion >= 6 && (this.tok.type === tt.parenL || this.tok.type === tt.braceL)) {
  347. prop.kind = "init"
  348. prop.method = true
  349. prop.value = this.parseMethod(isGenerator, isAsync)
  350. } else if (this.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
  351. !prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
  352. (this.tok.type != tt.comma && this.tok.type != tt.braceR)) {
  353. prop.kind = prop.key.name
  354. this.parsePropertyName(prop)
  355. prop.value = this.parseMethod(false)
  356. } else {
  357. prop.kind = "init"
  358. if (this.options.ecmaVersion >= 6) {
  359. if (this.eat(tt.eq)) {
  360. let assign = this.startNodeAt(start)
  361. assign.operator = "="
  362. assign.left = prop.key
  363. assign.right = this.parseMaybeAssign()
  364. prop.value = this.finishNode(assign, "AssignmentExpression")
  365. } else {
  366. prop.value = prop.key
  367. }
  368. } else {
  369. prop.value = this.dummyIdent()
  370. }
  371. prop.shorthand = true
  372. }
  373. node.properties.push(this.finishNode(prop, "Property"))
  374. this.eat(tt.comma)
  375. }
  376. this.popCx()
  377. if (!this.eat(tt.braceR)) {
  378. // If there is no closing brace, make the node span to the start
  379. // of the next token (this is useful for Tern)
  380. this.last.end = this.tok.start
  381. if (this.options.locations) this.last.loc.end = this.tok.loc.start
  382. }
  383. return this.finishNode(node, "ObjectExpression")
  384. }
  385. lp.parsePropertyName = function(prop) {
  386. if (this.options.ecmaVersion >= 6) {
  387. if (this.eat(tt.bracketL)) {
  388. prop.computed = true
  389. prop.key = this.parseExpression()
  390. this.expect(tt.bracketR)
  391. return
  392. } else {
  393. prop.computed = false
  394. }
  395. }
  396. let key = (this.tok.type === tt.num || this.tok.type === tt.string) ? this.parseExprAtom() : this.parseIdent()
  397. prop.key = key || this.dummyIdent()
  398. }
  399. lp.parsePropertyAccessor = function() {
  400. if (this.tok.type === tt.name || this.tok.type.keyword) return this.parseIdent()
  401. }
  402. lp.parseIdent = function() {
  403. let name = this.tok.type === tt.name ? this.tok.value : this.tok.type.keyword
  404. if (!name) return this.dummyIdent()
  405. let node = this.startNode()
  406. this.next()
  407. node.name = name
  408. return this.finishNode(node, "Identifier")
  409. }
  410. lp.initFunction = function(node) {
  411. node.id = null
  412. node.params = []
  413. if (this.options.ecmaVersion >= 6) {
  414. node.generator = false
  415. node.expression = false
  416. }
  417. if (this.options.ecmaVersion >= 8)
  418. node.async = false
  419. }
  420. // Convert existing expression atom to assignable pattern
  421. // if possible.
  422. lp.toAssignable = function(node, binding) {
  423. if (!node || node.type == "Identifier" || (node.type == "MemberExpression" && !binding)) {
  424. // Okay
  425. } else if (node.type == "ParenthesizedExpression") {
  426. node.expression = this.toAssignable(node.expression, binding)
  427. } else if (this.options.ecmaVersion < 6) {
  428. return this.dummyIdent()
  429. } else if (node.type == "ObjectExpression") {
  430. node.type = "ObjectPattern"
  431. let props = node.properties
  432. for (let i = 0; i < props.length; i++)
  433. props[i].value = this.toAssignable(props[i].value, binding)
  434. } else if (node.type == "ArrayExpression") {
  435. node.type = "ArrayPattern"
  436. this.toAssignableList(node.elements, binding)
  437. } else if (node.type == "SpreadElement") {
  438. node.type = "RestElement"
  439. node.argument = this.toAssignable(node.argument, binding)
  440. } else if (node.type == "AssignmentExpression") {
  441. node.type = "AssignmentPattern"
  442. delete node.operator
  443. } else {
  444. return this.dummyIdent()
  445. }
  446. return node
  447. }
  448. lp.toAssignableList = function(exprList, binding) {
  449. for (let i = 0; i < exprList.length; i++)
  450. exprList[i] = this.toAssignable(exprList[i], binding)
  451. return exprList
  452. }
  453. lp.parseFunctionParams = function(params) {
  454. params = this.parseExprList(tt.parenR)
  455. return this.toAssignableList(params, true)
  456. }
  457. lp.parseMethod = function(isGenerator, isAsync) {
  458. let node = this.startNode(), oldInAsync = this.inAsync
  459. this.initFunction(node)
  460. if (this.options.ecmaVersion >= 6)
  461. node.generator = !!isGenerator
  462. if (this.options.ecmaVersion >= 8)
  463. node.async = !!isAsync
  464. this.inAsync = node.async
  465. node.params = this.parseFunctionParams()
  466. node.expression = this.options.ecmaVersion >= 6 && this.tok.type !== tt.braceL
  467. node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
  468. this.inAsync = oldInAsync
  469. return this.finishNode(node, "FunctionExpression")
  470. }
  471. lp.parseArrowExpression = function(node, params, isAsync) {
  472. let oldInAsync = this.inAsync
  473. this.initFunction(node)
  474. if (this.options.ecmaVersion >= 8)
  475. node.async = !!isAsync
  476. this.inAsync = node.async
  477. node.params = this.toAssignableList(params, true)
  478. node.expression = this.tok.type !== tt.braceL
  479. node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
  480. this.inAsync = oldInAsync
  481. return this.finishNode(node, "ArrowFunctionExpression")
  482. }
  483. lp.parseExprList = function(close, allowEmpty) {
  484. this.pushCx()
  485. let indent = this.curIndent, line = this.curLineStart, elts = []
  486. this.next() // Opening bracket
  487. while (!this.closes(close, indent + 1, line)) {
  488. if (this.eat(tt.comma)) {
  489. elts.push(allowEmpty ? null : this.dummyIdent())
  490. continue
  491. }
  492. let elt = this.parseMaybeAssign()
  493. if (isDummy(elt)) {
  494. if (this.closes(close, indent, line)) break
  495. this.next()
  496. } else {
  497. elts.push(elt)
  498. }
  499. this.eat(tt.comma)
  500. }
  501. this.popCx()
  502. if (!this.eat(close)) {
  503. // If there is no closing brace, make the node span to the start
  504. // of the next token (this is useful for Tern)
  505. this.last.end = this.tok.start
  506. if (this.options.locations) this.last.loc.end = this.tok.loc.start
  507. }
  508. return elts
  509. }
  510. lp.parseAwait = function() {
  511. let node = this.startNode()
  512. this.next()
  513. node.argument = this.parseMaybeUnary()
  514. return this.finishNode(node, "AwaitExpression")
  515. }