Skip to content

Commit

Permalink
clang-format: [JS] recognize exported type definitions.
Browse files Browse the repository at this point in the history
Summary: Support "export type T = {...};", in addition to just "type T = {...};".

Reviewers: klimek

Differential Revision: https://reviews.llvm.org/D33980

llvm-svn: 304904
  • Loading branch information
mprobst committed Jun 7, 2017
1 parent ab0ecc0 commit d96a052
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -135,8 +135,11 @@ class AnnotatingParser {
if (Left->is(TT_OverloadedOperatorLParen)) {
Contexts.back().IsExpression = false;
} else if (Style.Language == FormatStyle::LK_JavaScript &&
Line.startsWith(Keywords.kw_type, tok::identifier)) {
(Line.startsWith(Keywords.kw_type, tok::identifier) ||
Line.startsWith(tok::kw_export, Keywords.kw_type,
tok::identifier))) {
// type X = (...);
// export type X = (...);
Contexts.back().IsExpression = false;
} else if (Left->Previous &&
(Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
Expand Down Expand Up @@ -979,9 +982,12 @@ class AnnotatingParser {
void modifyContext(const FormatToken &Current) {
if (Current.getPrecedence() == prec::Assignment &&
!Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
// Type aliases use `type X = ...;` in TypeScript.
// Type aliases use `type X = ...;` in TypeScript and can be exported
// using `export type ...`.
!(Style.Language == FormatStyle::LK_JavaScript &&
Line.startsWith(Keywords.kw_type, tok::identifier)) &&
(Line.startsWith(Keywords.kw_type, tok::identifier) ||
Line.startsWith(tok::kw_export, Keywords.kw_type,
tok::identifier))) &&
(!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Contexts.back().IsExpression = true;
if (!Line.startsWith(TT_UnaryOperator)) {
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -1226,6 +1226,12 @@ TEST_F(FormatTestJS, UnionIntersectionTypes) {
verifyFormat("let x: Bar|Baz;");
verifyFormat("let x: Bar<X>|Baz;");
verifyFormat("let x: (Foo|Bar)[];");
verifyFormat("type X = {\n"
" a: Foo|Bar;\n"
"};");
verifyFormat("export type X = {\n"
" a: Foo|Bar;\n"
"};");
}

TEST_F(FormatTestJS, ClassDeclarations) {
Expand Down

0 comments on commit d96a052

Please sign in to comment.