Skip to content

ast-codegen: support #[visit(enter_before)] attribute #4240

@Dunqing

Description

@Dunqing

We have discussed this at #4147 (comment). But we do need this at the moment.

In semantic builder, We need enter_scope before enter_node in visit_function and visit_class.

fn visit_function(&mut self, func: &Function<'a>, flags: Option<ScopeFlags>) {
let kind = AstKind::Function(self.alloc(func));
self.enter_scope(
{
let mut flags = flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function;
if func.is_strict() {
flags |= ScopeFlags::StrictMode;
}
flags
},
&func.scope_id,
);
/* cfg */
let (before_function_graph_ix, error_harness, function_graph_ix) =
control_flow!(|self, cfg| {
let before_function_graph_ix = cfg.current_node_ix;
cfg.push_finalization_stack();
let error_harness = cfg.attach_error_harness(ErrorEdgeKind::Implicit);
let function_graph_ix = cfg.new_basic_block_function();
cfg.ctx(None).new_function();
(before_function_graph_ix, error_harness, function_graph_ix)
});
/* cfg */
// We add a new basic block to the cfg before entering the node
// so that the correct cfg_ix is associated with the ast node.
self.enter_node(kind);

We want to treat FunctionExpression and FunctionDeclaration differently and the class is the same too.

FunctionExpression: create a binding for function id to the current scope
FunctionDeclaration: create a binding for function id to the parent scope

To add the binding to the parent scope, we must need enter_scope before enter_node.

I tried another way to get around it (Same as TypeScript). But I find it a bit confusing. See: #4195 (review)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory - New feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions