1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:27:45 +00:00

LibJS: Add for_each_bound_identifier for AST::Declaration

The same as for_each_bound_name but for identifiers.
This commit is contained in:
Aliaksandr Kalenik 2023-07-02 01:04:07 +03:00 committed by Andreas Kling
parent a6cdb1655b
commit 7f0074ac23
2 changed files with 78 additions and 1 deletions

View file

@ -2308,6 +2308,14 @@ ThrowCompletionOr<void> ClassDeclaration::for_each_bound_name(ThrowCompletionOrV
return callback(m_class_expression->name());
}
ThrowCompletionOr<void> ClassDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const
{
if (!m_class_expression->m_name)
return {};
return callback(*m_class_expression->m_name);
}
void ClassExpression::dump(int indent) const
{
print_indent(indent);
@ -2447,6 +2455,23 @@ ThrowCompletionOr<void> BindingPattern::for_each_bound_name(ThrowCompletionOrVoi
return {};
}
ThrowCompletionOr<void> BindingPattern::for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const
{
for (auto const& entry : entries) {
auto const& alias = entry.alias;
if (alias.has<NonnullRefPtr<Identifier const>>()) {
TRY(callback(alias.get<NonnullRefPtr<Identifier const>>()));
} else if (alias.has<NonnullRefPtr<BindingPattern const>>()) {
TRY(alias.get<NonnullRefPtr<BindingPattern const>>()->for_each_bound_identifier(forward<decltype(callback)>(callback)));
} else {
auto const& name = entry.name;
if (name.has<NonnullRefPtr<Identifier const>>())
TRY(callback(name.get<NonnullRefPtr<Identifier const>>()));
}
}
return {};
}
void BindingPattern::dump(int indent) const
{
print_indent(indent);
@ -2541,6 +2566,13 @@ ThrowCompletionOr<void> FunctionDeclaration::for_each_bound_name(ThrowCompletion
return callback(name());
}
ThrowCompletionOr<void> FunctionDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const
{
if (!m_name)
return {};
return callback(*m_name);
}
void FunctionExpression::dump(int indent) const
{
FunctionNode::dump(indent, class_name());
@ -3099,6 +3131,23 @@ ThrowCompletionOr<void> VariableDeclaration::for_each_bound_name(ThrowCompletion
return {};
}
ThrowCompletionOr<void> VariableDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const
{
for (auto const& entry : declarations()) {
TRY(entry->target().visit(
[&](NonnullRefPtr<Identifier const> const& id) {
return callback(id);
},
[&](NonnullRefPtr<BindingPattern const> const& binding) {
return binding->for_each_bound_identifier([&](auto const& id) {
return callback(id);
});
}));
}
return {};
}
void VariableDeclaration::dump(int indent) const
{
char const* declaration_kind_string = nullptr;
@ -3156,6 +3205,16 @@ ThrowCompletionOr<void> UsingDeclaration::for_each_bound_name(ThrowCompletionOrV
return {};
}
ThrowCompletionOr<void> UsingDeclaration::for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const
{
for (auto const& entry : m_declarations) {
VERIFY(entry->target().has<NonnullRefPtr<Identifier const>>());
TRY(callback(entry->target().get<NonnullRefPtr<Identifier const>>()));
}
return {};
}
void UsingDeclaration::dump(int indent) const
{
ASTNode::dump(indent);

View file

@ -609,6 +609,7 @@ public:
}
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const = 0;
virtual ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const = 0;
// 8.1.3 Static Semantics: IsConstantDeclaration, https://tc39.es/ecma262/#sec-static-semantics-isconstantdeclaration
virtual bool is_constant_declaration() const { return false; }
@ -628,6 +629,11 @@ public:
{
VERIFY_NOT_REACHED();
}
ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override
{
VERIFY_NOT_REACHED();
}
};
struct BindingPattern : RefCounted<BindingPattern> {
@ -650,6 +656,7 @@ struct BindingPattern : RefCounted<BindingPattern> {
void dump(int indent) const;
ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const;
bool contains_expression() const;
@ -729,8 +736,9 @@ protected:
void dump(int indent, DeprecatedString const& class_name) const;
private:
RefPtr<Identifier const> m_name { nullptr };
private:
DeprecatedString m_source_text;
NonnullRefPtr<Statement const> m_body;
Vector<FunctionParameter> const m_parameters;
@ -762,6 +770,8 @@ public:
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
virtual bool is_function_declaration() const override { return true; }
void set_should_do_additional_annexB_steps() { m_is_hoisted = true; }
@ -1458,6 +1468,8 @@ public:
private:
virtual bool is_class_expression() const override { return true; }
friend ClassDeclaration;
RefPtr<Identifier const> m_name;
DeprecatedString m_source_text;
RefPtr<FunctionExpression const> m_constructor;
@ -1479,6 +1491,8 @@ public:
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
virtual bool is_lexical_declaration() const override { return true; }
StringView name() const { return m_class_expression->name(); }
@ -1785,6 +1799,8 @@ public:
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
virtual bool is_constant_declaration() const override { return m_declaration_kind == DeclarationKind::Const; };
virtual bool is_lexical_declaration() const override { return m_declaration_kind != DeclarationKind::Var; }
@ -1809,6 +1825,8 @@ public:
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
virtual bool is_constant_declaration() const override { return true; };
virtual bool is_lexical_declaration() const override { return true; }