forked from qt-creator/qt-creator
QmlJs: Fix reformatter for arrow functions
Fixes: QTCREATORBUG-23019 Change-Id: I6c6bee7092cb12f225ad744df2b3834dfd4bbc8f Reviewed-by: Fawzi Mohamed <fawzi.mohamed@qt.io>
This commit is contained in:
@@ -4019,14 +4019,16 @@ ArrowFunction_In: ArrowParameters T_ARROW ConciseBodyLookahead AssignmentExpress
|
|||||||
/.
|
/.
|
||||||
case $rule_number: {
|
case $rule_number: {
|
||||||
AST::ReturnStatement *ret = new (pool) AST::ReturnStatement(sym(4).Expression);
|
AST::ReturnStatement *ret = new (pool) AST::ReturnStatement(sym(4).Expression);
|
||||||
ret->returnToken = sym(4).Node->firstSourceLocation();
|
const auto zeroLength = [](SourceLocation l){ l.length = 0; return l; };
|
||||||
ret->semicolonToken = sym(4).Node->lastSourceLocation();
|
ret->returnToken = zeroLength(sym(4).Node->firstSourceLocation());
|
||||||
|
ret->semicolonToken = zeroLength(sym(4).Node->lastSourceLocation());
|
||||||
AST::StatementList *statements = (new (pool) AST::StatementList(ret))->finish();
|
AST::StatementList *statements = (new (pool) AST::StatementList(ret))->finish();
|
||||||
AST::FunctionExpression *f = new (pool) AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, statements);
|
AST::FunctionExpression *f = new (pool)
|
||||||
|
AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, statements);
|
||||||
f->isArrowFunction = true;
|
f->isArrowFunction = true;
|
||||||
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
||||||
f->lbraceToken = sym(4).Node->firstSourceLocation();
|
f->lbraceToken = zeroLength(sym(4).Node->firstSourceLocation());
|
||||||
f->rbraceToken = sym(4).Node->lastSourceLocation();
|
f->rbraceToken = zeroLength(sym(4).Node->lastSourceLocation());
|
||||||
sym(1).Node = f;
|
sym(1).Node = f;
|
||||||
} break;
|
} break;
|
||||||
./
|
./
|
||||||
@@ -4039,7 +4041,7 @@ ArrowFunction_In: ArrowParameters T_ARROW ConciseBodyLookahead T_FORCE_BLOCK Fun
|
|||||||
AST::FunctionExpression *f = new (pool) AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, sym(6).StatementList);
|
AST::FunctionExpression *f = new (pool) AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, sym(6).StatementList);
|
||||||
f->isArrowFunction = true;
|
f->isArrowFunction = true;
|
||||||
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
||||||
f->lbraceToken = loc(6);
|
f->lbraceToken = loc(5);
|
||||||
f->rbraceToken = loc(7);
|
f->rbraceToken = loc(7);
|
||||||
sym(1).Node = f;
|
sym(1).Node = f;
|
||||||
} break;
|
} break;
|
||||||
|
@@ -3001,15 +3001,16 @@ case 241: {
|
|||||||
|
|
||||||
case 528: {
|
case 528: {
|
||||||
AST::ReturnStatement *ret = new (pool) AST::ReturnStatement(sym(4).Expression);
|
AST::ReturnStatement *ret = new (pool) AST::ReturnStatement(sym(4).Expression);
|
||||||
ret->returnToken = sym(4).Node->firstSourceLocation();
|
const auto zeroLength = [](SourceLocation l){ l.length = 0; return l; };
|
||||||
ret->semicolonToken = sym(4).Node->lastSourceLocation();
|
ret->returnToken = zeroLength(sym(4).Node->firstSourceLocation());
|
||||||
|
ret->semicolonToken = zeroLength(sym(4).Node->lastSourceLocation());
|
||||||
AST::StatementList *statements = (new (pool) AST::StatementList(ret))->finish();
|
AST::StatementList *statements = (new (pool) AST::StatementList(ret))->finish();
|
||||||
AST::FunctionExpression *f = new (pool)
|
AST::FunctionExpression *f = new (pool)
|
||||||
AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, statements);
|
AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, statements);
|
||||||
f->isArrowFunction = true;
|
f->isArrowFunction = true;
|
||||||
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
||||||
f->lbraceToken = sym(4).Node->firstSourceLocation();
|
f->lbraceToken = zeroLength(sym(4).Node->firstSourceLocation());
|
||||||
f->rbraceToken = sym(4).Node->lastSourceLocation();
|
f->rbraceToken = zeroLength(sym(4).Node->lastSourceLocation());
|
||||||
sym(1).Node = f;
|
sym(1).Node = f;
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
@@ -3022,7 +3023,7 @@ case 241: {
|
|||||||
AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, sym(6).StatementList);
|
AST::FunctionExpression(QStringView(), sym(1).FormalParameterList, sym(6).StatementList);
|
||||||
f->isArrowFunction = true;
|
f->isArrowFunction = true;
|
||||||
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
f->functionToken = sym(1).Node ? sym(1).Node->firstSourceLocation() : loc(1);
|
||||||
f->lbraceToken = loc(6);
|
f->lbraceToken = loc(5);
|
||||||
f->rbraceToken = loc(7);
|
f->rbraceToken = loc(7);
|
||||||
sym(1).Node = f;
|
sym(1).Node = f;
|
||||||
} break;
|
} break;
|
||||||
|
@@ -1097,7 +1097,8 @@ protected:
|
|||||||
{
|
{
|
||||||
out(ast->returnToken);
|
out(ast->returnToken);
|
||||||
if (ast->expression) {
|
if (ast->expression) {
|
||||||
out(" ");
|
if (ast->returnToken.isValid())
|
||||||
|
out(" ");
|
||||||
accept(ast->expression);
|
accept(ast->expression);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -1218,17 +1219,32 @@ protected:
|
|||||||
|
|
||||||
bool visit(FunctionExpression *ast) override
|
bool visit(FunctionExpression *ast) override
|
||||||
{
|
{
|
||||||
out("function ", ast->functionToken);
|
if (!ast->isArrowFunction) {
|
||||||
if (!ast->name.isNull())
|
out("function ", ast->functionToken);
|
||||||
out(ast->identifierToken);
|
if (!ast->name.isNull())
|
||||||
|
out(ast->identifierToken);
|
||||||
|
}
|
||||||
out(ast->lparenToken);
|
out(ast->lparenToken);
|
||||||
|
if (ast->isArrowFunction && ast->formals && ast->formals->next)
|
||||||
|
out("(");
|
||||||
accept(ast->formals);
|
accept(ast->formals);
|
||||||
|
if (ast->isArrowFunction && ast->formals && ast->formals->next)
|
||||||
|
out(")");
|
||||||
out(ast->rparenToken);
|
out(ast->rparenToken);
|
||||||
|
if (ast->isArrowFunction && !ast->formals)
|
||||||
|
out("()");
|
||||||
out(" ");
|
out(" ");
|
||||||
|
if (ast->isArrowFunction)
|
||||||
|
out("=> ");
|
||||||
out(ast->lbraceToken);
|
out(ast->lbraceToken);
|
||||||
if (ast->body) {
|
if (ast->body) {
|
||||||
lnAcceptIndented(ast->body);
|
if (ast->body->next || ast->lbraceToken.isValid()) {
|
||||||
newLine();
|
lnAcceptIndented(ast->body);
|
||||||
|
newLine();
|
||||||
|
} else {
|
||||||
|
// print a single statement in one line. E.g. x => x * 2
|
||||||
|
accept(ast->body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out(ast->rbraceToken);
|
out(ast->rbraceToken);
|
||||||
return false;
|
return false;
|
||||||
|
@@ -12,6 +12,15 @@ function foo(a, b) {
|
|||||||
|
|
||||||
var foo = function (a, b) {}
|
var foo = function (a, b) {}
|
||||||
|
|
||||||
|
const func1 = x => x * 2
|
||||||
|
const func2 = x => {
|
||||||
|
return x * 7
|
||||||
|
}
|
||||||
|
const func3 = (x, y) => x + y
|
||||||
|
const func4 = (x, y) => {
|
||||||
|
return x + y
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
for (var a = 1; a < 5; ++a) {
|
for (var a = 1; a < 5; ++a) {
|
||||||
switch (a) {
|
switch (a) {
|
||||||
|
Reference in New Issue
Block a user