Re-order imports statements to import nested imports later

Re-order the imports for a script by increasing order of URI length.
This ensures that an import of the type 'import X.Y' is processed
after the import of 'import X' which contains the type definitions for
the namespace X.Y.

Task-number: QTBUG-24369
Change-Id: I1b06e9d114a97c9f47279f8f33383a27e0efb4bb
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Reviewed-by: Martin Jones <martin.jones@nokia.com>
This commit is contained in:
Matthew Vogt 2012-03-26 16:39:00 +10:00 committed by Qt by Nokia
parent 5de103291d
commit e4baefacff
17 changed files with 229 additions and 41 deletions

View File

@ -545,8 +545,7 @@ QString QQmlImportsPrivate::add(const QQmlDirComponents &qmldircomponentsnetwork
}
}
// TODO: Should this search be omitted if found == true?
if (!found) {
// step 2: search for extension with encoded version major
foreach (const QString &p, database->fileImportPath) {
dir = p+Slash+url;
@ -568,6 +567,7 @@ QString QQmlImportsPrivate::add(const QQmlDirComponents &qmldircomponentsnetwork
break;
}
}
}
if (!found) {
// step 3: search for extension without version number

View File

@ -1340,6 +1340,11 @@ bool QQmlScript::Parser::parse(const QByteArray &qmldata, const QUrl &url,
_errors[ii].setUrl(url);
}
if (_errors.isEmpty()) {
// Sort the imports into desired order
qStableSort(_imports.begin(), _imports.end());
}
return _errors.isEmpty();
}

View File

@ -121,6 +121,11 @@ public:
void extractVersion(int *maj, int *min) const;
QQmlScript::LocationSpan location;
bool operator<(const Import& other) const {
// Shorter URIs first, so 'import X' is before 'import X.Y'
return (uri.length() < other.uri.length());
}
};
class Object;

View File

@ -0,0 +1,2 @@
import QtQuick 2.0
Rectangle {}

View File

@ -1,2 +1,3 @@
InstalledTest 1.4 InstalledTest2.qml
InstalledTestTP 0.0 InstalledTest.qml
Rectangle 1.5 InstalledTest3.qml

View File

