diff options
| author | Martin Probst <martin@probst.io> | 2017-08-14 16:08:16 +0000 |
|---|---|---|
| committer | Martin Probst <martin@probst.io> | 2017-08-14 16:08:16 +0000 |
| commit | 83e0220b3ff81cd265e93d3d6029cb330b3fd87a (patch) | |
| tree | 82aba455466e8699dc04fc7676b5d1f42d1eefa5 /clang/lib/Format/TokenAnnotator.cpp | |
| parent | 6e01dc1b840139e07b60759a5cfa42d99bef8a43 (diff) | |
| download | bcm5719-llvm-83e0220b3ff81cd265e93d3d6029cb330b3fd87a.tar.gz bcm5719-llvm-83e0220b3ff81cd265e93d3d6029cb330b3fd87a.zip | |
clang-format: [JS] do not insert whitespace in call positions.
Summary:
In JavaScript, may keywords can be used in method names and thus call sites:
foo.delete();
foo.instanceof();
clang-format would previously insert whitespace after the `instanceof`. This
change generically skips inserting whitespace between a keyword and a
parenthesis if preceded by a dot, i.e. in a callsite.
Reviewers: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D36142
llvm-svn: 310851
Diffstat (limited to 'clang/lib/Format/TokenAnnotator.cpp')
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index c3ed348ad39..bfa31febd78 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2367,13 +2367,20 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Left.isOneOf(Keywords.kw_function, Keywords.kw_yield, Keywords.kw_extends, Keywords.kw_implements)) return true; - // JS methods can use some keywords as names (e.g. `delete()`). - if (Right.is(tok::l_paren) && Line.MustBeDeclaration && - Left.Tok.getIdentifierInfo()) - return false; - if (Right.is(tok::l_paren) && - Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof, tok::kw_void)) - return true; + if (Right.is(tok::l_paren)) { + // JS methods can use some keywords as names (e.g. `delete()`). + if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo()) + return false; + // Valid JS method names can include keywords, e.g. `foo.delete()` or + // `bar.instanceof()`. Recognize call positions by preceding period. + if (Left.Previous && Left.Previous->is(tok::period) && + Left.Tok.getIdentifierInfo()) + return false; + // Additional unary JavaScript operators that need a space after. + if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof, + tok::kw_void)) + return true; + } 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 |

