Skip to content

Commit 4591ba3

Browse files
committed
feat(ast): provide NONE type for AST builder calls
1 parent 805fbac commit 4591ba3

27 files changed

+111
-219
lines changed

crates/oxc_ast/src/ast_builder_impl.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77

88
use std::mem;
99

10-
use oxc_allocator::{Allocator, Box, String, Vec};
10+
use oxc_allocator::{Allocator, Box, FromIn, String, Vec};
1111
use oxc_span::{Atom, GetSpan, Span};
1212
use oxc_syntax::{number::NumberBase, operator::UnaryOperator};
1313

1414
#[allow(clippy::wildcard_imports)]
1515
use crate::ast::*;
1616
use crate::AstBuilder;
1717

18+
/// Type that can be used in any AST builder method call which requires an `IntoIn<'a, Anything<'a>>`.
19+
/// Pass `NONE` instead of `None::<Anything<'a>>`.
20+
#[allow(clippy::upper_case_acronyms)]
21+
pub struct NONE;
22+
23+
impl<'a, T> FromIn<'a, NONE> for Option<Box<'a, T>> {
24+
fn from_in(_: NONE, _: &'a Allocator) -> Self {
25+
None
26+
}
27+
}
28+
1829
impl<'a> AstBuilder<'a> {
1930
#[inline]
2031
pub fn new(allocator: &'a Allocator) -> Self {
@@ -145,19 +156,9 @@ impl<'a> AstBuilder<'a> {
145156
params: FormalParameters<'a>,
146157
body: Option<FunctionBody<'a>>,
147158
) -> Box<'a, Function<'a>> {
148-
self.alloc(self.function(
149-
r#type,
150-
span,
151-
id,
152-
false,
153-
false,
154-
false,
155-
Option::<TSTypeParameterDeclaration>::None,
156-
None::<Box<'a, TSThisParameter<'a>>>,
157-
params,
158-
Option::<TSTypeAnnotation>::None,
159-
body,
160-
))
159+
self.alloc(
160+
self.function(r#type, span, id, false, false, false, NONE, NONE, params, NONE, body),
161+
)
161162
}
162163

163164
/* ---------- Modules ---------- */
@@ -174,7 +175,7 @@ impl<'a> AstBuilder<'a> {
174175
self.vec(),
175176
None,
176177
ImportOrExportKind::Value,
177-
None::<WithClause>,
178+
NONE,
178179
))
179180
}
180181

@@ -191,7 +192,7 @@ impl<'a> AstBuilder<'a> {
191192
specifiers,
192193
source,
193194
ImportOrExportKind::Value,
194-
None::<WithClause>,
195+
NONE,
195196
))
196197
}
197198

crates/oxc_ast/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub use num_bigint::BigUint;
5959

