JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.1k stars 59 forks source link

optionally support typed macros through babel macros #157

Open thecotne opened 4 years ago

thecotne commented 4 years ago

the idea is to first execute macros on AST and then type check that AST so that whatever macros produce can be trusted...

also we can support some js features through transformations so that codebase does not need to separately type check every new syntax sugar that TC39 approves

JSMonk commented 4 years ago

It sounds great, but I (a little bit) don't understand what you mean when say "macros on AST". Can you show some examples or links, where I can read about it? Or, the greatest thing which you can do, it's PR with the feature ^_^

thecotne commented 4 years ago

i will try to implement it myself but before that i can explain it better

so let's start from basics

how does babel work?

there is parser that produces AST from code (string basically) there are plugins that produces AST from AST (transforming some js features to others like classes to functions with prototype etc.) and there is code generator that produces code (string) from AST

what i want to do is to allow hegel to have any number of plugins executed on AST before hegel itself starts to consume it (AST) so for example if we want to support do expressions we can just add @babel/plugin-proposal-do-expressions and do expressions will become function call or conditional (ternary) operator and do expressions will be auto magically supported just like that

and other use case for having babel plugins will be to support macros with babel-plugin-macros and hegel will be only js type checker with support for type safe macros

JSMonk commented 4 years ago

Oh, sorry, but Hegel's goal is only analysis. Hegel isn't a compiler and currently, we don't want to convert it into a compiler because there will be a lot of problems which we can't solve with good level quality (optimization, result bundle code, etc.) Maybe in a bright future when we solve all our analysis problems we will try to make a good compilation. But not now. Sorry 😞

thecotne commented 4 years ago

compilation and bundling still will be out of scope but we can just support analysis of AST after it has been transformed by few plugins (this even can be configured by user)

most of plugins does not make sense and analysis after them will be harder but some of them do make sense for example @babel/plugin-proposal-do-expressions and babel-plugin-macros but having those plugins in hegel does not mean we need to produce code (compilation) it's still babels job to transform code into something that browser/node will understand.

i am still going to implement this and maybe seeing it in action would be better explanation

JSMonk commented 4 years ago

Okay, will check your implementation ^_^