acorn_loose.es.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. import { defaultOptions, addLooseExports, SourceLocation, tokTypes, tokenizer, Node, lineBreak, isNewLine, getLineInfo, Token, lineBreakG } from './acorn';
  2. // Registered plugins
  3. var pluginsLoose = {}
  4. var LooseParser = function LooseParser(input, options) {
  5. if ( options === void 0 ) options = {};
  6. this.toks = tokenizer(input, options)
  7. this.options = this.toks.options
  8. this.input = this.toks.input
  9. this.tok = this.last = {type: tokTypes.eof, start: 0, end: 0}
  10. if (this.options.locations) {
  11. var here = this.toks.curPosition()
  12. this.tok.loc = new SourceLocation(this.toks, here, here)
  13. }
  14. this.ahead = [] // Tokens ahead
  15. this.context = [] // Indentation contexted
  16. this.curIndent = 0
  17. this.curLineStart = 0
  18. this.nextLineStart = this.lineEnd(this.curLineStart) + 1
  19. this.inAsync = false
  20. // Load plugins
  21. this.options.pluginsLoose = options.pluginsLoose || {}
  22. this.loadPlugins(this.options.pluginsLoose)
  23. };
  24. LooseParser.prototype.startNode = function startNode () {
  25. return new Node(this.toks, this.tok.start, this.options.locations ? this.tok.loc.start : null)
  26. };
  27. LooseParser.prototype.storeCurrentPos = function storeCurrentPos () {
  28. return this.options.locations ? [this.tok.start, this.tok.loc.start] : this.tok.start
  29. };
  30. LooseParser.prototype.startNodeAt = function startNodeAt (pos) {
  31. if (this.options.locations) {
  32. return new Node(this.toks, pos[0], pos[1])
  33. } else {
  34. return new Node(this.toks, pos)
  35. }
  36. };
  37. LooseParser.prototype.finishNode = function finishNode (node, type) {
  38. node.type = type
  39. node.end = this.last.end
  40. if (this.options.locations)
  41. node.loc.end = this.last.loc.end
  42. if (this.options.ranges)
  43. node.range[1] = this.last.end
  44. return node
  45. };
  46. LooseParser.prototype.dummyNode = function dummyNode (type) {
  47. var dummy = this.startNode()
  48. dummy.type = type
  49. dummy.end = dummy.start
  50. if (this.options.locations)
  51. dummy.loc.end = dummy.loc.start
  52. if (this.options.ranges)
  53. dummy.range[1] = dummy.start
  54. this.last = {type: tokTypes.name, start: dummy.start, end: dummy.start, loc: dummy.loc}
  55. return dummy
  56. };
  57. LooseParser.prototype.dummyIdent = function dummyIdent () {
  58. var dummy = this.dummyNode("Identifier")
  59. dummy.name = "✖"
  60. return dummy
  61. };
  62. LooseParser.prototype.dummyString = function dummyString () {
  63. var dummy = this.dummyNode("Literal")
  64. dummy.value = dummy.raw = "✖"
  65. return dummy
  66. };
  67. LooseParser.prototype.eat = function eat (type) {
  68. if (this.tok.type === type) {
  69. this.next()
  70. return true
  71. } else {
  72. return false
  73. }
  74. };
  75. LooseParser.prototype.isContextual = function isContextual (name) {
  76. return this.tok.type === tokTypes.name && this.tok.value === name
  77. };
  78. LooseParser.prototype.eatContextual = function eatContextual (name) {
  79. return this.tok.value === name && this.eat(tokTypes.name)
  80. };
  81. LooseParser.prototype.canInsertSemicolon = function canInsertSemicolon () {
  82. return this.tok.type === tokTypes.eof || this.tok.type === tokTypes.braceR ||
  83. lineBreak.test(this.input.slice(this.last.end, this.tok.start))
  84. };
  85. LooseParser.prototype.semicolon = function semicolon () {
  86. return this.eat(tokTypes.semi)
  87. };
  88. LooseParser.prototype.expect = function expect (type) {
  89. var this$1 = this;
  90. if (this.eat(type)) return true
  91. for (var i = 1; i <= 2; i++) {
  92. if (this$1.lookAhead(i).type == type) {
  93. for (var j = 0; j < i; j++) this$1.next()
  94. return true
  95. }
  96. }
  97. };
  98. LooseParser.prototype.pushCx = function pushCx () {
  99. this.context.push(this.curIndent)
  100. };
  101. LooseParser.prototype.popCx = function popCx () {
  102. this.curIndent = this.context.pop()
  103. };
  104. LooseParser.prototype.lineEnd = function lineEnd (pos) {
  105. while (pos < this.input.length && !isNewLine(this.input.charCodeAt(pos))) ++pos
  106. return pos
  107. };
  108. LooseParser.prototype.indentationAfter = function indentationAfter (pos) {
  109. var this$1 = this;
  110. for (var count = 0;; ++pos) {
  111. var ch = this$1.input.charCodeAt(pos)
  112. if (ch === 32) ++count
  113. else if (ch === 9) count += this$1.options.tabSize
  114. else return count
  115. }
  116. };
  117. LooseParser.prototype.closes = function closes (closeTok, indent, line, blockHeuristic) {
  118. if (this.tok.type === closeTok || this.tok.type === tokTypes.eof) return true
  119. return line != this.curLineStart && this.curIndent < indent && this.tokenStartsLine() &&
  120. (!blockHeuristic || this.nextLineStart >= this.input.length ||
  121. this.indentationAfter(this.nextLineStart) < indent)
  122. };
  123. LooseParser.prototype.tokenStartsLine = function tokenStartsLine () {
  124. var this$1 = this;
  125. for (var p = this.tok.start - 1; p >= this.curLineStart; --p) {
  126. var ch = this$1.input.charCodeAt(p)
  127. if (ch !== 9 && ch !== 32) return false
  128. }
  129. return true
  130. };
  131. LooseParser.prototype.extend = function extend (name, f) {
  132. this[name] = f(this[name])
  133. };
  134. LooseParser.prototype.loadPlugins = function loadPlugins (pluginConfigs) {
  135. var this$1 = this;
  136. for (var name in pluginConfigs) {
  137. var plugin = pluginsLoose[name]
  138. if (!plugin) throw new Error("Plugin '" + name + "' not found")
  139. plugin(this$1, pluginConfigs[name])
  140. }
  141. };
  142. var lp = LooseParser.prototype
  143. function isSpace(ch) {
  144. return (ch < 14 && ch > 8) || ch === 32 || ch === 160 || isNewLine(ch)
  145. }
  146. lp.next = function() {
  147. var this$1 = this;
  148. this.last = this.tok
  149. if (this.ahead.length)
  150. this.tok = this.ahead.shift()
  151. else
  152. this.tok = this.readToken()
  153. if (this.tok.start >= this.nextLineStart) {
  154. while (this.tok.start >= this.nextLineStart) {
  155. this$1.curLineStart = this$1.nextLineStart
  156. this$1.nextLineStart = this$1.lineEnd(this$1.curLineStart) + 1
  157. }
  158. this.curIndent = this.indentationAfter(this.curLineStart)
  159. }
  160. }
  161. lp.readToken = function() {
  162. var this$1 = this;
  163. for (;;) {
  164. try {
  165. this$1.toks.next()
  166. if (this$1.toks.type === tokTypes.dot &&
  167. this$1.input.substr(this$1.toks.end, 1) === "." &&
  168. this$1.options.ecmaVersion >= 6) {
  169. this$1.toks.end++
  170. this$1.toks.type = tokTypes.ellipsis
  171. }
  172. return new Token(this$1.toks)
  173. } catch(e) {
  174. if (!(e instanceof SyntaxError)) throw e
  175. // Try to skip some text, based on the error message, and then continue
  176. var msg = e.message, pos = e.raisedAt, replace = true
  177. if (/unterminated/i.test(msg)) {
  178. pos = this$1.lineEnd(e.pos + 1)
  179. if (/string/.test(msg)) {
  180. replace = {start: e.pos, end: pos, type: tokTypes.string, value: this$1.input.slice(e.pos + 1, pos)}
  181. } else if (/regular expr/i.test(msg)) {
  182. var re = this$1.input.slice(e.pos, pos)
  183. try { re = new RegExp(re) } catch(e) {}
  184. replace = {start: e.pos, end: pos, type: tokTypes.regexp, value: re}
  185. } else if (/template/.test(msg)) {
  186. replace = {start: e.pos, end: pos,
  187. type: tokTypes.template,
  188. value: this$1.input.slice(e.pos, pos)}
  189. } else {
  190. replace = false
  191. }
  192. } else if (/invalid (unicode|regexp|number)|expecting unicode|octal literal|is reserved|directly after number|expected number in radix/i.test(msg)) {
  193. while (pos < this.input.length && !isSpace(this.input.charCodeAt(pos))) ++pos
  194. } else if (/character escape|expected hexadecimal/i.test(msg)) {
  195. while (pos < this.input.length) {
  196. var ch = this$1.input.charCodeAt(pos++)
  197. if (ch === 34 || ch === 39 || isNewLine(ch)) break
  198. }
  199. } else if (/unexpected character/i.test(msg)) {
  200. pos++
  201. replace = false
  202. } else if (/regular expression/i.test(msg)) {
  203. replace = true
  204. } else {
  205. throw e
  206. }
  207. this$1.resetTo(pos)
  208. if (replace === true) replace = {start: pos, end: pos, type: tokTypes.name, value: "✖"}
  209. if (replace) {
  210. if (this$1.options.locations)
  211. replace.loc = new SourceLocation(
  212. this$1.toks,
  213. getLineInfo(this$1.input, replace.start),
  214. getLineInfo(this$1.input, replace.end))
  215. return replace
  216. }
  217. }
  218. }
  219. }
  220. lp.resetTo = function(pos) {
  221. var this$1 = this;
  222. this.toks.pos = pos
  223. var ch = this.input.charAt(pos - 1)
  224. this.toks.exprAllowed = !ch || /[\[\{\(,;:?\/*=+\-~!|&%^<>]/.test(ch) ||
  225. /[enwfd]/.test(ch) &&
  226. /\b(keywords|case|else|return|throw|new|in|(instance|type)of|delete|void)$/.test(this.input.slice(pos - 10, pos))
  227. if (this.options.locations) {
  228. this.toks.curLine = 1
  229. this.toks.lineStart = lineBreakG.lastIndex = 0
  230. var match
  231. while ((match = lineBreakG.exec(this.input)) && match.index < pos) {
  232. ++this$1.toks.curLine
  233. this$1.toks.lineStart = match.index + match[0].length
  234. }
  235. }
  236. }
  237. lp.lookAhead = function(n) {
  238. var this$1 = this;
  239. while (n > this.ahead.length)
  240. this$1.ahead.push(this$1.readToken())
  241. return this.ahead[n - 1]
  242. }
  243. function isDummy(node) { return node.name == "✖" }
  244. var lp$1 = LooseParser.prototype
  245. lp$1.parseTopLevel = function() {
  246. var this$1 = this;
  247. var node = this.startNodeAt(this.options.locations ? [0, getLineInfo(this.input, 0)] : 0)
  248. node.body = []
  249. while (this.tok.type !== tokTypes.eof) node.body.push(this$1.parseStatement())
  250. this.last = this.tok
  251. if (this.options.ecmaVersion >= 6) {
  252. node.sourceType = this.options.sourceType
  253. }
  254. return this.finishNode(node, "Program")
  255. }
  256. lp$1.parseStatement = function() {
  257. var this$1 = this;
  258. var starttype = this.tok.type, node = this.startNode(), kind
  259. if (this.toks.isLet()) {
  260. starttype = tokTypes._var
  261. kind = "let"
  262. }
  263. switch (starttype) {
  264. case tokTypes._break: case tokTypes._continue:
  265. this.next()
  266. var isBreak = starttype === tokTypes._break
  267. if (this.semicolon() || this.canInsertSemicolon()) {
  268. node.label = null
  269. } else {
  270. node.label = this.tok.type === tokTypes.name ? this.parseIdent() : null
  271. this.semicolon()
  272. }
  273. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
  274. case tokTypes._debugger:
  275. this.next()
  276. this.semicolon()
  277. return this.finishNode(node, "DebuggerStatement")
  278. case tokTypes._do:
  279. this.next()
  280. node.body = this.parseStatement()
  281. node.test = this.eat(tokTypes._while) ? this.parseParenExpression() : this.dummyIdent()
  282. this.semicolon()
  283. return this.finishNode(node, "DoWhileStatement")
  284. case tokTypes._for:
  285. this.next()
  286. this.pushCx()
  287. this.expect(tokTypes.parenL)
  288. if (this.tok.type === tokTypes.semi) return this.parseFor(node, null)
  289. var isLet = this.toks.isLet()
  290. if (isLet || this.tok.type === tokTypes._var || this.tok.type === tokTypes._const) {
  291. var init$1 = this.parseVar(true, isLet ? "let" : this.tok.value)
  292. if (init$1.declarations.length === 1 && (this.tok.type === tokTypes._in || this.isContextual("of"))) {
  293. return this.parseForIn(node, init$1)
  294. }
  295. return this.parseFor(node, init$1)
  296. }
  297. var init = this.parseExpression(true)
  298. if (this.tok.type === tokTypes._in || this.isContextual("of"))
  299. return this.parseForIn(node, this.toAssignable(init))
  300. return this.parseFor(node, init)
  301. case tokTypes._function:
  302. this.next()
  303. return this.parseFunction(node, true)
  304. case tokTypes._if:
  305. this.next()
  306. node.test = this.parseParenExpression()
  307. node.consequent = this.parseStatement()
  308. node.alternate = this.eat(tokTypes._else) ? this.parseStatement() : null
  309. return this.finishNode(node, "IfStatement")
  310. case tokTypes._return:
  311. this.next()
  312. if (this.eat(tokTypes.semi) || this.canInsertSemicolon()) node.argument = null
  313. else { node.argument = this.parseExpression(); this.semicolon() }
  314. return this.finishNode(node, "ReturnStatement")
  315. case tokTypes._switch:
  316. var blockIndent = this.curIndent, line = this.curLineStart
  317. this.next()
  318. node.discriminant = this.parseParenExpression()
  319. node.cases = []
  320. this.pushCx()
  321. this.expect(tokTypes.braceL)
  322. var cur
  323. while (!this.closes(tokTypes.braceR, blockIndent, line, true)) {
  324. if (this$1.tok.type === tokTypes._case || this$1.tok.type === tokTypes._default) {
  325. var isCase = this$1.tok.type === tokTypes._case
  326. if (cur) this$1.finishNode(cur, "SwitchCase")
  327. node.cases.push(cur = this$1.startNode())
  328. cur.consequent = []
  329. this$1.next()
  330. if (isCase) cur.test = this$1.parseExpression()
  331. else cur.test = null
  332. this$1.expect(tokTypes.colon)
  333. } else {
  334. if (!cur) {
  335. node.cases.push(cur = this$1.startNode())
  336. cur.consequent = []
  337. cur.test = null
  338. }
  339. cur.consequent.push(this$1.parseStatement())
  340. }
  341. }
  342. if (cur) this.finishNode(cur, "SwitchCase")
  343. this.popCx()
  344. this.eat(tokTypes.braceR)
  345. return this.finishNode(node, "SwitchStatement")
  346. case tokTypes._throw:
  347. this.next()
  348. node.argument = this.parseExpression()
  349. this.semicolon()
  350. return this.finishNode(node, "ThrowStatement")
  351. case tokTypes._try:
  352. this.next()
  353. node.block = this.parseBlock()
  354. node.handler = null
  355. if (this.tok.type === tokTypes._catch) {
  356. var clause = this.startNode()
  357. this.next()
  358. this.expect(tokTypes.parenL)
  359. clause.param = this.toAssignable(this.parseExprAtom(), true)
  360. this.expect(tokTypes.parenR)
  361. clause.body = this.parseBlock()
  362. node.handler = this.finishNode(clause, "CatchClause")
  363. }
  364. node.finalizer = this.eat(tokTypes._finally) ? this.parseBlock() : null
  365. if (!node.handler && !node.finalizer) return node.block
  366. return this.finishNode(node, "TryStatement")
  367. case tokTypes._var:
  368. case tokTypes._const:
  369. return this.parseVar(false, kind || this.tok.value)
  370. case tokTypes._while:
  371. this.next()
  372. node.test = this.parseParenExpression()
  373. node.body = this.parseStatement()
  374. return this.finishNode(node, "WhileStatement")
  375. case tokTypes._with:
  376. this.next()
  377. node.object = this.parseParenExpression()
  378. node.body = this.parseStatement()
  379. return this.finishNode(node, "WithStatement")
  380. case tokTypes.braceL:
  381. return this.parseBlock()
  382. case tokTypes.semi:
  383. this.next()
  384. return this.finishNode(node, "EmptyStatement")
  385. case tokTypes._class:
  386. return this.parseClass(true)
  387. case tokTypes._import:
  388. return this.parseImport()
  389. case tokTypes._export:
  390. return this.parseExport()
  391. default:
  392. if (this.toks.isAsyncFunction()) {
  393. this.next()
  394. this.next()
  395. return this.parseFunction(node, true, true)
  396. }
  397. var expr = this.parseExpression()
  398. if (isDummy(expr)) {
  399. this.next()
  400. if (this.tok.type === tokTypes.eof) return this.finishNode(node, "EmptyStatement")
  401. return this.parseStatement()
  402. } else if (starttype === tokTypes.name && expr.type === "Identifier" && this.eat(tokTypes.colon)) {
  403. node.body = this.parseStatement()
  404. node.label = expr
  405. return this.finishNode(node, "LabeledStatement")
  406. } else {
  407. node.expression = expr
  408. this.semicolon()
  409. return this.finishNode(node, "ExpressionStatement")
  410. }
  411. }
  412. }
  413. lp$1.parseBlock = function() {
  414. var this$1 = this;
  415. var node = this.startNode()
  416. this.pushCx()
  417. this.expect(tokTypes.braceL)
  418. var blockIndent = this.curIndent, line = this.curLineStart
  419. node.body = []
  420. while (!this.closes(tokTypes.braceR, blockIndent, line, true))
  421. node.body.push(this$1.parseStatement())
  422. this.popCx()
  423. this.eat(tokTypes.braceR)
  424. return this.finishNode(node, "BlockStatement")
  425. }
  426. lp$1.parseFor = function(node, init) {
  427. node.init = init
  428. node.test = node.update = null
  429. if (this.eat(tokTypes.semi) && this.tok.type !== tokTypes.semi) node.test = this.parseExpression()
  430. if (this.eat(tokTypes.semi) && this.tok.type !== tokTypes.parenR) node.update = this.parseExpression()
  431. this.popCx()
  432. this.expect(tokTypes.parenR)
  433. node.body = this.parseStatement()
  434. return this.finishNode(node, "ForStatement")
  435. }
  436. lp$1.parseForIn = function(node, init) {
  437. var type = this.tok.type === tokTypes._in ? "ForInStatement" : "ForOfStatement"
  438. this.next()
  439. node.left = init
  440. node.right = this.parseExpression()
  441. this.popCx()
  442. this.expect(tokTypes.parenR)
  443. node.body = this.parseStatement()
  444. return this.finishNode(node, type)
  445. }
  446. lp$1.parseVar = function(noIn, kind) {
  447. var this$1 = this;
  448. var node = this.startNode()
  449. node.kind = kind
  450. this.next()
  451. node.declarations = []
  452. do {
  453. var decl = this$1.startNode()
  454. decl.id = this$1.options.ecmaVersion >= 6 ? this$1.toAssignable(this$1.parseExprAtom(), true) : this$1.parseIdent()
  455. decl.init = this$1.eat(tokTypes.eq) ? this$1.parseMaybeAssign(noIn) : null
  456. node.declarations.push(this$1.finishNode(decl, "VariableDeclarator"))
  457. } while (this.eat(tokTypes.comma))
  458. if (!node.declarations.length) {
  459. var decl$1 = this.startNode()
  460. decl$1.id = this.dummyIdent()
  461. node.declarations.push(this.finishNode(decl$1, "VariableDeclarator"))
  462. }
  463. if (!noIn) this.semicolon()
  464. return this.finishNode(node, "VariableDeclaration")
  465. }
  466. lp$1.parseClass = function(isStatement) {
  467. var this$1 = this;
  468. var node = this.startNode()
  469. this.next()
  470. if (isStatement == null) isStatement = this.tok.type === tokTypes.name
  471. if (this.tok.type === tokTypes.name) node.id = this.parseIdent()
  472. else if (isStatement) node.id = this.dummyIdent()
  473. else node.id = null
  474. node.superClass = this.eat(tokTypes._extends) ? this.parseExpression() : null
  475. node.body = this.startNode()
  476. node.body.body = []
  477. this.pushCx()
  478. var indent = this.curIndent + 1, line = this.curLineStart
  479. this.eat(tokTypes.braceL)
  480. if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
  481. while (!this.closes(tokTypes.braceR, indent, line)) {
  482. if (this$1.semicolon()) continue
  483. var method = this$1.startNode(), isGenerator, isAsync
  484. if (this$1.options.ecmaVersion >= 6) {
  485. method.static = false
  486. isGenerator = this$1.eat(tokTypes.star)
  487. }
  488. this$1.parsePropertyName(method)
  489. if (isDummy(method.key)) { if (isDummy(this$1.parseMaybeAssign())) this$1.next(); this$1.eat(tokTypes.comma); continue }
  490. if (method.key.type === "Identifier" && !method.computed && method.key.name === "static" &&
  491. (this$1.tok.type != tokTypes.parenL && this$1.tok.type != tokTypes.braceL)) {
  492. method.static = true
  493. isGenerator = this$1.eat(tokTypes.star)
  494. this$1.parsePropertyName(method)
  495. } else {
  496. method.static = false
  497. }
  498. if (!method.computed &&
  499. method.key.type === "Identifier" && method.key.name === "async" && this$1.tok.type !== tokTypes.parenL &&
  500. !this$1.canInsertSemicolon()) {
  501. this$1.parsePropertyName(method)
  502. isAsync = true
  503. } else {
  504. isAsync = false
  505. }
  506. if (this$1.options.ecmaVersion >= 5 && method.key.type === "Identifier" &&
  507. !method.computed && (method.key.name === "get" || method.key.name === "set") &&
  508. this$1.tok.type !== tokTypes.parenL && this$1.tok.type !== tokTypes.braceL) {
  509. method.kind = method.key.name
  510. this$1.parsePropertyName(method)
  511. method.value = this$1.parseMethod(false)
  512. } else {
  513. if (!method.computed && !method.static && !isGenerator && !isAsync && (
  514. method.key.type === "Identifier" && method.key.name === "constructor" ||
  515. method.key.type === "Literal" && method.key.value === "constructor")) {
  516. method.kind = "constructor"
  517. } else {
  518. method.kind = "method"
  519. }
  520. method.value = this$1.parseMethod(isGenerator, isAsync)
  521. }
  522. node.body.body.push(this$1.finishNode(method, "MethodDefinition"))
  523. }
  524. this.popCx()
  525. if (!this.eat(tokTypes.braceR)) {
  526. // If there is no closing brace, make the node span to the start
  527. // of the next token (this is useful for Tern)
  528. this.last.end = this.tok.start
  529. if (this.options.locations) this.last.loc.end = this.tok.loc.start
  530. }
  531. this.semicolon()
  532. this.finishNode(node.body, "ClassBody")
  533. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
  534. }
  535. lp$1.parseFunction = function(node, isStatement, isAsync) {
  536. var oldInAsync = this.inAsync
  537. this.initFunction(node)
  538. if (this.options.ecmaVersion >= 6) {
  539. node.generator = this.eat(tokTypes.star)
  540. }
  541. if (this.options.ecmaVersion >= 8) {
  542. node.async = !!isAsync
  543. }
  544. if (isStatement == null) isStatement = this.tok.type === tokTypes.name
  545. if (this.tok.type === tokTypes.name) node.id = this.parseIdent()
  546. else if (isStatement) node.id = this.dummyIdent()
  547. this.inAsync = node.async
  548. node.params = this.parseFunctionParams()
  549. node.body = this.parseBlock()
  550. this.inAsync = oldInAsync
  551. return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
  552. }
  553. lp$1.parseExport = function() {
  554. var node = this.startNode()
  555. this.next()
  556. if (this.eat(tokTypes.star)) {
  557. node.source = this.eatContextual("from") ? this.parseExprAtom() : this.dummyString()
  558. return this.finishNode(node, "ExportAllDeclaration")
  559. }
  560. if (this.eat(tokTypes._default)) {
  561. // export default (function foo() {}) // This is FunctionExpression.
  562. var isAsync
  563. if (this.tok.type === tokTypes._function || (isAsync = this.toks.isAsyncFunction())) {
  564. var fNode = this.startNode()
  565. this.next()
  566. if (isAsync) this.next()
  567. node.declaration = this.parseFunction(fNode, null, isAsync)
  568. } else if (this.tok.type === tokTypes._class) {
  569. node.declaration = this.parseClass(null)
  570. } else {
  571. node.declaration = this.parseMaybeAssign()
  572. this.semicolon()
  573. }
  574. return this.finishNode(node, "ExportDefaultDeclaration")
  575. }
  576. if (this.tok.type.keyword || this.toks.isLet() || this.toks.isAsyncFunction()) {
  577. node.declaration = this.parseStatement()
  578. node.specifiers = []
  579. node.source = null
  580. } else {
  581. node.declaration = null
  582. node.specifiers = this.parseExportSpecifierList()
  583. node.source = this.eatContextual("from") ? this.parseExprAtom() : null
  584. this.semicolon()
  585. }
  586. return this.finishNode(node, "ExportNamedDeclaration")
  587. }
  588. lp$1.parseImport = function() {
  589. var node = this.startNode()
  590. this.next()
  591. if (this.tok.type === tokTypes.string) {
  592. node.specifiers = []
  593. node.source = this.parseExprAtom()
  594. node.kind = ''
  595. } else {
  596. var elt
  597. if (this.tok.type === tokTypes.name && this.tok.value !== "from") {
  598. elt = this.startNode()
  599. elt.local = this.parseIdent()
  600. this.finishNode(elt, "ImportDefaultSpecifier")
  601. this.eat(tokTypes.comma)
  602. }
  603. node.specifiers = this.parseImportSpecifierList()
  604. node.source = this.eatContextual("from") && this.tok.type == tokTypes.string ? this.parseExprAtom() : this.dummyString()
  605. if (elt) node.specifiers.unshift(elt)
  606. }
  607. this.semicolon()
  608. return this.finishNode(node, "ImportDeclaration")
  609. }
  610. lp$1.parseImportSpecifierList = function() {
  611. var this$1 = this;
  612. var elts = []
  613. if (this.tok.type === tokTypes.star) {
  614. var elt = this.startNode()
  615. this.next()
  616. elt.local = this.eatContextual("as") ? this.parseIdent() : this.dummyIdent()
  617. elts.push(this.finishNode(elt, "ImportNamespaceSpecifier"))
  618. } else {
  619. var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart
  620. this.pushCx()
  621. this.eat(tokTypes.braceL)
  622. if (this.curLineStart > continuedLine) continuedLine = this.curLineStart
  623. while (!this.closes(tokTypes.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
  624. var elt$1 = this$1.startNode()
  625. if (this$1.eat(tokTypes.star)) {
  626. elt$1.local = this$1.eatContextual("as") ? this$1.parseIdent() : this$1.dummyIdent()
  627. this$1.finishNode(elt$1, "ImportNamespaceSpecifier")
  628. } else {
  629. if (this$1.isContextual("from")) break
  630. elt$1.imported = this$1.parseIdent()
  631. if (isDummy(elt$1.imported)) break
  632. elt$1.local = this$1.eatContextual("as") ? this$1.parseIdent() : elt$1.imported
  633. this$1.finishNode(elt$1, "ImportSpecifier")
  634. }
  635. elts.push(elt$1)
  636. this$1.eat(tokTypes.comma)
  637. }
  638. this.eat(tokTypes.braceR)
  639. this.popCx()
  640. }
  641. return elts
  642. }
  643. lp$1.parseExportSpecifierList = function() {
  644. var this$1 = this;
  645. var elts = []
  646. var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart
  647. this.pushCx()
  648. this.eat(tokTypes.braceL)
  649. if (this.curLineStart > continuedLine) continuedLine = this.curLineStart
  650. while (!this.closes(tokTypes.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
  651. if (this$1.isContextual("from")) break
  652. var elt = this$1.startNode()
  653. elt.local = this$1.parseIdent()
  654. if (isDummy(elt.local)) break
  655. elt.exported = this$1.eatContextual("as") ? this$1.parseIdent() : elt.local
  656. this$1.finishNode(elt, "ExportSpecifier")
  657. elts.push(elt)
  658. this$1.eat(tokTypes.comma)
  659. }
  660. this.eat(tokTypes.braceR)
  661. this.popCx()
  662. return elts
  663. }
  664. var lp$2 = LooseParser.prototype
  665. lp$2.checkLVal = function(expr) {
  666. if (!expr) return expr
  667. switch (expr.type) {
  668. case "Identifier":
  669. case "MemberExpression":
  670. return expr
  671. case "ParenthesizedExpression":
  672. expr.expression = this.checkLVal(expr.expression)
  673. return expr
  674. default:
  675. return this.dummyIdent()
  676. }
  677. }
  678. lp$2.parseExpression = function(noIn) {
  679. var this$1 = this;
  680. var start = this.storeCurrentPos()
  681. var expr = this.parseMaybeAssign(noIn)
  682. if (this.tok.type === tokTypes.comma) {
  683. var node = this.startNodeAt(start)
  684. node.expressions = [expr]
  685. while (this.eat(tokTypes.comma)) node.expressions.push(this$1.parseMaybeAssign(noIn))
  686. return this.finishNode(node, "SequenceExpression")
  687. }
  688. return expr
  689. }
  690. lp$2.parseParenExpression = function() {
  691. this.pushCx()
  692. this.expect(tokTypes.parenL)
  693. var val = this.parseExpression()
  694. this.popCx()
  695. this.expect(tokTypes.parenR)
  696. return val
  697. }
  698. lp$2.parseMaybeAssign = function(noIn) {
  699. if (this.toks.isContextual("yield")) {
  700. var node = this.startNode()
  701. this.next()
  702. if (this.semicolon() || this.canInsertSemicolon() || (this.tok.type != tokTypes.star && !this.tok.type.startsExpr)) {
  703. node.delegate = false
  704. node.argument = null
  705. } else {
  706. node.delegate = this.eat(tokTypes.star)
  707. node.argument = this.parseMaybeAssign()
  708. }
  709. return this.finishNode(node, "YieldExpression")
  710. }
  711. var start = this.storeCurrentPos()
  712. var left = this.parseMaybeConditional(noIn)
  713. if (this.tok.type.isAssign) {
  714. var node$1 = this.startNodeAt(start)
  715. node$1.operator = this.tok.value
  716. node$1.left = this.tok.type === tokTypes.eq ? this.toAssignable(left) : this.checkLVal(left)
  717. this.next()
  718. node$1.right = this.parseMaybeAssign(noIn)
  719. return this.finishNode(node$1, "AssignmentExpression")
  720. }
  721. return left
  722. }
  723. lp$2.parseMaybeConditional = function(noIn) {
  724. var start = this.storeCurrentPos()
  725. var expr = this.parseExprOps(noIn)
  726. if (this.eat(tokTypes.question)) {
  727. var node = this.startNodeAt(start)
  728. node.test = expr
  729. node.consequent = this.parseMaybeAssign()
  730. node.alternate = this.expect(tokTypes.colon) ? this.parseMaybeAssign(noIn) : this.dummyIdent()
  731. return this.finishNode(node, "ConditionalExpression")
  732. }
  733. return expr
  734. }
  735. lp$2.parseExprOps = function(noIn) {
  736. var start = this.storeCurrentPos()
  737. var indent = this.curIndent, line = this.curLineStart
  738. return this.parseExprOp(this.parseMaybeUnary(false), start, -1, noIn, indent, line)
  739. }
  740. lp$2.parseExprOp = function(left, start, minPrec, noIn, indent, line) {
  741. if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) return left
  742. var prec = this.tok.type.binop
  743. if (prec != null && (!noIn || this.tok.type !== tokTypes._in)) {
  744. if (prec > minPrec) {
  745. var node = this.startNodeAt(start)
  746. node.left = left
  747. node.operator = this.tok.value
  748. this.next()
  749. if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) {
  750. node.right = this.dummyIdent()
  751. } else {
  752. var rightStart = this.storeCurrentPos()
  753. node.right = this.parseExprOp(this.parseMaybeUnary(false), rightStart, prec, noIn, indent, line)
  754. }
  755. this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression")
  756. return this.parseExprOp(node, start, minPrec, noIn, indent, line)
  757. }
  758. }
  759. return left
  760. }
  761. lp$2.parseMaybeUnary = function(sawUnary) {
  762. var this$1 = this;
  763. var start = this.storeCurrentPos(), expr
  764. if (this.options.ecmaVersion >= 8 && this.inAsync && this.toks.isContextual("await")) {
  765. expr = this.parseAwait()
  766. sawUnary = true
  767. } else if (this.tok.type.prefix) {
  768. var node = this.startNode(), update = this.tok.type === tokTypes.incDec
  769. if (!update) sawUnary = true
  770. node.operator = this.tok.value
  771. node.prefix = true
  772. this.next()
  773. node.argument = this.parseMaybeUnary(true)
  774. if (update) node.argument = this.checkLVal(node.argument)
  775. expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
  776. } else if (this.tok.type === tokTypes.ellipsis) {
  777. var node$1 = this.startNode()
  778. this.next()
  779. node$1.argument = this.parseMaybeUnary(sawUnary)
  780. expr = this.finishNode(node$1, "SpreadElement")
  781. } else {
  782. expr = this.parseExprSubscripts()
  783. while (this.tok.type.postfix && !this.canInsertSemicolon()) {
  784. var node$2 = this$1.startNodeAt(start)
  785. node$2.operator = this$1.tok.value
  786. node$2.prefix = false
  787. node$2.argument = this$1.checkLVal(expr)
  788. this$1.next()
  789. expr = this$1.finishNode(node$2, "UpdateExpression")
  790. }
  791. }
  792. if (!sawUnary && this.eat(tokTypes.starstar)) {
  793. var node$3 = this.startNodeAt(start)
  794. node$3.operator = "**"
  795. node$3.left = expr
  796. node$3.right = this.parseMaybeUnary(false)
  797. return this.finishNode(node$3, "BinaryExpression")
  798. }
  799. return expr
  800. }
  801. lp$2.parseExprSubscripts = function() {
  802. var start = this.storeCurrentPos()
  803. return this.parseSubscripts(this.parseExprAtom(), start, false, this.curIndent, this.curLineStart)
  804. }
  805. lp$2.parseSubscripts = function(base, start, noCalls, startIndent, line) {
  806. var this$1 = this;
  807. for (;;) {
  808. if (this$1.curLineStart != line && this$1.curIndent <= startIndent && this$1.tokenStartsLine()) {
  809. if (this$1.tok.type == tokTypes.dot && this$1.curIndent == startIndent)
  810. --startIndent
  811. else
  812. return base
  813. }
  814. var maybeAsyncArrow = base.type === "Identifier" && base.name === "async" && !this$1.canInsertSemicolon()
  815. if (this$1.eat(tokTypes.dot)) {
  816. var node = this$1.startNodeAt(start)
  817. node.object = base
  818. if (this$1.curLineStart != line && this$1.curIndent <= startIndent && this$1.tokenStartsLine())
  819. node.property = this$1.dummyIdent()
  820. else
  821. node.property = this$1.parsePropertyAccessor() || this$1.dummyIdent()
  822. node.computed = false
  823. base = this$1.finishNode(node, "MemberExpression")
  824. } else if (this$1.tok.type == tokTypes.bracketL) {
  825. this$1.pushCx()
  826. this$1.next()
  827. var node$1 = this$1.startNodeAt(start)
  828. node$1.object = base
  829. node$1.property = this$1.parseExpression()
  830. node$1.computed = true
  831. this$1.popCx()
  832. this$1.expect(tokTypes.bracketR)
  833. base = this$1.finishNode(node$1, "MemberExpression")
  834. } else if (!noCalls && this$1.tok.type == tokTypes.parenL) {
  835. var exprList = this$1.parseExprList(tokTypes.parenR)
  836. if (maybeAsyncArrow && this$1.eat(tokTypes.arrow))
  837. return this$1.parseArrowExpression(this$1.startNodeAt(start), exprList, true)
  838. var node$2 = this$1.startNodeAt(start)
  839. node$2.callee = base
  840. node$2.arguments = exprList
  841. base = this$1.finishNode(node$2, "CallExpression")
  842. } else if (this$1.tok.type == tokTypes.backQuote) {
  843. var node$3 = this$1.startNodeAt(start)
  844. node$3.tag = base
  845. node$3.quasi = this$1.parseTemplate()
  846. base = this$1.finishNode(node$3, "TaggedTemplateExpression")
  847. } else {
  848. return base
  849. }
  850. }
  851. }
  852. lp$2.parseExprAtom = function() {
  853. var node
  854. switch (this.tok.type) {
  855. case tokTypes._this:
  856. case tokTypes._super:
  857. var type = this.tok.type === tokTypes._this ? "ThisExpression" : "Super"
  858. node = this.startNode()
  859. this.next()
  860. return this.finishNode(node, type)
  861. case tokTypes.name:
  862. var start = this.storeCurrentPos()
  863. var id = this.parseIdent()
  864. var isAsync = false
  865. if (id.name === "async" && !this.canInsertSemicolon()) {
  866. if (this.eat(tokTypes._function))
  867. return this.parseFunction(this.startNodeAt(start), false, true)
  868. if (this.tok.type === tokTypes.name) {
  869. id = this.parseIdent()
  870. isAsync = true
  871. }
  872. }
  873. return this.eat(tokTypes.arrow) ? this.parseArrowExpression(this.startNodeAt(start), [id], isAsync) : id
  874. case tokTypes.regexp:
  875. node = this.startNode()
  876. var val = this.tok.value
  877. node.regex = {pattern: val.pattern, flags: val.flags}
  878. node.value = val.value
  879. node.raw = this.input.slice(this.tok.start, this.tok.end)
  880. this.next()
  881. return this.finishNode(node, "Literal")
  882. case tokTypes.num: case tokTypes.string:
  883. node = this.startNode()
  884. node.value = this.tok.value
  885. node.raw = this.input.slice(this.tok.start, this.tok.end)
  886. this.next()
  887. return this.finishNode(node, "Literal")
  888. case tokTypes._null: case tokTypes._true: case tokTypes._false:
  889. node = this.startNode()
  890. node.value = this.tok.type === tokTypes._null ? null : this.tok.type === tokTypes._true
  891. node.raw = this.tok.type.keyword
  892. this.next()
  893. return this.finishNode(node, "Literal")
  894. case tokTypes.parenL:
  895. var parenStart = this.storeCurrentPos()
  896. this.next()
  897. var inner = this.parseExpression()
  898. this.expect(tokTypes.parenR)
  899. if (this.eat(tokTypes.arrow)) {
  900. // (a,)=>a // SequenceExpression makes dummy in the last hole. Drop the dummy.
  901. var params = inner.expressions || [inner]
  902. if (params.length && isDummy(params[params.length - 1]))
  903. params.pop()
  904. return this.parseArrowExpression(this.startNodeAt(parenStart), params)
  905. }
  906. if (this.options.preserveParens) {
  907. var par = this.startNodeAt(parenStart)
  908. par.expression = inner
  909. inner = this.finishNode(par, "ParenthesizedExpression")
  910. }
  911. return inner
  912. case tokTypes.bracketL:
  913. node = this.startNode()
  914. node.elements = this.parseExprList(tokTypes.bracketR, true)
  915. return this.finishNode(node, "ArrayExpression")
  916. case tokTypes.braceL:
  917. return this.parseObj()
  918. case tokTypes._class:
  919. return this.parseClass(false)
  920. case tokTypes._function:
  921. node = this.startNode()
  922. this.next()
  923. return this.parseFunction(node, false)
  924. case tokTypes._new:
  925. return this.parseNew()
  926. case tokTypes.backQuote:
  927. return this.parseTemplate()
  928. default:
  929. return this.dummyIdent()
  930. }
  931. }
  932. lp$2.parseNew = function() {
  933. var node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart
  934. var meta = this.parseIdent(true)
  935. if (this.options.ecmaVersion >= 6 && this.eat(tokTypes.dot)) {
  936. node.meta = meta
  937. node.property = this.parseIdent(true)
  938. return this.finishNode(node, "MetaProperty")
  939. }
  940. var start = this.storeCurrentPos()
  941. node.callee = this.parseSubscripts(this.parseExprAtom(), start, true, startIndent, line)
  942. if (this.tok.type == tokTypes.parenL) {
  943. node.arguments = this.parseExprList(tokTypes.parenR)
  944. } else {
  945. node.arguments = []
  946. }
  947. return this.finishNode(node, "NewExpression")
  948. }
  949. lp$2.parseTemplateElement = function() {
  950. var elem = this.startNode()
  951. elem.value = {
  952. raw: this.input.slice(this.tok.start, this.tok.end).replace(/\r\n?/g, '\n'),
  953. cooked: this.tok.value
  954. }
  955. this.next()
  956. elem.tail = this.tok.type === tokTypes.backQuote
  957. return this.finishNode(elem, "TemplateElement")
  958. }
  959. lp$2.parseTemplate = function() {
  960. var this$1 = this;
  961. var node = this.startNode()
  962. this.next()
  963. node.expressions = []
  964. var curElt = this.parseTemplateElement()
  965. node.quasis = [curElt]
  966. while (!curElt.tail) {
  967. this$1.next()
  968. node.expressions.push(this$1.parseExpression())
  969. if (this$1.expect(tokTypes.braceR)) {
  970. curElt = this$1.parseTemplateElement()
  971. } else {
  972. curElt = this$1.startNode()
  973. curElt.value = {cooked: '', raw: ''}
  974. curElt.tail = true
  975. this$1.finishNode(curElt, "TemplateElement")
  976. }
  977. node.quasis.push(curElt)
  978. }
  979. this.expect(tokTypes.backQuote)
  980. return this.finishNode(node, "TemplateLiteral")
  981. }
  982. lp$2.parseObj = function() {
  983. var this$1 = this;
  984. var node = this.startNode()
  985. node.properties = []
  986. this.pushCx()
  987. var indent = this.curIndent + 1, line = this.curLineStart
  988. this.eat(tokTypes.braceL)
  989. if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
  990. while (!this.closes(tokTypes.braceR, indent, line)) {
  991. var prop = this$1.startNode(), isGenerator, isAsync, start
  992. if (this$1.options.ecmaVersion >= 6) {
  993. start = this$1.storeCurrentPos()
  994. prop.method = false
  995. prop.shorthand = false
  996. isGenerator = this$1.eat(tokTypes.star)
  997. }
  998. this$1.parsePropertyName(prop)
  999. if (!prop.computed &&
  1000. prop.key.type === "Identifier" && prop.key.name === "async" && this$1.tok.type !== tokTypes.parenL &&
  1001. this$1.tok.type !== tokTypes.colon && !this$1.canInsertSemicolon()) {
  1002. this$1.parsePropertyName(prop)
  1003. isAsync = true
  1004. } else {
  1005. isAsync = false
  1006. }
  1007. if (isDummy(prop.key)) { if (isDummy(this$1.parseMaybeAssign())) this$1.next(); this$1.eat(tokTypes.comma); continue }
  1008. if (this$1.eat(tokTypes.colon)) {
  1009. prop.kind = "init"
  1010. prop.value = this$1.parseMaybeAssign()
  1011. } else if (this$1.options.ecmaVersion >= 6 && (this$1.tok.type === tokTypes.parenL || this$1.tok.type === tokTypes.braceL)) {
  1012. prop.kind = "init"
  1013. prop.method = true
  1014. prop.value = this$1.parseMethod(isGenerator, isAsync)
  1015. } else if (this$1.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
  1016. !prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
  1017. (this$1.tok.type != tokTypes.comma && this$1.tok.type != tokTypes.braceR)) {
  1018. prop.kind = prop.key.name
  1019. this$1.parsePropertyName(prop)
  1020. prop.value = this$1.parseMethod(false)
  1021. } else {
  1022. prop.kind = "init"
  1023. if (this$1.options.ecmaVersion >= 6) {
  1024. if (this$1.eat(tokTypes.eq)) {
  1025. var assign = this$1.startNodeAt(start)
  1026. assign.operator = "="
  1027. assign.left = prop.key
  1028. assign.right = this$1.parseMaybeAssign()
  1029. prop.value = this$1.finishNode(assign, "AssignmentExpression")
  1030. } else {
  1031. prop.value = prop.key
  1032. }
  1033. } else {
  1034. prop.value = this$1.dummyIdent()
  1035. }
  1036. prop.shorthand = true
  1037. }
  1038. node.properties.push(this$1.finishNode(prop, "Property"))
  1039. this$1.eat(tokTypes.comma)
  1040. }
  1041. this.popCx()
  1042. if (!this.eat(tokTypes.braceR)) {
  1043. // If there is no closing brace, make the node span to the start
  1044. // of the next token (this is useful for Tern)
  1045. this.last.end = this.tok.start
  1046. if (this.options.locations) this.last.loc.end = this.tok.loc.start
  1047. }
  1048. return this.finishNode(node, "ObjectExpression")
  1049. }
  1050. lp$2.parsePropertyName = function(prop) {
  1051. if (this.options.ecmaVersion >= 6) {
  1052. if (this.eat(tokTypes.bracketL)) {
  1053. prop.computed = true
  1054. prop.key = this.parseExpression()
  1055. this.expect(tokTypes.bracketR)
  1056. return
  1057. } else {
  1058. prop.computed = false
  1059. }
  1060. }
  1061. var key = (this.tok.type === tokTypes.num || this.tok.type === tokTypes.string) ? this.parseExprAtom() : this.parseIdent()
  1062. prop.key = key || this.dummyIdent()
  1063. }
  1064. lp$2.parsePropertyAccessor = function() {
  1065. if (this.tok.type === tokTypes.name || this.tok.type.keyword) return this.parseIdent()
  1066. }
  1067. lp$2.parseIdent = function() {
  1068. var name = this.tok.type === tokTypes.name ? this.tok.value : this.tok.type.keyword
  1069. if (!name) return this.dummyIdent()
  1070. var node = this.startNode()
  1071. this.next()
  1072. node.name = name
  1073. return this.finishNode(node, "Identifier")
  1074. }
  1075. lp$2.initFunction = function(node) {
  1076. node.id = null
  1077. node.params = []
  1078. if (this.options.ecmaVersion >= 6) {
  1079. node.generator = false
  1080. node.expression = false
  1081. }
  1082. if (this.options.ecmaVersion >= 8)
  1083. node.async = false
  1084. }
  1085. // Convert existing expression atom to assignable pattern
  1086. // if possible.
  1087. lp$2.toAssignable = function(node, binding) {
  1088. var this$1 = this;
  1089. if (!node || node.type == "Identifier" || (node.type == "MemberExpression" && !binding)) {
  1090. // Okay
  1091. } else if (node.type == "ParenthesizedExpression") {
  1092. node.expression = this.toAssignable(node.expression, binding)
  1093. } else if (this.options.ecmaVersion < 6) {
  1094. return this.dummyIdent()
  1095. } else if (node.type == "ObjectExpression") {
  1096. node.type = "ObjectPattern"
  1097. var props = node.properties
  1098. for (var i = 0; i < props.length; i++)
  1099. props[i].value = this$1.toAssignable(props[i].value, binding)
  1100. } else if (node.type == "ArrayExpression") {
  1101. node.type = "ArrayPattern"
  1102. this.toAssignableList(node.elements, binding)
  1103. } else if (node.type == "SpreadElement") {
  1104. node.type = "RestElement"
  1105. node.argument = this.toAssignable(node.argument, binding)
  1106. } else if (node.type == "AssignmentExpression") {
  1107. node.type = "AssignmentPattern"
  1108. delete node.operator
  1109. } else {
  1110. return this.dummyIdent()
  1111. }
  1112. return node
  1113. }
  1114. lp$2.toAssignableList = function(exprList, binding) {
  1115. var this$1 = this;
  1116. for (var i = 0; i < exprList.length; i++)
  1117. exprList[i] = this$1.toAssignable(exprList[i], binding)
  1118. return exprList
  1119. }
  1120. lp$2.parseFunctionParams = function(params) {
  1121. params = this.parseExprList(tokTypes.parenR)
  1122. return this.toAssignableList(params, true)
  1123. }
  1124. lp$2.parseMethod = function(isGenerator, isAsync) {
  1125. var node = this.startNode(), oldInAsync = this.inAsync
  1126. this.initFunction(node)
  1127. if (this.options.ecmaVersion >= 6)
  1128. node.generator = !!isGenerator
  1129. if (this.options.ecmaVersion >= 8)
  1130. node.async = !!isAsync
  1131. this.inAsync = node.async
  1132. node.params = this.parseFunctionParams()
  1133. node.expression = this.options.ecmaVersion >= 6 && this.tok.type !== tokTypes.braceL
  1134. node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
  1135. this.inAsync = oldInAsync
  1136. return this.finishNode(node, "FunctionExpression")
  1137. }
  1138. lp$2.parseArrowExpression = function(node, params, isAsync) {
  1139. var oldInAsync = this.inAsync
  1140. this.initFunction(node)
  1141. if (this.options.ecmaVersion >= 8)
  1142. node.async = !!isAsync
  1143. this.inAsync = node.async
  1144. node.params = this.toAssignableList(params, true)
  1145. node.expression = this.tok.type !== tokTypes.braceL
  1146. node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
  1147. this.inAsync = oldInAsync
  1148. return this.finishNode(node, "ArrowFunctionExpression")
  1149. }
  1150. lp$2.parseExprList = function(close, allowEmpty) {
  1151. var this$1 = this;
  1152. this.pushCx()
  1153. var indent = this.curIndent, line = this.curLineStart, elts = []
  1154. this.next() // Opening bracket
  1155. while (!this.closes(close, indent + 1, line)) {
  1156. if (this$1.eat(tokTypes.comma)) {
  1157. elts.push(allowEmpty ? null : this$1.dummyIdent())
  1158. continue
  1159. }
  1160. var elt = this$1.parseMaybeAssign()
  1161. if (isDummy(elt)) {
  1162. if (this$1.closes(close, indent, line)) break
  1163. this$1.next()
  1164. } else {
  1165. elts.push(elt)
  1166. }
  1167. this$1.eat(tokTypes.comma)
  1168. }
  1169. this.popCx()
  1170. if (!this.eat(close)) {
  1171. // If there is no closing brace, make the node span to the start
  1172. // of the next token (this is useful for Tern)
  1173. this.last.end = this.tok.start
  1174. if (this.options.locations) this.last.loc.end = this.tok.loc.start
  1175. }
  1176. return elts
  1177. }
  1178. lp$2.parseAwait = function() {
  1179. var node = this.startNode()
  1180. this.next()
  1181. node.argument = this.parseMaybeUnary()
  1182. return this.finishNode(node, "AwaitExpression")
  1183. }
  1184. // Acorn: Loose parser
  1185. //
  1186. // This module provides an alternative parser (`parse_dammit`) that
  1187. // exposes that same interface as `parse`, but will try to parse
  1188. // anything as JavaScript, repairing syntax error the best it can.
  1189. // There are circumstances in which it will raise an error and give
  1190. // up, but they are very rare. The resulting AST will be a mostly
  1191. // valid JavaScript AST (as per the [Mozilla parser API][api], except
  1192. // that:
  1193. //
  1194. // - Return outside functions is allowed
  1195. //
  1196. // - Label consistency (no conflicts, break only to existing labels)
  1197. // is not enforced.
  1198. //
  1199. // - Bogus Identifier nodes with a name of `"✖"` are inserted whenever
  1200. // the parser got too confused to return anything meaningful.
  1201. //
  1202. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  1203. //
  1204. // The expected use for this is to *first* try `acorn.parse`, and only
  1205. // if that fails switch to `parse_dammit`. The loose parser might
  1206. // parse badly indented code incorrectly, so **don't** use it as
  1207. // your default parser.
  1208. //
  1209. // Quite a lot of acorn.js is duplicated here. The alternative was to
  1210. // add a *lot* of extra cruft to that file, making it less readable
  1211. // and slower. Copying and editing the code allowed me to make
  1212. // invasive changes and simplifications without creating a complicated
  1213. // tangle.
  1214. defaultOptions.tabSize = 4
  1215. function parse_dammit(input, options) {
  1216. var p = new LooseParser(input, options)
  1217. p.next()
  1218. return p.parseTopLevel()
  1219. }
  1220. addLooseExports(parse_dammit, LooseParser, pluginsLoose)
  1221. export { parse_dammit, LooseParser, pluginsLoose };