cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 160 forks source link

External instantiation of non-static inner classes produces broken typescript #674

Open rendaw opened 3 years ago

rendaw commented 3 years ago
public class ThingA {
  static void somefunc(ThingA a) {
    a.new InnerY();
  }

  public class InnerY {
    InnerY() {}
  }
}

produces

namespace jsweettest.moda {
    export class ThingA {
        static somefunc(a : ThingA) {
            new ThingA.InnerY(this);
        }
    }
    ThingA["__class"] = "jsweettest.moda.ThingA";

    export namespace ThingA {

        export class InnerY {
            public __parent: any;
            constructor(__parent: any) {
                this.__parent = __parent;
            }
        }
        InnerY["__class"] = "jsweettest.moda.ThingA.InnerY";

    }

}

The use of this in a static context results in undefined. Typescript doesn't seem to catch this either, but this is a problem with generation I think.

The following slightly expanded example shows how the issue can produce more obvious breakage:

public class ThingA {
  void somefunc() {
    new InnerX(this);
  }

  public static class InnerX {
    InnerX(ThingA a) {
      a.new InnerY();
    }
  }

  public class InnerY {
    InnerY() {}
  }
}

produces the error property '__parent' does not exist on type 'InnerX' with this generated code:

/* Generated from Java with JSweet 2.4.0-SNAPSHOT - http://www.jsweet.org */
namespace jsweettest.moda {
    export class ThingA {
        somefunc() {
            new ThingA.InnerX(this);
        }
    }
    ThingA["__class"] = "jsweettest.moda.ThingA";

    export namespace ThingA {

        export class InnerX {
            constructor(a : jsweettest.moda.ThingA) {
                new ThingA.InnerY(this.__parent);
            }
        }
        InnerX["__class"] = "jsweettest.moda.ThingA.InnerX";

        export class InnerY {
            public __parent: any;
            constructor(__parent: any) {
                this.__parent = __parent;
            }
        }
        InnerY["__class"] = "jsweettest.moda.ThingA.InnerY";

    }

}