From feca8417f610f8f64193292379eb9e0b9423936f Mon Sep 17 00:00:00 2001 From: Semih Yavuz Date: Thu, 4 May 2023 14:43:25 +0200 Subject: [PATCH] qmldom: Add more literals Dom construction fails before this commit if it encounters one of null expression, true and false literals. Add dom representation for them. Change-Id: I2dbb2ebfce83b32426eb5e159fe9e4f0f68c56c3 Reviewed-by: Sami Shalayel Reviewed-by: Fabian Kosmale --- src/qmldom/qqmldomastcreator.cpp | 33 +++++++++++++++++++ src/qmldom/qqmldomastcreator_p.h | 3 ++ src/qmldom/qqmldomscriptelements_p.h | 2 +- .../domdata/domitem/variableDeclarations.qml | 4 +++ tests/auto/qmldom/domitem/tst_qmldomitem.h | 33 +++++++++++++++++-- 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/qmldom/qqmldomastcreator.cpp b/src/qmldom/qqmldomastcreator.cpp index 0462c7c65c..ae6acbb366 100644 --- a/src/qmldom/qqmldomastcreator.cpp +++ b/src/qmldom/qqmldomastcreator.cpp @@ -1155,6 +1155,39 @@ bool QQmlDomAstCreator::visit(AST::StringLiteral *expression) return true; } +bool QQmlDomAstCreator::visit(AST::NullExpression *expression) +{ + if (!m_enableScriptExpressions) + return false; + + auto current = makeScriptElement(expression); + current->setLiteralValue(nullptr); + pushScriptElement(current); + return true; +} + +bool QQmlDomAstCreator::visit(AST::TrueLiteral *expression) +{ + if (!m_enableScriptExpressions) + return false; + + auto current = makeScriptElement(expression); + current->setLiteralValue(true); + pushScriptElement(current); + return true; +} + +bool QQmlDomAstCreator::visit(AST::FalseLiteral *expression) +{ + if (!m_enableScriptExpressions) + return false; + + auto current = makeScriptElement(expression); + current->setLiteralValue(false); + pushScriptElement(current); + return true; +} + bool QQmlDomAstCreator::visit(AST::VariableDeclarationList *) { if (!m_enableScriptExpressions) diff --git a/src/qmldom/qqmldomastcreator_p.h b/src/qmldom/qqmldomastcreator_p.h index a99294fd23..77fe2af823 100644 --- a/src/qmldom/qqmldomastcreator_p.h +++ b/src/qmldom/qqmldomastcreator_p.h @@ -321,6 +321,9 @@ public: bool visit(AST::IdentifierExpression *expression) override; bool visit(AST::NumericLiteral *expression) override; bool visit(AST::StringLiteral *expression) override; + bool visit(AST::NullExpression *expression) override; + bool visit(AST::TrueLiteral *expression) override; + bool visit(AST::FalseLiteral *expression) override; void throwRecursionDepthError() override; diff --git a/src/qmldom/qqmldomscriptelements_p.h b/src/qmldom/qqmldomscriptelements_p.h index 0ddcb54938..00cc25fe49 100644 --- a/src/qmldom/qqmldomscriptelements_p.h +++ b/src/qmldom/qqmldomscriptelements_p.h @@ -167,7 +167,7 @@ public: using BaseT::BaseT; ~Literal() override{}; - using VariantT = std::variant; + using VariantT = std::variant; void setLiteralValue(VariantT value) { m_value = value; } VariantT literalValue() const { return m_value; } diff --git a/tests/auto/qmldom/domdata/domitem/variableDeclarations.qml b/tests/auto/qmldom/domdata/domitem/variableDeclarations.qml index 7c5ecd7d4f..330ae35c11 100644 --- a/tests/auto/qmldom/domdata/domitem/variableDeclarations.qml +++ b/tests/auto/qmldom/domdata/domitem/variableDeclarations.qml @@ -10,6 +10,10 @@ Item { const a = 3; const b = "patron"; var aa = helloWorld, bb = aa; + + const bool1 = true; + let bool2 = false; + var nullVar = null; return sum; } } diff --git a/tests/auto/qmldom/domitem/tst_qmldomitem.h b/tests/auto/qmldom/domitem/tst_qmldomitem.h index 2d9b6782f0..09f103d7fb 100644 --- a/tests/auto/qmldom/domitem/tst_qmldomitem.h +++ b/tests/auto/qmldom/domitem/tst_qmldomitem.h @@ -956,9 +956,11 @@ private slots: QVERIFY(blockSemanticScope.value()->JSIdentifier(u"b"_s)); QVERIFY(blockSemanticScope.value()->JSIdentifier(u"aa"_s)); QVERIFY(blockSemanticScope.value()->JSIdentifier(u"bb"_s)); - + QVERIFY(blockSemanticScope.value()->JSIdentifier(u"bool1"_s)); + QVERIFY(blockSemanticScope.value()->JSIdentifier(u"bool2"_s)); + QVERIFY(blockSemanticScope.value()->JSIdentifier(u"nullVar"_s)); DomItem statements = block.field(Fields::statements); - QCOMPARE(statements.indexes(), 5); + QCOMPARE(statements.indexes(), 8); { // let sum = 0, helloWorld = "hello" @@ -1024,6 +1026,33 @@ private slots: QCOMPARE(bb.field(Fields::initializer).field(Fields::identifier).value().toString(), "aa"); } + { + // const bool1 = true + DomItem bool1 = statements.index(4).field(Fields::declarations).index(0); + QCOMPARE(bool1.field(Fields::scopeType).value().toInteger(), + ScriptElements::VariableDeclarationEntry::ScopeType::Const); + QCOMPARE(bool1.field(Fields::identifier).value().toString(), "bool1"); + QCOMPARE(bool1.field(Fields::initializer).internalKind(), DomType::ScriptLiteral); + QVERIFY(bool1.field(Fields::initializer).field(Fields::value).value().isTrue()); + } + { + // let bool2 = false + DomItem bool2 = statements.index(5).field(Fields::declarations).index(0); + QCOMPARE(bool2.field(Fields::scopeType).value().toInteger(), + ScriptElements::VariableDeclarationEntry::ScopeType::Let); + QCOMPARE(bool2.field(Fields::identifier).value().toString(), "bool2"); + QCOMPARE(bool2.field(Fields::initializer).internalKind(), DomType::ScriptLiteral); + QVERIFY(bool2.field(Fields::initializer).field(Fields::value).value().isFalse()); + } + { + // var nullVar = null + DomItem nullVar = statements.index(6).field(Fields::declarations).index(0); + QCOMPARE(nullVar.field(Fields::scopeType).value().toInteger(), + ScriptElements::VariableDeclarationEntry::ScopeType::Var); + QCOMPARE(nullVar.field(Fields::identifier).value().toString(), "nullVar"); + QCOMPARE(nullVar.field(Fields::initializer).internalKind(), DomType::ScriptLiteral); + QVERIFY(nullVar.field(Fields::initializer).field(Fields::value).value().isNull()); + } } void ifStatements()