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

refactor(ast): introduce ThisParameter #1728

Merged
merged 8 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 1 addition & 4 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ pub struct Function<'a> {
pub expression: bool,
pub generator: bool,
pub r#async: bool,
pub this_param: Option<TSThisParameter<'a>>,
Boshen marked this conversation as resolved.
Show resolved Hide resolved
pub params: Box<'a, FormalParameters<'a>>,
pub body: Option<Box<'a, FunctionBody<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
Expand Down Expand Up @@ -1590,10 +1591,6 @@ impl<'a> FormalParameters<'a> {
pub fn parameters_count(&self) -> usize {
self.items.len() + self.rest.as_ref().map_or(0, |_| 1)
}

pub fn this_parameter(&self) -> Option<&FormalParameter<'a>> {
self.items.first().filter(|item| matches!(&item.pattern.kind, BindingPatternKind::BindingIdentifier(ident) if ident.name == "this"))
}
}

#[derive(Debug, Hash)]
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ use serde::Serialize;
#[allow(clippy::wildcard_imports)]
use crate::ast::*;

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct TSThisParameter<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
}

/// Enum Declaration
///
/// `const_opt` enum `BindingIdentifier` { `EnumBody_opt` }
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,14 @@ impl<'a> AstBuilder<'a> {
FormalParameter { span, pattern, accessibility, readonly, decorators }
}

pub fn ts_this_parameter(
&self,
span: Span,
type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
) -> TSThisParameter<'a> {
TSThisParameter { span, type_annotation }
}

