Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -1759,7 +1759,8 @@ if (Right.is(TT_JsTypeColon)) return false; if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && - Line.First->is(Keywords.kw_import)) + (Line.First->is(Keywords.kw_import) || + Line.First->is(tok::kw_export))) return false; } else if (Style.Language == FormatStyle::LK_Java) { if (Left.is(tok::r_square) && Right.is(tok::l_brace)) Index: lib/Format/UnwrappedLineParser.h =================================================================== --- lib/Format/UnwrappedLineParser.h +++ lib/Format/UnwrappedLineParser.h @@ -103,7 +103,7 @@ void parseObjCUntilAtEnd(); void parseObjCInterfaceOrImplementation(); void parseObjCProtocol(); - void parseJavaScriptEs6Import(); + void parseJavaScriptEs6ImportExport(); bool tryToParseLambda(); bool tryToParseLambdaIntroducer(); void tryToParseJSFunction(); Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -734,14 +734,16 @@ } } break; + case tok::kw_export: case tok::identifier: if (FormatTok->IsForEachMacro) { parseForOrWhileLoop(); return; } if (Style.Language == FormatStyle::LK_JavaScript && - FormatTok->is(Keywords.kw_import)) { - parseJavaScriptEs6Import(); + (FormatTok->is(Keywords.kw_import) || + FormatTok->is(tok::kw_export))) { + parseJavaScriptEs6ImportExport(); return; } // In all other cases, parse the declaration. @@ -1603,11 +1605,23 @@ parseObjCUntilAtEnd(); } -void UnwrappedLineParser::parseJavaScriptEs6Import() { - nextToken(); // 'import' - if (FormatTok->is(tok::l_brace)) { +void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { + nextToken(); // 'import' or 'export' + switch (FormatTok->Tok.getKind()) { + case tok::l_brace: FormatTok->BlockKind = BK_Block; parseBracedList(); + break; + case tok::kw_default: // export default ... + nextToken(); + return; + case tok::kw_const: // export const ... + case tok::kw_class: // export class {...} + return; + default: + if (FormatTok->isOneOf(Keywords.kw_function, Keywords.kw_var)) + return; // export function ... + break; } while (!eof() && FormatTok->isNot(tok::semi) && FormatTok->isNot(tok::l_brace)) { Index: unittests/Format/FormatTestJS.cpp =================================================================== --- unittests/Format/FormatTestJS.cpp +++ unittests/Format/FormatTestJS.cpp @@ -545,9 +545,30 @@ verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); verifyFormat("import * as lib from 'some/module.js';"); verifyFormat("var x = {\n import: 1\n};\nx.import = 2;"); - verifyFormat("export function fn() {\n return 'fn';\n}"); + + verifyFormat("export function fn() {\n" + " return 'fn';\n" + "}"); verifyFormat("export const x = 12;"); verifyFormat("export default class X {}"); + verifyFormat("export {X, Y} from 'some/module.js';"); + verifyFormat("export {\n" + " X,\n" + " Y,\n" + "} from 'some/module.js';"); + verifyFormat("export class C {\n" + " x: number;\n" + " y: string;\n" + "}"); + verifyFormat("export class X { y: number; }"); + verifyFormat("export default class X { y: number }"); + verifyFormat("export default function() {\n return 1;\n}"); + verifyFormat("export var x = 12;"); + verifyFormat("export var x: number = 12;"); + verifyFormat("export const y = {\n" + " a: 1,\n" + " b: 2\n" + "};"); } } // end namespace tooling