-
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
constructor renaming parameters #4865
Comments
It's been doing this since 1.9.0, 29jan 2015. |
Is there any documentation or issue as to why coffeescript does this? I can understand why it may be necessary if the variable is declared outside the class, but inside the function doesn't seem to make any sense. |
@crisward Why does it matter to you what a generated variable name looks like? Could you show an example? |
We use a dependency injection system which parses the output of the constructor to know what modules to inject (a bit like Angularjs did). This can't find a module with a 1 at the end. |
Just checked and coffeescript 1.9.1 didn't do this. |
Just search for “angular” in this issue tracker and you’ll find everything you need. |
CoffeeScript 1.9.1 did do that. |
Also, how does your DI deal with minified code? I know that was an issue in AngularJS and that's why people ultimately stopped doing it (... or developed modules to do it automatically). |
It's node, so doesn't need to be minified. Also you can just not mangle in uglify for client side stuff. I have a codebase I'm converting from 1.9.1 to 2.* and it's breaking in lots of places. So there is a difference somewhere. I'll take a look around the issues and see if I can get a work around. The other option I have is to avoid using a lib name in my code, just seems a bit unnecessary. BTW thanks for the rapid response. |
Just to be clear: if you write |
@vendethiel That is super fragile though becase if you name a variable |
@vendethiel thanks for the help, I looked at some of the other issues. I'm injecting @request, and using the variable name request in a method, which causes @request to become @Request1. I think the simplest solution as I'm migrating old code is just to add an alias to my injector for generic library names to be added with the additional '1' on the end. Request is the main one as it's a commonly used variable name in node code. Thanks. |
It definitely is. That's why I recommend against it. It's just to help migrate code. |
@crisward It is not necessarily a A more robust solution is: class A
m: (a) ->
@a = a But you’ll probably get away with the |
If I had to do that, I'd probably just give up and use plain js 😢 |
I seem to recall that when this change was made it was reduced to only cover cases where the same name is used within the function (e.g. |
@connec The code simply gets a list of all used identifiers in the whole file and then avoids using those for generated variable names. Why? Because it’s simple. Tracking scope is more complicated for no gain. And even if we did that it wouldn’t help if the name is in the same scope. |
I see, we only keep track of assignments in |
In my example the variable name is not in the same scope, but I understand the need for simplicity. |
Hi @GeoffreyBooth, maybe this should be reopened: The above comments said narrowing down the scope tracking would be unnecessary. However, @robert-boulanger brought up a very valid use case, explained here: JSDoc. for example class ClassTest
###*
* @param {object} option
###
constructor: (@option,@callMe, name)->
option = 1
|
I'm working on a fix to this (it's relatively simple), but am running into an issue with the current codebase: Update: the infinite loop is caused by tests failing in a bad way; never mind! |
What are you going to do in this case? option = 1
class ClassTest
###*
* @param {object} option
###
constructor: (@option,@callMe, name)->
console.log("sum", @option + option)
new ClassTest(5, "", "") # Should log `sum 6` |
@lydell In that case, my code uses |
Instead of a global `referencedVars` option that is computed at the lexer level (via all IDENTIFIER tokens), recurse through the AST to mark each scope with all variables used within it and descendant scopes. Likely also more efficient via `Set`s instead of `Array`s. Also fix some missing `children` in `For` nodes, which otherwise caused this rewrite to fail. Fixes jashkenas#4865 by causing parameter renaming in fewer (more predictable) scenarios.
For those following this issue, #5398 is the code I was describing above, which I believe fixes this issue where possible. |
If you do this
Coffeescript compiles to
Why the 1 after the thing?? I'm using an dependency injection system, and the rename is playing havoc with it. Coffeescript 1 didn't do this.
The text was updated successfully, but these errors were encountered: