From a2638dc8d0d511e2735fb1faeb262509d93cb2f0 Mon Sep 17 00:00:00 2001 From: Daniel Jasper <djasper@google.com> Date: Thu, 19 Feb 2015 16:07:32 +0000 Subject: [PATCH] clang-format: [js] Support ES6 module imports. Patch by Martin Probst. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@229863 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/FormatToken.h | 2 ++ lib/Format/TokenAnnotator.cpp | 18 ++++++++++-------- lib/Format/UnwrappedLineParser.cpp | 18 ++++++++++++++++++ lib/Format/UnwrappedLineParser.h | 1 + unittests/Format/FormatTestJS.cpp | 26 ++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 41988bf5908..559f9105bfd 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 b26b0e8ffe4..ced8ff4f4fa 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 395d3d80536..43ed6364879 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 3218afecad3..b4430f76524 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 b60e47083ef..ab4af8090bf 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 -- GitLab