forked from OSchip/llvm-project
clang-format: [js] Support ES6 module exports.
Patch by Martin Probst, thank you! llvm-svn: 229865
This commit is contained in:
parent
8c97e19124
commit
fca735cd58
|
@ -1759,7 +1759,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
|
|||
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))
|
||||
|
|
|
@ -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::parseStructuralElement() {
|
|||
}
|
||||
}
|
||||
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::parseStructuralElement() {
|
|||
}
|
||||
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::tryToParseJSFunction() {
|
|||
|
||||
// Consume function name.
|
||||
if (FormatTok->is(tok::identifier))
|
||||
nextToken();
|
||||
nextToken();
|
||||
|
||||
if (FormatTok->isNot(tok::l_paren))
|
||||
return;
|
||||
|
@ -1604,13 +1610,24 @@ void UnwrappedLineParser::parseObjCProtocol() {
|
|||
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();
|
||||
|
|
|
@ -103,7 +103,7 @@ private:
|
|||
void parseObjCUntilAtEnd();
|
||||
void parseObjCInterfaceOrImplementation();
|
||||
void parseObjCProtocol();
|
||||
void parseJavaScriptEs6Import();
|
||||
void parseJavaScriptEs6ImportExport();
|
||||
bool tryToParseLambda();
|
||||
bool tryToParseLambdaIntroducer();
|
||||
void tryToParseJSFunction();
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue