AlignConsecutiveDeclarations not working for 'const' keyword in JavsScript

https://github.com/llvm/llvm-project/issues/49846

Fixes #49846

AlignConsecutiveDeclarations  is not working for "let" and "const" in JavaScript

let letVariable     = 5;
const constVariable = 10;

Reviewed By: owenpan, HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D115990
This commit is contained in:
mydeveloperday 2021-12-21 13:57:43 +00:00
parent 9a05a7b00c
commit 6e28b86cc6
3 changed files with 135 additions and 8 deletions

View File

@ -2669,6 +2669,7 @@ struct FormatStyle {
bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; } bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; }
bool isCSharp() const { return Language == LK_CSharp; } bool isCSharp() const { return Language == LK_CSharp; }
bool isJson() const { return Language == LK_Json; } bool isJson() const { return Language == LK_Json; }
bool isJavaScript() const { return Language == LK_JavaScript; }
/// Language, this format style is targeted at. /// Language, this format style is targeted at.
/// \version 3.5 /// \version 3.5

View File

@ -1826,14 +1826,16 @@ private:
if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof, if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
Keywords.kw_as)) Keywords.kw_as))
return false; return false;
if (Style.Language == FormatStyle::LK_JavaScript && if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
Tok.Previous->is(Keywords.kw_in))
return false; return false;
// Skip "const" as it does not have an influence on whether this is a name. // Skip "const" as it does not have an influence on whether this is a name.
FormatToken *PreviousNotConst = Tok.getPreviousNonComment(); FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
PreviousNotConst = PreviousNotConst->getPreviousNonComment(); // For javascript const can be like "let" or "var"
if (!Style.isJavaScript())
while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
PreviousNotConst = PreviousNotConst->getPreviousNonComment();
if (!PreviousNotConst) if (!PreviousNotConst)
return false; return false;
@ -1852,10 +1854,24 @@ private:
PreviousNotConst->is(TT_TypeDeclarationParen)) PreviousNotConst->is(TT_TypeDeclarationParen))
return true; return true;
return (!IsPPKeyword && // If is a preprocess keyword like #define.
PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) || if (IsPPKeyword)
PreviousNotConst->is(TT_PointerOrReference) || return false;
PreviousNotConst->isSimpleTypeSpecifier();
// int a or auto a.
if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
return true;
// *a or &a or &&a.
if (PreviousNotConst->is(TT_PointerOrReference))
return true;
// MyClass a;
if (PreviousNotConst->isSimpleTypeSpecifier())
return true;
// const a = in JavaScript.
return (Style.isJavaScript() && PreviousNotConst->is(tok::kw_const));
} }
/// Determine whether ')' is ending a cast. /// Determine whether ')' is ending a cast.

View File

@ -2696,5 +2696,115 @@ TEST_F(FormatTestJS, NumericSeparators) {
verifyFormat("x = 1_000_000 + 12;", "x = 1_000_000 + 12;"); verifyFormat("x = 1_000_000 + 12;", "x = 1_000_000 + 12;");
} }
TEST_F(FormatTestJS, AlignConsecutiveDeclarations) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
verifyFormat("let letVariable = 5;\n"
"double constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"const constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"static const constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"static var constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("double letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("const letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("int letVariable = 5;\n"
"int constVariable = 10;",
Style);
}
TEST_F(FormatTestJS, AlignConsecutiveAssignments) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
verifyFormat("let letVariable = 5;\n"
"double constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"const constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"static const constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"static var constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("double letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("const letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("int letVariable = 5;\n"
"int constVariable = 10;",
Style);
}
TEST_F(FormatTestJS, AlignConsecutiveAssignmentsAndDeclarations) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
verifyFormat("let letVariable = 5;\n"
"double constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"const constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"static const constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"static var constVariable = 10;",
Style);
verifyFormat("let letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("double letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("const letVariable = 5;\n"
"var constVariable = 10;",
Style);
verifyFormat("int letVariable = 5;\n"
"int constVariable = 10;",
Style);
}
} // namespace format } // namespace format
} // end namespace clang } // end namespace clang