summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format/FormatTestJS.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2016-03-02 22:44:03 +0000
committerDaniel Jasper <djasper@google.com>2016-03-02 22:44:03 +0000
commitabd1f574535fd57d2099daff486afb0ded29138a (patch)
tree3f8f70a7022cbcbf9f2b0d0899258e38daa28d23 /clang/unittests/Format/FormatTestJS.cpp
parenta0d7a2cd3f878f8fa9624006998558057bed9081 (diff)
downloadbcm5719-llvm-abd1f574535fd57d2099daff486afb0ded29138a.tar.gz
bcm5719-llvm-abd1f574535fd57d2099daff486afb0ded29138a.zip
clang-format: [JS] Optionally re-quote string literals.
Turns "foo" into 'foo' (or vice versa, depending on configuration). This makes it more convenient to follow the Google JavaScript style guide: https://google.github.io/styleguide/javascriptguide.xml?showone=Strings#Strings This functionality is behind the option "JavaScriptQuotes", which can be: * "leave" (no re-quoting) * "single" (change to single quotes) * "double" (change to double quotes) This also changes single quoted JavaScript string literals to be treated as tok::string_literal, not tok::char_literal, which fixes two unrelated tests. Patch by Martin Probst. Thank you. llvm-svn: 262534
Diffstat (limited to 'clang/unittests/Format/FormatTestJS.cpp')
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp35
1 files changed, 33 insertions, 2 deletions
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 39e4ea8566b..589a7e48da3 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -257,7 +257,7 @@ TEST_F(FormatTestJS, SpacesInContainerLiterals) {
verifyFormat("f({'a': [{}]});");
}
-TEST_F(FormatTestJS, SingleQuoteStrings) {
+TEST_F(FormatTestJS, SingleQuotedStrings) {
verifyFormat("this.function('', true);");
}
@@ -883,7 +883,7 @@ TEST_F(FormatTestJS, Modules) {
verifyFormat("import {\n"
" X,\n"
" Y,\n"
- "} from 'some/long/module.js';",
+ "} from\n 'some/long/module.js';",
getGoogleJSStyleWithColumns(20));
verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
verifyFormat("import * as lib from 'some/module.js';");
@@ -1094,5 +1094,36 @@ TEST_F(FormatTestJS, JSDocAnnotations) {
getGoogleJSStyleWithColumns(20)));
}
+TEST_F(FormatTestJS, RequoteStringsSingle) {
+ EXPECT_EQ("var x = 'foo';", format("var x = \"foo\";"));
+ EXPECT_EQ("var x = 'fo\\'o\\'';", format("var x = \"fo'o'\";"));
+ EXPECT_EQ("var x = 'fo\\'o\\'';", format("var x = \"fo\\'o'\";"));
+ EXPECT_EQ("var x =\n"
+ " 'foo\\'';",
+ // Code below is 15 chars wide, doesn't fit into the line with the
+ // \ escape added.
+ format("var x = \"foo'\";", getGoogleJSStyleWithColumns(15)));
+ // Removes no-longer needed \ escape from ".
+ EXPECT_EQ("var x = 'fo\"o';", format("var x = \"fo\\\"o\";"));
+ // Code below fits into 15 chars *after* removing the \ escape.
+ EXPECT_EQ("var x = 'fo\"o';",
+ format("var x = \"fo\\\"o\";", getGoogleJSStyleWithColumns(15)));
+}
+
+TEST_F(FormatTestJS, RequoteStringsDouble) {
+ FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
+ DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
+ verifyFormat("var x = \"foo\";", DoubleQuotes);
+ EXPECT_EQ("var x = \"foo\";", format("var x = 'foo';", DoubleQuotes));
+ EXPECT_EQ("var x = \"fo'o\";", format("var x = 'fo\\'o';", DoubleQuotes));
+}
+
+TEST_F(FormatTestJS, RequoteStringsLeave) {
+ FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
+ LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
+ verifyFormat("var x = \"foo\";", LeaveQuotes);
+ verifyFormat("var x = 'foo';", LeaveQuotes);
+}
+
} // end namespace tooling
} // end namespace clang
OpenPOWER on IntegriCloud