rokucommunity / brightscript-formatter

This project is no longer maintained. Its successor is brighterscript-formatter
https://github.com/RokuCommunity/brighterscript-formatter
Apache License 2.0
5 stars 5 forks source link

TabCount is less than zero error #2

Closed crwecker closed 6 years ago

crwecker commented 6 years ago

I'm getting the following error in the following situations:

/Users/carl/Workspace/node_modules/brightscript-formatter/dist/BrightScriptFormatter.js:189 throw new Error('TabCount is less than zero for ' + JSON.stringify(lineTokens)); ^

Error: TabCount is less than zero for [{"tokenType":"endSub","value":"end sub","startIndex":7322},{"tokenType":"newline","value":"\n","startIndex":7329}] at BrightScriptFormatter.formatIndentation (/Users/carl/Workspace/node_modules/brightscript-formatter/dist/BrightScriptFormatter.js:189:23) at BrightScriptFormatter.format (/Users/carl/Workspace/node_modules/brightscript-formatter/dist/BrightScriptFormatter.js:22:27) at /Users/carl/Workspace/roku/VidAngelRoku/brightscript-format.js:17:20 at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

  1. When I have an else statement followed immediately by another if statement :
    if (a) then
    some code...
    else
    if (b) then
    more code...
    end if
    more code...
    end if
  2. When the function .next is called on an ifEnum object
    m.top.returnString = m.someArray.next
TwitchBronBron commented 6 years ago

The error "TabCount is less than zero for..." is when the formatter "closes" more indentation blocks than it opens. I would expect the following invalid code to produce that error as well.

if true then
   doSomething()
end if
end if

However, I'm sure it's a bug where the code is not properly determining when a block opens and closes.

For item 2, I'll bet the bug is related to the word "next" being a keyword. I'll have to tighten up the tokenizer's regex matchers.

I'll try reproducing these locally and see if I can come up with a fix.

Thanks for passing these along!

TwitchBronBron commented 6 years ago

The issue with item 1 is that the tokenizer is converting

else
    if

into

else if

Which is obviously not correct.

Similar issue with item 2. The tokenizer is identifying "next" as a keyword, when it should be an identifier based on where it's placed.

Good catches!

TwitchBronBron commented 6 years ago

@crwecker I believe that I fixed both of these issues and published to npm. Upgrade to version 1.1.1 and let me know how it goes!

crwecker commented 6 years ago

Worked great for the couple of files I checked. Thanks again for putting this together and for being so responsive!!

TwitchBronBron commented 6 years ago

You're quite welcome. It's nice knowing someone else other than myself is using this. :D