aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmllint/main.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2019-06-14 14:21:25 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2019-07-12 15:31:16 +0200
commit392521048ce6ef43a127b3dba199eee58557b1f6 (patch)
tree5dcf3c0343ecb34f299ceb468aba7f4d442d8dd7 /tests/auto/qml/qmllint/main.cpp
parentde0d91abbbcf58a66018a08ca77bb4d63a5efda1 (diff)
Extend linter to check for unqualified ids
The linter has gained a new option (-U/--check-unqualified). If run with this option, it warns about occurrences of unqualified identifiers. Furthermore, it attempts to detect the reason for why the identifier can be used unqalified: - If the id originates from the root element, it suggests to qualify the access either with the root element's id, or with "parent" if applicable. - If the id is the parameter of a signal, it suggests to use functions in the handler, instead of relying on the signal parameters to be "magically" injected into scope. The linter does not attempt to handle with statements, but warns the user instead that they are a bad idea. Change-Id: I9aaf28c37595d84886a1071d49b86799b222a617 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qml/qmllint/main.cpp')
-rw-r--r--tests/auto/qml/qmllint/main.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/auto/qml/qmllint/main.cpp b/tests/auto/qml/qmllint/main.cpp
index eedbd7c2f2..038826790b 100644
--- a/tests/auto/qml/qmllint/main.cpp
+++ b/tests/auto/qml/qmllint/main.cpp
@@ -38,6 +38,8 @@ private Q_SLOTS:
void initTestCase();
void test();
void test_data();
+ void testUnqualified();
+ void testUnqualified_data();
private:
QString m_qmllintPath;
};
@@ -69,6 +71,49 @@ void TestQmllint::test_data()
QTest::newRow("Invalid_syntax_JS") << QStringLiteral("failure1.js") << false;
}
+void TestQmllint::testUnqualified()
+{
+ QFETCH(QString, filename);
+ QFETCH(QString, warningMessage);
+ QFETCH(int, warningLine);
+ QFETCH(int, warningColumn);
+ filename.prepend(QStringLiteral("data/"));
+ QStringList args;
+ args << QStringLiteral("-U") << filename;
+
+ QProcess process;
+ process.start(m_qmllintPath, args);
+ QVERIFY(process.waitForFinished());
+ QVERIFY(process.exitStatus() == QProcess::NormalExit);
+ QVERIFY(process.exitCode());
+ QString output = process.readAllStandardError();
+ QVERIFY(output.contains(QString::asprintf("Warning: unqualified access at %d:%d", warningLine, warningColumn)));
+ QVERIFY(output.contains(warningMessage));
+}
+
+void TestQmllint::testUnqualified_data()
+{
+ QTest::addColumn<QString>("filename");
+ QTest::addColumn<QString>("warningMessage");
+ QTest::addColumn<int>("warningLine");
+ QTest::addColumn<int>("warningColumn");
+
+ // check for false positive due to and warning about with statement
+ QTest::newRow("WithStatement") << QStringLiteral("WithStatement.qml") << QStringLiteral("with statements are strongly discouraged") << 10 << 25;
+ // id from nowhere (as with setContextProperty)
+ QTest::newRow("IdFromOuterSpaceDirect") << QStringLiteral("IdFromOuterSpace.qml") << "alien.x" << 4 << 8;
+ QTest::newRow("IdFromOuterSpaceAccess") << QStringLiteral("IdFromOuterSpace.qml") << "console.log(alien)" << 7 << 21;
+ // access property of root object
+ QTest::newRow("FromRootDirect") << QStringLiteral("FromRoot.qml") << QStringLiteral("x: root.unqualified") << 9 << 16; // new property
+ QTest::newRow("FromRootAccess") << QStringLiteral("FromRoot.qml") << QStringLiteral("property int check: root.x") << 13 << 33; // builtin property
+ // access property of root object from direct child
+ QTest::newRow("FromRootDirectParentDirect") << QStringLiteral("FromRootDirectParent.qml") << QStringLiteral("x: parent.unqualified") << 8 << 12;
+ QTest::newRow("FromRootDirectParentAccess") << QStringLiteral("FromRootDirectParent.qml") << QStringLiteral("property int check: parent.x") << 12 << 29;
+ // access injected name from signal
+ QTest::newRow("SignalHandler") << QStringLiteral("SignalHandler.qml") << QStringLiteral("onDoubleClicked: function(mouse) {...") << 4 << 34;
+
+}
+
void TestQmllint::test()
{
QFETCH(QString, filename);