For context, I'm working on the typescript type engine and I'm trying to handle code like
export function foo() {
return a;
}
export const a = 5, b = foo();
While validating types, I need &mut reference to ast nodes because of .d.ts
files.
So I defined a validator trait like the below.
pub trait Validate<T: ?Sized> {
type Output;
fn validate(&mut self, node: &mut T) -> Self::Output;
}
I'm using ast nodes (https://swc.rs/rustdoc/swc_ecma_ast) from the swc project, and Validate
is implemented per each node types.
To handle the code above, I should validate the function foo
after the variable b
, but after the variable a
.
.
With the current design, I can't do so within Validate<VarDecl>
obviously.
I thought long and hard about how to solve this problem, and I got a solution.
It can be implemented by converting all statements into kind of RefCell<StmtWrapper>
and tracking the index of variable declarator with StmtWrapper
in Validate<Vec<Stmt>>
.
But destructuring and restoring everything is really cumbersome and I want to know if there's a better way.