diff options
author | Martin Probst <martin@probst.io> | 2017-11-24 10:48:25 +0000 |
---|---|---|
committer | Martin Probst <martin@probst.io> | 2017-11-24 10:48:25 +0000 |
commit | 70cec59e237fcd2d26da3e5994ed5572a87130d1 (patch) | |
tree | 8fd71c2fd9b6d0179da166e217412e83539fe5bf /clang | |
parent | 4b9ee769ca09bac2fd5710d5145be35569b4fc61 (diff) | |
download | bcm5719-llvm-70cec59e237fcd2d26da3e5994ed5572a87130d1.tar.gz bcm5719-llvm-70cec59e237fcd2d26da3e5994ed5572a87130d1.zip |
clang-format: [JS] handle destructuring `of`.
Summary:
Previously, clang-format would drop a space character between `of` and
then following (non-identifier) token if the preceding token was part of
a destructuring assignment (`}` or `]`).
Before:
for (const [a, b] of[]) {}
After:
for (const [a, b] of []) {}
Reviewers: djasper
Subscribers: klimek
Differential Revision: https://reviews.llvm.org/D40411
llvm-svn: 318942
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 6 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 4 |
2 files changed, 8 insertions, 2 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 3e102f8d3ca..0e2e8a8374d 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2397,9 +2397,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in, tok::kw_const) || // "of" is only a keyword if it appears after another identifier - // (e.g. as "const x of y" in a for loop). + // (e.g. as "const x of y" in a for loop), or after a destructuring + // operation (const [x, y] of z, const {a, b} of c). (Left.is(Keywords.kw_of) && Left.Previous && - Left.Previous->Tok.getIdentifierInfo())) && + (Left.Previous->Tok.getIdentifierInfo() || + Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) && (!Left.Previous || !Left.Previous->is(tok::period))) return true; if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous && diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 3e5abdc098a..d9176599fb7 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -1110,6 +1110,10 @@ TEST_F(FormatTestJS, ForLoops) { "}"); verifyFormat("for (let {a, b} of x) {\n" "}"); + verifyFormat("for (let {a, b} of [x]) {\n" + "}"); + verifyFormat("for (let [a, b] of [x]) {\n" + "}"); verifyFormat("for (let {a, b} in x) {\n" "}"); } |