Skip to content

Commit

Permalink
refactor(ast)!: replace Modifiers with declare on Function (#3844)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 23, 2024
1 parent ee6ec4e commit 0673677
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 106 deletions.
3 changes: 1 addition & 2 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,7 @@ pub struct Function<'a> {
pub id: Option<BindingIdentifier<'a>>,
pub generator: bool,
pub r#async: bool,
pub declare: bool,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
/// Declaring `this` in a Function <https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function>
///
Expand All @@ -1456,8 +1457,6 @@ pub struct Function<'a> {
pub params: Box<'a, FormalParameters<'a>>,
pub body: Option<Box<'a, FunctionBody<'a>>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// Valid modifiers: `export`, `default`, `async`
pub modifiers: Modifiers<'a>,
pub scope_id: Cell<Option<ScopeId>>,
}

Expand Down
18 changes: 3 additions & 15 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,19 +953,7 @@ impl<'a> AstBuilder<'a> {
params: Box<'a, FormalParameters<'a>>,
body: Option<Box<'a, FunctionBody<'a>>>,
) -> Box<'a, Function<'a>> {
self.function(
r#type,
span,
id,
false,
false,
None,
params,
body,
None,
None,
Modifiers::empty(),
)
self.function(r#type, span, id, false, false, false, None, params, body, None, None)
}

#[inline]
Expand All @@ -976,25 +964,25 @@ impl<'a> AstBuilder<'a> {
id: Option<BindingIdentifier<'a>>,
generator: bool,
r#async: bool,
declare: bool,
this_param: Option<TSThisParameter<'a>>,
params: Box<'a, FormalParameters<'a>>,
body: Option<Box<'a, FunctionBody<'a>>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
modifiers: Modifiers<'a>,
) -> Box<'a, Function<'a>> {
self.alloc(Function::new(
r#type,
span,
id,
generator,
r#async,
declare,
this_param,
params,
body,
type_parameters,
return_type,
modifiers,
))
}

Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ impl<'a> Declaration<'a> {
pub fn declare(&self) -> bool {
match self {
Declaration::VariableDeclaration(decl) => decl.declare,
Declaration::FunctionDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::FunctionDeclaration(decl) => decl.declare,
Declaration::ClassDeclaration(decl) => decl.declare,
Declaration::TSEnumDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::TSTypeAliasDeclaration(decl) => decl.modifiers.is_contains_declare(),
Expand Down Expand Up @@ -946,25 +946,25 @@ impl<'a> Function<'a> {
id: Option<BindingIdentifier<'a>>,
generator: bool,
r#async: bool,
declare: bool,
this_param: Option<TSThisParameter<'a>>,
params: Box<'a, FormalParameters<'a>>,
body: Option<Box<'a, FunctionBody<'a>>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
modifiers: Modifiers<'a>,
) -> Self {
Self {
r#type,
span,
id,
generator,
r#async,
declare,
this_param,
params,
body,
type_parameters,
return_type,
modifiers,
scope_id: Cell::default(),
}
}
Expand All @@ -974,7 +974,7 @@ impl<'a> Function<'a> {
self.r#type,
FunctionType::TSDeclareFunction | FunctionType::TSEmptyBodyFunctionExpression
) || self.body.is_none()
|| self.modifiers.contains(ModifierKind::Declare)
|| self.declare
}

pub fn is_expression(&self) -> bool {
Expand Down Expand Up @@ -1004,12 +1004,12 @@ impl<'a> Hash for Function<'a> {
self.id.hash(state);
self.generator.hash(state);
self.r#async.hash(state);
self.declare.hash(state);
self.this_param.hash(state);
self.params.hash(state);
self.body.hash(state);
self.type_parameters.hash(state);
self.return_type.hash(state);
self.modifiers.hash(state);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for Function<'a> {
let n = p.code_len();
let wrap = self.is_expression() && (p.start_of_stmt == n || p.start_of_default_export == n);
p.wrap(wrap, |p| {
if self.modifiers.contains(ModifierKind::Declare) {
if self.declare {
p.print_str(b"declare ");
}
if self.r#async {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&function.id),
function.generator,
function.r#async,
false,
self.ast.copy(&function.this_param),
params,
None,
self.ast.copy(&function.type_parameters),
return_type,
Modifiers::empty(),
);

self.ast.class_method(
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'a> IsolatedDeclarations<'a> {
func: &Function<'a>,
modifiers: Option<Modifiers<'a>>,
) -> Option<Box<'a, Function<'a>>> {
if func.modifiers.is_contains_declare() {
if func.declare {
None
} else {
let return_type = self.infer_function_return_type(func);
Expand All @@ -32,12 +32,12 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&func.id),
false,
false,
modifiers.unwrap_or_else(|| self.modifiers_declare()).is_contains_declare(),
self.ast.copy(&func.this_param),
params,
None,
self.ast.copy(&func.type_parameters),
return_type,
modifiers.unwrap_or_else(|| self.modifiers_declare()),
))
}
}
Expand Down
21 changes: 15 additions & 6 deletions crates/oxc_parser/src/js/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a> ParserImpl<'a> {
r#async: bool,
generator: bool,
func_kind: FunctionKind,
modifiers: Modifiers<'a>,
modifiers: &Modifiers<'a>,
) -> Result<Box<'a, Function<'a>>> {
let ctx = self.ctx;
self.ctx = self.ctx.and_in(true).and_await(r#async).and_yield(generator);
Expand Down Expand Up @@ -102,18 +102,27 @@ impl<'a> ParserImpl<'a> {
self.asi()?;
}

for modifier in modifiers.iter() {
if !matches!(modifier.kind, ModifierKind::Declare | ModifierKind::Async) {
self.error(diagnostics::modifiers_cannot_appear(
modifier.span,
modifier.kind.as_str(),
));
}
}

Ok(self.ast.function(
function_type,
self.end_span(span),
id,
generator,
r#async,
modifiers.is_contains_declare(),
this_param,
params,
body,
type_parameters,
return_type,
modifiers,
))
}

Expand Down Expand Up @@ -152,7 +161,7 @@ impl<'a> ParserImpl<'a> {
self.expect(Kind::Function)?;
let generator = self.eat(Kind::Star);
let id = self.parse_function_id(func_kind, r#async, generator)?;
self.parse_function(span, id, r#async, generator, func_kind, Modifiers::empty())
self.parse_function(span, id, r#async, generator, func_kind, &Modifiers::empty())
}

/// Parse function implementation in Typescript, cursor
Expand All @@ -161,7 +170,7 @@ impl<'a> ParserImpl<'a> {
&mut self,
start_span: Span,
func_kind: FunctionKind,
modifiers: Modifiers<'a>,
modifiers: &Modifiers<'a>,
) -> Result<Box<'a, Function<'a>>> {
let r#async = modifiers.contains(ModifierKind::Async);
self.expect(Kind::Function)?;
Expand All @@ -182,7 +191,7 @@ impl<'a> ParserImpl<'a> {
let generator = self.eat(Kind::Star);
let id = self.parse_function_id(func_kind, r#async, generator)?;
let function =
self.parse_function(span, id, r#async, generator, func_kind, Modifiers::empty())?;
self.parse_function(span, id, r#async, generator, func_kind, &Modifiers::empty())?;

Ok(self.ast.function_expression(function))
}
Expand All @@ -207,7 +216,7 @@ impl<'a> ParserImpl<'a> {
r#async,
generator,
FunctionKind::Expression,
Modifiers::empty(),
&Modifiers::empty(),
)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ impl<'a> ParserImpl<'a> {
_ if self.at_function_with_async() => {
let declare = modifiers.contains(ModifierKind::Declare);
if declare {
self.parse_ts_declare_function(start_span, modifiers)
self.parse_ts_declare_function(start_span, &modifiers)
.map(Declaration::FunctionDeclaration)
} else if self.ts_enabled() {
self.parse_ts_function_impl(start_span, FunctionKind::Declaration, modifiers)
self.parse_ts_function_impl(start_span, FunctionKind::Declaration, &modifiers)
.map(Declaration::FunctionDeclaration)
} else {
self.parse_function_impl(FunctionKind::Declaration)
Expand All @@ -327,7 +327,7 @@ impl<'a> ParserImpl<'a> {
pub(crate) fn parse_ts_declare_function(
&mut self,
start_span: Span,
modifiers: Modifiers<'a>,
modifiers: &Modifiers<'a>,
) -> Result<Box<'a, Function<'a>>> {
let r#async = modifiers.contains(ModifierKind::Async);
self.expect(Kind::Function)?;
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_semantic/src/checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub fn check<'a>(node: &AstNode<'a>, ctx: &SemanticBuilder<'a>) {
AstKind::RegExpLiteral(lit) => js::check_regexp_literal(lit, ctx),

AstKind::Directive(dir) => js::check_directive(dir, ctx),
AstKind::Function(func) => ts::check_function(func, node, ctx),
AstKind::ModuleDeclaration(decl) => {
js::check_module_declaration(decl, node, ctx);
}
Expand Down
4 changes: 0 additions & 4 deletions crates/oxc_semantic/src/checker/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ fn check_declaration_modifiers<'a>(
}
}

pub fn check_function<'a>(function: &Function<'a>, node: &AstNode<'a>, ctx: &SemanticBuilder<'a>) {
check_declaration_modifiers(&function.modifiers, node, ctx);
}

pub fn check_ts_type_alias_declaration<'a>(
decl: &TSTypeAliasDeclaration<'a>,
node: &AstNode<'a>,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ impl<'a> ArrowFunctions<'a> {
id: None,
generator: false,
r#async: arrow_function_expr.r#async,
declare: false,
this_param: None,
params: self.ctx.ast.copy(&arrow_function_expr.params),
body: Some(body),
type_parameters: self.ctx.ast.copy(&arrow_function_expr.type_parameters),
return_type: self.ctx.ast.copy(&arrow_function_expr.return_type),
modifiers: Modifiers::empty(),
scope_id: Cell::new(scope_id),
};

Expand Down
Loading

0 comments on commit 0673677

Please sign in to comment.