6060
pub use crate::{
6161
ast_builder::AstBuilder,
62+
ast_builder_impl::NONE,
6263
ast_kind::{AstKind, AstType},
6364
trivia::{Comment, CommentKind, SortedComments, Trivias},
6465
visit::{Visit, VisitMut},

crates/oxc_isolated_declarations/src/class.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use oxc_allocator::Box;
22
#[allow(clippy::wildcard_imports)]
3-
use oxc_ast::ast::*;
3+
use oxc_ast::{ast::*, NONE};
44
use oxc_span::{Atom, GetSpan, SPAN};
55
use rustc_hash::FxHashMap;
66

@@ -130,7 +130,7 @@ impl<'a> IsolatedDeclarations<'a> {
130130
unsafe { self.ast.copy(&function.this_param) },
131131
params,
132132
return_type,
133-
Option::<FunctionBody>::None,
133+
NONE,
134134
);
135135

136136
self.ast.class_element_method_definition(
@@ -170,7 +170,7 @@ impl<'a> IsolatedDeclarations<'a> {
170170
false,
171171
false,
172172
false,
173-
Option::<TSTypeAnnotation>::None,
173+
NONE,
174174
accessibility,
175175
)
176176
}
@@ -228,7 +228,7 @@ impl<'a> IsolatedDeclarations<'a> {
228228
SPAN,
229229
FormalParameterKind::Signature,
230230
self.ast.vec(),
231-
Option::<BindingRestElement>::None,
231+
NONE,
232232
);
233233
self.transform_class_method_definition(method, params, None)
234234
}
@@ -491,20 +491,8 @@ impl<'a> IsolatedDeclarations<'a> {
491491
let r#type = PropertyDefinitionType::PropertyDefinition;
492492
let decorators = self.ast.vec();
493493
let element = self.ast.class_element_property_definition(
494-
r#type,
495-
SPAN,
496-
decorators,
497-
ident,
498-
None,
499-
false,
500-
false,
501-
false,
502-
false,
503-
false,
504-
false,
505-
false,
506-
Option::<TSTypeAnnotation>::None,
507-
None,
494+
r#type, SPAN, decorators, ident, None, false, false, false, false, false, false,
495+
false, NONE, None,
508496
);
509497

510498
elements.insert(0, element);
@@ -562,11 +550,6 @@ impl<'a> IsolatedDeclarations<'a> {
562550
let parameter =
563551
self.ast.formal_parameter(SPAN, self.ast.vec(), pattern, None, false, false);
564552
let items = self.ast.vec1(parameter);
565-
self.ast.alloc_formal_parameters(
566-
SPAN,
567-
FormalParameterKind::Signature,
568-
items,
569-
Option::<BindingRestElement>::None,
570-
)
553+
self.ast.alloc_formal_parameters(SPAN, FormalParameterKind::Signature, items, NONE)
571554
}
572555
}

crates/oxc_isolated_declarations/src/function.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use oxc_allocator::Box;
2-
use oxc_ast::ast::Function;
32
#[allow(clippy::wildcard_imports)]
4-
use oxc_ast::ast::*;
3+
use oxc_ast::{ast::*, NONE};
54
use oxc_span::{Span, SPAN};
65

76
use crate::{
@@ -41,7 +40,7 @@ impl<'a> IsolatedDeclarations<'a> {
4140
unsafe { self.ast.copy(&func.this_param) },
4241
params,
4342
return_type,
44-
Option::<FunctionBody>::None,
43+
NONE,
4544
))
4645
}
4746
}

