Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import seems to be broken #96

Open
lostfictions opened this issue May 25, 2022 · 2 comments
Open

import seems to be broken #96

lostfictions opened this issue May 25, 2022 · 2 comments

Comments

@lostfictions
Copy link

lostfictions commented May 25, 2022

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 #66) I get that sense that it didn't formerly crash even if not all use cases were supported. Is this a regression?

@lostfictions lostfictions changed the title import type doesn't seem to be supported import seems to be broken May 25, 2022
@adamuso
Copy link
Contributor

adamuso commented May 25, 2022

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
Copy link
Owner

johnfn commented May 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants