Function swc_ecma_transforms::resolver
source · [−]pub fn resolver(
unresolved_mark: Mark,
top_level_mark: Mark,
typescript: bool
) -> impl Fold + VisitMut + 'static
Expand description
See Ident for know how does swc manages identifiers.
When to run
The resolver expects ‘clean’ ast. You can get clean ast by parsing, or by removing all syntax context in ast nodes.
What does it do
Firstly all scopes (fn, block) has it’s own SyntaxContext. Resolver visits all identifiers in module, and look for binding identifies in the scope. Those identifiers now have the SyntaxContext of scope (fn, block). While doing so, resolver tries to resolve normal identifiers (no hygiene info) as a reference to identifier of scope. If the resolver find suitable variable, the identifier reference will have same context as the variable.
Panics
top_level_mark
should not be root.
Example
let a = 1;
{
let a = 2;
use(a);
}
use(a)
resolver does
-
Define
a
with top level context. -
Found a block, so visit block with a new syntax context.
-
Defined
a
with syntax context of the block statement. -
Found usage of
a
, and determines that it’s reference toa
in the block. So the reference toa
will have same syntax context asa
in the block. -
Found usage of
a
(last line), and determines that it’s a reference to top-levela
, and change syntax context ofa
on last line to top-level syntax context.
Parameters
unresolved_mark
Mark applied to unresolved references.
A pass should accept this Mark if it’s going to generate a refernce to
globals like require
.
e.g. common_js
pass generates calls to require
, and this should not
be shadowed by a declaration named require
in the same file.
So it uses this value.
top_level_mark
Mark applied to top-level bindings.
NOTE: This is not globals. This is for top level items declared by users.
A pass should accept this Mark if it requires user-defined top-level items.
e.g. jsx
pass requires to call React
imported by the user.
import React from 'react';
In the code above, React
has this Mark. jsx
passes need to
reference this Mark, so it accpets this.
typescript
Enable this only if you are going to strip types or apply type-aware passes like decorators pass.