crates/oxc_isolated_declarations/src/lib.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::{cell::RefCell, collections::VecDeque, mem};
2424
use diagnostics::function_with_assigning_properties;
2525
use oxc_allocator::Allocator;
2626
#[allow(clippy::wildcard_imports)]
27-
use oxc_ast::{ast::*, AstBuilder, Visit};
27+
use oxc_ast::{ast::*, AstBuilder, Visit, NONE};
2828
use oxc_diagnostics::OxcDiagnostic;
2929
use oxc_span::{Atom, SourceType, SPAN};
3030
use rustc_hash::{FxHashMap, FxHashSet};
@@ -308,14 +308,8 @@ impl<'a> IsolatedDeclarations<'a> {
308308
if need_empty_export_marker {
309309
let specifiers = self.ast.vec();
310310
let kind = ImportOrExportKind::Value;
311-
let empty_export = self.ast.alloc_export_named_declaration(
312-
SPAN,
313-
None,
314-
specifiers,
315-
None,
316-
kind,
317-
None::<WithClause>,
318-
);
311+
let empty_export =
312+
self.ast.alloc_export_named_declaration(SPAN, None, specifiers, None, kind, NONE);
319313
new_ast_stmts
320314
.push(Statement::from(ModuleDeclaration::ExportNamedDeclaration(empty_export)));
321315
}

crates/oxc_isolated_declarations/src/types.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use oxc_allocator::Box;
2-
use oxc_ast::ast::{
3-
ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, Expression, Function,
4-
ObjectExpression, ObjectPropertyKind, TSLiteral, TSMethodSignatureKind, TSThisParameter,
5-
TSTupleElement, TSType, TSTypeOperatorOperator,
1+
use oxc_ast::{
2+
ast::{
3+
ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, Expression, Function,
4+
ObjectExpression, ObjectPropertyKind, TSLiteral, TSMethodSignatureKind, TSTupleElement,
5+
TSType, TSTypeOperatorOperator,
6+
},
7+
NONE,
68
};
79
use oxc_span::{GetSpan, Span, SPAN};
810

@@ -55,7 +57,7 @@ impl<'a> IsolatedDeclarations<'a> {
5557
return_type.map(|return_type| {
5658
self.ast.ts_type_function_type(
5759
func.span,
58-
None::<Box<'a, TSThisParameter<'a>>>,
60+
NONE,
5961
params,
6062
return_type,
6163
// SAFETY: `ast.copy` is unsound! We need to fix.

crates/oxc_minifier/src/keep_var.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, AstBuilder, Visit};
1+
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, AstBuilder, Visit, NONE};
22
use oxc_span::{Atom, Span, SPAN};
33

44
pub struct KeepVar<'a> {
@@ -57,8 +57,7 @@ impl<'a> KeepVar<'a> {
5757
let kind = VariableDeclarationKind::Var;
5858
let decls = self.ast.vec_from_iter(self.vars.into_iter().map(|(name, span)| {
5959
let binding_kind = self.ast.binding_pattern_kind_binding_identifier(span, name);
60-
let id =
61-
self.ast.binding_pattern::<Option<TSTypeAnnotation>>(binding_kind, None, false);
60+
let id = self.ast.binding_pattern(binding_kind, NONE, false);
6261
self.ast.variable_declarator(span, kind, id, None, false)
6362
}));
6463

crates/oxc_minifier/src/plugins/inject_global_variables.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use cow_utils::CowUtils;
44

55
use oxc_allocator::Allocator;
6-
use oxc_ast::{ast::*, AstBuilder};
6+
use oxc_ast::{ast::*, AstBuilder, NONE};
77
use oxc_semantic::{ScopeTree, SymbolTable};
88
use oxc_span::{CompactStr, SPAN};
99
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};
@@ -197,13 +197,9 @@ impl<'a> InjectGlobalVariables<'a> {
197197
let specifiers = Some(self.ast.vec1(self.inject_import_to_specifier(inject)));
198198
let source = self.ast.string_literal(SPAN, inject.source.as_str());
199199
let kind = ImportOrExportKind::Value;
200-
let import_decl = self.ast.module_declaration_import_declaration(
201-
SPAN,
202-
specifiers,
203-
source,
204-
None::<WithClause>,
205-
kind,
206-
);
200+
let import_decl = self
201+
.ast
202+
.module_declaration_import_declaration(SPAN, specifiers, source, NONE, kind);
207203
self.ast.statement_module_declaration(import_decl)
208204
});
209205
program.body.splice(0..0, imports);

crates/oxc_parser/src/js/arrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use oxc_allocator::Box;
2-
use oxc_ast::ast::*;
2+
use oxc_ast::{ast::*, NONE};
33
use oxc_diagnostics::Result;
44
use oxc_span::{GetSpan, Span};
55
use oxc_syntax::precedence::Precedence;
@@ -218,13 +218,13 @@ impl<'a> ParserImpl<'a> {
218218
};
219219
let params_span = self.end_span(ident.span);
220220
let ident = self.ast.binding_pattern_kind_from_binding_identifier(ident);
221-
let pattern = self.ast.binding_pattern(ident, Option::<TSTypeAnnotation>::None, false);
221+
let pattern = self.ast.binding_pattern(ident, NONE, false);
222222
let formal_parameter = self.ast.plain_formal_parameter(params_span, pattern);
223223
self.ast.alloc_formal_parameters(
224224
params_span,
225225
FormalParameterKind::ArrowFormalParameters,
226226
self.ast.vec1(formal_parameter),
227-
Option::<BindingRestElement>::None,
227+
NONE,
228228
)
229229
};
230230

crates/oxc_parser/src/js/binding.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use oxc_ast::ast::*;
1+
use oxc_ast::{ast::*, NONE};
22
use oxc_diagnostics::Result;
33
use oxc_span::{GetSpan, Span};
44

@@ -145,8 +145,7 @@ impl<'a> ParserImpl<'a> {
145145
shorthand = true;
146146
let identifier =
147147
self.ast.binding_pattern_kind_binding_identifier(ident.span, &ident.name);
148-
let left =
149-
self.ast.binding_pattern(identifier, Option::<TSTypeAnnotation>::None, false);
148+
let left = self.ast.binding_pattern(identifier, NONE, false);
150149
self.context(Context::In, Context::empty(), |p| p.parse_initializer(span, left))?
151150
} else {
152151
return Err(self.unexpected());
@@ -172,7 +171,7 @@ impl<'a> ParserImpl<'a> {
172171
let expr = self.parse_assignment_expression_or_higher()?;
173172
Ok(self.ast.binding_pattern(
174173
self.ast.binding_pattern_kind_assignment_pattern(self.end_span(span), left, expr),
175-
Option::<TSTypeAnnotation>::None,
174+
NONE,
176175
false,
177176
))
178177
} else {

crates/oxc_parser/src/js/declaration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use oxc_allocator::Box;
2-
use oxc_ast::ast::*;
2+
use oxc_ast::{ast::*, NONE};
33
use oxc_diagnostics::Result;
44
use oxc_span::{GetSpan, Span};
55

@@ -113,7 +113,7 @@ impl<'a> ParserImpl<'a> {
113113
}
114114
(self.ast.binding_pattern(binding_kind, type_annotation, optional), definite)
115115
} else {
116-
(self.ast.binding_pattern(binding_kind, Option::<TSTypeAnnotation>::None, false), false)
116+
(self.ast.binding_pattern(binding_kind, NONE, false), false)
117117
};
118118

119119
let init =

crates/oxc_parser/src/js/module.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use oxc_allocator::{Box, Vec};
2-
use oxc_ast::ast::*;
2+
use oxc_ast::{ast::*, NONE};
33
use oxc_diagnostics::Result;
44
use oxc_span::{GetSpan, Span};
55
use rustc_hash::FxHashMap;
@@ -347,7 +347,7 @@ impl<'a> ParserImpl<'a> {
347347
self.ast.vec(),
348348
None,
349349
ImportOrExportKind::Value,
350-
None::<WithClause>,
350+
NONE,
351351
))
352352
}
353353

crates/oxc_parser/src/ts/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use oxc_allocator::{Box, Vec};
2-
use oxc_ast::ast::*;
2+
use oxc_ast::{ast::*, NONE};
33
use oxc_diagnostics::Result;
44
use oxc_span::GetSpan;
55
use oxc_syntax::operator::UnaryOperator;
@@ -1169,7 +1169,7 @@ impl<'a> ParserImpl<'a> {
11691169
this_param,
11701170
params,
11711171
return_type,
1172-
Option::<TSTypeParameterDeclaration>::None,
1172+
NONE,
11731173
))
11741174
}
11751175

@@ -1195,7 +1195,7 @@ impl<'a> ParserImpl<'a> {
11951195
this_param,
11961196
params,
11971197
return_type,
1198-
Option::<TSTypeParameterDeclaration>::None,
1198+
NONE,
11991199
))
12001200
}
12011201

crates/oxc_transformer/src/es2015/arrow_functions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
use std::cell::Cell;
6363

6464
use oxc_allocator::Vec;
65-
use oxc_ast::ast::*;
65+
use oxc_ast::{ast::*, NONE};
6666
use oxc_span::SPAN;
6767
use oxc_syntax::{scope::ScopeFlags, symbol::SymbolFlags};
6868
use oxc_traverse::{Traverse, TraverseCtx};
@@ -294,7 +294,7 @@ impl<'a> ArrowFunctions<'a> {
294294
self.ctx
295295
.ast
296296
.binding_pattern_kind_from_binding_identifier(id.create_binding_identifier()),
297-
Option::<TSTypeAnnotation>::None,
297+
NONE,
298298
false,
299299
);
300300

0 commit comments

Comments
 (0)