r229863 - clang-format: [js] Support ES6 module imports.
Daniel Jasper
djasper at google.com
Thu Feb 19 08:07:32 PST 2015
Author: djasper
Date: Thu Feb 19 10:07:32 2015
New Revision: 229863
URL: http://llvm.org/viewvc/llvm-project?rev=229863&view=rev
Log:
clang-format: [js] Support ES6 module imports.
Patch by Martin Probst.
Modified:
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=229863&r1=229862&r2=229863&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Thu Feb 19 10:07:32 2015
@@ -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.
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=229863&r1=229862&r2=229863&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Feb 19 10:07:32 2015
@@ -1758,6 +1758,9 @@ bool TokenAnnotator::spaceRequiredBefore
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(con
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(con
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;
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=229863&r1=229862&r2=229863&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Feb 19 10:07:32 2015
@@ -739,6 +739,11 @@ void UnwrappedLineParser::parseStructura
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::parseObjCProto
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 << ")"
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=229863&r1=229862&r2=229863&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Thu Feb 19 10:07:32 2015
@@ -103,6 +103,7 @@ private:
void parseObjCUntilAtEnd();
void parseObjCInterfaceOrImplementation();
void parseObjCProtocol();
+ void parseJavaScriptEs6Import();
bool tryToParseLambda();
bool tryToParseLambdaIntroducer();
void tryToParseJSFunction();
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=229863&r1=229862&r2=229863&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu Feb 19 10:07:32 2015
@@ -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
More information about the cfe-commits
mailing list