1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +00:00

LibJS: Allow the choice of a scope of declaration for a variable (#1408)

Previously, we were assuming all declared variables were bound to a
block scope, now, with the addition of declaration types, we can bind
a variable to a block scope using `let`, or a function scope (the scope
of the inner-most enclosing function of a `var` declaration) using
`var`.
This commit is contained in:
0xtechnobabble 2020-03-11 21:09:20 +02:00 committed by GitHub
parent 542108421e
commit df40c85f80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 16 deletions

View file

@ -332,10 +332,16 @@ private:
NonnullOwnPtr<Expression> m_rhs;
};
enum class DeclarationType {
Var,
Let,
};
class VariableDeclaration : public ASTNode {
public:
VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer)
: m_name(move(name))
VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer, DeclarationType declaration_type)
: m_declaration_type(declaration_type)
, m_name(move(name))
, m_initializer(move(initializer))
{
}
@ -348,6 +354,7 @@ public:
private:
virtual const char* class_name() const override { return "VariableDeclaration"; }
DeclarationType m_declaration_type;
NonnullOwnPtr<Identifier> m_name;
OwnPtr<Expression> m_initializer;
};