coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Proposal: allow super in object literals

Open aleclarson opened this issue 6 years ago • 13 comments

As seen on MDN.

obj1 = ->
  method1: ->
    console.log 'method1'

obj2 =
  method2: ->
    super.method1()

Object.setPrototypeOf obj2, obj1
obj2.method2() # logs "method1"

The obj2 variable would transpile to:

obj2 = {
  method2() {
    super.method1();
  }
};

aleclarson avatar May 23 '18 23:05 aleclarson

Any thoughts @connec or @helixbass ?

GeoffreyBooth avatar May 24 '18 02:05 GeoffreyBooth

I didn't realise that was valid :smile: I don't see any reason not to support it.

connec avatar May 24 '18 20:05 connec

Should all methods in object literals transpile to foo() {} with this change? Or only if super is used?

aleclarson avatar May 24 '18 21:05 aleclarson

Should all methods in object literals transpile to foo() {} with this change? Or only if super is used?

I see no reason not to use the method (foo() {}) syntax all the time, that’s what we do for classes. It’s also more concise, and it’s the form that MDN uses in their example.

@aleclarson are you up for implementing this in a PR?

GeoffreyBooth avatar Jun 11 '18 05:06 GeoffreyBooth

I'm not familiar with the architecture, and have no time to learn it currently. 😢

aleclarson avatar Jun 11 '18 17:06 aleclarson

Alright, anyone else? @connec ?

GeoffreyBooth avatar Jun 11 '18 22:06 GeoffreyBooth

Okay, I'm trying to see how this could be done, but it seems like Code#isMethod could be of help here.

I haven't been able to test it out or see how it would get compiled in the context of an object.

joallard avatar Apr 23 '20 03:04 joallard

I think isMethod in the context of Code determines whether to output as fn() {} (method style syntax) or fn = function() {} (traditional style, with function keyword). So it would need to be in Object probably that sets isMethod to be true on one of Object’s properties that is a function.

GeoffreyBooth avatar Apr 23 '20 20:04 GeoffreyBooth

Here's where I'm at right now: https://gist.github.com/joallard/ff34ca6c31db0db7f214d4c199d6ebec

There seems to be a bad infinite-loop bug with Code#isStatement(). I have not figured out how Class#addInitializerMethod manages to do it.

joallard avatar Apr 23 '20 23:04 joallard

So first off, you want to be editing nodes.coffee, not nodes.js. See https://github.com/jashkenas/coffeescript/wiki/%5BHowTo%5D-Hacking-on-the-CoffeeScript-Compiler

GeoffreyBooth avatar Apr 24 '20 02:04 GeoffreyBooth

The edit to the .coffee is below in the diff

vendethiel avatar Apr 24 '20 10:04 vendethiel

@GeoffreyBooth Yes, of course, but I preferred editing nodes.js directly and test than nodes.coffee, then rebuild, wait, then test.

I read the wiki post forwards and backwards, but there's not much currently on the workflow, I've had to hack things and figure them out.

I still can't manage (or have invested the time) to break out a Repl in the middle of code, so I'm down to console-logs. I'll try to add some contrib-workflow tips to the wiki with the things I've learned along the way.

But obviously, a PR wouldn't be so hacky.

joallard avatar Apr 24 '20 19:04 joallard

Sorry I didn't look closer. You might want to review https://github.com/jashkenas/coffeescript/wiki/%5BHowTo%5D-How-parsing-works, that provides an overview.

Getting debugging working in Node is well worth the time. Basically create a test.coffee with very minimal code, like one object with one method, at the root of the CoffeeScript repo. Then run:

node --inspect-brk ./bin/coffee -bp ./test.coffee

And open Chrome DevTools and connect to the Node debugger. You can step through Node code the same as using DevTools for frontend code.

GeoffreyBooth avatar Apr 24 '20 20:04 GeoffreyBooth