Extending an ES6 class with a CS class fails

CoffeeScript calls classes as functions (rather than newing them as constructors) when instantiating a class that inherits from an ES6 class.

The fix is to interoperate with ES6 classes – no need to go all the way and compile Coffee to ES6 classes. We should check to see if a class is an ES6 class, and treat it differently (eg. see what angular does here).

Test case:

js:

class A {}

coffee:

class B extends A {}
new B # TypeError: Class constructor A cannot be invoked without 'new'

2 thoughts on “Extending an ES6 class with a CS class fails

  1. I had thought about executable class bodies as one of the really neat things about CS that I would sorely miss. You could achieve the same effect, though. Also, ES6 still supports prototype as normal.

    class A
      counter = 0
      # other executable class body statements
    
      # Non-function prototype property
      b: 'nice'
    
      constructor: ->
        @id = ++counter
    let A = (() => {
      let counter
    
      class A {
        constructor () {
          this.id = ++counter
        }
      }
    
      // Non-function prototype property
      A.prototype.b = 'nice'
    
      (function () {
        counter = 0
        // other executable class body statements
      }).call(A)
    
      return A
    })()

    The main issue with this is that it does spread the code out a bit from how it was written (e.g. all functions would be ‘hoisted’ into the class definition).