diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-09-23 11:56:43 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-09-29 13:39:41 +0200 |
commit | b3fe31a0ff129bb87f81901f2f50c0041b8f4a80 (patch) | |
tree | 14c79359f968ab8918eb805e73ab693c5330a3ab /src/qml/compiler/qv4compilerscanfunctions_p.h | |
parent | 6fa72b1fb78267633812d56d1bcda76e327025c9 (diff) | |
download | qtdeclarative-b3fe31a0ff129bb87f81901f2f50c0041b8f4a80.tar.gz |
Fix scoping of JavaScript function names
Function declarations add their name to the outer scope, but not the
inner scope. Function expressions add their name to the inner scope,
unless the name is actually picked from the outer scope rather than
given after the function token.
We don't add the name to any scope in the case of functions declared in
QML elements because the QML element will receive the function as
appropriately named, and typed, property. It is always better to use
that one than to use a JavaScript local.
This causes some additional ecmascript tests to pass.
Pick-to: 6.2
Fixes: QTBUG-96625
Change-Id: I0b8ee98917d102a99fb6b9bd918037c71867a4a5
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/compiler/qv4compilerscanfunctions_p.h')
-rw-r--r-- | src/qml/compiler/qv4compilerscanfunctions_p.h | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/qml/compiler/qv4compilerscanfunctions_p.h b/src/qml/compiler/qv4compilerscanfunctions_p.h index 0336398cac..073ee5c2a4 100644 --- a/src/qml/compiler/qv4compilerscanfunctions_p.h +++ b/src/qml/compiler/qv4compilerscanfunctions_p.h @@ -89,9 +89,19 @@ public: void leaveEnvironment(); void enterQmlFunction(QQmlJS::AST::FunctionExpression *ast) - { enterFunction(ast, false); } + { enterFunction(ast, FunctionNameContext::None); } protected: + // Function declarations add their name to the outer scope, but not the + // inner scope. Function expressions add their name to the inner scope, + // unless the name is actually picked from the outer scope rather than + // given after the function token. QML functions don't add their name + // anywhere because the name is already recorded in the QML element. + // This enum is used to control the behavior of enterFunction(). + enum class FunctionNameContext { + None, Inner, Outer + }; + using Visitor::visit; using Visitor::endVisit; @@ -118,7 +128,8 @@ protected: bool visit(QQmlJS::AST::FieldMemberExpression *) override; bool visit(QQmlJS::AST::ArrayPattern *) override; - bool enterFunction(QQmlJS::AST::FunctionExpression *ast, bool enterName); + bool enterFunction(QQmlJS::AST::FunctionExpression *ast, + FunctionNameContext nameContext); void endVisit(QQmlJS::AST::FunctionExpression *) override; @@ -161,7 +172,7 @@ protected: protected: bool enterFunction(QQmlJS::AST::Node *ast, const QString &name, QQmlJS::AST::FormalParameterList *formals, - QQmlJS::AST::StatementList *body, bool enterName); + QQmlJS::AST::StatementList *body, FunctionNameContext nameContext); void calcEscapingVariables(); // fields: |