aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-01-26 10:17:05 +0100
committerUlf Hermann <ulf.hermann@qt.io>2021-01-26 13:19:24 +0100
commit74060990bc83b8714d017da6b2238b00e6aba0e4 (patch)
tree7a4b580fb89436e795c3f8103bf9c010f27827aa
parent7d8552d0753ccf62b14ae3cbdca200602a1050b0 (diff)
QQmlJSTypeReader: Guard against empty JS files
Change-Id: Ie52ce15b7fa960ce84a6d17a21a0e307a38c726e Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qmlcompiler/qqmljstypereader.cpp20
-rw-r--r--tests/auto/qml/qmllint/data/Empty.js0
-rw-r--r--tests/auto/qml/qmllint/data/badScript.qml6
-rw-r--r--tests/auto/qml/qmllint/tst_qmllint.cpp4
4 files changed, 21 insertions, 9 deletions
diff --git a/src/qmlcompiler/qqmljstypereader.cpp b/src/qmlcompiler/qqmljstypereader.cpp
index 25636dbe32..7cd7691050 100644
--- a/src/qmlcompiler/qqmljstypereader.cpp
+++ b/src/qmlcompiler/qqmljstypereader.cpp
@@ -55,13 +55,16 @@ QQmlJSScope::Ptr QQmlJSTypeReader::operator()()
const bool isESModule = lowerSuffix == QLatin1String("mjs");
const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
- QFile file(m_file);
- if (!file.open(QFile::ReadOnly)) {
+ auto errorResult = [&](){
QQmlJSScope::Ptr result = QQmlJSScope::create(
isJavaScript ? QQmlJSScope::JSLexicalScope : QQmlJSScope::QMLScope);
result->setInternalName(scopeName);
return result;
- }
+ };
+
+ QFile file(m_file);
+ if (!file.open(QFile::ReadOnly))
+ return errorResult();
QString code = QString::fromUtf8(file.readAll());
file.close();
@@ -72,14 +75,13 @@ QQmlJSScope::Ptr QQmlJSTypeReader::operator()()
const bool success = isJavaScript ? (isESModule ? parser.parseModule()
: parser.parseProgram())
: parser.parse();
- if (!success) {
- QQmlJSScope::Ptr result = QQmlJSScope::create(
- isJavaScript ? QQmlJSScope::JSLexicalScope : QQmlJSScope::QMLScope);
- result->setInternalName(scopeName);
- return result;
- }
+ if (!success)
+ return errorResult();
QQmlJS::AST::Node *rootNode = parser.rootNode();
+ if (!rootNode)
+ return errorResult();
+
QQmlJSImportVisitor membersVisitor(m_importer, QFileInfo(m_file).canonicalPath(),
m_qmltypesFiles);
rootNode->accept(&membersVisitor);
diff --git a/tests/auto/qml/qmllint/data/Empty.js b/tests/auto/qml/qmllint/data/Empty.js
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/auto/qml/qmllint/data/Empty.js
diff --git a/tests/auto/qml/qmllint/data/badScript.qml b/tests/auto/qml/qmllint/data/badScript.qml
new file mode 100644
index 0000000000..13f3e33acc
--- /dev/null
+++ b/tests/auto/qml/qmllint/data/badScript.qml
@@ -0,0 +1,6 @@
+import QtQml
+import "Empty.js" as Foo
+
+QtObject {
+ objectName: Foo.stuff()
+}
diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp
index f5ef58940b..c1ccae7235 100644
--- a/tests/auto/qml/qmllint/tst_qmllint.cpp
+++ b/tests/auto/qml/qmllint/tst_qmllint.cpp
@@ -269,6 +269,10 @@ void TestQmllint::dirtyQmlCode_data()
<< QStringLiteral("badAliasObject.qml")
<< QString("Warning: Property \"wrongwrongwrong\" not found on type \"QtObject\"")
<< QString();
+ QTest::newRow("badScript")
+ << QStringLiteral("badScript.qml")
+ << QString("Warning: Property \"stuff\" not found on type \"Empty\"")
+ << QString();
}
void TestQmllint::dirtyQmlCode()