Skip to content

Commit

Permalink
refactor(ast): pass final ScopeFlags into visit_function
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored and Dunqing committed Jul 15, 2024
1 parent 16698bc commit a53f300
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 48 deletions.
22 changes: 11 additions & 11 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub enum Expression<'a> {
ChainExpression(Box<'a, ChainExpression<'a>>) = 16,
ClassExpression(Box<'a, Class<'a>>) = 17,
ConditionalExpression(Box<'a, ConditionalExpression<'a>>) = 18,
#[visit_args(flags = None)]
#[visit_args(flags = ScopeFlags::Function)]
FunctionExpression(Box<'a, Function<'a>>) = 19,
ImportExpression(Box<'a, ImportExpression<'a>>) = 20,
LogicalExpression(Box<'a, LogicalExpression<'a>>) = 21,
Expand Down Expand Up @@ -970,7 +970,7 @@ pub struct BlockStatement<'a> {
#[cfg_attr(feature = "serialize", serde(untagged))]
pub enum Declaration<'a> {
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 32,
#[visit_args(flags = None)]
#[visit_args(flags = ScopeFlags::Function)]
FunctionDeclaration(Box<'a, Function<'a>>) = 33,
ClassDeclaration(Box<'a, Class<'a>>) = 34,
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 35,
Expand Down Expand Up @@ -1437,8 +1437,8 @@ pub struct BindingRestElement<'a> {
/// Function Definitions
#[visited_node]
#[scope(
// TODO: `ScopeFlags::Function` is not correct if this is a `MethodDefinition`
flags(flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function),
// `flags` passed in to visitor via parameter defined by `#[visit_args(flags = ...)]` on parents
flags(flags),
strict_if(self.is_strict()),
)]
#[derive(Debug)]
Expand Down Expand Up @@ -1642,12 +1642,12 @@ pub struct MethodDefinition<'a> {
pub span: Span,
pub decorators: Vec<'a, Decorator<'a>>,
pub key: PropertyKey<'a>,
#[visit_args(flags = Some(match self.kind {
MethodDefinitionKind::Get => ScopeFlags::GetAccessor,
MethodDefinitionKind::Set => ScopeFlags::SetAccessor,
MethodDefinitionKind::Constructor => ScopeFlags::Constructor,
MethodDefinitionKind::Method => ScopeFlags::empty(),
}))]
#[visit_args(flags = match self.kind {
MethodDefinitionKind::Get => ScopeFlags::Function | ScopeFlags::GetAccessor,
MethodDefinitionKind::Set => ScopeFlags::Function | ScopeFlags::SetAccessor,
MethodDefinitionKind::Constructor => ScopeFlags::Function | ScopeFlags::Constructor,
MethodDefinitionKind::Method => ScopeFlags::Function,
})]
pub value: Box<'a, Function<'a>>, // FunctionExpression
pub kind: MethodDefinitionKind,
pub computed: bool,
Expand Down Expand Up @@ -1959,7 +1959,7 @@ inherit_variants! {
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(untagged))]
pub enum ExportDefaultDeclarationKind<'a> {
#[visit_args(flags = None)]
#[visit_args(flags = ScopeFlags::Function)]
FunctionDeclaration(Box<'a, Function<'a>>) = 64,
ClassDeclaration(Box<'a, Class<'a>>) = 65,

Expand Down
28 changes: 12 additions & 16 deletions crates/oxc_ast/src/generated/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ pub trait Visit<'a>: Sized {
}

