From: Martin Probst Date: Thu, 4 May 2017 15:04:04 +0000 (+0000) Subject: clang-format: [JS] exponentiation operator X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6bdf8252b7275f86882c06c1cf1fb98979b4c410;p=clang clang-format: [JS] exponentiation operator Summary: While its precedence should be higher than multiplicative, LLVM does not have a level for that, so for the time being just treat it as multiplicative. Reviewers: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D32864 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@302156 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 3b3600fede..0c5a528462 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -53,6 +53,8 @@ namespace format { TYPE(InlineASMColon) \ TYPE(JavaAnnotation) \ TYPE(JsComputedPropertyName) \ + TYPE(JsExponentiation) \ + TYPE(JsExponentiationEqual) \ TYPE(JsFatArrow) \ TYPE(JsNonNullAssertion) \ TYPE(JsTypeColon) \ diff --git a/lib/Format/FormatTokenLexer.cpp b/lib/Format/FormatTokenLexer.cpp index 1acc0c3065..45c3ae1afe 100644 --- a/lib/Format/FormatTokenLexer.cpp +++ b/lib/Format/FormatTokenLexer.cpp @@ -74,6 +74,10 @@ void FormatTokenLexer::tryMergePreviousTokens() { static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater, tok::greaterequal}; static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater}; + static const tok::TokenKind JSExponentiation[] = {tok::star, tok::star}; + static const tok::TokenKind JSExponentiationEqual[] = {tok::star, + tok::starequal}; + // FIXME: Investigate what token type gives the correct operator priority. if (tryMergeTokens(JSIdentity, TT_BinaryOperator)) return; @@ -83,6 +87,12 @@ void FormatTokenLexer::tryMergePreviousTokens() { return; if (tryMergeTokens(JSRightArrow, TT_JsFatArrow)) return; + if (tryMergeTokens(JSExponentiation, TT_JsExponentiation)) + return; + if (tryMergeTokens(JSExponentiationEqual, TT_JsExponentiationEqual)) { + Tokens.back()->Tok.setKind(tok::starequal); + return; + } } if (Style.Language == FormatStyle::LK_Java) { diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index eda9e0a31d..9144fe17e9 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -1786,5 +1786,11 @@ TEST_F(FormatTestJS, ImportComments) { getGoogleJSStyleWithColumns(25)); verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10)); } + +TEST_F(FormatTestJS, Exponentiation) { + verifyFormat("squared = x ** 2;"); + verifyFormat("squared **= 2;"); +} + } // end namespace tooling } // end namespace clang