diff options
author | Martin Probst <martin@probst.io> | 2017-08-01 17:35:57 +0000 |
---|---|---|
committer | Martin Probst <martin@probst.io> | 2017-08-01 17:35:57 +0000 |
commit | cde9815dc2ccb31a39b67ef9c2788c42b1a11829 (patch) | |
tree | b3eb4badfc3a2284fe346a9041596cb6d982753f | |
parent | 4dbdd470a8da9a44cbe124a6a56174537f7dca3f (diff) | |
download | bcm5719-llvm-cde9815dc2ccb31a39b67ef9c2788c42b1a11829.tar.gz bcm5719-llvm-cde9815dc2ccb31a39b67ef9c2788c42b1a11829.zip |
clang-format: [JS] prefer wrapping chains over empty literals.
Summary:
E.g. don't wrap like this:
(foo.bar.baz).and.bam(Blah.of({
}))
But rather:
(foo.bar.baz)
.and.bam(Blah.of({}))
Reviewers: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D36139
llvm-svn: 309712
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 3 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 9 |
2 files changed, 12 insertions, 0 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index cb3e8335b3f..150d786c333 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2006,6 +2006,9 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) return 100; + // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()". + if (Left.opensScope() && Right.closesScope()) + return 200; } if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 4ae6b866b9f..02d608eadfe 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -841,6 +841,15 @@ TEST_F(FormatTestJS, FunctionLiterals) { } +TEST_F(FormatTestJS, DontWrapEmptyLiterals) { + verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n" + " .and.returnValue(Observable.of([]));"); + verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n" + " .and.returnValue(Observable.of({}));"); + verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n" + " .and.returnValue(Observable.of(()));"); +} + TEST_F(FormatTestJS, InliningFunctionLiterals) { FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |