Skip to content

Nodes: Class body passthrough expressions get hoisted if anything in a class is hoisted #4724

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

Closed
stevefan1999-personal opened this issue Sep 29, 2017 · 6 comments
Assignees
Labels

Comments

@stevefan1999-personal
Copy link

stevefan1999-personal commented Sep 29, 2017

http://tinyurl.com/yb476vtn
Although it was still a legit prototype construct, this is very problematic for backtick embeded JS expression.
http://tinyurl.com/ya7xo5fz
The entire qux = 10 was shifted outside of the class declaration.
I do believe this is a compiler bug.

Workaround:
http://tinyurl.com/y7gzwfup for backtick property-based apply/or
http://tinyurl.com/y8xa58a6 for constructor apply

*PS: backtick property-based apply will still be boil down to constructor apply, tested with babel online repl: http://tinyurl.com/yd94hv4y

@stevefan1999-personal
Copy link
Author

I thought MobX was cool with CS2 with relaxation on backtick JS expression generation, turns out I was wrong,

@GeoffreyBooth
Copy link
Collaborator

There’s a lot going on in your post, so I’m not sure exactly what bug or bugs you’re referring to. But I did find at least one issue in your examples, that I’ve simplified to the minimal case:

class Class
  `field = 3`
  hoisted: 4
var Class;

Class = (function() {
  class Class {
    ;

  };

  field = 3;

  Class.prototype.hoisted = 4;

  return Class;

})();

It appears that whenever anything in a class is hoisted, all passthrough expressions get improperly hoisted as well. If you change hoisted: 4 above to hoisted: -> 4 instead, the backticked block appears in the class body as desired.

@GeoffreyBooth GeoffreyBooth changed the title CS2 messed up backtick JS expression with functions who have functor applied Class body passthrough expressions get hoisted if anything in a class is hoisted Sep 30, 2017
@GeoffreyBooth GeoffreyBooth changed the title Class body passthrough expressions get hoisted if anything in a class is hoisted Nodes: Class body passthrough expressions get hoisted if anything in a class is hoisted Sep 30, 2017
@connec
Copy link
Collaborator

connec commented Oct 3, 2017

I will look into this asap.

GeoffreyBooth added a commit to GeoffreyBooth/coffeescript that referenced this issue Oct 7, 2017
@GeoffreyBooth
Copy link
Collaborator

GeoffreyBooth commented Oct 7, 2017

@connec I noticed that I still had my original “backticked expressions in class body” branch (the one you refactored for #4668 / #4712) in my local repo. Out of curiosity, I checked out my old branch, where I left off before you started working in it, and I tried the minimal case above. To my surprise, my old branch compiles it correctly:

var Class;

Class = (function() {
  class Class {
    field = 3;
  };

  Class.prototype.hoisted = 4;

  return Class;

})();

I pushed the branch up: https://github.com/GeoffreyBooth/coffeescript/tree/backticked-expressions-in-class-body and added a test for this case.

I assume you probably want to start with current master, but maybe there’s something in this old branch that you might find useful? At least you can pluck GeoffreyBooth@1fdd6df to get the test.

@stevefan1999-personal
Copy link
Author

stevefan1999-personal commented Oct 7, 2017

I never believed it would involve hoisting, one of the nightmares of JS.

@connec connec mentioned this issue Oct 7, 2017
@connec
Copy link
Collaborator

connec commented Oct 7, 2017

@GeoffreyBooth / @stevefan1999 turned out to indeed be a bug in the hoisting logic that was pretty easy to fix - we were setting hoisted on the Value wrapping a PassthroughLiteral, and then unwrapping that Value before checking for the hoisted flag 😓

GeoffreyBooth pushed a commit that referenced this issue Oct 7, 2017
The handling of hoisted nodes in class bodies was incorrect, as the node
was being unwrapped *before* checking if it was hoisted, meaning nodes
that should have been hoisted would be output normally.

This affected `PassthroughLiteral`s as they were wrapped in a `Value`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants