Skip to content

Commit 4ef0370

Browse files
committedMay 4, 2017
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 llvm-svn: 302156
1 parent 808f2d3 commit 4ef0370

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed
 

‎clang/lib/Format/FormatToken.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ namespace format {
5353
TYPE(InlineASMColon) \
5454
TYPE(JavaAnnotation) \
5555
TYPE(JsComputedPropertyName) \
56+
TYPE(JsExponentiation) \
57+
TYPE(JsExponentiationEqual) \
5658
TYPE(JsFatArrow) \
5759
TYPE(JsNonNullAssertion) \
5860
TYPE(JsTypeColon) \

‎clang/lib/Format/FormatTokenLexer.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ void FormatTokenLexer::tryMergePreviousTokens() {
7474
static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
7575
tok::greaterequal};
7676
static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
77+
static const tok::TokenKind JSExponentiation[] = {tok::star, tok::star};
78+
static const tok::TokenKind JSExponentiationEqual[] = {tok::star,
79+
tok::starequal};
80+
7781
// FIXME: Investigate what token type gives the correct operator priority.
7882
if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
7983
return;
@@ -83,6 +87,12 @@ void FormatTokenLexer::tryMergePreviousTokens() {
8387
return;
8488
if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
8589
return;
90+
if (tryMergeTokens(JSExponentiation, TT_JsExponentiation))
91+
return;
92+
if (tryMergeTokens(JSExponentiationEqual, TT_JsExponentiationEqual)) {
93+
Tokens.back()->Tok.setKind(tok::starequal);
94+
return;
95+
}
8696
}
8797

8898
if (Style.Language == FormatStyle::LK_Java) {

‎clang/unittests/Format/FormatTestJS.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1786,5 +1786,11 @@ TEST_F(FormatTestJS, ImportComments) {
17861786
getGoogleJSStyleWithColumns(25));
17871787
verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
17881788
}
1789+
1790+
TEST_F(FormatTestJS, Exponentiation) {
1791+
verifyFormat("squared = x ** 2;");
1792+
verifyFormat("squared **= 2;");
1793+
}
1794+
17891795
} // end namespace tooling
17901796
} // end namespace clang

0 commit comments

Comments
 (0)
Please sign in to comment.