diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2019-01-08 16:27:56 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2019-07-09 16:52:01 +0200 |
commit | a06736cab722c2a4283cdcbda8d003d8e91d7f54 (patch) | |
tree | f083fe5bca85a06254e63729802d6c081c88cf87 /tests/auto/qml/qqmlparser/tst_qqmlparser.cpp | |
parent | d9f115327cd36cef370172bc87706cca6e57f912 (diff) |
Add basic type annotation support for QML functions to the grammar
Inspired by TypeScript syntax, allow optional type annotations in the
style of ": <name of type>" in for the parameters of functions and their
return type.
These annotations are not used at the moment, so by default we produce
an error message when encountering them in the AST.
In addition their usage is limited to functions declared in the QML
scope. All other uses attempt to produce readable syntax errors. So for
example this is okay:
Item {
function blah(param: string) string { ... }
}
And this is not okay:
// some file.js
function blah(param: string) : string { ... }
Change-Id: I12d20b4e2ce92e01108132975a06cfd13ba4a254
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlparser/tst_qqmlparser.cpp')
-rw-r--r-- | tests/auto/qml/qqmlparser/tst_qqmlparser.cpp | 108 |
1 files changed, 106 insertions, 2 deletions
diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp index 5f58df75d4..9d8818d01e 100644 --- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp +++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp @@ -32,12 +32,14 @@ #include <private/qqmljsastvisitor_p.h> #include <private/qqmljsast_p.h> +#include "../../shared/util.h" + #include <qtest.h> #include <QDir> #include <QDebug> #include <cstdlib> -class tst_qqmlparser : public QObject +class tst_qqmlparser : public QQmlDataTest { Q_OBJECT public: @@ -56,6 +58,10 @@ private slots: void leadingSemicolonInClass(); void templatedReadonlyProperty(); void qmlImportInJSRequiresFullVersion(); + void typeAnnotations_data(); + void typeAnnotations(); + void disallowedTypeAnnotations_data(); + void disallowedTypeAnnotations(); private: QStringList excludedDirs; @@ -88,7 +94,7 @@ public: qDebug() << "first source loc failed: node:" << node->kind << "at" << node->firstSourceLocation().startLine << "/" << node->firstSourceLocation().startColumn << "parent" << parent->kind << "at" << parent->firstSourceLocation().startLine << "/" << parent->firstSourceLocation().startColumn; if (node->lastSourceLocation().end() > parentEnd) - qDebug() << "first source loc failed: node:" << node->kind << "at" << node->lastSourceLocation().startLine << "/" << node->lastSourceLocation().startColumn + qDebug() << "last source loc failed: node:" << node->kind << "at" << node->lastSourceLocation().startLine << "/" << node->lastSourceLocation().startColumn << "parent" << parent->kind << "at" << parent->lastSourceLocation().startLine << "/" << parent->lastSourceLocation().startColumn; QVERIFY(node->firstSourceLocation().begin() >= parentBegin); @@ -114,6 +120,27 @@ public: } }; +struct TypeAnnotationObserver: public AST::Visitor +{ + bool typeAnnotationSeen = false; + + void operator()(AST::Node *node) + { + AST::Node::accept(node, this); + } + + virtual bool visit(AST::TypeAnnotation *) + { + typeAnnotationSeen = true; + return true; + } + + void throwRecursionDepthError() final + { + QFAIL("Maximum statement or expression depth exceeded"); + } +}; + } tst_qqmlparser::tst_qqmlparser() @@ -122,6 +149,7 @@ tst_qqmlparser::tst_qqmlparser() void tst_qqmlparser::initTestCase() { + QQmlDataTest::initTestCase(); // Add directories you want excluded here // These snippets are not expected to run on their own. @@ -334,6 +362,82 @@ void tst_qqmlparser::qmlImportInJSRequiresFullVersion() } } +void tst_qqmlparser::typeAnnotations_data() +{ + QTest::addColumn<QString>("file"); + + QString tests = dataDirectory() + "/typeannotations/"; + + QStringList files; + files << findFiles(QDir(tests)); + + for (const QString &file: qAsConst(files)) + QTest::newRow(qPrintable(file)) << file; +} + +void tst_qqmlparser::typeAnnotations() +{ + using namespace QQmlJS; + + QFETCH(QString, file); + + QString code; + + QFile f(file); + if (f.open(QFile::ReadOnly)) + code = QString::fromUtf8(f.readAll()); + + const bool qmlMode = file.endsWith(QLatin1String(".qml")); + + Engine engine; + Lexer lexer(&engine); + lexer.setCode(code, 1, qmlMode); + Parser parser(&engine); + bool ok = qmlMode ? parser.parse() : parser.parseProgram(); + QVERIFY(ok); + + check::TypeAnnotationObserver observer; + observer(parser.rootNode()); + + QVERIFY(observer.typeAnnotationSeen); +} + +void tst_qqmlparser::disallowedTypeAnnotations_data() +{ + QTest::addColumn<QString>("file"); + + QString tests = dataDirectory() + "/disallowedtypeannotations/"; + + QStringList files; + files << findFiles(QDir(tests)); + + for (const QString &file: qAsConst(files)) + QTest::newRow(qPrintable(file)) << file; +} + +void tst_qqmlparser::disallowedTypeAnnotations() +{ + using namespace QQmlJS; + + QFETCH(QString, file); + + QString code; + + QFile f(file); + if (f.open(QFile::ReadOnly)) + code = QString::fromUtf8(f.readAll()); + + const bool qmlMode = file.endsWith(QLatin1String(".qml")); + + Engine engine; + Lexer lexer(&engine); + lexer.setCode(code, 1, qmlMode); + Parser parser(&engine); + bool ok = qmlMode ? parser.parse() : parser.parseProgram(); + QVERIFY(!ok); + QVERIFY2(parser.errorMessage().startsWith("Type annotations are not permitted "), qPrintable(parser.errorMessage())); +} + QTEST_MAIN(tst_qqmlparser) #include "tst_qqmlparser.moc" |