Upgrade to https://github.com/acornjs/acorn.git commit 84eda6bf
[jst.git] / src / tokencontext.js
index 6a89c7f..4df2b41 100644 (file)
@@ -2,9 +2,9 @@
 // given point in the program is loosely based on sweet.js' approach.
 // See https://github.com/mozilla/sweet.js/wiki/design
 
-import {Parser} from "./state"
-import {types as tt} from "./tokentype"
-import {lineBreak} from "./whitespace"
+import {Parser} from "./state.js"
+import {types as tt} from "./tokentype.js"
+import {lineBreak} from "./whitespace.js"
 
 export class TokContext {
   constructor(token, isExpr, preserveSpace, override, generator) {
@@ -35,6 +35,10 @@ pp.initialContext = function() {
   return [types.b_stat]
 }
 
+pp.curContext = function() {
+  return this.context[this.context.length - 1]
+}
+
 pp.braceIsBlock = function(prevType) {
   let parent = this.curContext()
   if (parent === types.f_expr || parent === types.f_stat)
@@ -51,7 +55,7 @@ pp.braceIsBlock = function(prevType) {
     return true
   if (prevType === tt.braceL)
     return parent === types.b_stat
-  if (prevType === tt._var || prevType === tt.name)
+  if (prevType === tt._var || prevType === tt._const || prevType === tt.name)
     return false
   return !this.exprAllowed
 }
@@ -75,6 +79,13 @@ pp.updateContext = function(prevType) {
     this.exprAllowed = type.beforeExpr
 }
 
+// Used to handle egde case when token context could not be inferred correctly in tokenize phase
+pp.overrideContext = function(tokenCtx) {
+  if (this.curContext() !== tokenCtx) {
+    this.context[this.context.length - 1] = tokenCtx
+  }
+}
+
 // Token-specific context update code
 
 tt.parenR.updateContext = tt.braceR.updateContext = function() {
@@ -110,7 +121,9 @@ tt.incDec.updateContext = function() {
 }
 
 tt._function.updateContext = tt._class.updateContext = function(prevType) {
-  if (prevType.beforeExpr && prevType !== tt.semi && prevType !== tt._else &&
+  if (prevType.beforeExpr && prevType !== tt._else &&
+      !(prevType === tt.semi && this.curContext() !== types.p_stat) &&
+      !(prevType === tt._return && lineBreak.test(this.input.slice(this.lastTokEnd, this.start))) &&
       !((prevType === tt.colon || prevType === tt.braceL) && this.curContext() === types.b_stat))
     this.context.push(types.f_expr)
   else