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-26 17:56:07 +0100
commitd962fe4abbf185abb1bc7464a00a93bc0e9d288b (patch)
treefe0bf08517deaf5906711c50262fa5493f16cabe
parent2d97f9bdf0d21997a6ee7e43d1364803842aa635 (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. Pick-to: 6.5 6.2 Fixes: QTBUG-110321 Change-Id: I9fa2561902dbcfde5e8fb041bc5dedcc56bb66cc Reviewed-by: Ulf Hermann <ulf.hermann@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 c29dce46fe..9109e2ca01 100644
--- a/src/qmldom/qqmldomreformatter.cpp
+++ b/src/qmldom/qqmldomreformatter.cpp
@@ -954,7 +954,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
{
@@ -977,7 +981,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 b69c324fc2..c14dd794fc 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -301,6 +301,9 @@ void TestQmlformat::testFormat_data()
QTest::newRow("dontRemoveComments")
<< "dontRemoveComments.qml"
<< "dontRemoveComments.formatted.qml" << QStringList {} << RunOption::OnCopy;
+ QTest::newRow("ecmaScriptClassInQml")
+ << "ecmaScriptClassInQml.qml"
+ << "ecmaScriptClassInQml.formatted.qml" << QStringList{} << RunOption::OnCopy;
}
void TestQmlformat::testFormat()