@ -181,7 +181,7 @@ private slots:
private:
QQmlEngine engine;
void testType(const QString& qml, const QString& type, const QString& error);
void testType(const QString& qml, const QString& type, const QString& error, bool partialMatch = false);
};
#define DETERMINE_ERRORS(errorfile,expected,actual)\
@ -1480,7 +1480,7 @@ void tst_qqmllanguage::reservedWords()
}
// Check that first child of qml is of given type. Empty type insists on error.
void tst_qqmllanguage::testType(const QString& qml, const QString& type, const QString& expectederror)
void tst_qqmllanguage::testType(const QString& qml, const QString& type, const QString& expectederror, bool partialMatch)
{
QQmlComponent component(&engine);
component.setData(qml.toUtf8(), TEST_FILE("empty.qml")); // just a file for relative local imports
@ -1495,7 +1495,7 @@ void tst_qqmllanguage::testType(const QString& qml, const QString& type, const Q
actualerror.append("; ");
actualerror.append(e.description());
}
QCOMPARE(actualerror,expectederror);
QCOMPARE(actualerror.left(partialMatch ? expectederror.length(): -1),expectederror);
} else {
VERIFY_ERRORS(0);
QObject *object = component.create();
@ -1616,13 +1616,13 @@ void tst_qqmllanguage::importsBuiltin_data()
"import com.nokia.Test 1.12\n"
"Test {}"
<< (!qmlCheckTypes()?"TestType2":"")
<< (!qmlCheckTypes()?"":"Test is ambiguous. Found in com/nokia/Test in version 1.12 and 1.11");
<< (!qmlCheckTypes()?"":"Test is ambiguous. Found in com/nokia/Test/ in version 1.12 and 1.11");
QTest::newRow("multiversion 2")
<< "import com.nokia.Test 1.11\n"
"import com.nokia.Test 1.12\n"
"OldTest {}"
<< (!qmlCheckTypes()?"TestType":"")
<< (!qmlCheckTypes()?"":"OldTest is ambiguous. Found in com/nokia/Test in version 1.12 and 1.11");
<< (!qmlCheckTypes()?"":"OldTest is ambiguous. Found in com/nokia/Test/ in version 1.12 and 1.11");
QTest::newRow("qualified multiversion 3")
<< "import com.nokia.Test 1.0 as T0\n"
"import com.nokia.Test 1.8 as T8\n"
@ -1691,7 +1691,7 @@ void tst_qqmllanguage::importsLocal_data()
"import com.nokia.Test 1.0\n"
"Test {}"
<< (!qmlCheckTypes()?"TestType":"")
<< (!qmlCheckTypes()?"":"Test is ambiguous. Found in com/nokia/Test and in subdir");
<< (!qmlCheckTypes()?"":"Test is ambiguous. Found in com/nokia/Test/ and in subdir/");
}
void tst_qqmllanguage::importsLocal()
@ -1841,32 +1841,37 @@ void tst_qqmllanguage::importsOrder_data()
QTest::addColumn<QString>("qml");
QTest::addColumn<QString>("type");
QTest::addColumn<QString>("error");
QTest::addColumn<bool>("partialMatch");
QTest::newRow("double import") <<
"import com.nokia.installedtest 1.4\n"
"import com.nokia.installedtest 1.4\n"
"InstalledTest {}"
<< (!qmlCheckTypes()?"QQuickText":"")
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest in version 1.4 and 1.4");
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest/ in version 1.4 and 1.4")
<< false;
QTest::newRow("installed import overrides 1") <<
"import com.nokia.installedtest 1.0\n"
"import com.nokia.installedtest 1.4\n"
"InstalledTest {}"
<< (!qmlCheckTypes()?"QQuickText":"")
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest in version 1.4 and 1.0");
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest/ in version 1.4 and 1.0")
<< false;
QTest::newRow("installed import overrides 2") <<
"import com.nokia.installedtest 1.4\n"
"import com.nokia.installedtest 1.0\n"
"InstalledTest {}"
<< (!qmlCheckTypes()?"QQuickRectangle":"")
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest in version 1.0 and 1.4");
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest/ in version 1.0 and 1.4")
<< false;
QTest::newRow("installed import re-overrides 1") <<
"import com.nokia.installedtest 1.4\n"
"import com.nokia.installedtest 1.0\n"
"import com.nokia.installedtest 1.4\n"
"InstalledTest {}"
<< (!qmlCheckTypes()?"QQuickText":"")
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest in version 1.4 and 1.0");
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest/ in version 1.4 and 1.0")
<< false;
QTest::newRow("installed import re-overrides 2") <<
"import com.nokia.installedtest 1.4\n"
"import com.nokia.installedtest 1.0\n"
@ -1874,41 +1879,49 @@ void tst_qqmllanguage::importsOrder_data()
"import com.nokia.installedtest 1.0\n"
"InstalledTest {}"
<< (!qmlCheckTypes()?"QQuickRectangle":"")
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest in version 1.0 and 1.4");
<< (!qmlCheckTypes()?"":"InstalledTest is ambiguous. Found in lib/com/nokia/installedtest/ in version 1.0 and 1.4")
<< false;
// Note: imports are now reordered by increasing order of URI length
QTest::newRow("installed import versus builtin 1") <<
"import com.nokia.installedtest0 1.5\n"
"import com.nokia.installedtest 1.5\n"
"import QtQuick 2.0\n"
"Rectangle {}"
<< (!qmlCheckTypes()?"QQuickRectangle":"")
<< (!qmlCheckTypes()?"":"Rectangle is ambiguous. Found in Qt and in lib/com/nokia/installedtest");
<< (!qmlCheckTypes()?"":"Rectangle is ambiguous. Found in lib/com/nokia/installedtest0/ and in ")
<< true;
QTest::newRow("installed import versus builtin 2") <<
"import QtQuick 2.0\n"
"import com.nokia.installedtest 1.5\n"
"Rectangle {}"
<< (!qmlCheckTypes()?"QQuickText":"")
<< (!qmlCheckTypes()?"":"Rectangle is ambiguous. Found in lib/com/nokia/installedtest and in Qt");
<< (!qmlCheckTypes()?"":"Rectangle is ambiguous. Found in lib/com/nokia/installedtest/ and in ")
<< true;
QTest::newRow("namespaces cannot be overridden by types 1") <<
"import QtQuick 2.0 as Rectangle\n"
"import com.nokia.installedtest 1.5\n"
"Rectangle {}"
<< ""
<< "Namespace Rectangle cannot be used as a type";
<< "Namespace Rectangle cannot be used as a type"
<< false;
QTest::newRow("namespaces cannot be overridden by types 2") <<
"import QtQuick 2.0 as Rectangle\n"
"import com.nokia.installedtest 1.5\n"
"Rectangle.Image {}"
<< "QQuickImage"
<< "";
<< ""
<< false;
QTest::newRow("local last 1") <<
"LocalLast {}"
<< "QQuickText"
<< "";
<< ""
<< false;
QTest::newRow("local last 2") <<
"import com.nokia.installedtest 1.0\n"
"LocalLast {}"
<< (!qmlCheckTypes()?"QQuickRectangle":"")// i.e. from com.nokia.installedtest, not data/LocalLast.qml
<< (!qmlCheckTypes()?"":"LocalLast is ambiguous. Found in lib/com/nokia/installedtest and in local directory");
<< (!qmlCheckTypes()?"":"LocalLast is ambiguous. Found in lib/com/nokia/installedtest/ and in ")
<< false;
}
void tst_qqmllanguage::importsOrder()
@ -1916,7 +1929,8 @@ void tst_qqmllanguage::importsOrder()
QFETCH(QString, qml);
QFETCH(QString, type);
QFETCH(QString, error);
testType(qml,type,error);
QFETCH(bool, partialMatch);
testType(qml,type,error,partialMatch);
}
void tst_qqmllanguage::importIncorrectCase()

View File

