From c0f9f290970f3a562411d808594e557014c10f4e Mon Sep 17 00:00:00 2001 From: Fawzi Mohamed Date: Tue, 23 Feb 2021 08:59:37 +0100 Subject: [PATCH] qmljs: correctly reformat js spread operator (...) Fixes: QTCREATORBUG-23402 Change-Id: I6b4bd0846dac67116711b7ed046bd52d137b7674 Reviewed-by: Fabian Kosmale --- src/libs/qmljs/qmljsreformatter.cpp | 17 +++++++++++++++++ tests/auto/qml/reformatter/jssyntax.js | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/libs/qmljs/qmljsreformatter.cpp b/src/libs/qmljs/qmljsreformatter.cpp index de69df50438..60e32288c97 100644 --- a/src/libs/qmljs/qmljsreformatter.cpp +++ b/src/libs/qmljs/qmljsreformatter.cpp @@ -989,6 +989,21 @@ protected: out("const "); } } + switch (ast->type) { + case PatternElement::Literal: + case PatternElement::Method: + case PatternElement::Binding: + break; + case PatternElement::Getter: + out("get "); + break; + case PatternElement::Setter: + out("set "); + break; + case PatternElement::SpreadElement: + out("..."); + break; + } out(ast->identifierToken); if (ast->initializer) { if (ast->isVariableDeclaration()) @@ -1312,6 +1327,8 @@ protected: bool visit(ArgumentList *ast) override { for (ArgumentList *it = ast; it; it = it->next) { + if (it->isSpreadElement) + out("..."); accept(it->expression); if (it->next) { out(", ", it->commaToken); diff --git a/tests/auto/qml/reformatter/jssyntax.js b/tests/auto/qml/reformatter/jssyntax.js index edba1a406cf..b6c2a9e9054 100644 --- a/tests/auto/qml/reformatter/jssyntax.js +++ b/tests/auto/qml/reformatter/jssyntax.js @@ -12,6 +12,17 @@ function foo(a, b) { var foo = function (a, b) {} +function spread() { + iterableObj = [1, 2] + obj = { + "a": 42 + } + foo(...iterableObj) + let arr = [...iterableObj, '4', 'five', 6] + foo(-1, ...args, 2, ...[3]) + console.log(Math.max(...[1, 2, 3, 4])) +} + const func1 = x => x * 2 const func2 = x => { return x * 7