aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSemih Yavuz <semih.yavuz@qt.io>2023-01-25 14:11:18 +0100
committerSemih Yavuz <semih.yavuz@qt.io>2023-01-27 12:57:15 +0100
commit20e1812301b74a3c07796b69de3022e6f5633666 (patch)
tree02d404a2c9e98ad43ef642da116d299c74a69a70
parenta6b0fa3945144171e656f62eeb9d5e4aa7bc9a11 (diff)
qmlformat: Add ECMAScript class reformatter
qmlformat currently discards ES classes, and only reformats the element list inside that class. Implement a class declaration visitor which reformats ES classes in qml file. Fixes: QTBUG-110321 Change-Id: I9fa2561902dbcfde5e8fb041bc5dedcc56bb66cc Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit d962fe4abbf185abb1bc7464a00a93bc0e9d288b) Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qmldom/qqmldomreformatter.cpp62
-rw-r--r--tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml37
-rw-r--r--tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.qml45
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp3
4 files changed, 145 insertions, 2 deletions
diff --git a/src/qmldom/qqmldomreformatter.cpp b/src/qmldom/qqmldomreformatter.cpp
index b11d36096f..1861bd1dd1 100644
--- a/src/qmldom/qqmldomreformatter.cpp
+++ b/src/qmldom/qqmldomreformatter.cpp
@@ -972,7 +972,11 @@ protected:
// to check
bool visit(TypeExpression *) override { return true; }
- bool visit(SuperLiteral *) override { return true; }
+ bool visit(SuperLiteral *) override
+ {
+ out("super");
+ return true;
+ }
bool visit(PatternProperty *) override { return true; }
bool visit(ComputedPropertyName *) override
{
@@ -989,7 +993,61 @@ protected:
}
bool visit(YieldExpression *) override { return true; }
bool visit(ClassExpression *) override { return true; }
- bool visit(ClassDeclaration *) override { return true; }
+
+ // Return false because we want to omit default function callsĀ in accept0 implementation.
+ bool visit(ClassDeclaration *ast) override
+ {
+ preVisit(ast);
+ out(ast->classToken);
+ out(" ");
+ out(ast->name);
+ if (ast->heritage) {
+ out(" extends ");
+ accept(ast->heritage);
+ }
+ out(" {");
+ int baseIndent = lw.increaseIndent();
+ for (ClassElementList *it = ast->elements; it; it = it->next) {
+ PatternProperty *property = it->property;
+ lw.newline();
+ preVisit(property);
+ if (it->isStatic)
+ out("static ");
+ if (property->type == PatternProperty::Getter)
+ out("get ");
+ else if (property->type == PatternProperty::Setter)
+ out("set ");
+ FunctionExpression *f = AST::cast<FunctionExpression *>(property->initializer);
+ const bool scoped = f->lbraceToken.length != 0;
+ out(f->functionToken);
+ out(f->lparenToken);
+ accept(f->formals);
+ out(f->rparenToken);
+ out(f->lbraceToken);
+ if (scoped)
+ ++expressionDepth;
+ if (f->body) {
+ if (f->body->next || scoped) {
+ lnAcceptIndented(f->body);
+ lw.newline();
+ } else {
+ baseIndent = lw.increaseIndent(1);
+ accept(f->body);
+ lw.decreaseIndent(1, baseIndent);
+ }
+ }
+ if (scoped)
+ --expressionDepth;
+ out(f->rbraceToken);
+ lw.newline();
+ postVisit(property);
+ }
+ lw.decreaseIndent(1, baseIndent);
+ out("}");
+ postVisit(ast);
+ return false;
+ }
+
bool visit(ClassElementList *) override { return true; }
bool visit(Program *) override { return true; }
bool visit(NameSpaceImport *) override { return true; }
diff --git a/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml
new file mode 100644
index 0000000000..edbb12c6e6
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml
@@ -0,0 +1,37 @@
+import QtQuick
+
+Item {
+
+ function f() {
+ var count = 0;
+ class Person {
+ constructor(name){
+ this._name = name;
+ }
+ }
+ class Employee extends Person {
+ constructor(name, age){
+ super(name);
+ this._name = name;
+ this._age = age;
+ ++count;
+ }
+
+ doWork(){}
+
+ /* do we get the comment? */ get name(){
+ return this._name.toUpperCase();
+ }
+
+ set name(newName){
+ if (newName) {
+ this._name = newName;
+ }
+ }
+
+ static get count(){
+ return count;
+ }
+ }
+ }
+}
diff --git a/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.qml b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.qml
new file mode 100644
index 0000000000..268859d3cc
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.qml
@@ -0,0 +1,45 @@
+import QtQuick
+
+Item {
+
+function f() {
+
+var count = 0;
+
+class Person {
+ constructor(name) {
+ this._name = name
+ }
+}
+
+class Employee extends Person{
+
+
+ constructor(name, age) {
+ super(name);
+ this._name = name;
+ this._age = age;
+ ++count;
+ }
+
+ doWork() {
+
+ }
+
+ get /* do we get the comment? */ name() {
+ return this._name.toUpperCase();
+ }
+
+ set name(newName){
+ if(newName){
+ this._name = newName;
+ }
+ }
+
+ static get count() { return count;}
+}
+
+
+}
+
+} \ No newline at end of file
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index 65b423b919..843c078f8c 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -252,6 +252,9 @@ void TestQmlformat::testFormat_data()
<< "emptyObject.formatted.qml" << QStringList {};
QTest::newRow("arrow functions") << "arrowFunctions.qml"
<< "arrowFunctions.formatted.qml" << QStringList {};
+ QTest::newRow("ecmaScriptClassInQml")
+ << "ecmaScriptClassInQml.qml"
+ << "ecmaScriptClassInQml.formatted.qml" << QStringList {};
}
void TestQmlformat::testFormat()