-
Notifications
You must be signed in to change notification settings - Fork 2k
Detect when from
in a for
loop declaration is an identifier
#4393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect when from
in a for
loop declaration is an identifier
#4393
Conversation
…r, not a keyword
I can't think of more Other than that this looks good to me. |
This bug is only because the lexer (previously) was declaring a
|
@IvanVergiliev or @vendethiel, can you think of any other cases where |
# `export` statements (handled above) and in the declaration line of a `for` | ||
# loop. Try to detect when `from` is a variable identifier and when it is this | ||
# “sometimes” keyword. | ||
fromIsKeywordInForDeclaration = (prev) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: would isForFrom
be an acceptable, more concise name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes 👍
else if prev[0] is 'FOR' | ||
no | ||
# `for {from}…`, `for [from]…` | ||
else if prev[1] in ['{', '['] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: if prev[1] in '{['
would save an array allocation (not really sure if that's meaningful).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if prev[1] in '{['
looks confusing to me. The way I wrote it seems to be the style of the codebase.
Thanks for the quick fix! How about destructuring with aliasing (not sure if that's the right term): for {b: from} in a
print from ? This is broken right now as well, and I think there might have to be a separate check for it in the lexer. (Also, all this special casing is making me a bit sad, but I guess there's no other way to detect this so it's much better than before.) |
./bin/coffee -bce 'for {a: from} from 1 then 1'
[stdin]:1:9: error: unexpected from
for {a: from} from 1 then 1
^^^^
./bin/coffee -bce 'for [a: from] from 1 then 1'
[stdin]:1:9: error: unexpected from
for [a: from] from 1 then 1
^^^^ Somewhat reminds me of satyr/coco#195 (though we don't have this exact issue when you can have a typeless |
Thanks, anything else? |
I can't think of any other cases. |
@lydell we should probably release 1.12.1. I can prep it tonight. |
Per #4390, this should be valid:
But it throws an error on thinking that
from
is a keyword. Sincefrom
has never been a reserved word (outside the context of animport
orexport
statement), we should try to detect when afrom
on afor
declaration line is really a keyword or rather a variable identifier.I’ve tried to make the detection by looking at the previous token. If the previous token is an identifier itself, like
for a from b
I think that that would mean that the presentfrom
is always a keyword. If the previous token is{
or[
or,
, then we’re inside destructuring andfrom
is always an identifier. If the previous token value isfrom
, we havefor from from a
orfor a from from
and the firstfrom
is an identifier and the second is a keyword.Are there any other cases I haven’t thought of?