CoffeeScriptRedux icon indicating copy to clipboard operation
CoffeeScriptRedux copied to clipboard

Legal semicolon at the end of file leads to "unexpected end of input"

Open jtmalinowski opened this issue 11 years ago • 11 comments

To reproduce:

create a file with following contents:

a = 1
require('./main2');

observe error:

/home/developer/.nvm/v0.8.20/lib/node_modules/commonjs-everywhere/node_modules/coffee-script-redux/lib/module.js:73
      throw new Error(formatParserError(preprocessed, e));
            ^
Error: Syntax error on line 2, column 19: unexpected end of input
    at Object.module.exports.parse (/home/developer/.nvm/v0.8.20/lib/node_modules/commonjs-everywhere/node_modules/coffee-script-redux/lib/module.js:73:13)
    at Object.exports.cjsify.handlers..coffee (/home/developer/.nvm/v0.8.20/lib/node_modules/commonjs-everywhere/lib/index.js:138:50)
    at Object.exports.cjsify (/home/developer/.nvm/v0.8.20/lib/node_modules/commonjs-everywhere/lib/index.js:172:101)
    at Object. (/home/developer/.nvm/v0.8.20/lib/node_modules/commonjs-everywhere/lib/command.js:29:28)
    at Object. (/home/developer/.nvm/v0.8.20/lib/node_modules/commonjs-everywhere/lib/command.js:75:3)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)

if you change to

require('./main');
a = 1

it works fine, should show no error for 1. case

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/825325-legal-semicolon-at-the-end-of-file-leads-to-unexpected-end-of-input?utm_campaign=plugin&utm_content=tracker%2F33145&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F33145&utm_medium=issues&utm_source=github).

jtmalinowski avatar Mar 04 '13 20:03 jtmalinowski

; is a binary operator in coffee-script. I don't believe @michaelficarra wants it to be able to be used as a null statement but I could be wrong.

mehcode avatar Mar 04 '13 20:03 mehcode

Looks fine to me. The semicolon is CoffeeScript's , operator. You wouldn't expect this JavaScript program to work, would you?

a = 1
require('./main2'),

edit: @mehcode just beat me to it.

michaelficarra avatar Mar 04 '13 20:03 michaelficarra

ok, my bad. Is original coffee compiler wrong then?

jtmalinowski avatar Mar 04 '13 21:03 jtmalinowski

In this case, I'd say it depends who you ask. I have no qualms about not supporting trailing semicolons, though. If that's how you were writing your CoffeeScript code before, you were doing it wrong.

michaelficarra avatar Mar 04 '13 21:03 michaelficarra

nope its just that I was re writing some js and forgot to remove the semicolon. browserify was fine with it but commonjs-everywhere not, coffee pushed it but redux not, that confused me. thanks for help

jtmalinowski avatar Mar 04 '13 21:03 jtmalinowski

Ah, I see how that could happen. My editor actually points out trailing semicolons in bright red. Well, good luck with commonjs-everywhere!

michaelficarra avatar Mar 04 '13 21:03 michaelficarra

you were doing it wrong.

In your opinion. The way it is now it will break code for no reason. I personally believe carrying this over from CS is harmless.

On Mon, Mar 4, 2013 at 1:29 PM, Michael Ficarra [email protected]:

Ah, I see how that could happen. My editor actually points out trailing semicolons in bright red. Well, good luck with commonjs-everywhere!

— Reply to this email directly or view it on GitHubhttps://github.com/michaelficarra/CoffeeScriptRedux/issues/174#issuecomment-14406776 .

mark-hahn avatar Mar 04 '13 22:03 mark-hahn

I'm with @mark-hahn on this one. The CoffeeScript documentation mentions semicolons pretty early (so this is something many people will have read and assimilated):

First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code. You don't need to use semicolons ; to terminate expressions, ending the line will do just as well (although semicolons can still be used to fit multiple expressions onto a single line).

So, my interpretation from that is that semicolons are optional statement delimiters, that can be used instead of newlines. I see no reason to take them as operators. And i think their rules regarding semicolons on the original CS compiler are pretty consistent:

# Just like you can use newlines to separate statements in a block...
if foo
  bar()
  baz()

# ... you can also use semicolons.
if foo
  bar(); baz()

# And just like you can have more than one newline between statements 
# and the code will mean the same...
if foo
  bar()

  baz()

# ... you can have more than one semicolon.
if foo
  bar();; baz();;;;

So i see no reason to prohibit semicolons at the end of the last statement of a block.

epidemian avatar Mar 05 '13 00:03 epidemian

@epidemian: Actually, I think that quote makes my point perfectly. But really, I don't care either way. I could add a special exception that allows trailing semicolons for blocks, and I could change the ; operator to read one or more semicolons. It's not going to be high priority, though. Code like that really disgusts me.

michaelficarra avatar Mar 05 '13 01:03 michaelficarra

My perfect code must compile.

bar = (x) ->
  x+=32;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;;;;;;;x+3;;;;;;;;x-=4;;;
  ;;;;;;;;;;;;;;;;;;x==3;;;;;;
  ;;;;;;;;x-51;;;;;;;;;;;;;;;;

In all seriousness I suppose it does make sense to allow the semicolon operator to read one or more semicolons as people are just used to doing that (though I'm not a fan I must say).


although semicolons can still be used to fit multiple expressions onto a single line

Just to be clear, redux is doing this. Note that the quote doesn't state the semicolon can be used as the null statement or that empty statements are valid (nor does the document state the latter anywhere).

mehcode avatar Mar 05 '13 02:03 mehcode

@mehcode: I couldn't have put it better myself.

michaelficarra avatar Mar 05 '13 02:03 michaelficarra