rollup-plugin-coffee-script icon indicating copy to clipboard operation
rollup-plugin-coffee-script copied to clipboard

Rollup writes `this` to `undefined` in CoffeeScript 2

Open aminya opened this issue 5 years ago • 0 comments

When an operation is performed in the class (like Tags.forEach in the following example), CoffeeScript generates a function to call that. However, rollup rewrites this to undefied

Tags = [1,2]
class MyClass
  constructor: -> console.log("constructor")
  @hey: -> console.log("hey")

  Tags.forEach (t) ->
    @hey()
    console.log(t)

which is compiled to

var MyClass, Tags;

Tags = [1, 2];

MyClass = (function() {
  class MyClass {
    constructor() {
      console.log("constructor");
    }

    static hey() {
      return console.log("hey");
    }

  };

  Tags.forEach(function(t) {
    this.hey();
    return console.log(t);
  });

  return MyClass;

}).call(this);

Then rollup throws this warning:

(!) `this` has been rewritten to `undefined`
https://rollupjs.org/guide/en/#error-this-is-undefined
  return MyClass;

 }).call(this);
            ^

this is replaced with undefined

aminya avatar Jul 13 '20 04:07 aminya