-
Notifications
You must be signed in to change notification settings - Fork 2k
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
possible to shadow _arg in parameter list #1574
Comments
compiles to:
|
Same is true for
...compiles to
Fix in #1543 -- #1002 identical params (revised) . That pull was closed since prohibiting duplicate param names depends on CoffeeScript's adoption of Fixing |
This is strictly speaking not exactly a bug ... we've been using |
+1 for two-pass variable naming (or some other approach that accomplishes the same thing) |
+1, and here's the example that led me here:
allows reliance on |
How about making the internal variables used reserved in some way? You could issue a warning or an error when they're used. It's the unexpected behaviour that's the problem, much more so than not being able to use certain words as variable names. |
@almost: Ideally, you should be able to use any variable name you wish, as long as it doesn't conflict with a CS keyword. We already do something like you suggest, though: https://github.com/jashkenas/coffee-script/blob/5d7a83468abce3abe5ac236342201dd3ce6653a7/src/lexer.coffee#L580-581 |
I agree that ideally you should be able to. But right now you can't, and it's not obvious that you can't. I think it would be a good idea to add the extra special variables that CS uses into that list of reserved words. I guess it would break code that uses those variable names in contexts where they are allowed, but code like that is already skating on thin ice. |
@almost: submit a pull request. |
Just started. What led me here was this: foo = (_results for _results in [1..2]) #OK
(_this) => @ #NOT OK
// generated
var foo, _results,
_this = this;
foo = (function() {
var _i, _results1;
_results1 = [];
for (_results = _i = 1; _i <= 2; _results = ++_i) {
_results1.push(_results);
}
return _results1;
})();
(function(_this) {
return _this;
}); What concerned me more was the inconsistency (was _this overlooked?). If there's a certain group of keywords to avoid, that's fine. But it looks like sometimes there's a safety net and sometimes not. |
…rs uniquely Any variables generated by CoffeeScript are now made sure to be named to something not present in the source code being compiled. This way you can no longer interfere with them, either on purpose or by mistake. (jashkenas#1500, jashkenas#1574) For example, `({a}, _arg) ->` now compiles correctly. (jashkenas#1574) As opposed to the somewhat complex implementations discussed in jashkenas#1500, this commit takes a very simple approach by saving all used variables names using a single pass over the token stream. Any generated variables are then made sure not to exist in that list. `(@A) -> a` used to be equivalent to `(@A) -> @a`, but now throws a runtime `ReferenceError` instead (unless `a` exists in an upper scope of course). (jashkenas#3318) `(@A) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to `(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either, of course.) Because of the above, `(@A, a) ->` is now valid; `@a` and `a` are not duplicate parameters. Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`, used to compile but now throws, just like regular duplicate parameters.
…rs uniquely Any variables generated by CoffeeScript are now made sure to be named to something not present in the source code being compiled. This way you can no longer interfere with them, either on purpose or by mistake. (jashkenas#1500, jashkenas#1574) For example, `({a}, _arg) ->` now compiles correctly. (jashkenas#1574) As opposed to the somewhat complex implementations discussed in jashkenas#1500, this commit takes a very simple approach by saving all used variables names using a single pass over the token stream. Any generated variables are then made sure not to exist in that list. `(@A) -> a` used to be equivalent to `(@A) -> @a`, but now throws a runtime `ReferenceError` instead (unless `a` exists in an upper scope of course). (jashkenas#3318) `(@A) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to `(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either, of course.) Because of the above, `(@A, a) ->` is now valid; `@a` and `a` are not duplicate parameters. Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`, used to compile but now throws, just like regular duplicate parameters.
Fixed by #3784. |
should alert 3, alerts undefined.
The text was updated successfully, but these errors were encountered: