Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improved semicolon handling for return and for statements

  • Loading branch information...
commit 6ceabd43426ef8713534e5db8f40999a424e5ad3 1 parent 3f15ca0
@rcgill authored
Showing with 28 additions and 27 deletions.
  1. +28 −27 lib/parse.js
View
55 lib/parse.js
@@ -140,26 +140,6 @@ define(["./types", "./asn"], function(types, asn) {
}
},
- maybeBeforeSemicolon= function(tryBranch) {
- if (token.puncEq(";")) {
- return [0, getTokenAndAdvance()];
- } else {
- var start= token;
- try {
- var result = tryBranch.call();
- result.optionalSemicolon = expectSemicolon();
- return result;
- } catch (e) {
- //TODO--revisit this
- if (token===start && token.newlineBefore && !strictSemicolons) {
- return [0, 0];
- } else {
- throw e;
- }
- }
- }
- },
-
vardefs= function() {
eatComments();
if (token.type!==tName) {
@@ -285,7 +265,7 @@ define(["./types", "./asn"], function(types, asn) {
},
"for": function(forToken) {
- var leftParen = expectPunc("("),
+ var rightParen, leftParen = expectPunc("("),
varToken= 0;
if (token.keywordEq("var")) {
varToken= getTokenAndAdvance();
@@ -293,13 +273,23 @@ define(["./types", "./asn"], function(types, asn) {
if (token.type===tName && peek().opEq("in")) {
var varNameToken= getTokenAndAdvance(),
inToken = next(),
- objectExpr= expression(),
- rightParen = expectPunc(")");
+ objectExpr= expression();
+ rightParen = expectPunc(")");
return new asn.ForInNode(forToken, leftParen, varToken, varNameToken, inToken, objectExpr, rightParen, statement());
} else {
- var
- init= maybeBeforeSemicolon(varToken ? vardefs : expression),
- test= maybeBeforeSemicolon(expression),
+ var init, test, step;
+ if (token.puncEq(";")) {
+ init= getTokenAndAdvance();
+ } else {
+ init= varToken ? vardefs() : expression();
+ init.semicolon = expectPunc(";");
+ }
+ if (token.puncEq(";")) {
+ test= getTokenAndAdvance();
+ } else {
+ test= expression();
+ test.semicolon = expectPunc(";");
+ }
step= token.puncEq(")") ? 0 : expression(),
rightParen= expectPunc(")");
return new asn.ForNode(forToken, leftParen, varToken, init, test, step, rightParen, statement());
@@ -362,9 +352,20 @@ define(["./types", "./asn"], function(types, asn) {
},
"return": function(returnToken) {
- return new asn.ReturnNode(returnToken, maybeBeforeSemicolon(expression));
+ var expr;
+ if (token.puncEq(";")) {
+ expr = getTokenAndAdvance();
+ }else if((token.puncEq("}") || token.newlineBefore) && !strictSemicolons){
+ // return expr as undefined
+ }else{
+ expr = expression();
+ expr.optionalSemicolon = expectSemicolon();
+ }
+ return new asn.ReturnNode(returnToken, expr);
},
+
+
"switch": function(switchToken) {
var
switchExpr= parenthesized(),
Please sign in to comment.
Something went wrong with that request. Please try again.