From cc81f868e3f2b22de24bdeb0838032f5e2df1ebd Mon Sep 17 00:00:00 2001 From: "William C. Johnson" Date: Fri, 6 Oct 2017 17:30:54 -0400 Subject: [PATCH] Eliminate nested block level tracking See https://github.com/wcjohnson/lightscript/issues/33 --- src/parser/statement.js | 2 -- src/plugins/bangCall.js | 14 ++++++++------ src/plugins/lightscript.js | 4 ---- src/tokenizer/state.js | 1 - 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index 57845ed916..0356e712ad 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -663,7 +663,6 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) { const oldInWhiteBlock = this.state.inWhiteBlock; const oldWhiteBlockIndentLevel = this.state.whiteBlockIndentLevel; - this.state.nestedBlockLevel++; let isEnd; if (this.hasPlugin("lightscript") && typeof end === "number") { @@ -701,7 +700,6 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) { node.body.push(stmt); } - this.state.nestedBlockLevel--; if (this.hasPlugin("lightscript")) { this.state.inWhiteBlock = oldInWhiteBlock; this.state.whiteBlockIndentLevel = oldWhiteBlockIndentLevel; diff --git a/src/plugins/bangCall.js b/src/plugins/bangCall.js index f21200d92b..919b2d721e 100644 --- a/src/plugins/bangCall.js +++ b/src/plugins/bangCall.js @@ -40,13 +40,15 @@ export default function(parser) { this.unexpected(null, "Whitespace required between `!` and first argument."); } - // Read args - let first = true; + // Collect state const oldBangUnwindLevel = this.state.bangUnwindLevel; - const oldBangBlockLevel = this.state.bangBlockLevel; - this.state.bangBlockLevel = this.state.nestedBlockLevel; this.state.bangUnwindLevel = bangIndentLevel + 1; + const oldBangWhiteBlockLevel = this.state.bangWhiteBlockLevel; + this.state.bangWhiteBlockLevel = this.state.whiteBlockIndentLevel; + + // Read args + let first = true; while (true) { // First argument on a different line from the `!` establishes indent level if (this.state.curLine !== bangLine && argIndentLevel === null) { @@ -83,7 +85,7 @@ export default function(parser) { } this.state.bangUnwindLevel = oldBangUnwindLevel; - this.state.bangBlockLevel = oldBangBlockLevel; + this.state.bangWhiteBlockLevel = oldBangWhiteBlockLevel; node = this.finishNode(node, nodeType); @@ -97,7 +99,7 @@ export default function(parser) { // Subscripts to a bang call must appear at the arg indent level pp.shouldUnwindBangSubscript = function() { return this.isLineBreak() && - (this.state.bangBlockLevel == this.state.nestedBlockLevel) && + (this.state.bangWhiteBlockLevel === this.state.whiteBlockIndentLevel) && (this.state.indentLevel <= this.state.bangUnwindLevel); }; } diff --git a/src/plugins/lightscript.js b/src/plugins/lightscript.js index ccedac4fed..f87a720b80 100644 --- a/src/plugins/lightscript.js +++ b/src/plugins/lightscript.js @@ -140,9 +140,7 @@ pp.parseObjectComprehension = function(node) { pp.parseInlineWhiteBlock = function(node) { if (this.state.type.startsExpr) return this.parseMaybeAssign(); // oneline statement case - this.state.nestedBlockLevel++; node.body = [this.parseStatement(true)]; - this.state.nestedBlockLevel--; node.directives = []; this.addExtra(node, "curly", false); return this.finishNode(node, "BlockStatement"); @@ -167,9 +165,7 @@ pp.parseWhiteBlock = function (isExpression?) { return this.parseInlineWhiteBlock(node); } - this.state.nestedBlockLevel++; const stmt = this.parseStatement(false); - this.state.nestedBlockLevel--; return stmt; } diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index bdcd1c0d67..9e19e9f21c 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -40,7 +40,6 @@ export default class State { // for lightscript this.indentLevel = 0; - this.nestedBlockLevel = 0; this.inMatchCaseTest = false; this.type = tt.eof;