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

[Merged by Bors] - Replace contains and friends with visitors #2403

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 0 additions & 37 deletions boa_ast/src/declaration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use super::{
expression::Identifier,
function::{AsyncFunction, AsyncGenerator, Class, Function, Generator},
ContainsSymbol,
};
use boa_interner::{Interner, ToIndentedString, ToInternedString};
use core::ops::ControlFlow;
Expand Down Expand Up @@ -116,42 +115,6 @@ impl Declaration {
}
}
}

/// Returns true if the node contains a identifier reference named 'arguments'.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-static-semantics-containsarguments
// TODO: replace with a visitor
pub(crate) fn contains_arguments(&self) -> bool {
match self {
Self::Function(_)
| Self::Generator(_)
| Self::AsyncGenerator(_)
| Self::AsyncFunction(_) => false,
Self::Lexical(decl) => decl.contains_arguments(),
Self::Class(class) => class.contains_arguments(),
}
}

/// Returns `true` if the node contains the given token.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-static-semantics-contains
// TODO: replace with a visitor
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
match self {
Self::Function(_)
| Self::Generator(_)
| Self::AsyncGenerator(_)
| Self::AsyncFunction(_) => false,
Self::Class(class) => class.contains(symbol),
Self::Lexical(decl) => decl.contains(symbol),
}
}
}

impl ToIndentedString for Declaration {
Expand Down
70 changes: 1 addition & 69 deletions boa_ast/src/declaration/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
expression::{Expression, Identifier},
join_nodes,
pattern::Pattern,
ContainsSymbol, Statement,
Statement,
};
use boa_interner::{Interner, ToInternedString};

Expand Down Expand Up @@ -48,17 +48,6 @@ use super::Declaration;
#[derive(Clone, Debug, PartialEq)]
pub struct VarDeclaration(pub VariableList);

impl VarDeclaration {
#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.0.as_ref().iter().any(Variable::contains_arguments)
}
#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.0.as_ref().iter().any(|decl| decl.contains(symbol))
}
}

impl From<VarDeclaration> for Statement {
fn from(var: VarDeclaration) -> Self {
Statement::Var(var)
Expand Down Expand Up @@ -124,22 +113,6 @@ impl LexicalDeclaration {
LexicalDeclaration::Const(list) | LexicalDeclaration::Let(list) => list,
}
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.variable_list()
.as_ref()
.iter()
.any(Variable::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.variable_list()
.as_ref()
.iter()
.any(|decl| decl.contains(symbol))
}
}

impl From<LexicalDeclaration> for Declaration {
Expand Down Expand Up @@ -332,33 +305,6 @@ impl Variable {
pub fn idents(&self) -> Vec<Identifier> {
self.binding.idents()
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
if let Some(ref node) = self.init {
if node.contains_arguments() {
return true;
}
}
self.binding.contains_arguments()
}

/// Returns `true` if the variable declaration contains the given token.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-static-semantics-contains
#[inline]
#[must_use]
pub fn contains(&self, symbol: ContainsSymbol) -> bool {
if let Some(ref node) = self.init {
if node.contains(symbol) {
return true;
}
}
self.binding.contains(symbol)
}
}

impl VisitWith for Variable {
Expand Down Expand Up @@ -413,20 +359,6 @@ impl From<Pattern> for Binding {
}

impl Binding {
pub(crate) fn contains_arguments(&self) -> bool {
matches!(self, Binding::Pattern(ref pattern) if pattern.contains_arguments())
}

/// Returns `true` if the node contains the given token.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-static-semantics-contains
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
matches!(self, Binding::Pattern(ref pattern) if pattern.contains(symbol))
}

/// Gets the list of declared identifiers.
pub(crate) fn idents(&self) -> Vec<Identifier> {
match self {
Expand Down
68 changes: 1 addition & 67 deletions boa_ast/src/expression/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
//! [spec]: https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-property-accessors
//! [access]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

use crate::expression::Expression;
use crate::try_break;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use crate::{expression::Expression, ContainsSymbol};
use boa_interner::{Interner, Sym, ToInternedString};
use core::ops::ControlFlow;

Expand All @@ -32,22 +32,6 @@ pub enum PropertyAccessField {
Expr(Box<Expression>),
}

impl PropertyAccessField {
pub(crate) fn contains_arguments(&self) -> bool {
match self {
PropertyAccessField::Const(_) => false,
PropertyAccessField::Expr(expr) => expr.contains_arguments(),
}
}
#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
match self {
PropertyAccessField::Const(_) => false,
PropertyAccessField::Expr(expr) => expr.contains(symbol),
}
}
}

impl From<Sym> for PropertyAccessField {
#[inline]
fn from(id: Sym) -> Self {
Expand Down Expand Up @@ -98,26 +82,6 @@ pub enum PropertyAccess {
Super(SuperPropertyAccess),
}

impl PropertyAccess {
#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
match self {
PropertyAccess::Simple(s) => s.contains_arguments(),
PropertyAccess::Private(p) => p.contains_arguments(),
PropertyAccess::Super(s) => s.contains_arguments(),
}
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
match self {
PropertyAccess::Simple(s) => s.contains(symbol),
PropertyAccess::Private(p) => p.contains(symbol),
PropertyAccess::Super(s) => s.contains(symbol),
}
}
}

impl ToInternedString for PropertyAccess {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
Expand Down Expand Up @@ -194,16 +158,6 @@ impl SimplePropertyAccess {
field: field.into(),
}
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.target.contains_arguments() || self.field.contains_arguments()
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.target.contains(symbol) || self.field.contains(symbol)
}
}

impl ToInternedString for SimplePropertyAccess {
Expand Down Expand Up @@ -285,16 +239,6 @@ impl PrivatePropertyAccess {
pub fn field(&self) -> Sym {
self.field
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.target.contains_arguments()
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.target.contains(symbol)
}
}

impl ToInternedString for PrivatePropertyAccess {
Expand Down Expand Up @@ -359,16 +303,6 @@ impl SuperPropertyAccess {
pub fn field(&self) -> &PropertyAccessField {
&self.field
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.field.contains_arguments()
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
symbol == ContainsSymbol::SuperProperty || self.field.contains(symbol)
}
}

impl ToInternedString for SuperPropertyAccess {
Expand Down
11 changes: 0 additions & 11 deletions boa_ast/src/expression/await.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Await expression Expression.

use crate::ContainsSymbol;
use core::ops::ControlFlow;

use super::Expression;
Expand Down Expand Up @@ -29,16 +28,6 @@ impl Await {
pub fn target(&self) -> &Expression {
&self.target
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.target.contains_arguments()
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.target.contains(symbol)
}
}

impl<T> From<T> for Await
Expand Down
22 changes: 1 addition & 21 deletions boa_ast/src/expression/call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::join_nodes;
use crate::try_break;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use crate::{join_nodes, ContainsSymbol};
use boa_interner::{Interner, ToInternedString};
use core::ops::ControlFlow;

Expand Down Expand Up @@ -51,16 +51,6 @@ impl Call {
pub fn args(&self) -> &[Expression] {
&self.args
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.function.contains_arguments() || self.args.iter().any(Expression::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.function.contains(symbol) || self.args.iter().any(|expr| expr.contains(symbol))
}
}

impl ToInternedString for Call {
Expand Down Expand Up @@ -133,16 +123,6 @@ impl SuperCall {
pub fn arguments(&self) -> &[Expression] {
&self.args
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.args.iter().any(Expression::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.args.iter().any(|expr| expr.contains(symbol))
}
}

impl ToInternedString for SuperCall {
Expand Down
15 changes: 1 addition & 14 deletions boa_ast/src/expression/literal/array.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Array declaration Expression.

use crate::expression::operator::assign::AssignTarget;
use crate::expression::Expression;
use crate::pattern::{ArrayPattern, ArrayPatternElement, Pattern};
use crate::try_break;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use crate::{expression::Expression, ContainsSymbol};
use boa_interner::{Interner, Sym, ToInternedString};
use core::ops::ControlFlow;

Expand Down Expand Up @@ -151,19 +151,6 @@ impl ArrayLiteral {
}
Some(ArrayPattern::new(bindings.into()))
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.arr
.iter()
.flatten()
.any(Expression::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.arr.iter().flatten().any(|expr| expr.contains(symbol))
}
}

impl AsRef<[Option<Expression>]> for ArrayLiteral {
Expand Down
13 changes: 0 additions & 13 deletions boa_ast/src/expression/literal/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
property::{MethodDefinition, PropertyDefinition, PropertyName},
try_break,
visitor::{VisitWith, Visitor, VisitorMut},
ContainsSymbol,
};
use boa_interner::{Interner, Sym, ToIndentedString, ToInternedString};
use core::ops::ControlFlow;
Expand Down Expand Up @@ -205,18 +204,6 @@ impl ObjectLiteral {

Some(ObjectPattern::new(bindings.into()))
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.properties
.iter()
.any(PropertyDefinition::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.properties.iter().any(|prop| prop.contains(symbol))
}
}

impl ToIndentedString for ObjectLiteral {
Expand Down
Loading