-
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
Class name mangled after minification #2052
Comments
I would be in favour of |
Something to consider: |
@kitcambridge: Huh. That's interesting, since we're already assigning the edit: We could additionally include |
Assigning .name property would be very confusing since this leads to different behavior in IE and other browsers. It's especially notable when everything stops working right after auto-minification on the production. IMHO the current fix on #494 make the situation way worse then it was before. With that you have even more chances to stuck into reality much too late. My vote goes to |
+1. Or possibly just |
@jamesmacaulay Good thinking, but I think it goes along more with |
@jashkenas: do you have enough information to make a decision for us here? |
Ugh -- this is bad news. I now wish I had stuck to my guns on #494, and left forced class names out. Let's remove them at our nearest convenience. |
@jashkenas: Do you want to define an alternative property or just remove it entirely? |
I'd like to remove it entirely. If you want to tag a name on your class -- tag a name on your class yourself. |
Sure thing. I'll get on it. |
NOOOOOOOOO! Such a useful feature! Can't we just go with |
Nope. You can already see the name of the function in any decent debugger. |
Probably. But I use the name feature for other reasons, as detailed in #494 by myself and @hornairs such as automatically figuring out the REST url for a resource from the class name. Pretty soon we're gonna have to start writing extensions to CoffeeScript to get the useful features that keep getting rejected, like this and the extended hook. :( |
Fixed. @jashkenas: It seems like this issue is affecting a lot of our users. Time to cut another bugfix release? |
Ah :(. Really bad news. We've been waiting for this so much time and now it will never happen 😱 |
... I wasn't getting that sense -- I think it's only a problem if you're intentionally strict-mode-ing your CoffeeScript ... which you certainly don't have to do. Let's wait a reasonable interval before the next bugfix point release. |
Just lending my support to backing out the assignment. |
@jashkenas: See #2249, #2250. Within 24 hours, we've already had 2 issues opened. At this rate, we'll have over a dozen by next week . |
Indeed, new release please; cannot upgrade until this is fixed. Another example. |
Let's shoot for a stable one tomorrow afternoon then. |
@michaelficarra -- What's the "ping" regarding? Aren't we settled on this? |
But it seems now that a lot of people don't want the change that's currently on edit: Oh, it looks like you just turned that down. We should push out another bugfix release, then. |
Hah. Got away from me... I really don't want to mint new "name" semantics that are special for CoffeeScript classes. I think it'll end in tears. |
That's entirely reasonable. Are you planning on pushing the release that removes the |
Yep -- and maybe bringing a few other tickets along for the ride. I'm glad that nothing else pressing has come up over the weekend, apart from "use strict". |
Would somebody mind uploading this fixed version to npm? |
@bennedich: as soon as a new release is pushed. See #2282. |
Great points @wuyuntao. I agree wholeheartedly. ( #2052 (comment) ) |
Agree with @wuyuntao - class names are incredibly useful (used all over the place in Spine, for example). The only issue here is that the standards committee made the wrong call by making .name readonly. I think .displayName would suffice, and wouldn't necessarily lead us down a slippery slope of polluting classes. |
Add me to the camp of "give me access to my class name". I like |
very confused about the status of this. is it still a wontfix or has it already been fixed as jeremy and paul's comments here (#2262) seem to suggest? |
CoffeeScript has removed the additional setting of the |
Any plan to add |
Certainly on our project, it would simplify some otherwise-gnarly code dramatically; I'm sorry, but the tediousness and likelihood of errors scales up far more rapidly than the number of classes involved in a non-trivial hierarchy. If you're being a Good Little Scripter™ and making extensive use of private vars/methods, sticking an extra, public "Hi! My Name is FooClass!" sticker on to cover a weakness in the language just Feels Very, Very Wrong™. And it really doesn't matter if the "weakness" is in CoffeeScript's layer or the underlying JS-in-strict-mode; enough people have expressed enough pain induced by this that we need to find a solution. Our project's "workaround" looks to be moving as much current front-end Script functionality as possible to the back-end Ruby code and just hitting the network much more than we are now, which has implications that should be quite foreseeable. I'm already short two developers; we don't have the time to grok the source and fix it ourselves, and even if we did, our fix would be too unlikely to be compatible with what @jashkenas or others in the community come up with simply because we don't have the same level of expertise. So where do we go from here? |
At some point coffeescript will become too big to please everyone, right? Well, it's probably too late for that already. What about a config file that allows you to toggle on or off some options like this one, since I think a lot of people are for it, even if a lot are against it, why not make that a simple true false in a compiler.conf? I'm on the side for it by the way, since I don't want minifying my code to break any lazy references I've made to classes, and developers here are lazy and referencing classes in such a way, and I don't blame them. If you make a class, that class should know what it is made of in a reliable fashion. I have 98 more cents to spend, anyone have any gum? |
@danschumann: This is no longer a problem when using CoffeeScriptRedux's source maps. |
@michaelficarra So.. what's with the original coffeescript then? Is it moving to become the redux coffescript 2, or are they both being maintained in parallel? Is it easy to use the customization options? The readme on cs2 doesn't seem to say anything.. what's the difference and what do I have to read? |
"Due to the new semantics of JavaScript's strict mode, CoffeeScript no longer guarantees that constructor functions have names in all runtimes. See #2052 for discussion." jashkenas/coffeescript#2052
I was seeing some odd behavior when using a minified version of the js on a project that leveraged the IdentityMap. It was caused by the use of the class as the cache key -- js hashes only support string-based keys so the toString() of the class constructor was being used as the hash key. This was fine (though inefficient) until the minification tool reused function names when they weren't in the same scope (which is fairly common when using CommonJS). A similar issue was covered in jashkenas/coffeescript#2052. This was solved in our case by assigning a unique CACHE_KEY property to the class the first time it requests its cache. **Be aware, however, that if you extend a class that already has a CACHE_KEY then it will use that existing class's CACHE_KEY and therefore will mix IdentityCache entries.** You can explicitly add a CACHE_KEY class property to avoid this situation.
See also: * http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript#comment19454004_332429 * jashkenas/coffeescript#2052 * http://code.dblock.org/2012/10/13/how-do-i-get-the-name-of-an-objects-type-in-javascript.html * https://github.com/arsnebula/classx/issues/1 * https://github.com/meonkeys/classx-2-0-3-minification-bug
Sorry to bump such an old thread, but I had a few questions. I am working on a project where we need to get a hold of the class name. We are building some features into our models for auto-configuration (convention over configuration), where the names of id properties, json urls, etc. are intuited from the model class name (similar to what Rails does). What is the current official recommendation from coffeescript on how to do this? I loved the name property that was added to constructors, but based on this thread, that seems to have been removed. I also loved the idea of Is the current recommendation to simply add another property on the class manually? Something like this? class Foo
@name: 'Foo' That works, but it certainly isn't very DRY. If the manual class property is all we've got then I will use it of course, but I just wanted to check what the official recommendation was. Sorry if this was the wrong place to ask. I thought about opening a new issue, because this one is so old, but it seemed like the correct thread. If there is a better place to ask let me know and I can repost and do better in the future. Thanks! |
@CyborgMaster you can assign the class to some object's property: class Models.Cat
talk: -> 'meow'
# Or...
Models.Cat = class
talk: -> 'meow'
# Or...
@Cat = class
talk: -> 'meow' That way, you can trust that the name will be preserved upon minification because you've assigned it to a named property on an object, and not just a local variable. Don't take this for an "official recommendation" though. It's just what i would do to keep a mapping between classes and some names. I hope it helps! |
If someone else is stumbling on this, here are three solutions:
unless Function::name?
Object.defineProperty Function::, 'name',
configurable: yes
get: ->
name = ( @toString().match /^\s*function\s*(\S*)\s*\(/ )[1]
Object.defineProperty @, 'name', value: name
return name |
JavaScript minification tools rename local functions for performance, and this mangles CoffeeScript class names too.
Since Function.name is a read-only property, forcing class names which implemented in #494 does not work.
How about setting class name to some other property, like
Foo.__name__
orFoo.className
?A brief example
Compiles into
Minified
The text was updated successfully, but these errors were encountered: