Skip to content

Commit

Permalink
clang-format: [JS] avoid indent after ambient function declarations.
Browse files Browse the repository at this point in the history
Summary:
Before:
  declare function foo();
    let x = 1;

After:
  declare function foo();
  let x = 1;

The problem was that clang-format would unconditionally try to parse a child block, even though ambient function declarations do not have a body (similar to forward declarations).

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 290959
  • Loading branch information
mprobst committed Jan 4, 2017
1 parent 039368e commit af16c50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Expand Up @@ -1255,10 +1255,13 @@ void UnwrappedLineParser::tryToParseJSFunction() {
if (FormatTok->is(tok::l_brace))
tryToParseBracedList();
else
while (FormatTok->isNot(tok::l_brace) && !eof())
while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
nextToken();
}

if (FormatTok->is(tok::semi))
return;

parseChildBlock();
}

Expand Down
10 changes: 10 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -377,6 +377,16 @@ TEST_F(FormatTestJS, AmbientDeclarations) {
"declare function\n"
"x();", // TODO(martinprobst): should ideally be indented.
NineCols);
verifyFormat("declare function foo();\n"
"let x = 1;\n");
verifyFormat("declare function foo(): string;\n"
"let x = 1;\n");
verifyFormat("declare function foo(): {x: number};\n"
"let x = 1;\n");
verifyFormat("declare class X {}\n"
"let x = 1;\n");
verifyFormat("declare interface Y {}\n"
"let x = 1;\n");
verifyFormat(
"declare enum X {\n"
"}",
Expand Down

0 comments on commit af16c50

Please sign in to comment.