JairusSW / as-json

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

Compile Time check for json decorator #93

Open anyon17 opened 1 month ago

anyon17 commented 1 month ago

Right now a class decorated with @json containing fields which might not have types whose declaration have @json decorator panics at the runtime. Is there any way to have this check at compile time so that compiler can exactly point to the field which does not have @json decorator in it's declaration.

JairusSW commented 1 month ago

Here's an example for context A class decorated with @json, but not all fields implement the JSON Type

@json
class Foo {
  public bar: string = "this is ok"
  public baz: i32 | null = 0
  // ^ this is not okay
}
Threw "unreachable" at runtime!
 at 0:30 ...

This will fail miserably and throw something like "index out of range" or "unreachable" which are highly non-descriptive. Rather:

@json
class Foo {
  public bar: string = "this is ok"
  public baz: i32 | null = 0
  // ^ this is not okay
}

ERROR: Property Foo.baz in <./src/index.ts> does not implement a JSON-compatible type!
Either decorate it with the `@omit` decorator or fix it!

Instead, we actually throw at COMPILE TIME to ensure that the user has fault tolerant code

anyon17 commented 1 month ago

Yeah that's perfectly put @JairusSW . Thanks for attaching the example to make the issue much clear.

JairusSW commented 1 month ago

Reopening this as there are a few bugs

JairusSW commented 1 month ago

@anyon17, after taking a closer look, it seems like to properly implement this (and a few other things), I'm going to have to take multiple passes over the AST via the transform. I'm not sure if your familiar with transforms, but its a pretty big undertaking, so I'll pull some features out and release a non-feature-complete version that's stable. This means I'll add full type checking in a later release. I'm still going to be working on this, just thought I'd let you know it may take a bit longer.

anyon17 commented 1 month ago

hey @JairusSW thanks for letting me know this detail. Will wait for the feature to become stable and looking forward to use it. Thanks again for picking up the requested feature.