diff options
| author | Krasimir Georgiev <krasimir@google.com> | 2018-11-20 14:22:43 +0000 |
|---|---|---|
| committer | Krasimir Georgiev <krasimir@google.com> | 2018-11-20 14:22:43 +0000 |
| commit | c1631019a8cb0845d2f939613329d6210c7d4a2f (patch) | |
| tree | 09a71cccbf4e3ef03da2b95741a256a83b4deab4 | |
| parent | f5e4f0af7b450562af46a5df5e071bc79d021c16 (diff) | |
| download | bcm5719-llvm-c1631019a8cb0845d2f939613329d6210c7d4a2f.tar.gz bcm5719-llvm-c1631019a8cb0845d2f939613329d6210c7d4a2f.zip | |
[clang-format] JS: don't treat is: as a type matcher
Summary:
Clang-format is treating all occurences of `is` in js as type matchers. In some
cases this is wrong, as it might be a dict key.
Reviewers: mprobst
Reviewed By: mprobst
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D54753
llvm-svn: 347307
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 17 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 10 |
2 files changed, 25 insertions, 2 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 705ee1582cf..fbc7691af45 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3113,8 +3113,21 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, // Don't wrap between ":" and "!" of a strict prop init ("field!: type;"). if (Left.is(tok::exclaim) && Right.is(tok::colon)) return false; - if (Right.is(Keywords.kw_is)) - return false; + // Look for is type annotations like: + // function f(): a is B { ... } + // Do not break before is in these cases. + if (Right.is(Keywords.kw_is)) { + const FormatToken* Next = Right.getNextNonComment(); + // If `is` is followed by a colon, it's likely that it's a dict key, so + // ignore it for this check. + // For example this is common in Polymer: + // Polymer({ + // is: 'name', + // ... + // }); + if (!Next || !Next->is(tok::colon)) + return false; + } if (Left.is(Keywords.kw_in)) return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None; if (Right.is(Keywords.kw_in)) diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 5a71f003371..a14724f85eb 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -1186,6 +1186,16 @@ TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { getGoogleJSStyleWithColumns(25)); } +TEST_F(FormatTestJS, AddsIsTheDictKeyOnNewline) { + // Do not confuse is, the dict key with is, the type matcher. Put is, the dict + // key, on a newline. + verifyFormat("Polymer({\n" + " is: '', //\n" + " rest: 1\n" + "});", + getGoogleJSStyleWithColumns(20)); +} + TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { verifyFormat("a\n" "b;", |

