rainforestapp / decaf

Coffeescript to ES.next transpiler. Now maintained over at
https://github.com/juliankrispel/decaf
MIT License
106 stars 10 forks source link

Class static property is not handled #31

Closed tsuchikazu closed 8 years ago

tsuchikazu commented 8 years ago

This is error report and proposal es6 syntax.

I tried transpile this coffeescript, then get the following error

coffeescript

class A
  @staticProp = 'hoge'

errors

$ decaf --version
0.1.0
# error
TypeError: Cannot read property 'properties' of undefined
  at mapClassBody (node_modules/decafjs/dist/index.js:267:45)
  at mapClassDeclaration (node_modules/decafjs/dist/index.js:310:82)
  at mapStatement (node_modules/decafjs/dist/index.js:413:12)
  at node_modules/decafjs/dist/index.js:427:12
  at Array.map (native)
  at mapBlockStatements (node_modules/decafjs/dist/index.js:426:27)
  at mapBlockStatement (node_modules/decafjs/dist/index.js:434:23)
  at transpile (node_modules/decafjs/dist/index.js:1008:17)
  at node_modules/decafjs/node_modules/lodash/internal/createFlow.js:67:31
  at Object.compile (node_modules/decafjs/dist/index.js:1024:10)
  at runByFile (node_modules/decafjs/bin/decaf:49:20)
  at node_modules/decafjs/bin/decaf:41:7
  at Array.forEach (native)
  at run (node_modules/decafjs/bin/decaf:25:9)
  at Object.<anonymous> (node_modules/decafjs/bin/decaf:22:1)
  at Module._compile (module.js:435:26)
  at Object.Module._extensions..js (module.js:442:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:311:12)
  at Function.Module.runMain (module.js:467:10)
  at startup (node.js:134:18)
  at node.js:961:3

I propose es6 syntax for this coffeescript transpile.

  1. ES.next

    class A {
     static staticProp = 'hoge';
    }

    Most simple syntax. But this syntax is still stage 1 proposal. Maybe don't work other npm package. etc recast. ref: jeffmo/es-class-fields-and-static-properties: Stage 1 proposal for declarative class properties in ES

  2. getter and setter for static

    class A {
     static get staticProp() {
       return this._staticProp || 'hoge';
     }
     static set staticProp(bar) {
       this._staticProp = bar;
     }
    }

    This syntax is valid in es6(2015) and declared in class. But this is little complex.

  3. Class and set property

    class A {
    }
    A.staticProp = 'hoge'

    This syntax is valid in es6(2015) and simple. But separated declaring class and static property. ref: Moving to ES6 from CoffeeScript

I think 3. Class and set property is better. What do you think about it?

tsuchikazu commented 8 years ago

I see class properties pull request https://github.com/juliankrispel/decaf/pull/29 now. If ES.next is no problem, I want to transpile to 1. ES.next.

juliankrispel commented 8 years ago

Yea it makes absolute sense, I'm looking into it currently

juliankrispel commented 8 years ago

I'd also use the static property

juliankrispel commented 8 years ago

basically there's two types of class properties

juliankrispel commented 8 years ago
class A
  b: 'b'
  @c = 'c'

should compile to

class A {
  b = 'b';
  static c = 'c';
}
tsuchikazu commented 8 years ago

Thanks a lot!! :smile:

juliankrispel commented 8 years ago

@tsuchikazu published that on npm as well