Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -1758,6 +1758,9 @@ return true; if (Right.is(TT_JsTypeColon)) return false; + if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && + Line.First->is(tok::identifier) && Line.First->TokenText == "import") + return false; } else if (Style.Language == FormatStyle::LK_Java) { if (Left.is(tok::r_square) && Right.is(tok::l_brace)) return true; @@ -1914,6 +1917,13 @@ if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine) return true; + if ((Style.Language == FormatStyle::LK_Java || + Style.Language == FormatStyle::LK_JavaScript) && + Left.is(TT_LeadingJavaAnnotation) && + Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && + Line.Last->is(tok::l_brace)) + return true; + if (Style.Language == FormatStyle::LK_JavaScript) { // FIXME: This might apply to other languages and token kinds. if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && @@ -1922,15 +1932,7 @@ if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Left.NestingLevel == 0) return true; - if (Left.is(TT_LeadingJavaAnnotation) && - Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && - Line.Last->is(tok::l_brace)) - return true; } else if (Style.Language == FormatStyle::LK_Java) { - if (Left.is(TT_LeadingJavaAnnotation) && - Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && - Line.Last->is(tok::l_brace)) - return true; if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && Right.Next->is(tok::string_literal)) return true; Index: lib/Format/UnwrappedLineParser.h =================================================================== --- lib/Format/UnwrappedLineParser.h +++ lib/Format/UnwrappedLineParser.h @@ -103,6 +103,7 @@ void parseObjCUntilAtEnd(); void parseObjCInterfaceOrImplementation(); void parseObjCProtocol(); + void parseJavaScriptEs6Import(); bool tryToParseLambda(); bool tryToParseLambdaIntroducer(); void tryToParseJSFunction(); Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -739,6 +739,11 @@ parseForOrWhileLoop(); return; } + if (Style.Language == FormatStyle::LK_JavaScript && + FormatTok->TokenText == "import") { + parseJavaScriptEs6Import(); + return; + } // In all other cases, parse the declaration. break; default: @@ -1598,6 +1603,18 @@ parseObjCUntilAtEnd(); } +void UnwrappedLineParser::parseJavaScriptEs6Import() { + nextToken(); // 'import' + if (FormatTok->is(tok::l_brace)) { + FormatTok->BlockKind = BK_Block; + parseBracedList(); + } + while (!eof() && FormatTok->isNot(tok::semi) && + FormatTok->isNot(tok::l_brace)) { + nextToken(); + } +} + LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, StringRef Prefix = "") { llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" Index: unittests/Format/FormatTestJS.cpp =================================================================== --- unittests/Format/FormatTestJS.cpp +++ unittests/Format/FormatTestJS.cpp @@ -523,5 +523,32 @@ "}"); } +TEST_F(FormatTestJS, Modules) { + verifyFormat("import SomeThing from 'some/module.js';"); + verifyFormat("import {X, Y} from 'some/module.js';"); + // TODO(martinprobst): +2 indent only, and handle lists w/o trailing comma. + verifyFormat("import {\n" + " VeryLongImportsAreAnnoying,\n" + " VeryLongImportsAreAnnoying,\n" + " VeryLongImportsAreAnnoying,\n" + " VeryLongImportsAreAnnoying\n" + "} from 'some/module.js';"); + verifyFormat("import {\n" + " X,\n" + " Y,\n" + "} from 'some/module.js';"); + verifyFormat("import {\n" + " X,\n" + " Y,\n" + "} from 'some/long/module.js';", + getGoogleJSStyleWithColumns(20)); + 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 const x = 12;"); + verifyFormat("export default class X {}"); +} + } // end namespace tooling } // end namespace clang