aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-02-20 16:54:35 +0100
committerUlf Hermann <ulf.hermann@qt.io>2020-03-18 11:00:31 +0100
commitc5b48c735e1c26444e53c4ea7dc6df4c57b5e9b4 (patch)
tree42ee42ccf75a6a5ed24b303fd70d37af157dc905
parent36fb7cf832e801a7b3718fa443ec2f1b83e0fea2 (diff)
Also support partly specified versions in JS .imports
Task-number: QTBUG-71278 Change-Id: Ie3167d44780a192b5010052eea5192eee8c21c32 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qml/compiler/qqmlirbuilder.cpp2
-rw-r--r--src/qml/parser/qqmljslexer.cpp37
-rw-r--r--tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp6
-rw-r--r--tests/auto/qml/qqmlparser/tst_qqmlparser.cpp19
4 files changed, 26 insertions, 38 deletions
diff --git a/src/qml/compiler/qqmlirbuilder.cpp b/src/qml/compiler/qqmlirbuilder.cpp
index 0316e54c09..1717fb18f8 100644
--- a/src/qml/compiler/qqmlirbuilder.cpp
+++ b/src/qml/compiler/qqmlirbuilder.cpp
@@ -1052,7 +1052,7 @@ QTypeRevision IRBuilder::extractVersion(const QStringRef &string)
const int dot = string.indexOf(QLatin1Char('.'));
return (dot < 0)
- ? QTypeRevision::fromVersion(string.toInt(), 0)
+ ? QTypeRevision::fromMajorVersion(string.toInt())
: QTypeRevision::fromVersion(string.left(dot).toInt(), string.mid(dot + 1).toInt());
}
diff --git a/src/qml/parser/qqmljslexer.cpp b/src/qml/parser/qqmljslexer.cpp
index 243fc5bd30..4f694447bb 100644
--- a/src/qml/parser/qqmljslexer.cpp
+++ b/src/qml/parser/qqmljslexer.cpp
@@ -1538,9 +1538,10 @@ bool Lexer::scanDirectives(Directives *directives, DiagnosticMessage *error)
setError(QCoreApplication::translate("QQmlParser","Imported file must be a script"));
return false;
}
+ lex();
} else if (_tokenKind == T_IDENTIFIER) {
- // .import T_IDENTIFIER (. T_IDENTIFIER)* T_VERSION_NUMBER . T_VERSION_NUMBER as T_IDENTIFIER
+ // .import T_IDENTIFIER (. T_IDENTIFIER)* (T_VERSION_NUMBER (. T_VERSION_NUMBER)?)? as T_IDENTIFIER
while (true) {
if (!isUriToken(_tokenKind)) {
setError(QCoreApplication::translate("QQmlParser","Invalid module URI"));
@@ -1566,31 +1567,27 @@ bool Lexer::scanDirectives(Directives *directives, DiagnosticMessage *error)
}
}
- if (_tokenKind != T_VERSION_NUMBER) {
- setError(QCoreApplication::translate("QQmlParser","Module import requires a version"));
- return false; // expected the module version number
- }
-
- version = tokenText();
- lex();
- if (_tokenKind != T_DOT) {
- setError(QCoreApplication::translate( "QQmlParser", "Module import requires a minor version (missing dot)"));
- return false; // expected the module version number
- }
- version += QLatin1Char('.');
-
- lex();
- if (_tokenKind != T_VERSION_NUMBER) {
- setError(QCoreApplication::translate( "QQmlParser", "Module import requires a minor version (missing number)"));
- return false; // expected the module version number
+ if (_tokenKind == T_VERSION_NUMBER) {
+ version = tokenText();
+ lex();
+ if (_tokenKind == T_DOT) {
+ version += QLatin1Char('.');
+ lex();
+ if (_tokenKind != T_VERSION_NUMBER) {
+ setError(QCoreApplication::translate(
+ "QQmlParser", "Incomplete version number (dot but no minor)"));
+ return false; // expected the module version number
+ }
+ version += tokenText();
+ lex();
+ }
}
- version += tokenText();
}
//
// recognize the mandatory `as' followed by the module name
//
- if (! (lex() == T_AS && tokenStartLine() == lineNumber)) {
+ if (! (_tokenKind == T_AS && tokenStartLine() == lineNumber)) {
if (fileImport)
setError(QCoreApplication::translate("QQmlParser", "File import requires a qualifier"));
else
diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
index a136235f90..b63867ec3d 100644
--- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
+++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
@@ -4514,9 +4514,9 @@ void tst_qqmlecmascript::importScripts_data()
QTest::newRow("missing module version")
<< testFileUrl("jsimportfail/missingModuleVersion.qml")
- << false /* compilation should succeed */
+ << true /* compilation should succeed */
<< QString()
- << (QStringList() << testFileUrl("jsimportfail/missingModuleVersion.js").toString() + QLatin1String(":1:17: Module import requires a version"))
+ << QStringList()
<< QStringList()
<< QVariantList();
@@ -4524,7 +4524,7 @@ void tst_qqmlecmascript::importScripts_data()
<< testFileUrl("jsimportfail/malformedModuleVersion.qml")
<< false /* compilation should succeed */
<< QString()
- << (QStringList() << testFileUrl("jsimportfail/malformedModuleVersion.js").toString() + QLatin1String(":1:17: Module import requires a version"))
+ << (QStringList() << testFileUrl("jsimportfail/malformedModuleVersion.js").toString() + QLatin1String(":1:17: Module import requires a qualifier"))
<< QStringList()
<< QVariantList();
diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
index 8483bd1f95..e4bcfa7796 100644
--- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
+++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
@@ -59,7 +59,7 @@ private slots:
void templateLiteral();
void leadingSemicolonInClass();
void templatedReadonlyProperty();
- void qmlImportInJSRequiresFullVersion();
+ void qmlImportInJS();
void typeAnnotations_data();
void typeAnnotations();
void disallowedTypeAnnotations_data();
@@ -361,37 +361,28 @@ void tst_qqmlparser::templatedReadonlyProperty()
QVERIFY(parser.parse());
}
-void tst_qqmlparser::qmlImportInJSRequiresFullVersion()
+void tst_qqmlparser::qmlImportInJS()
{
{
QQmlJS::Engine engine;
QQmlJS::Lexer lexer(&engine);
lexer.setCode(QLatin1String(".import Test 1.0 as T"), 0, false);
QQmlJS::Parser parser(&engine);
- bool b = parser.parseProgram();
- qDebug() << parser.errorMessage();
- QVERIFY(b);
+ QVERIFY(parser.parseProgram());
}
{
QQmlJS::Engine engine;
QQmlJS::Lexer lexer(&engine);
lexer.setCode(QLatin1String(".import Test 1 as T"), 0, false);
QQmlJS::Parser parser(&engine);
- QVERIFY(!parser.parseProgram());
- }
- {
- QQmlJS::Engine engine;
- QQmlJS::Lexer lexer(&engine);
- lexer.setCode(QLatin1String(".import Test 1 as T"), 0, false);
- QQmlJS::Parser parser(&engine);
- QVERIFY(!parser.parseProgram());
+ QVERIFY(parser.parseProgram());
}
{
QQmlJS::Engine engine;
QQmlJS::Lexer lexer(&engine);
lexer.setCode(QLatin1String(".import Test as T"), 0, false);
QQmlJS::Parser parser(&engine);
- QVERIFY(!parser.parseProgram());
+ QVERIFY(parser.parseProgram());
}
}