diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2021-04-09 17:42:06 +0200 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2021-04-13 19:19:56 +0200 |
commit | 5f7ecce23321f499b1b002c32a27c63815535baa (patch) | |
tree | 4e1e23e18cacd4e0d9883fc0f581a77cd3df56e6 /tools | |
parent | 9b0069d94a5b725923e303ccdb3d7739088e06fc (diff) |
Implement optional chaining
This change implements optional chaining (https://github.com/tc39/proposal-optional-chaining) by adding a new type of optional lookup with an offset to the end of a chain.
If `undefined` or `null` is encountered during an access marked as optional, we jump to that end offset.
Features:
- Full support for all kinds of optional chain
- With some codegen overhead but zero overhead during normal non-optional FieldMemberExpression resolution
- Properly retains this contexts and does not need to resolve anything twice (this has been an issue previously)
- No extra AST structures, just flags for existing ones
[ChangeLog][QtQml] Added support for optional chaining (https://github.com/tc39/proposal-optional-chaining)
Fixes: QTBUG-77926
Change-Id: I9a41cdc4ca272066c79c72b9b22206498a546843
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index 04967ed0af..baf6651b20 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -405,14 +405,14 @@ QString DumpAstVisitor::parseExpression(ExpressionNode *expression) if (fieldMemberExpr->base->kind == Node::Kind_NumericLiteral) result = "(" + result + ")"; - result += "." + fieldMemberExpr->name.toString(); + result += (fieldMemberExpr->isOptional ? "?." : ".") + fieldMemberExpr->name.toString(); return result; } case Node::Kind_ArrayMemberExpression: { auto *arrayMemberExpr = cast<ArrayMemberExpression *>(expression); return parseExpression(arrayMemberExpr->base) - + "[" + parseExpression(arrayMemberExpr->expression) + "]"; + + (arrayMemberExpr->isOptional ? "?." : "") + "[" + parseExpression(arrayMemberExpr->expression) + "]"; } case Node::Kind_NestedExpression: return "("+parseExpression(cast<NestedExpression *>(expression)->expression)+")"; @@ -458,7 +458,7 @@ QString DumpAstVisitor::parseExpression(ExpressionNode *expression) case Node::Kind_CallExpression: { auto *callExpr = cast<CallExpression *>(expression); - return parseExpression(callExpr->base) + "(" + parseArgumentList(callExpr->arguments) + ")"; + return parseExpression(callExpr->base) + (callExpr->isOptional ? "?." : "") + "(" + parseArgumentList(callExpr->arguments) + ")"; } case Node::Kind_NewExpression: return "new "+parseExpression(cast<NewExpression *>(expression)->expression); |