Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): provide NONE type for AST builder calls #5737

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@

use std::mem;

use oxc_allocator::{Allocator, Box, String, Vec};
use oxc_allocator::{Allocator, Box, FromIn, String, Vec};
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::{number::NumberBase, operator::UnaryOperator};

#[allow(clippy::wildcard_imports)]
use crate::ast::*;
use crate::AstBuilder;

/// Type that can be used in any AST builder method call which requires an `IntoIn<'a, Anything<'a>>`.
/// Pass `NONE` instead of `None::<Anything<'a>>`.
#[allow(clippy::upper_case_acronyms)]
pub struct NONE;

impl<'a, T> FromIn<'a, NONE> for Option<Box<'a, T>> {
fn from_in(_: NONE, _: &'a Allocator) -> Self {
None
}
}

impl<'a> AstBuilder<'a> {
#[inline]
pub fn new(allocator: &'a Allocator) -> Self {
Expand Down Expand Up @@ -145,19 +156,9 @@ impl<'a> AstBuilder<'a> {
params: FormalParameters<'a>,
body: Option<FunctionBody<'a>>,
) -> Box<'a, Function<'a>> {
self.alloc(self.function(
r#type,
span,
id,
false,
false,
false,
Option::<TSTypeParameterDeclaration>::None,
None::<Box<'a, TSThisParameter<'a>>>,
params,
Option::<TSTypeAnnotation>::None,
body,
))
self.alloc(
self.function(r#type, span, id, false, false, false, NONE, NONE, params, NONE, body),
)
}

/* ---------- Modules ---------- */
Expand All @@ -174,7 +175,7 @@ impl<'a> AstBuilder<'a> {
self.vec(),
None,
ImportOrExportKind::Value,
None::<WithClause>,
NONE,
))
}

Expand All @@ -191,7 +192,7 @@ impl<'a> AstBuilder<'a> {
specifiers,
source,
ImportOrExportKind::Value,
None::<WithClause>,
NONE,
))
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub use num_bigint::BigUint;

pub use crate::{
ast_builder::AstBuilder,
ast_builder_impl::NONE,
ast_kind::{AstKind, AstType},
trivia::{Comment, CommentKind, SortedComments, Trivias},
visit::{Visit, VisitMut},
Expand Down
31 changes: 7 additions & 24 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use oxc_allocator::Box;
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_span::{Atom, GetSpan, SPAN};
use rustc_hash::FxHashMap;

Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'a> IsolatedDeclarations<'a> {
unsafe { self.ast.copy(&function.this_param) },
params,
return_type,
Option::<FunctionBody>::None,
NONE,
);

self.ast.class_element_method_definition(
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<'a> IsolatedDeclarations<'a> {
false,
false,
false,
Option::<TSTypeAnnotation>::None,
NONE,
accessibility,
)
}
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'a> IsolatedDeclarations<'a> {
SPAN,
FormalParameterKind::Signature,
self.ast.vec(),
Option::<BindingRestElement>::None,
NONE,
);
self.transform_class_method_definition(method, params, None)
}
Expand Down Expand Up @@ -491,20 +491,8 @@ impl<'a> IsolatedDeclarations<'a> {
let r#type = PropertyDefinitionType::PropertyDefinition;
let decorators = self.ast.vec();
let element = self.ast.class_element_property_definition(
r#type,
SPAN,
decorators,
ident,
None,
false,
false,
false,
false,
false,
false,
false,
Option::<TSTypeAnnotation>::None,
None,
r#type, SPAN, decorators, ident, None, false, false, false, false, false, false,
false, NONE, None,
);

elements.insert(0, element);
Expand Down Expand Up @@ -562,11 +550,6 @@ impl<'a> IsolatedDeclarations<'a> {
let parameter =
self.ast.formal_parameter(SPAN, self.ast.vec(), pattern, None, false, false);
let items = self.ast.vec1(parameter);
self.ast.alloc_formal_parameters(
SPAN,
FormalParameterKind::Signature,
items,
Option::<BindingRestElement>::None,
)
self.ast.alloc_formal_parameters(SPAN, FormalParameterKind::Signature, items, NONE)
}
}
5 changes: 2 additions & 3 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use oxc_allocator::Box;
use oxc_ast::ast::Function;
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_span::{Span, SPAN};

use crate::{
Expand Down Expand Up @@ -41,7 +40,7 @@ impl<'a> IsolatedDeclarations<'a> {
unsafe { self.ast.copy(&func.this_param) },
params,
return_type,
Option::<FunctionBody>::None,
NONE,
))
}
}
Expand Down
12 changes: 3 additions & 9 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{cell::RefCell, collections::VecDeque, mem};
use diagnostics::function_with_assigning_properties;
use oxc_allocator::Allocator;
#[allow(clippy::wildcard_imports)]
use oxc_ast::{ast::*, AstBuilder, Visit};
use oxc_ast::{ast::*, AstBuilder, Visit, NONE};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Atom, SourceType, SPAN};
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -308,14 +308,8 @@ impl<'a> IsolatedDeclarations<'a> {
if need_empty_export_marker {
let specifiers = self.ast.vec();
let kind = ImportOrExportKind::Value;
let empty_export = self.ast.alloc_export_named_declaration(
SPAN,
None,
specifiers,
None,
kind,
None::<WithClause>,
);
let empty_export =
self.ast.alloc_export_named_declaration(SPAN, None, specifiers, None, kind, NONE);
new_ast_stmts
.push(Statement::from(ModuleDeclaration::ExportNamedDeclaration(empty_export)));
}
Expand Down
14 changes: 8 additions & 6 deletions crates/oxc_isolated_declarations/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use oxc_allocator::Box;
use oxc_ast::ast::{
ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, Expression, Function,
ObjectExpression, ObjectPropertyKind, TSLiteral, TSMethodSignatureKind, TSThisParameter,
TSTupleElement, TSType, TSTypeOperatorOperator,
use oxc_ast::{
ast::{
ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, Expression, Function,
ObjectExpression, ObjectPropertyKind, TSLiteral, TSMethodSignatureKind, TSTupleElement,
TSType, TSTypeOperatorOperator,
},
NONE,
};
use oxc_span::{GetSpan, Span, SPAN};

Expand Down Expand Up @@ -55,7 +57,7 @@ impl<'a> IsolatedDeclarations<'a> {
return_type.map(|return_type| {
self.ast.ts_type_function_type(
func.span,
None::<Box<'a, TSThisParameter<'a>>>,
NONE,
params,
return_type,
// SAFETY: `ast.copy` is unsound! We need to fix.
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_minifier/src/keep_var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, AstBuilder, Visit};
use oxc_ast::{ast::*, syntax_directed_operations::BoundNames, AstBuilder, Visit, NONE};
use oxc_span::{Atom, Span, SPAN};

pub struct KeepVar<'a> {
Expand Down Expand Up @@ -57,8 +57,7 @@ impl<'a> KeepVar<'a> {
let kind = VariableDeclarationKind::Var;
let decls = self.ast.vec_from_iter(self.vars.into_iter().map(|(name, span)| {
let binding_kind = self.ast.binding_pattern_kind_binding_identifier(span, name);
let id =
self.ast.binding_pattern::<Option<TSTypeAnnotation>>(binding_kind, None, false);
let id = self.ast.binding_pattern(binding_kind, NONE, false);
self.ast.variable_declarator(span, kind, id, None, false)
}));

Expand Down
12 changes: 4 additions & 8 deletions crates/oxc_minifier/src/plugins/inject_global_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use cow_utils::CowUtils;

use oxc_allocator::Allocator;
use oxc_ast::{ast::*, AstBuilder};
use oxc_ast::{ast::*, AstBuilder, NONE};
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::{CompactStr, SPAN};
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};
Expand Down Expand Up @@ -197,13 +197,9 @@ impl<'a> InjectGlobalVariables<'a> {
let specifiers = Some(self.ast.vec1(self.inject_import_to_specifier(inject)));
let source = self.ast.string_literal(SPAN, inject.source.as_str());
let kind = ImportOrExportKind::Value;
let import_decl = self.ast.module_declaration_import_declaration(
SPAN,
specifiers,
source,
None::<WithClause>,
kind,
);
let import_decl = self
.ast
.module_declaration_import_declaration(SPAN, specifiers, source, NONE, kind);
self.ast.statement_module_declaration(import_decl)
});
program.body.splice(0..0, imports);
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_parser/src/js/arrow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_allocator::Box;
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};
use oxc_syntax::precedence::Precedence;
Expand Down Expand Up @@ -218,13 +218,13 @@ impl<'a> ParserImpl<'a> {
};
let params_span = self.end_span(ident.span);
let ident = self.ast.binding_pattern_kind_from_binding_identifier(ident);
let pattern = self.ast.binding_pattern(ident, Option::<TSTypeAnnotation>::None, false);
let pattern = self.ast.binding_pattern(ident, NONE, false);
let formal_parameter = self.ast.plain_formal_parameter(params_span, pattern);
self.ast.alloc_formal_parameters(
params_span,
FormalParameterKind::ArrowFormalParameters,
self.ast.vec1(formal_parameter),
Option::<BindingRestElement>::None,
NONE,
)
};

Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_parser/src/js/binding.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};

Expand Down Expand Up @@ -145,8 +145,7 @@ impl<'a> ParserImpl<'a> {
shorthand = true;
let identifier =
self.ast.binding_pattern_kind_binding_identifier(ident.span, &ident.name);
let left =
self.ast.binding_pattern(identifier, Option::<TSTypeAnnotation>::None, false);
let left = self.ast.binding_pattern(identifier, NONE, false);
self.context(Context::In, Context::empty(), |p| p.parse_initializer(span, left))?
} else {
return Err(self.unexpected());
Expand All @@ -172,7 +171,7 @@ impl<'a> ParserImpl<'a> {
let expr = self.parse_assignment_expression_or_higher()?;
Ok(self.ast.binding_pattern(
self.ast.binding_pattern_kind_assignment_pattern(self.end_span(span), left, expr),
Option::<TSTypeAnnotation>::None,
NONE,
false,
))
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/js/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_allocator::Box;
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};

Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'a> ParserImpl<'a> {
}
(self.ast.binding_pattern(binding_kind, type_annotation, optional), definite)
} else {
(self.ast.binding_pattern(binding_kind, Option::<TSTypeAnnotation>::None, false), false)
(self.ast.binding_pattern(binding_kind, NONE, false), false)
};

let init =
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -347,7 +347,7 @@ impl<'a> ParserImpl<'a> {
self.ast.vec(),
None,
ImportOrExportKind::Value,
None::<WithClause>,
NONE,
))
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_diagnostics::Result;
use oxc_span::GetSpan;
use oxc_syntax::operator::UnaryOperator;
Expand Down Expand Up @@ -1169,7 +1169,7 @@ impl<'a> ParserImpl<'a> {
this_param,
params,
return_type,
Option::<TSTypeParameterDeclaration>::None,
NONE,
))
}

Expand All @@ -1195,7 +1195,7 @@ impl<'a> ParserImpl<'a> {
this_param,
params,
return_type,
Option::<TSTypeParameterDeclaration>::None,
NONE,
))
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
use std::cell::Cell;

use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_ast::{ast::*, NONE};
use oxc_span::SPAN;
use oxc_syntax::{scope::ScopeFlags, symbol::SymbolFlags};
use oxc_traverse::{Traverse, TraverseCtx};
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<'a> ArrowFunctions<'a> {
self.ctx
.ast
.binding_pattern_kind_from_binding_identifier(id.create_binding_identifier()),
Option::<TSTypeAnnotation>::None,
NONE,
false,
);

Expand Down
Loading