johnfn / ts2gd

💥 Compile TypeScript to GDScript for Godot
210 stars 14 forks source link

`import` seems to be broken #96

Open lostfictions opened 2 years ago

lostfictions commented 2 years ago

Given this code:

import type Ball from "./ball";

export class Paddle extends Area2D {
  _ball_dir: int;

  constructor() {
    super();
    this._ball_dir = 1;
  }

  _on_area_entered(area: Area2D) {
    if (area.name.begins_with("Ball")) {
      // would be better expressed with a type guard, but nevermind that for now
      (area as unknown as Ball).direction = Vector2(this._ball_dir, randf() * 2 - 1).normalized()
    }
  }
}

ts2gd errors out with the following:

<nodepath>/node_modules/ts2gd/js/parse_node/parse_import_declaration.js:64
        throw new Error("Unsupported import type!");
              ^

Error: Unsupported import type!
    at Object.parseImportDeclaration (<nodepath>/node_modules/ts2gd/js/parse_node/parse_import_declaration.js:64:15)
    at Object.parseNode (<nodepath>/node_modules/ts2gd/js/parse_node.js:169:47)
    at <nodepath>/node_modules/ts2gd/js/parse_node/parse_source_file.js:33:73
    at Array.map (<anonymous>)
    at Object.parseSourceFile (<nodepath>/node_modules/ts2gd/js/parse_node/parse_source_file.js:33:41)
    at Object.parseNode (<nodepath>/node_modules/ts2gd/js/parse_node.js:167:40)
    at AssetSourceFile.compile (<nodepath>/node_modules/ts2gd/js/project/assets/asset_source_file.js:266:41)
    at <nodepath>/node_modules/ts2gd/js/project/project.js:203:79
    at Array.map (<anonymous>)
    at TsGdProjectClass.compileAllSourceFiles (<nodepath>/node_modules/ts2gd/js/project/project.js:203:58)

Expected behaviour: import type statements (all forms, including import type X, import type { X }, and import { type X }) are erased, as in normal TypeScript.

A workaround is to write the following:

--- import type Ball from "./ball";
+++ type Ball = import("./ball").default;

EDIT: actually, it seems to be more than import type -- import appears to be broken outright. Support isn't documented at all in the README and there aren't any examples, but from other issues (like https://github.com/johnfn/ts2gd/issues/66) I get that sense that it didn't formerly crash even if not all use cases were supported. Is this a regression?

adamuso commented 2 years ago

Current version does not support default imports (import A from "...") and for named imports (like import { A, B } from "...") type keyword should be ignored (but still it will generate code in .gd files because it is not handled what needs to be fixed). Support for default imports will come at some point after merging #61.

johnfn commented 2 years ago

this SHOULD be an extremely simple fix as import type generates absolutely no code :)