diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 41988bf5908af49ce94c1aca86567ddc7d716b68..559f9105bfd53d3b59f9ef91f45acbb1daa8bbd0 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -527,6 +527,7 @@ struct AdditionalKeywords { kw_finally = &IdentTable.get("finally"); kw_function = &IdentTable.get("function"); + kw_import = &IdentTable.get("import"); kw_var = &IdentTable.get("var"); kw_abstract = &IdentTable.get("abstract"); @@ -559,6 +560,7 @@ struct AdditionalKeywords { // JavaScript keywords. IdentifierInfo *kw_finally; IdentifierInfo *kw_function; + IdentifierInfo *kw_import; IdentifierInfo *kw_var; // Java keywords. diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index b26b0e8ffe4168972b0118af15ca8a2813e0e6dc..ced8ff4f4facfaa7ea1dac857c57706aa6946f34 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1758,6 +1758,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return true; if (Right.is(TT_JsTypeColon)) return false; + if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && + Line.First->is(Keywords.kw_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 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 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 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 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; diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 395d3d80536faa439a3bb4eeb51c0cf6c5a8bae1..43ed6364879c860fe2f2be2fafc366342846e9ce 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -739,6 +739,11 @@ void UnwrappedLineParser::parseStructuralElement() { parseForOrWhileLoop(); return; } + if (Style.Language == FormatStyle::LK_JavaScript && + FormatTok->is(Keywords.kw_import)) { + parseJavaScriptEs6Import(); + return; + } // In all other cases, parse the declaration. break; default: @@ -1599,6 +1604,19 @@ void UnwrappedLineParser::parseObjCProtocol() { parseObjCUntilAtEnd(); } +void UnwrappedLineParser::parseJavaScriptEs6Import() { + assert(FormatTok->is(Keywords.kw_import)); + nextToken(); + 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 << ")" diff --git a/lib/Format/UnwrappedLineParser.h b/lib/Format/UnwrappedLineParser.h index 3218afecad309e568ae304c92bb5cf2e2fd08ea3..b4430f7652499bf5586f1424ea0c9f2c1fd532aa 100644 --- a/lib/Format/UnwrappedLineParser.h +++ b/lib/Format/UnwrappedLineParser.h @@ -103,6 +103,7 @@ private: void parseObjCUntilAtEnd(); void parseObjCInterfaceOrImplementation(); void parseObjCProtocol(); + void parseJavaScriptEs6Import(); bool tryToParseLambda(); bool tryToParseLambdaIntroducer(); void tryToParseJSFunction(); diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index b60e47083efa786a80580eebf8593f89d5679458..ab4af8090bf901cf8c91d6e8890fa2be48b58efc 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -525,5 +525,31 @@ TEST_F(FormatTestJS, MetadataAnnotations) { "class Y {}"); } +TEST_F(FormatTestJS, Modules) { + verifyFormat("import SomeThing from 'some/module.js';"); + verifyFormat("import {X, Y} from 'some/module.js';"); + 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