r301397 - clang-format: [JS] prevent wraps before class members.
Martin Probst via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 26 05:34:15 PDT 2017
Author: mprobst
Date: Wed Apr 26 07:34:15 2017
New Revision: 301397
URL: http://llvm.org/viewvc/llvm-project?rev=301397&view=rev
Log:
clang-format: [JS] prevent wraps before class members.
Summary: In JavaScript/TypeScript, class member definitions that use modifiers can be subject to Automatic Semicolon Insertion (ASI). For example, "class X { get \n foo }" defines a property called "get" and a property called "foo", both with no type annotation. This change prevents wrapping after the modifier keywords (visibility modifiers, static, get and set) to prevent accidental ASI.
Reviewers: djasper, bkramer
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D32531
Modified:
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=301397&r1=301396&r2=301397&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Wed Apr 26 07:34:15 2017
@@ -617,10 +617,12 @@ struct AdditionalKeywords {
kw_finally = &IdentTable.get("finally");
kw_from = &IdentTable.get("from");
kw_function = &IdentTable.get("function");
+ kw_get = &IdentTable.get("get");
kw_import = &IdentTable.get("import");
kw_is = &IdentTable.get("is");
kw_let = &IdentTable.get("let");
kw_module = &IdentTable.get("module");
+ kw_set = &IdentTable.get("set");
kw_type = &IdentTable.get("type");
kw_var = &IdentTable.get("var");
kw_yield = &IdentTable.get("yield");
@@ -675,10 +677,12 @@ struct AdditionalKeywords {
IdentifierInfo *kw_finally;
IdentifierInfo *kw_from;
IdentifierInfo *kw_function;
+ IdentifierInfo *kw_get;
IdentifierInfo *kw_import;
IdentifierInfo *kw_is;
IdentifierInfo *kw_let;
IdentifierInfo *kw_module;
+ IdentifierInfo *kw_set;
IdentifierInfo *kw_type;
IdentifierInfo *kw_var;
IdentifierInfo *kw_yield;
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=301397&r1=301396&r2=301397&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Apr 26 07:34:15 2017
@@ -2543,7 +2543,11 @@ bool TokenAnnotator::canBreakBefore(cons
if (NonComment &&
NonComment->isOneOf(tok::kw_return, tok::kw_continue, tok::kw_break,
tok::kw_throw, Keywords.kw_interface,
- Keywords.kw_type))
+ Keywords.kw_type, tok::kw_static, tok::kw_public,
+ tok::kw_private, tok::kw_protected,
+ Keywords.kw_abstract,
+ Keywords.kw_get,
+ Keywords.kw_set))
return false; // Otherwise a semicolon is inserted.
if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
return false;
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=301397&r1=301396&r2=301397&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Wed Apr 26 07:34:15 2017
@@ -318,6 +318,25 @@ TEST_F(FormatTestJS, MethodsInObjectLite
"};");
}
+TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) {
+ // Don't break after "protected"
+ verifyFormat("class X {\n"
+ " protected get getter():\n"
+ " number {\n"
+ " return 1;\n"
+ " }\n"
+ "}",
+ getGoogleJSStyleWithColumns(12));
+ // Don't break after "get"
+ verifyFormat("class X {\n"
+ " protected get someReallyLongGetterName():\n"
+ " number {\n"
+ " return 1;\n"
+ " }\n"
+ "}",
+ getGoogleJSStyleWithColumns(40));
+}
+
TEST_F(FormatTestJS, SpacesInContainerLiterals) {
verifyFormat("var arr = [1, 2, 3];");
verifyFormat("f({a: 1, b: 2, c: 3});");
More information about the cfe-commits
mailing list