Skip to content
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

Extra _ref variable created for no reason #1861

Closed
devongovett opened this issue Nov 12, 2011 · 4 comments
Closed

Extra _ref variable created for no reason #1861

devongovett opened this issue Nov 12, 2011 · 4 comments

Comments

@devongovett
Copy link

If I compile this with the latest version of CoffeeScript:

Array.isArray ?= (object) ->
    Object.prototype.toString.call(object) is '[object Array]'

I get this:

var _ref;

if ((_ref = Array.isArray) == null) {
  Array.isArray = function(object) {
    return Object.prototype.toString.call(object) === '[object Array]';
  };
}

Which has an extra _ref variable that is never used. Probably should just simplify that to:

if (Array.isArray == null) {
  Array.isArray = function(object) {
    return Object.prototype.toString.call(object) === '[object Array]';
  };
}
@michaelficarra
Copy link
Collaborator

Confirmed. Minimal test case: a.b ?= c

var _ref;

if ((_ref = a.b) == null) a.b = c;

edit: To whoever works on this: be careful because _ref is used when the result of the existential assignment is used:

$ coffee -bep 'a = (b.c ?= d)'
var a, _ref;

a = ((_ref = b.c) != null ? _ref : b.c = d);

@jashkenas
Copy link
Owner

The above commit should fix this case -- where the existential assignment occurs at the top level, we know that there's no need to cache the ref.

@haberbyte
Copy link
Contributor

This reoccurs in current master, is there a reason the commit got revoked?

@haberbyte
Copy link
Contributor

@jashkenas : This issue appears to be related!

#2197

There is a fix in there that causes this "useless" _ref variable to reappear.
Is there a way to fix both issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants