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-26 17:56:07 +0100 |
commit | d962fe4abbf185abb1bc7464a00a93bc0e9d288b (patch) | |
tree | fe0bf08517deaf5906711c50262fa5493f16cabe /src | |
parent | 2d97f9bdf0d21997a6ee7e43d1364803842aa635 (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>
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 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; } |