summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format/FormatTestJS.cpp
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2019-03-19 12:28:41 +0000
committerMartin Probst <martin@probst.io>2019-03-19 12:28:41 +0000
commit26a484f479d54ea5501628a65b99316e20f3dc99 (patch)
treeb9a8d52e1d48bff20832024588da6e4edc340172 /clang/unittests/Format/FormatTestJS.cpp
parent9497b2b2f7212896493756137cf9aa13bc1dede6 (diff)
downloadbcm5719-llvm-26a484f479d54ea5501628a65b99316e20f3dc99.tar.gz
bcm5719-llvm-26a484f479d54ea5501628a65b99316e20f3dc99.zip
[clang-format] [JS] handle private members.
Addresses PR40999 https://bugs.llvm.org/show_bug.cgi?id=40999 Private fields and methods in JavaScript would get incorrectly indented (it sees them as preprocessor directives and hence left aligns them) In this revision `#identifier` tokens `tok::hash->tok::identifier` are merged into a single new token `tok::identifier` with the `#` contained inside the TokenText. Before: ``` class Example { pub = 1; static pub2 = "foo"; static #priv2 = "bar"; method() { this.#priv = 5; } static staticMethod() { switch (this.#priv) { case '1': break; } } this.#privateMethod(); // infinite loop } static #staticPrivateMethod() {} } ``` After this fix the code will be correctly indented ``` class Example { pub = 1; #priv = 2; static pub2 = "foo"; static #priv2 = "bar"; method() { this.#priv = 5; } static staticMethod() { switch (this.#priv) { case '1': #priv = 3; break; } } #privateMethod() { this.#privateMethod(); // infinite loop } static #staticPrivateMethod() {} } ``` NOTE: There might be some JavaScript code out there which uses the C processor to preprocess .js files http://www.nongnu.org/espresso/js-cpp.html. It's not clear how this revision or even private fields and methods would interact. Patch originally by MyDeveloperDays (thanks!). llvm-svn: 356449
Diffstat (limited to 'clang/unittests/Format/FormatTestJS.cpp')
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 528e4cf57cb..b332f1bd975 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -2334,5 +2334,27 @@ TEST_F(FormatTestJS, ConditionalTypes) {
" never) extends((k: infer I) => void) ? I : never;");
}
-} // end namespace tooling
+TEST_F(FormatTestJS, SupportPrivateFieldsAndMethods) {
+ verifyFormat("class Example {\n"
+ " pub = 1;\n"
+ " #priv = 2;\n"
+ " static pub2 = 'foo';\n"
+ " static #priv2 = 'bar';\n"
+ " method() {\n"
+ " this.#priv = 5;\n"
+ " }\n"
+ " static staticMethod() {\n"
+ " switch (this.#priv) {\n"
+ " case '1':\n"
+ " #priv = 3;\n"
+ " break;\n"
+ " }\n"
+ " }\n"
+ " #privateMethod() {\n"
+ " this.#privateMethod(); // infinite loop\n"
+ " }\n"
+ " static #staticPrivateMethod() {}\n");
+}
+
+} // namespace format
} // end namespace clang
OpenPOWER on IntegriCloud