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 <sami.shalayel@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Semih Yavuz 2023-05-04 14:43:25 +02:00
parent d32ccc5f1b
commit feca8417f6
5 changed files with 72 additions and 3 deletions

View File

@ -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<ScriptElements::Literal>(expression);
current->setLiteralValue(nullptr);
pushScriptElement(current);
return true;
}
bool QQmlDomAstCreator::visit(AST::TrueLiteral *expression)
{
if (!m_enableScriptExpressions)
return false;
auto current = makeScriptElement<ScriptElements::Literal>(expression);
current->setLiteralValue(true);
pushScriptElement(current);
return true;
}
bool QQmlDomAstCreator::visit(AST::FalseLiteral *expression)
{
if (!m_enableScriptExpressions)
return false;
auto current = makeScriptElement<ScriptElements::Literal>(expression);
current->setLiteralValue(false);
pushScriptElement(current);
return true;
}
bool QQmlDomAstCreator::visit(AST::VariableDeclarationList *)
{
if (!m_enableScriptExpressions)

View File

@ -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;

View File

@ -167,7 +167,7 @@ public:
using BaseT::BaseT;
~Literal() override{};
using VariantT = std::variant<QString, double>;
using VariantT = std::variant<QString, double, bool, std::nullptr_t>;
void setLiteralValue(VariantT value) { m_value = value; }
VariantT literalValue() const { return m_value; }

View File

@ -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;
}
}

View File

@ -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()