diff options
author | Semih Yavuz <semih.yavuz@qt.io> | 2023-01-25 14:11:18 +0100 |
---|---|---|
committer | Semih Yavuz <semih.yavuz@qt.io> | 2023-01-27 12:57:15 +0100 |
commit | 20e1812301b74a3c07796b69de3022e6f5633666 (patch) | |
tree | 02d404a2c9e98ad43ef642da116d299c74a69a70 /src | |
parent | a6b0fa3945144171e656f62eeb9d5e4aa7bc9a11 (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>
Diffstat (limited to 'src')
-rw-r--r-- | src/qmldom/qqmldomreformatter.cpp | 62 |
1 files changed, 60 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; } |