#[inline]
fn visit_function(&mut self, it: &Function<'a>, flags: Option<ScopeFlags>) {
fn visit_function(&mut self, it: &Function<'a>, flags: ScopeFlags) {
walk_function(self, it, flags);
}

Expand Down Expand Up @@ -1509,7 +1509,7 @@ pub mod walk {
Expression::ClassExpression(it) => visitor.visit_class(it),
Expression::ConditionalExpression(it) => visitor.visit_conditional_expression(it),
Expression::FunctionExpression(it) => {
let flags = None;
let flags = ScopeFlags::Function;
visitor.visit_function(it, flags)
}
Expression::ImportExpression(it) => visitor.visit_import_expression(it),
Expand Down Expand Up @@ -3014,27 +3014,23 @@ pub mod walk {
visitor.visit_decorators(&it.decorators);
visitor.visit_property_key(&it.key);
{
let flags = Some(match it.kind {
MethodDefinitionKind::Get => ScopeFlags::GetAccessor,
MethodDefinitionKind::Set => ScopeFlags::SetAccessor,
MethodDefinitionKind::Constructor => ScopeFlags::Constructor,
MethodDefinitionKind::Method => ScopeFlags::empty(),
});
let flags = match it.kind {
MethodDefinitionKind::Get => ScopeFlags::Function | ScopeFlags::GetAccessor,
MethodDefinitionKind::Set => ScopeFlags::Function | ScopeFlags::SetAccessor,
MethodDefinitionKind::Constructor => ScopeFlags::Function | ScopeFlags::Constructor,
MethodDefinitionKind::Method => ScopeFlags::Function,
};
visitor.visit_function(&it.value, flags);
}
visitor.leave_node(kind);
}

pub fn walk_function<'a, V: Visit<'a>>(
visitor: &mut V,
it: &Function<'a>,
flags: Option<ScopeFlags>,
) {
pub fn walk_function<'a, V: Visit<'a>>(visitor: &mut V, it: &Function<'a>, flags: ScopeFlags) {
let kind = AstKind::Function(visitor.alloc(it));
visitor.enter_node(kind);
visitor.enter_scope(
{
let mut flags = flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function;
let mut flags = flags;
if it.is_strict() {
flags |= ScopeFlags::StrictMode;
}
Expand Down Expand Up @@ -3738,7 +3734,7 @@ pub mod walk {
match it {
Declaration::VariableDeclaration(it) => visitor.visit_variable_declaration(it),
Declaration::FunctionDeclaration(it) => {
let flags = None;
let flags = ScopeFlags::Function;
visitor.visit_function(it, flags)
}
Declaration::ClassDeclaration(it) => visitor.visit_class(it),
Expand Down Expand Up @@ -4119,7 +4115,7 @@ pub mod walk {
) {
match it {
ExportDefaultDeclarationKind::FunctionDeclaration(it) => {
let flags = None;
let flags = ScopeFlags::Function;
visitor.visit_function(it, flags)
}
ExportDefaultDeclarationKind::ClassDeclaration(it) => visitor.visit_class(it),
Expand Down
24 changes: 12 additions & 12 deletions crates/oxc_ast/src/generated/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ pub trait VisitMut<'a>: Sized {
}

#[inline]
fn visit_function(&mut self, it: &mut Function<'a>, flags: Option<ScopeFlags>) {
fn visit_function(&mut self, it: &mut Function<'a>, flags: ScopeFlags) {
walk_function(self, it, flags);
}

Expand Down Expand Up @@ -1516,7 +1516,7 @@ pub mod walk_mut {
Expression::ClassExpression(it) => visitor.visit_class(it),
Expression::ConditionalExpression(it) => visitor.visit_conditional_expression(it),
Expression::FunctionExpression(it) => {
let flags = None;
let flags = ScopeFlags::Function;
visitor.visit_function(it, flags)
}
Expression::ImportExpression(it) => visitor.visit_import_expression(it),
Expand Down Expand Up @@ -3150,12 +3150,12 @@ pub mod walk_mut {
visitor.visit_decorators(&mut it.decorators);
visitor.visit_property_key(&mut it.key);
{
let flags = Some(match it.kind {
MethodDefinitionKind::Get => ScopeFlags::GetAccessor,
MethodDefinitionKind::Set => ScopeFlags::SetAccessor,
MethodDefinitionKind::Constructor => ScopeFlags::Constructor,
MethodDefinitionKind::Method => ScopeFlags::empty(),
});
let flags = match it.kind {
MethodDefinitionKind::Get => ScopeFlags::Function | ScopeFlags::GetAccessor,
MethodDefinitionKind::Set => ScopeFlags::Function | ScopeFlags::SetAccessor,
MethodDefinitionKind::Constructor => ScopeFlags::Function | ScopeFlags::Constructor,
MethodDefinitionKind::Method => ScopeFlags::Function,
};
visitor.visit_function(&mut it.value, flags);
}
visitor.leave_node(kind);
Expand All @@ -3164,13 +3164,13 @@ pub mod walk_mut {
pub fn walk_function<'a, V: VisitMut<'a>>(
visitor: &mut V,
it: &mut Function<'a>,
flags: Option<ScopeFlags>,
flags: ScopeFlags,
) {
let kind = AstType::Function;
visitor.enter_node(kind);
visitor.enter_scope(
{
let mut flags = flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function;
let mut flags = flags;
if it.is_strict() {
flags |= ScopeFlags::StrictMode;
}
Expand Down Expand Up @@ -3946,7 +3946,7 @@ pub mod walk_mut {
match it {
Declaration::VariableDeclaration(it) => visitor.visit_variable_declaration(it),
Declaration::FunctionDeclaration(it) => {
let flags = None;
let flags = ScopeFlags::Function;
visitor.visit_function(it, flags)
}
Declaration::ClassDeclaration(it) => visitor.visit_class(it),
Expand Down Expand Up @@ -4357,7 +4357,7 @@ pub mod walk_mut {
) {
match it {
ExportDefaultDeclarationKind::FunctionDeclaration(it) => {
let flags = None;
let flags = ScopeFlags::Function;
visitor.visit_function(it, flags)
}
ExportDefaultDeclarationKind::ClassDeclaration(it) => visitor.visit_class(it),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/return_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<'a> Visit<'a> for FunctionReturnType<'a> {
}
}

fn visit_function(&mut self, _func: &Function<'a>, _flags: Option<ScopeFlags>) {
fn visit_function(&mut self, _func: &Function<'a>, _flags: ScopeFlags) {
// We don't care about nested functions
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
}
}

fn visit_function(&mut self, func: &Function<'a>, flags: Option<ScopeFlags>) {
fn visit_function(&mut self, func: &Function<'a>, flags: ScopeFlags) {
walk_function(self, func, flags);
if func.type_parameters.is_some() {
self.leave_scope();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/require_await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a> Visit<'a> for AwaitFinder {

fn visit_arrow_function_expression(&mut self, _expr: &ArrowFunctionExpression<'a>) {}

fn visit_function(&mut self, _func: &Function<'a>, _flags: Option<ScopeFlags>) {}
fn visit_function(&mut self, _func: &Function<'a>, _flags: ScopeFlags) {}
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/ast_passes/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a> Visit<'a> for KeepVar<'a> {
}
}

fn visit_function(&mut self, _it: &Function<'a>, _flags: Option<ScopeFlags>) {
fn visit_function(&mut self, _it: &Function<'a>, _flags: ScopeFlags) {
/* skip functions */
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/examples/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct CountASTNodes {
}

impl<'a> Visit<'a> for CountASTNodes {
fn visit_function(&mut self, func: &Function<'a>, flags: Option<ScopeFlags>) {
fn visit_function(&mut self, func: &Function<'a>, flags: ScopeFlags) {
self.functions += 1;
walk::walk_function(self, func, flags);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
self.leave_node(kind);
}

fn visit_function(&mut self, func: &Function<'a>, flags: Option<ScopeFlags>) {
fn visit_function(&mut self, func: &Function<'a>, flags: ScopeFlags) {
/* cfg */
let (before_function_graph_ix, error_harness, function_graph_ix) =
control_flow!(self, |cfg| {
Expand All @@ -1460,7 +1460,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
self.enter_node(kind);
self.enter_scope(
{
let mut flags = flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function;
let mut flags = flags;
if func.is_strict() {
flags |= ScopeFlags::StrictMode;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/src/context/scoping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl<'a> Visit<'a> for ChildScopeCollector {
self.scope_ids.push(clause.scope_id.get().unwrap());
}

fn visit_function(&mut self, func: &Function<'a>, _flags: Option<ScopeFlags>) {
fn visit_function(&mut self, func: &Function<'a>, _flags: ScopeFlags) {
self.scope_ids.push(func.scope_id.get().unwrap());
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_codegen/src/generators/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<'a> VisitBuilder<'a> {

let as_param_type = self.with_ref_pat(&as_type);
let (extra_params, extra_args) = if ident == "Function" {
(quote!(, flags: Option<ScopeFlags>,), quote!(, flags))
(quote!(, flags: ScopeFlags,), quote!(, flags))
} else {
(TokenStream::default(), TokenStream::default())
};
Expand Down

0 comments on commit a53f300

Please sign in to comment.