Skip to content

Commit

Permalink
clang-format: [JS] revert over-eager ASI check.
Browse files Browse the repository at this point in the history
Summary: Change r291428 introduced ASI detection after closing curly braces. That would generally be correct, however this breaks indentation for structural statements. What happens is that CompoundStatementIndenter increases indentation for the current line, then after reading ASI creates a new line (with the increased line level), and only after the structural parser sees e.g. the if/then/else branch closed, line level is reduced. That leads to the new line started by ASI having a level too high.

Reviewers: djasper

Subscribers: sammccall, cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D28763

llvm-svn: 292099
  • Loading branch information
mprobst committed Jan 16, 2017
1 parent 8586772 commit e6b5b34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
3 changes: 1 addition & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Expand Up @@ -746,8 +746,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
tok::minusminus)))
return addUnwrappedLine();
if ((PreviousMustBeValue || Previous->is(tok::r_brace)) &&
isJSDeclOrStmt(Keywords, Next))
if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
return addUnwrappedLine();
}

Expand Down
26 changes: 22 additions & 4 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -858,13 +858,25 @@ TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
"return 1",
"a = null\n"
" return 1");
// Below "class Y {}" should ideally be on its own line.
verifyFormat(
"x = {\n"
" a: 1\n"
"}\n"
"class Y {}",
"} class Y {}",
" x = {a : 1}\n"
" class Y { }");
verifyFormat(
"if (x) {\n"
"}\n"
"return 1",
"if (x) {}\n"
" return 1");
verifyFormat(
"if (x) {\n"
"}\n"
"class X {}",
"if (x) {}\n"
" class X {}");
}

TEST_F(FormatTestJS, ImportExportASI) {
Expand All @@ -873,11 +885,17 @@ TEST_F(FormatTestJS, ImportExportASI) {
"export function z() {}",
"import {x} from 'y'\n"
" export function z() {}");
// Below "class Y {}" should ideally be on its own line.
verifyFormat(
"export {x}\n"
"class Y {}",
"export {x} class Y {}",
" export {x}\n"
" class Y {\n}");
verifyFormat(
"if (x) {\n"
"}\n"
"export class Y {}",
"if ( x ) { }\n"
" export class Y {}");
}

TEST_F(FormatTestJS, ClosureStyleCasts) {
Expand Down

0 comments on commit e6b5b34

Please sign in to comment.