Parse error: Unexpected 'FORIN'
Hi there,
I use the "value in [array]" or "char in string" syntax extensively and it never failed me. Except when compiling the following file:
###
Auto-generated from Core/Controls/Test.ctl
###
#if server
require '../../../_jscript/shared/Control'
#endif
smio = smoothio = global.smoothio
class smio.Packs_Core_Controls_Test extends smio.Control
test: (xyz) =>
arr = [1, 'a', 3]
x = 'a'
if (x in arr)
alert(345)
constructor: (client, parent, args) ->
super client, parent, args
@init()
className: ->
"Core_Controls_Test"
classNamespace: ->
"Core_Controls"
It's the test() function where the parse error (unexpected FORIN) occurs at the (x in arr) line.
In a normal non-class Coffee file the test() function compiles fine. In a normal CoffeeScript class (as a 'method') it compiles fine. Only in my auto-generated inheriting-class-files does the compiler raise this parse error. However if I simply replace the (x in arr) check with a normal .indexOf >= 0 then no compiler error occurs and it gets compiled to JS perfectly. And I do not see any other Coffeescript syntax error in the above. My 10+ auto-generated classes have compiled to JS just fine in the last few weeks so pretty sure I'm not "generating" (string-merging) invalid Coffeescript code. Only the (x in arr) line busts the parser. But if you can find the issue in the generated code, that's fine too.
It compiles fine for me. You don't need the parentheses for if x in arr, by the way. Can you give a more concrete test case?
Sounds like a duplicate of #1436 (my pull request for a fix is #1448), but I think that you altered your code - did you maybe write something like if x.for in arr?
Hi thejh, I definitely did not write or generate anything like if x.for in arr...
@michaelficarra, the "more concrete" problematic file is at:
http://dl.dropbox.com/u/136375/_ctl_Test.coffee
First off, you're right: this compiles fine with NO problems using the command-line coffee -c command.
BUT when passing the full text content of this to require('coffee-script').compile() -- which is the actual use-case here -- this throws the 'Unexpected FORIN'.
@pearjoint: can you provide the output of executing coffee --version in your terminal and require("coffee-script").VERSION in a node shell?
Sure!
Terminal:
$ node_modules/coffee-script/bin/coffee --version CoffeeScript version 1.1.1
Node shell:
require('coffee-script').VERSION '1.1.1'
@pearjoint: I can't reproduce your test case. You may not have isolated it properly. I'm going to close this until either someone else reports that they can reproduce it or you can provide a javascript file that I can execute under node to reproduce the issue.
All you need to execute is require('coffee-script').compile(readTextFileSync('_ctl_Test.coffee')) ... are you saying you're NOT getting the FORIN issue if you do exactly that and nothing else?
This is only about the compile ie. the code transform of the test-case file so no need for any of its run-time "dependencies" or whatever. This should compile to JS no matter its runtime semantics -- which is something the coffee command does but Coffee's compile() function doesn't, at my end. If the above single Node.js code line succeeds for you, OK. But let me know if this is exactly what you tried -- or if possible, give it a try...
Thanks a lot Michael!
require('coffee-script').compile(require('fs').readFileSync('_ctl_Test.coffee').toString())
No errors. Good output.
OK, thanks for taking the time to dig into this -- guess I'll go with indexOf for that particular file then :)
@pearjoint: I think it'd instead be wise to find the real source of your problem unless you want it affecting you again some time in the future.