@ -0,0 +1,5 @@
import com.nokia.AutoTestQmlNestedPluginType.Nested 1.0
import com.nokia.AutoTestQmlNestedPluginType 1.0
MyNestedPluginType {
}

View File

@ -0,0 +1,5 @@
import com.nokia.AutoTestQmlNestedPluginType 1.0
import com.nokia.AutoTestQmlNestedPluginType.Nested 1.0
MyNestedPluginType {
}

View File

@ -0,0 +1 @@
3:1:MyNestedPluginType is not a type

View File

@ -0,0 +1,4 @@
import com.nokia.AutoTestQmlNestedPluginType 1.0
MyNestedPluginType {
}

View File

@ -0,0 +1 @@
2:1:module "com.nokia.AutoTestQmlNestedPluginType.Nested" version 6.66 is not installed

View File

@ -0,0 +1,5 @@
import com.nokia.AutoTestQmlNestedPluginType 1.0
import com.nokia.AutoTestQmlNestedPluginType.Nested 6.66
MyNestedPluginType {
}

View File

@ -0,0 +1 @@
plugin nestedPlugin

View File

@ -0,0 +1,89 @@
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QStringList>
#include <QtQml/qqmlextensionplugin.h>
#include <QtQml/qqml.h>
#include <QDebug>
class MyPluginType : public QObject
{
Q_OBJECT
Q_PROPERTY(QString value READ value)
public:
MyPluginType(QObject *parent=0) : QObject(parent) {}
QString value() const { return "Hello"; }
};
class MyNestedPluginType : public QObject
{
Q_OBJECT
Q_PROPERTY(QString value READ value)
public:
MyNestedPluginType(QObject *parent=0) : QObject(parent) {}
QString value() const { return "Goodbye"; }
};
class MyPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface" FILE "../empty.json")
public:
MyPlugin() {}
void registerTypes(const char *uri)
{
Q_ASSERT(QLatin1String(uri) == "com.nokia.AutoTestQmlNestedPluginType");
qmlRegisterType<MyPluginType>(uri, 1, 0, "MyPluginType");
QString nestedUri(uri);
nestedUri += QLatin1String(".Nested");
qmlRegisterType<MyNestedPluginType>(nestedUri.toLatin1().constData(), 1, 0, "MyNestedPluginType");
}
};
#include "nestedPlugin.moc"

View File

@ -0,0 +1,7 @@
TEMPLATE = lib
CONFIG += nestedPlugin
SOURCES = nestedPlugin.cpp
QT = core qml
DESTDIR = ../imports/com/nokia/AutoTestQmlNestedPluginType
QT += core-private gui-private qml-private

View File

@ -1,6 +1,6 @@
QT = core
TEMPLATE = subdirs
SUBDIRS = plugin plugin.2 plugin.2.1 pluginWrongCase pluginWithQmlFile pluginMixed pluginVersion
SUBDIRS = plugin plugin.2 plugin.2.1 pluginWrongCase pluginWithQmlFile pluginMixed pluginVersion nestedPlugin
tst_qqmlmoduleplugin_pro.depends += plugin
SUBDIRS += tst_qqmlmoduleplugin.pro

View File

@ -70,6 +70,8 @@ private slots:
void versionNotInstalled_data();
void implicitQmldir();
void implicitQmldir_data();
void importsNested();
void importsNested_data();
private:
QString m_importsDirectory;
@ -347,6 +349,47 @@ void tst_qqmlmoduleplugin::implicitQmldir()
delete obj;
}
void tst_qqmlmoduleplugin::importsNested_data()
{
QTest::addColumn<QString>("file");
QTest::addColumn<QString>("errorFile");
// Note: specific order required to induce failure (no other test case should import the
// plugin used for this test, or the alternate order test will pass spuriously)
QTest::newRow("alternateOrder") << "importsNested.1.qml" << QString();
QTest::newRow("expectedOrder") << "importsNested.2.qml" << QString();
QTest::newRow("missingImport") << "importsNested.3.qml" << "importsNested.3.errors.txt";
QTest::newRow("invalidVersion") << "importsNested.4.qml" << "importsNested.4.errors.txt";
}
void tst_qqmlmoduleplugin::importsNested()
{
QFETCH(QString, file);
QFETCH(QString, errorFile);
// Note: because imports are cached between test case data rows (and the plugins remain loaded),
// these tests should really be run in new instances of the app...
QQmlEngine engine;
engine.addImportPath(m_importsDirectory);
if (!errorFile.isEmpty()) {
QTest::ignoreMessage(QtWarningMsg, "QQmlComponent: Component is not ready");
}
QQmlComponent component(&engine, testFile(file));
QObject *obj = component.create();
if (errorFile.isEmpty()) {
if (qgetenv("DEBUG") != "" && !component.errors().isEmpty())
qWarning() << "Unexpected Errors:" << component.errors();
QVERIFY(obj);
delete obj;
} else {
QList<QQmlError> errors = component.errors();
VERIFY_ERRORS(errorFile.toLatin1().constData());
QVERIFY(!obj);
}
}
QTEST_MAIN(tst_qqmlmoduleplugin)