qmllint: Don't crash when importing ES modules

Change-Id: Ica93a3e3fb0eb99be1498f1fcb94b09c113272d7
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2020-11-20 13:13:23 +01:00
parent f1a9c9b427
commit 89636733ba
4 changed files with 36 additions and 2 deletions

View File

@ -40,6 +40,26 @@
QT_BEGIN_NAMESPACE
static QQmlJSScope::Ptr parseModule(QQmlJS::AST::ESModule *module, const QString &name)
{
using namespace QQmlJS::AST;
QQmlJSScope::Ptr result = QQmlJSScope::create(QQmlJSScope::JSLexicalScope);
result->setInternalName(name);
for (auto *statement = module->body; statement; statement = statement->next) {
if (auto *exp = cast<ExportDeclaration *>(statement->statement)) {
// TODO: There are a number of other things we could find here
if (auto *function = cast<FunctionDeclaration *>(exp->variableStatementOrDeclaration)) {
QQmlJSMetaMethod method(function->name.toString());
method.setMethodType(QQmlJSMetaMethod::Method);
for (auto *argument = function->formals; argument; argument = argument->next)
method.addParameter(argument->element->bindingIdentifier.toString(), QString());
result->addOwnMethod(method);
}
}
}
return result;
}
static QQmlJSScope::Ptr parseProgram(QQmlJS::AST::Program *program, const QString &name)
{
using namespace QQmlJS::AST;
@ -105,8 +125,11 @@ QQmlJSScope::Ptr QQmlJSTypeReader::operator()()
return membersVisitor.result();
}
// TODO: Anything special to do with ES modules here?
return parseProgram(QQmlJS::AST::cast<QQmlJS::AST::Program *>(parser.rootNode()), scopeName);
if (isESModule)
return parseModule(cast<ESModule *>(parser.rootNode()), scopeName);
else
return parseProgram(cast<Program *>(parser.rootNode()), scopeName);
}
QT_END_NAMESPACE

View File

@ -0,0 +1,6 @@
import QtQml 2.0
import "script.mjs" as Script
QtObject {
property bool ok: Script.ok()
}

View File

@ -0,0 +1,4 @@
export function ok() {
return true
}

View File

@ -290,6 +290,7 @@ void TestQmllint::cleanQmlCode_data()
QTest::newRow("anchors2") << QStringLiteral("anchors2.qml");
QTest::newRow("optionalImport") << QStringLiteral("optionalImport.qml");
QTest::newRow("goodAliasObject") << QStringLiteral("goodAliasObject.qml");
QTest::newRow("jsmoduleimport") << QStringLiteral("jsmoduleimport.qml");
}
void TestQmllint::cleanQmlCode()