This is very much of work in progress but wanted to share my current status with refactoring our term and module handling.
For context, Term
has been our "class" for creating our internal AST. Our AST is basically the shift AST with some extra nodes to handle sweet specific syntax like syntax
and syntaxrec
.
Our ways for manipulating a Term
was an ugly combination of a visitor, reducer, and ad-hoc traversal methods. Also, there was no enforcement of how a Term
could be constructed so we had lots of hard to track down bugs due to malformed Term
s.
So, the solution is to use more macros! The sweet-spec repo defines all the Term
s via the sweet-spec-macro using a syntax that is identical with flow's types:
declare class ClassExpression extends Expression {
name?: BindingIdentifier;
super?: Expression;
elements: ClassElement[];
}
All these declarations are expanded into classes that check their arguments on construction. They also have a reduce
method that runs a shift-style reducer to make traversal/manipulation nice and easy.
Because the syntax is identical to flow we also can use the spec as a flow interface .flow
file and get the benefits of type checking on our AST constructors.
I'm pretty stoked about it.
Anyway, everything is currently broken since I'm halfway through converting everything to use the new constructors but it should converge soon.
/cc @gabejohnson @jlongster