summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2017-01-09 08:56:36 +0000
committerMartin Probst <martin@probst.io>2017-01-09 08:56:36 +0000
commitd40bca431dfab7deae906fae9eea72dbdd81de2c (patch)
treecd033ee0d1abe60916029787a9f807cc823ae11c
parent6eedfe77c15fa95b421b413bb6b09f405e212196 (diff)
downloadbcm5719-llvm-d40bca431dfab7deae906fae9eea72dbdd81de2c.tar.gz
bcm5719-llvm-d40bca431dfab7deae906fae9eea72dbdd81de2c.zip
clang-format: [JS] ASI after imports
Summary: Automatic semicolon insertion should break import and export statements: Before, this would format on one line: // Note: no semi after 'x' below! import {x} from 'x' export function foo() {} Into: import {x} from 'x' export function foo() {} With this change, the statements get separated. This also improves automatic semicolon insertion to consider closing braces preceding declarations and statements. Reviewers: klimek Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D28465 llvm-svn: 291428
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp18
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp18
2 files changed, 31 insertions, 5 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 370cf7afa33..8fc3b78aee0 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -737,7 +737,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
return;
}
if (Next->is(tok::exclaim) && PreviousMustBeValue)
- addUnwrappedLine();
+ return addUnwrappedLine();
bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
bool NextEndsTemplateExpr =
Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
@@ -745,9 +745,10 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
(PreviousMustBeValue ||
Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
tok::minusminus)))
- addUnwrappedLine();
- if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
- addUnwrappedLine();
+ return addUnwrappedLine();
+ if ((PreviousMustBeValue || Previous->is(tok::r_brace)) &&
+ isJSDeclOrStmt(Keywords, Next))
+ return addUnwrappedLine();
}
void UnwrappedLineParser::parseStructuralElement() {
@@ -1974,7 +1975,14 @@ void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
!FormatTok->isStringLiteral())
return;
- while (!eof() && FormatTok->isNot(tok::semi)) {
+ while (!eof()) {
+ if (FormatTok->is(tok::semi))
+ return;
+ if (Line->Tokens.size() == 0) {
+ // Common issue: Automatic Semicolon Insertion wrapped the line, so the
+ // import statement should terminate.
+ return;
+ }
if (FormatTok->is(tok::l_brace)) {
FormatTok->BlockKind = BK_Block;
parseBracedList();
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 59f4a4f6dcf..12554a1dc23 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -858,6 +858,24 @@ TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
"return 1",
"a = null\n"
" return 1");
+ verifyFormat(
+ "x = {a: 1}\n"
+ "class Y {}",
+ " x = {a : 1}\n"
+ " class Y { }");
+}
+
+TEST_F(FormatTestJS, ImportExportASI) {
+ verifyFormat(
+ "import {x} from 'y'\n"
+ "export function z() {}",
+ "import {x} from 'y'\n"
+ " export function z() {}");
+ verifyFormat(
+ "export {x}\n"
+ "class Y {}",
+ " export {x}\n"
+ " class Y {\n}");
}
TEST_F(FormatTestJS, ClosureStyleCasts) {
OpenPOWER on IntegriCloud