Skip to content

Commit 798ad93

Browse files
committed
refactor(semantic): update the order of visit_function and Visit fields in the builder to be consistent
1 parent 9fbe094 commit 798ad93

File tree

14 files changed

+145
-141
lines changed

14 files changed

+145
-141
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ pub struct BindingRestElement<'a> {
14391439
#[scope(
14401440
// TODO: `ScopeFlags::Function` is not correct if this is a `MethodDefinition`
14411441
flags(flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function),
1442-
strict_if(self.body.as_ref().is_some_and(|body| body.has_use_strict_directive())),
1442+
strict_if(self.is_strict()),
14431443
)]
14441444
#[derive(Debug)]
14451445
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
@@ -1470,8 +1470,8 @@ pub struct Function<'a> {
14701470
/// ```
14711471
pub this_param: Option<TSThisParameter<'a>>,
14721472
pub params: Box<'a, FormalParameters<'a>>,
1473-
pub body: Option<Box<'a, FunctionBody<'a>>>,
14741473
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
1474+
pub body: Option<Box<'a, FunctionBody<'a>>>,
14751475
pub scope_id: Cell<Option<ScopeId>>,
14761476
}
14771477

crates/oxc_ast/src/ast_builder_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<'a> AstBuilder<'a> {
150150
Option::<TSTypeParameterDeclaration>::None,
151151
None,
152152
params,
153-
body,
154153
Option::<TSTypeAnnotation>::None,
154+
body,
155155
))
156156
}
157157

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,14 @@ impl<'a> AstBuilder<'a> {
554554
type_parameters: T1,
555555
this_param: Option<TSThisParameter<'a>>,
556556
params: T2,
557-
body: T3,
558-
return_type: T4,
557+
return_type: T3,
558+
body: T4,
559559
) -> Expression<'a>
560560
where
561561
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
562562
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
563-
T3: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
564-
T4: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
563+
T3: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
564+
T4: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
565565
{
566566
Expression::FunctionExpression(self.alloc(self.function(
567567
r#type,
@@ -573,8 +573,8 @@ impl<'a> AstBuilder<'a> {
573573
type_parameters,
574574
this_param,
575575
params,
576-
body,
577576
return_type,
577+
body,
578578
)))
579579
}
580580

@@ -2689,14 +2689,14 @@ impl<'a> AstBuilder<'a> {
26892689
type_parameters: T1,
26902690
this_param: Option<TSThisParameter<'a>>,
26912691
params: T2,
2692-
body: T3,
2693-
return_type: T4,
2692+
return_type: T3,
2693+
body: T4,
26942694
) -> Declaration<'a>
26952695
where
26962696
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
26972697
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
2698-
T3: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
2699-
T4: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
2698+
T3: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
2699+
T4: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
27002700
{
27012701
Declaration::FunctionDeclaration(self.alloc(self.function(
27022702
r#type,
@@ -2708,8 +2708,8 @@ impl<'a> AstBuilder<'a> {
27082708
type_parameters,
27092709
this_param,
27102710
params,
2711-
body,
27122711
return_type,
2712+
body,
27132713
)))
27142714
}
27152715

@@ -3720,14 +3720,14 @@ impl<'a> AstBuilder<'a> {
37203720
type_parameters: T1,
37213721
this_param: Option<TSThisParameter<'a>>,
37223722
params: T2,
3723-
body: T3,
3724-
return_type: T4,
3723+
return_type: T3,
3724+
body: T4,
37253725
) -> Function<'a>
37263726
where
37273727
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
37283728
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
3729-
T3: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
3730-
T4: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
3729+
T3: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
3730+
T4: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
37313731
{
37323732
Function {
37333733
r#type,
@@ -3739,8 +3739,8 @@ impl<'a> AstBuilder<'a> {
37393739
type_parameters: type_parameters.into_in(self.allocator),
37403740
this_param,
37413741
params: params.into_in(self.allocator),
3742-
body: body.into_in(self.allocator),
37433742
return_type: return_type.into_in(self.allocator),
3743+
body: body.into_in(self.allocator),
37443744
scope_id: Default::default(),
37453745
}
37463746
}
@@ -3757,14 +3757,14 @@ impl<'a> AstBuilder<'a> {
37573757
type_parameters: T1,
37583758
this_param: Option<TSThisParameter<'a>>,
37593759
params: T2,
3760-
body: T3,
3761-
return_type: T4,
3760+
return_type: T3,
3761+
body: T4,
37623762
) -> Box<'a, Function<'a>>
37633763
where
37643764
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
37653765
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
3766-
T3: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
3767-
T4: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
3766+
T3: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
3767+
T4: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
37683768
{
37693769
self.function(
37703770
r#type,
@@ -3776,8 +3776,8 @@ impl<'a> AstBuilder<'a> {
37763776
type_parameters,
37773777
this_param,
37783778
params,
3779-
body,
37803779
return_type,
3780+
body,
37813781
)
37823782
.into_in(self.allocator)
37833783
}
@@ -4901,14 +4901,14 @@ impl<'a> AstBuilder<'a> {
49014901
type_parameters: T1,
49024902
this_param: Option<TSThisParameter<'a>>,
49034903
params: T2,
4904-
body: T3,
4905-
return_type: T4,
4904+
return_type: T3,
4905+
body: T4,
49064906
) -> ExportDefaultDeclarationKind<'a>
49074907
where
49084908
T1: IntoIn<'a, Option<Box<'a, TSTypeParameterDeclaration<'a>>>>,
49094909
T2: IntoIn<'a, Box<'a, FormalParameters<'a>>>,
4910-
T3: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
4911-
T4: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
4910+
T3: IntoIn<'a, Option<Box<'a, TSTypeAnnotation<'a>>>>,
4911+
T4: IntoIn<'a, Option<Box<'a, FunctionBody<'a>>>>,
49124912
{
49134913
ExportDefaultDeclarationKind::FunctionDeclaration(self.alloc(self.function(
49144914
r#type,
@@ -4920,8 +4920,8 @@ impl<'a> AstBuilder<'a> {
49204920
type_parameters,
49214921
this_param,
49224922
params,
4923-
body,
49244923
return_type,
4924+
body,
49254925
)))
49264926
}
49274927

crates/oxc_ast/src/generated/visit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,7 @@ pub mod walk {
30383038
visitor.enter_scope(
30393039
{
30403040
let mut flags = flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function;
3041-
if it.body.as_ref().is_some_and(|body| body.has_use_strict_directive()) {
3041+
if it.is_strict() {
30423042
flags |= ScopeFlags::StrictMode;
30433043
}
30443044
flags
@@ -3055,12 +3055,12 @@ pub mod walk {
30553055
visitor.visit_ts_this_parameter(this_param);
30563056
}
30573057
visitor.visit_formal_parameters(&it.params);
3058-
if let Some(body) = &it.body {
3059-
visitor.visit_function_body(body);
3060-
}
30613058
if let Some(return_type) = &it.return_type {
30623059
visitor.visit_ts_type_annotation(return_type);
30633060
}
3061+
if let Some(body) = &it.body {
3062+
visitor.visit_function_body(body);
3063+
}
30643064
visitor.leave_scope();
30653065
visitor.leave_node(kind);
30663066
}

crates/oxc_ast/src/generated/visit_mut.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ pub mod walk_mut {
31713171
visitor.enter_scope(
31723172
{
31733173
let mut flags = flags.unwrap_or(ScopeFlags::empty()) | ScopeFlags::Function;
3174-
if it.body.as_ref().is_some_and(|body| body.has_use_strict_directive()) {
3174+
if it.is_strict() {
31753175
flags |= ScopeFlags::StrictMode;
31763176
}
31773177
flags
@@ -3188,12 +3188,12 @@ pub mod walk_mut {
31883188
visitor.visit_ts_this_parameter(this_param);
31893189
}
31903190
visitor.visit_formal_parameters(&mut it.params);
3191-
if let Some(body) = &mut it.body {
3192-
visitor.visit_function_body(body);
3193-
}
31943191
if let Some(return_type) = &mut it.return_type {
31953192
visitor.visit_ts_type_annotation(return_type);
31963193
}
3194+
if let Some(body) = &mut it.body {
3195+
visitor.visit_function_body(body);
3196+
}
31973197
visitor.leave_scope();
31983198
visitor.leave_node(kind);
31993199
}

crates/oxc_isolated_declarations/src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ impl<'a> IsolatedDeclarations<'a> {
123123
self.ast.copy(&function.type_parameters),
124124
self.ast.copy(&function.this_param),
125125
params,
126-
Option::<FunctionBody>::None,
127126
return_type,
127+
Option::<FunctionBody>::None,
128128
);
129129

130130
self.ast.class_element_method_definition(

crates/oxc_isolated_declarations/src/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ impl<'a> IsolatedDeclarations<'a> {
3737
self.ast.copy(&func.type_parameters),
3838
self.ast.copy(&func.this_param),
3939
params,
40-
Option::<FunctionBody>::None,
4140
return_type,
41+
Option::<FunctionBody>::None,
4242
))
4343
}
4444
}

crates/oxc_linter/src/snapshots/ban_types.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,27 @@ source: crates/oxc_linter/src/tester.rs
209209
help: The `Function` type accepts any function-like value
210210

211211
⚠ typescript-eslint(ban-types): Do not use "String" as a type. Use "string" instead
212-
╭─[ban_types.tsx:6:24]
212+
╭─[ban_types.tsx:5:24]
213+
4 │
213214
5 │ arg(): Array<String> {
214-
6const foo: String = 1 as String;
215215
· ──────
216-
7}
216+
6 const foo: String = 1 as String;
217217
╰────
218218

219219
typescript-eslint(ban-types): Do not use "String" as a type. Use "string" instead
220-
╭─[ban_types.tsx:6:38]
220+
╭─[ban_types.tsx:6:24]
221221
5arg(): Array<String> {
222222
6const foo: String = 1 as String;
223-
· ──────
223+
· ──────
224224
7}
225225
╰────
226226

227227
⚠ typescript-eslint(ban-types): Do not use "String" as a type. Use "string" instead
228-
╭─[ban_types.tsx:5:24]
229-
4 │
228+
╭─[ban_types.tsx:6:38]
230229
5 │ arg(): Array<String> {
231-
· ──────
232230
6const foo: String = 1 as String;
231+
· ──────
232+
7}
233233
╰────
234234

235235
⚠ typescript-eslint(ban-types): Don't use `Function` as a type

crates/oxc_parser/src/js/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ impl<'a> ParserImpl<'a> {
181181
type_parameters,
182182
this_param,
183183
params,
184-
body,
185184
return_type,
185+
body,
186186
))
187187
}
188188

crates/oxc_semantic/src/binder.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ fn function_as_var(flags: ScopeFlags, source_type: SourceType) -> bool {
106106
impl<'a> Binder for Function<'a> {
107107
fn bind(&self, builder: &mut SemanticBuilder) {
108108
let current_scope_id = builder.current_scope_id;
109+
let scope_flags = builder.current_scope_flags();
109110
if let Some(ident) = &self.id {
110-
if !builder.current_scope_flags().is_strict_mode()
111+
if !scope_flags.is_strict_mode()
111112
&& matches!(
112113
builder.nodes.parent_kind(builder.current_node_id),
113114
Some(AstKind::IfStatement(_))
@@ -118,12 +119,10 @@ impl<'a> Binder for Function<'a> {
118119
} else if self.r#type == FunctionType::FunctionDeclaration {
119120
// The visitor is already inside the function scope,
120121
// retrieve the parent scope for the function id to bind to.
121-
let parent_scope_id = builder.scope.get_parent_id(current_scope_id).unwrap();
122-
let parent_flags = builder.scope.get_flags(parent_scope_id);
123122

124123
let (includes, excludes) =
125-
if (parent_flags.is_strict_mode() || self.r#async || self.generator)
126-
&& !function_as_var(parent_flags, builder.source_type)
124+
if (scope_flags.is_strict_mode() || self.r#async || self.generator)
125+
&& !function_as_var(scope_flags, builder.source_type)
127126
{
128127
(
129128
SymbolFlags::Function | SymbolFlags::BlockScopedVariable,
@@ -136,13 +135,7 @@ impl<'a> Binder for Function<'a> {
136135
)
137136
};
138137

139-
let symbol_id = builder.declare_symbol_on_scope(
140-
ident.span,
141-
&ident.name,
142-
parent_scope_id,
143-
includes,
144-
excludes,
145-
);
138+
let symbol_id = builder.declare_symbol(ident.span, &ident.name, includes, excludes);
146139
ident.symbol_id.set(Some(symbol_id));
147140
} else if self.r#type == FunctionType::FunctionExpression {
148141
// https://tc39.es/ecma262/#sec-runtime-semantics-instantiateordinaryfunctionexpression
@@ -158,7 +151,6 @@ impl<'a> Binder for Function<'a> {
158151
}
159152

160153
// bind scope flags: Constructor | GetAccessor | SetAccessor
161-
debug_assert!(builder.current_scope_flags().contains(ScopeFlags::Function));
162154
if let Some(kind) = builder.nodes.parent_kind(builder.current_node_id) {
163155
match kind {
164156
AstKind::MethodDefinition(def) => {

0 commit comments

Comments
 (0)