pub fn function(
&self,
r#type: FunctionType,
Expand All @@ -750,6 +758,7 @@ impl<'a> AstBuilder<'a> {
expression: bool,
generator: bool,
r#async: bool,
this_param: Option<TSThisParameter<'a>>,
params: Box<'a, FormalParameters<'a>>,
body: Option<Box<'a, FunctionBody<'a>>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
Expand All @@ -763,6 +772,7 @@ impl<'a> AstBuilder<'a> {
expression,
generator,
r#async,
this_param,
params,
body,
type_parameters,
Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,11 @@ impl<'a> Parser<'a> {

let value = self.parse_method(r#async, generator)?;

if kind == MethodDefinitionKind::Get
&& value.params.parameters_count() != value.params.this_parameter().map_or(0, |_| 1)
{
if kind == MethodDefinitionKind::Get && value.params.parameters_count() != 0 {
self.error(diagnostics::GetterParameters(value.params.span));
}

if kind == MethodDefinitionKind::Set
&& value.params.parameters_count() != value.params.this_parameter().map_or(1, |_| 2)
{
if kind == MethodDefinitionKind::Set && value.params.parameters_count() != 1 {
self.error(diagnostics::SetterParameters(value.params.span));
}

Expand Down
16 changes: 15 additions & 1 deletion crates/oxc_parser/src/js/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ impl<'a> Parser<'a> {
Ok(self.ast.formal_parameters(self.end_span(span), params_kind, list.elements, list.rest))
}

pub(crate) fn parse_formal_parameters_with_ts_this(
&mut self,
params_kind: FormalParameterKind,
) -> Result<(Option<TSThisParameter<'a>>, Box<'a, FormalParameters<'a>>)> {
let span = self.start_span();
Boshen marked this conversation as resolved.
Show resolved Hide resolved
let list: FormalParameterList<'_> = FormalParameterList::parse(self)?;
let formal_parameters =
self.ast.formal_parameters(self.end_span(span), params_kind, list.elements, list.rest);
let this_param = list.this_param;
Ok((this_param, formal_parameters))
}

pub(crate) fn parse_function(
&mut self,
span: Span,
Expand All @@ -88,7 +100,8 @@ impl<'a> Parser<'a> {

let type_parameters = self.parse_ts_type_parameters()?;

let params = self.parse_formal_parameters(FormalParameterKind::FormalParameter)?;
let (this_param, params) =
self.parse_formal_parameters_with_ts_this(FormalParameterKind::FormalParameter)?;

let return_type = self.parse_ts_return_type_annotation()?;

Expand Down Expand Up @@ -124,6 +137,7 @@ impl<'a> Parser<'a> {
false, // expression
generator,
r#async,
this_param,
params,
body,
type_parameters,
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_parser/src/js/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,12 @@ impl<'a> SeparatedList<'a> for SequenceExpressionList<'a> {
pub struct FormalParameterList<'a> {
pub elements: Vec<'a, FormalParameter<'a>>,
pub rest: Option<oxc_allocator::Box<'a, RestElement<'a>>>,
pub this_param: Option<TSThisParameter<'a>>,
}

impl<'a> SeparatedList<'a> for FormalParameterList<'a> {
fn new(p: &Parser<'a>) -> Self {
Self { elements: p.ast.new_vec(), rest: None }
Self { elements: p.ast.new_vec(), rest: None, this_param: None }
}

fn open(&self) -> Kind {
Expand All @@ -260,8 +261,8 @@ impl<'a> SeparatedList<'a> for FormalParameterList<'a> {

match p.cur_kind() {
Kind::This if p.ts_enabled() => {
let formal_parameter = p.parse_ts_this_parameter()?;
self.elements.push(formal_parameter);
let this_parameter = p.parse_ts_this_parameter()?;
self.this_param.replace(this_parameter);
}
Kind::Dot3 => {
let rest = p.parse_rest_element()?;
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/js/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl<'a> Parser<'a> {
self.expect(Kind::Get)?;
let (key, computed) = self.parse_property_name()?;
let method = self.parse_method(false, false)?;
if method.params.parameters_count() != method.params.this_parameter().map_or(0, |_| 1) {
if method.params.parameters_count() != 0 {
self.error(diagnostics::GetterParameters(method.params.span));
}
let value = self.ast.function_expression(method);
Expand All @@ -241,7 +241,7 @@ impl<'a> Parser<'a> {
let (key, computed) = self.parse_property_name()?;
let method = self.parse_method(false, false)?;

if method.params.parameters_count() != method.params.this_parameter().map_or(1, |_| 2) {
if method.params.parameters_count() != 1 {
self.error(diagnostics::SetterParameters(method.params.span));
}

Expand Down
14 changes: 3 additions & 11 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,11 @@ impl<'a> Parser<'a> {
))
}

pub(crate) fn parse_ts_this_parameter(&mut self) -> Result<FormalParameter<'a>> {
pub(crate) fn parse_ts_this_parameter(&mut self) -> Result<TSThisParameter<'a>> {
let span = self.start_span();
let (ident_span, name) = self.parse_identifier_kind(Kind::This);
let _ = self.parse_identifier_kind(Kind::This);
Boshen marked this conversation as resolved.
Show resolved Hide resolved
let type_annotation = self.parse_ts_type_annotation()?;
let kind = self.ast.binding_pattern_identifier(BindingIdentifier::new(ident_span, name));
let binding = self.ast.binding_pattern(kind, type_annotation, /* optional */ false);
Ok(self.ast.formal_parameter(
self.end_span(span),
binding,
/* accessibility */ None,
/* readonly */ false,
/* decorators */ self.ast.new_vec(),
))
Ok(self.ast.ts_this_parameter(self.end_span(span), type_annotation))
}

pub(crate) fn eat_decorators(&mut self) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl<'a> ArrowFunctions<'a> {
false,
arrow_expr.generator,
arrow_expr.r#async,
None,
self.ast.copy(&arrow_expr.params),
Some(body),
self.ast.copy(&arrow_expr.type_parameters),
Expand Down
12 changes: 0 additions & 12 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,6 @@ impl<'a> VisitMut<'a> for Transformer<'a> {
});
}

fn visit_formal_parameters(&mut self, params: &mut FormalParameters<'a>) {
self.typescript.as_mut().map(|t| t.transform_formal_parameters(params));

for param in params.items.iter_mut() {
self.visit_formal_parameter(param);
}

if let Some(rest) = &mut params.rest {
self.visit_rest_element(rest);
}
}

fn visit_variable_declarator(&mut self, declarator: &mut VariableDeclarator<'a>) {
self.es2015_function_name.as_mut().map(|t| t.transform_variable_declarator(declarator));

Expand Down
7 changes: 0 additions & 7 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ impl<'a> TypeScript<'a> {
Self { ast, ctx, verbatim_module_syntax, export_name_set: FxHashSet::default() }
}

#[allow(clippy::unused_self)]
pub fn transform_formal_parameters(&self, params: &mut FormalParameters<'a>) {
if params.items.get(0).is_some_and(|param| matches!(&param.pattern.kind, BindingPatternKind::BindingIdentifier(ident) if ident.name =="this")) {
params.items.remove(0);
}
}

/// ```TypeScript
/// enum Foo {
/// X
Expand Down