mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 03:08:13 +00:00
Only apply auto-naming of function expressions based on syntax
The auto naming of function expressions is a purely syntactic decision, so shouldn't be decided based on the dynamic type of an assignment. This moves the decision making into the parser. One icky hack is that we add a field to FunctionExpression to indicate whether we can autoname. The real solution is to actually generate a CompoundExpression node so that the parser can make the correct decision, however this would have a potentially significant run time cost. This does not correct the behaviour for class expressions. Patch from Anonymous.
This commit is contained in:
parent
7241ff3967
commit
0255c8d976
4 changed files with 55 additions and 9 deletions
|
@ -234,6 +234,13 @@ protected:
|
|||
|
||||
const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
|
||||
|
||||
protected:
|
||||
void set_name(FlyString name)
|
||||
{
|
||||
VERIFY(m_name.is_empty());
|
||||
m_name = move(name);
|
||||
}
|
||||
|
||||
private:
|
||||
FlyString m_name;
|
||||
NonnullRefPtr<Statement> m_body;
|
||||
|
@ -266,7 +273,7 @@ public:
|
|||
static bool must_have_name() { return false; }
|
||||
|
||||
FunctionExpression(SourceRange source_range, const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, bool is_strict_mode, bool is_arrow_function = false)
|
||||
: Expression(move(source_range))
|
||||
: Expression(source_range)
|
||||
, FunctionNode(name, move(body), move(parameters), function_length, move(variables), is_strict_mode)
|
||||
, m_is_arrow_function(is_arrow_function)
|
||||
{
|
||||
|
@ -275,8 +282,20 @@ public:
|
|||
virtual Value execute(Interpreter&, GlobalObject&) const override;
|
||||
virtual void dump(int indent) const override;
|
||||
|
||||
void set_name_if_possible(FlyString new_name)
|
||||
{
|
||||
if (m_cannot_auto_rename)
|
||||
return;
|
||||
m_cannot_auto_rename = true;
|
||||
if (name().is_empty())
|
||||
set_name(move(new_name));
|
||||
}
|
||||
bool cannot_auto_rename() const { return m_cannot_auto_rename; }
|
||||
void set_cannot_auto_rename() { m_cannot_auto_rename = true; }
|
||||
|
||||
private:
|
||||
bool m_is_arrow_function;
|
||||
bool m_cannot_auto_rename { false };
|
||||
bool m_is_arrow_function { false };
|
||||
};
|
||||
|
||||
class ErrorExpression final : public Expression {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue