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

SourceText collection & toString() for fns and methods #4038

Merged
merged 18 commits into from
Feb 3, 2025
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
13 changes: 13 additions & 0 deletions core/ast/src/expression/literal/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
scope::FunctionScopes,
try_break,
visitor::{VisitWith, Visitor, VisitorMut},
LinearPosition, LinearSpan, LinearSpanIgnoreEq,
};
use boa_interner::{Interner, Sym, ToIndentedString, ToInternedString};
use core::ops::ControlFlow;
Expand Down Expand Up @@ -411,6 +412,7 @@ pub struct ObjectMethodDefinition {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl ObjectMethodDefinition {
Expand All @@ -422,16 +424,20 @@ impl ObjectMethodDefinition {
parameters: FormalParameterList,
body: FunctionBody,
kind: MethodDefinitionKind,
start_linear_pos: LinearPosition,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
|| contains(&body, ContainsSymbol::DirectEval);
let linear_span = LinearSpan::new(start_linear_pos, body.linear_pos_end()).into();

Self {
name,
parameters,
body,
contains_direct_eval,
kind,
scopes: FunctionScopes::default(),
linear_span,
}
}

Expand Down Expand Up @@ -469,6 +475,13 @@ impl ObjectMethodDefinition {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for ObjectMethodDefinition {
Expand Down
12 changes: 11 additions & 1 deletion core/ast/src/function/arrow_function.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::operations::{contains, ContainsSymbol};
use crate::scope::FunctionScopes;
use crate::try_break;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use crate::{
expression::{Expression, Identifier},
join_nodes,
};
use crate::{try_break, LinearSpan, LinearSpanIgnoreEq};
use boa_interner::{Interner, ToIndentedString};
use core::ops::ControlFlow;

Expand All @@ -31,6 +31,7 @@ pub struct ArrowFunction {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl ArrowFunction {
Expand All @@ -41,6 +42,7 @@ impl ArrowFunction {
name: Option<Identifier>,
parameters: FormalParameterList,
body: FunctionBody,
linear_span: LinearSpan,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
|| contains(&body, ContainsSymbol::DirectEval);
Expand All @@ -50,6 +52,7 @@ impl ArrowFunction {
body,
contains_direct_eval,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -86,6 +89,13 @@ impl ArrowFunction {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for ArrowFunction {
Expand Down
12 changes: 11 additions & 1 deletion core/ast/src/function/async_arrow_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::ops::ControlFlow;
use super::{FormalParameterList, FunctionBody};
use crate::operations::{contains, ContainsSymbol};
use crate::scope::FunctionScopes;
use crate::try_break;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use crate::{
expression::{Expression, Identifier},
join_nodes,
};
use crate::{try_break, LinearSpan, LinearSpanIgnoreEq};
use boa_interner::{Interner, ToIndentedString};

/// An async arrow function expression, as defined by the [spec].
Expand All @@ -31,6 +31,7 @@ pub struct AsyncArrowFunction {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl AsyncArrowFunction {
Expand All @@ -41,6 +42,7 @@ impl AsyncArrowFunction {
name: Option<Identifier>,
parameters: FormalParameterList,
body: FunctionBody,
linear_span: LinearSpan,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
|| contains(&body, ContainsSymbol::DirectEval);
Expand All @@ -50,6 +52,7 @@ impl AsyncArrowFunction {
body,
contains_direct_eval,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -86,6 +89,13 @@ impl AsyncArrowFunction {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for AsyncArrowFunction {
Expand Down
28 changes: 26 additions & 2 deletions core/ast/src/function/async_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
scope::{FunctionScopes, Scope},
try_break,
visitor::{VisitWith, Visitor, VisitorMut},
Declaration,
Declaration, LinearSpan, LinearSpanIgnoreEq,
};
use boa_interner::{Interner, ToIndentedString};
use core::ops::ControlFlow;
Expand All @@ -33,13 +33,19 @@ pub struct AsyncFunctionDeclaration {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl AsyncFunctionDeclaration {
/// Creates a new async function declaration.
#[inline]
#[must_use]
pub fn new(name: Identifier, parameters: FormalParameterList, body: FunctionBody) -> Self {
pub fn new(
name: Identifier,
parameters: FormalParameterList,
body: FunctionBody,
linear_span: LinearSpan,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
|| contains(&body, ContainsSymbol::DirectEval);
Self {
Expand All @@ -48,6 +54,7 @@ impl AsyncFunctionDeclaration {
body,
contains_direct_eval,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -78,6 +85,13 @@ impl AsyncFunctionDeclaration {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for AsyncFunctionDeclaration {
Expand Down Expand Up @@ -141,6 +155,7 @@ pub struct AsyncFunctionExpression {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl AsyncFunctionExpression {
Expand All @@ -151,6 +166,7 @@ impl AsyncFunctionExpression {
name: Option<Identifier>,
parameters: FormalParameterList,
body: FunctionBody,
linear_span: LinearSpan,
has_binding_identifier: bool,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
Expand All @@ -163,6 +179,7 @@ impl AsyncFunctionExpression {
name_scope: None,
contains_direct_eval,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -207,6 +224,13 @@ impl AsyncFunctionExpression {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for AsyncFunctionExpression {
Expand Down
28 changes: 26 additions & 2 deletions core/ast/src/function/async_generator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Async Generator Expression
use crate::operations::{contains, ContainsSymbol};
use crate::scope::{FunctionScopes, Scope};
use crate::try_break;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use crate::{
block_to_string,
expression::{Expression, Identifier},
join_nodes, Declaration,
};
use crate::{try_break, LinearSpan, LinearSpanIgnoreEq};
use boa_interner::{Interner, ToIndentedString};
use core::ops::ControlFlow;

Expand All @@ -32,13 +32,19 @@ pub struct AsyncGeneratorDeclaration {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl AsyncGeneratorDeclaration {
/// Creates a new async generator declaration.
#[inline]
#[must_use]
pub fn new(name: Identifier, parameters: FormalParameterList, body: FunctionBody) -> Self {
pub fn new(
name: Identifier,
parameters: FormalParameterList,
body: FunctionBody,
linear_span: LinearSpan,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
|| contains(&body, ContainsSymbol::DirectEval);
Self {
Expand All @@ -47,6 +53,7 @@ impl AsyncGeneratorDeclaration {
body,
contains_direct_eval,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -77,6 +84,13 @@ impl AsyncGeneratorDeclaration {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for AsyncGeneratorDeclaration {
Expand Down Expand Up @@ -140,6 +154,7 @@ pub struct AsyncGeneratorExpression {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl AsyncGeneratorExpression {
Expand All @@ -150,6 +165,7 @@ impl AsyncGeneratorExpression {
name: Option<Identifier>,
parameters: FormalParameterList,
body: FunctionBody,
linear_span: LinearSpan,
has_binding_identifier: bool,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
Expand All @@ -162,6 +178,7 @@ impl AsyncGeneratorExpression {
name_scope: None,
contains_direct_eval,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -206,6 +223,13 @@ impl AsyncGeneratorExpression {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for AsyncGeneratorExpression {
Expand Down
15 changes: 14 additions & 1 deletion core/ast/src/function/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
scope::{FunctionScopes, Scope},
try_break,
visitor::{VisitWith, Visitor, VisitorMut},
Declaration,
Declaration, LinearPosition, LinearSpan, LinearSpanIgnoreEq,
};
use boa_interner::{Interner, Sym, ToIndentedString, ToInternedString};
use core::ops::ControlFlow;
Expand Down Expand Up @@ -674,6 +674,7 @@ pub struct ClassMethodDefinition {

#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) scopes: FunctionScopes,
linear_span: LinearSpanIgnoreEq,
}

impl ClassMethodDefinition {
Expand All @@ -686,9 +687,13 @@ impl ClassMethodDefinition {
body: FunctionBody,
kind: MethodDefinitionKind,
is_static: bool,
start_linear_pos: LinearPosition,
) -> Self {
let contains_direct_eval = contains(&parameters, ContainsSymbol::DirectEval)
|| contains(&body, ContainsSymbol::DirectEval);

let linear_span = LinearSpan::new(start_linear_pos, body.linear_pos_end());

Self {
name,
parameters,
Expand All @@ -697,6 +702,7 @@ impl ClassMethodDefinition {
kind,
is_static,
scopes: FunctionScopes::default(),
linear_span: linear_span.into(),
}
}

Expand Down Expand Up @@ -748,6 +754,13 @@ impl ClassMethodDefinition {
pub const fn scopes(&self) -> &FunctionScopes {
&self.scopes
}

/// Gets linear span of the function declaration.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
}

impl ToIndentedString for ClassMethodDefinition {
Expand Down
Loading
Loading