r229865 - clang-format: [js] Support ES6 module exports.
Daniel Jasper
djasper at google.com
Thu Feb 19 08:14:18 PST 2015
Author: djasper
Date: Thu Feb 19 10:14:18 2015
New Revision: 229865
URL: http://llvm.org/viewvc/llvm-project?rev=229865&view=rev
Log:
clang-format: [js] Support ES6 module exports.
Patch by Martin Probst, thank you!
Modified:
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/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=229865&r1=229864&r2=229865&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Feb 19 10:14:18 2015
@@ -1759,7 +1759,7 @@ bool TokenAnnotator::spaceRequiredBefore
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->isOneOf(Keywords.kw_import, tok::kw_export))
return false;
} else if (Style.Language == FormatStyle::LK_Java) {
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=229865&r1=229864&r2=229865&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Feb 19 10:14:18 2015
@@ -262,7 +262,7 @@ bool UnwrappedLineParser::parse() {
void UnwrappedLineParser::parseFile() {
ScopedDeclarationState DeclarationState(
*Line, DeclarationScopeStack,
- /*MustBeDeclaration=*/ !Line->InPPDirective);
+ /*MustBeDeclaration=*/!Line->InPPDirective);
parseLevel(/*HasOpeningBrace=*/false);
// Make sure to format the remaining tokens.
flushComments(true);
@@ -734,6 +734,12 @@ void UnwrappedLineParser::parseStructura
}
}
break;
+ case tok::kw_export:
+ if (Style.Language == FormatStyle::LK_JavaScript) {
+ parseJavaScriptEs6ImportExport();
+ return;
+ }
+ break;
case tok::identifier:
if (FormatTok->IsForEachMacro) {
parseForOrWhileLoop();
@@ -741,7 +747,7 @@ void UnwrappedLineParser::parseStructura
}
if (Style.Language == FormatStyle::LK_JavaScript &&
FormatTok->is(Keywords.kw_import)) {
- parseJavaScriptEs6Import();
+ parseJavaScriptEs6ImportExport();
return;
}
// In all other cases, parse the declaration.
@@ -970,7 +976,7 @@ void UnwrappedLineParser::tryToParseJSFu
// Consume function name.
if (FormatTok->is(tok::identifier))
- nextToken();
+ nextToken();
if (FormatTok->isNot(tok::l_paren))
return;
@@ -1604,13 +1610,24 @@ void UnwrappedLineParser::parseObjCProto
parseObjCUntilAtEnd();
}
-void UnwrappedLineParser::parseJavaScriptEs6Import() {
- assert(FormatTok->is(Keywords.kw_import));
+void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
+ assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
nextToken();
+
+ if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
+ Keywords.kw_var))
+ return; // Fall through to parsing the corresponding structure.
+
+ if (FormatTok->is(tok::kw_default)) {
+ nextToken(); // export default ..., fall through after eating 'default'.
+ return;
+ }
+
if (FormatTok->is(tok::l_brace)) {
FormatTok->BlockKind = BK_Block;
parseBracedList();
}
+
while (!eof() && FormatTok->isNot(tok::semi) &&
FormatTok->isNot(tok::l_brace)) {
nextToken();
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=229865&r1=229864&r2=229865&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Thu Feb 19 10:14:18 2015
@@ -103,7 +103,7 @@ private:
void parseObjCUntilAtEnd();
void parseObjCInterfaceOrImplementation();
void parseObjCProtocol();
- void parseJavaScriptEs6Import();
+ void parseJavaScriptEs6ImportExport();
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=229865&r1=229864&r2=229865&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu Feb 19 10:14:18 2015
@@ -546,9 +546,30 @@ TEST_F(FormatTestJS, Modules) {
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
More information about the cfe-commits
mailing list