From 668c7bb34fba27525682888eb71fc1cdabc0ab96 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 11 May 2015 09:03:10 +0000 Subject: [PATCH] clang-format: [JS] Parse exported functions as free-standing. Before: export function foo() {} export function bar() {} After: export function foo() { } export function bar() { } llvm-svn: 236978 --- clang/lib/Format/UnwrappedLineParser.cpp | 12 ++++++++++-- clang/unittests/Format/FormatTestJS.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index a85d9c773166..437d688dc57b 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1666,8 +1666,16 @@ void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export)); nextToken(); - if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function, - Keywords.kw_var)) + // Consume "function" and "default function", so that these get parsed as + // free-standing JS functions, i.e. do not require a trailing semicolon. + if (FormatTok->is(tok::kw_default)) + nextToken(); + if (FormatTok->is(Keywords.kw_function)) { + nextToken(); + return; + } + + if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_var)) return; // Fall through to parsing the corresponding structure. if (FormatTok->is(tok::kw_default)) { diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 7494bccea5ee..41ffbedea74e 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -600,6 +600,12 @@ TEST_F(FormatTestJS, Modules) { verifyFormat("export function fn() {\n" " return 'fn';\n" "}"); + verifyFormat("export function A() {\n" + "}\n" + "export default function B() {\n" + "}\n" + "export function C() {\n" + "}"); verifyFormat("export const x = 12;"); verifyFormat("export default class X {}"); verifyFormat("export {X, Y} from 'some/module.js';");