kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.04k stars 199 forks source link

Support for emitting ES6 classes in the JavaScript compiler. #539

Open jchv opened 5 years ago

jchv commented 5 years ago

Right now, the JavaScript compiler emits ES3-style classes. This is fine, but I propose that it may be useful to support emitting ES6-style classes for more modern toolchains. If the JavaScript compiler were to support emitting different kinds of modules (as in #267) then it might be natural to also consider adding ES6 classes alongside ES6 modules.

Thoughts? My eventual goal is getting TypeScript output from Kaitai structs, and I see basically two ways of going about it, either treating it as another language or treating it as an extension of JS. The latter actually seems more logical to me, but to get there we would probably need ES6 style classes first.

GreyCat commented 5 years ago

Shouldn't be that hard. Given the existence of cpp_stl_98 and cpp_stl_11 now, we can continue with javascript_es6 and typescript, if there would be any interest in these targets.

For ES6, I would assume that actually we won't be changing any test specs at all. For TypeScript, I'm not sure, but may be we can get away with existing specs too?

jchv commented 5 years ago

I believe we should be able to get away with the existing specs. For TypeScript, you would basically be doing the same thing as ES6 classes, in theory, just with type declarations. So as long as you have a way to strip those declarations (like by invoking tsc,) then I think the same tests should work.

Though, to get full type safety, it occurs to me that the runtime would also need either type definitions or to be written in TypeScript. Either option seems like it should be straightforward; the tsc supports compiling down to ES3 and outputting UMD modules, so you could maintain roughly the same exact support even if the whole thing was converted to TS, and on the flip side adding type definitions to an NPM package is also pretty simple.

GreyCat commented 5 years ago

I believe, to some extent, @koczkatamas has already implemented TS definitions in https://github.com/kaitai-io/kaitai_struct_webide/blob/master/lib/ts-types/kaitai.d.ts

jchv commented 5 years ago

While it's a little bit of a tangent to this specific issue, I did go ahead and create a PR for at least adding TypeScript definition files to the runtime. That should theoretically alleviate the need for some of those type definitions in the Web IDE, if i've done it right. (https://github.com/kaitai-io/kaitai_struct_javascript_runtime/pull/10)