JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
72 stars 13 forks source link

Classes in imported files are not automatically serializable #68

Closed mattjohnsonpint closed 4 months ago

mattjohnsonpint commented 4 months ago

Create a file message.ts with the following:

@json
export class Message {
    text!: string;
}

Create a file index.ts with the following:

import { JSON } from "json-as";
import { Message } from "./message";

export function generateMessage(): string {
  const m = <Message>{
    text: "Hello, world!",
  };

  return JSON.stringify(m);
}

Compiling fails with the following:

ERROR TS2304: Cannot find name 'JSON'.
   :
 3 │ return `{"text":${JSON.stringify<string>(this.text)}}`;
   │                   ~~~~
   └─ in assembly/message.ts(3,27)

If I add import { JSON } from "json-as"; to message.ts, it works, but I then have an unused import, which goes red in VS Code, creates a linting error in projects with eslint, etc.

mattjohnsonpint commented 4 months ago

My guess is we need to modify https://github.com/JairusSW/as-json/blob/a563bfe790b5baf465fa08d6883a3d775daa497a/transform/src/index.ts#L235-L258 to also import JSON for the generated code. But I couldn't quite make it work. Would be happy if you'd take a look. Thanks.

mattjohnsonpint commented 4 months ago

My temporary workaround:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { JSON } from "json-as";

but it's ugly. 😅

JairusSW commented 4 months ago

Pausing my work on the develop branch to work on this

JairusSW commented 4 months ago

Speaking of that the new version is coming along pretty well and is pretty dang performant

JairusSW commented 4 months ago

Dirty fix in https://github.com/JairusSW/as-json/commit/4351de6702aa79e2d15d27b12af35a4e31f0b5b9#diff-2d3ca7591f50902b81cd2a100aaa907fce4044f2a75d56eed0265a6dece2b310R897

JairusSW commented 4 months ago

My temporary workaround:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { JSON } from "json-as";

but it's ugly. 😅

I think I made more ugly shitcode 🗿 Should probably go and think of a better way to design that transform. its bad

mattjohnsonpint commented 4 months ago

Yeah, CI tests failed with this merged too.