Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional namestyle configuration for const variables #160

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ class LuaDiagnosticStyle {
std::vector<NameStyleRule> class_name_style = {
NameStyleRule(NameStyleType::SnakeCase),
NameStyleRule(NameStyleType::PascalCase)};

std::vector<NameStyleRule> const_variable_name_style = {
NameStyleRule(NameStyleType::SnakeCase),
NameStyleRule(NameStyleType::UpperSnakeCase)};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class NameDefineType {
ImportModuleName,
ModuleDefineName,
TableFieldDefineName,
ConstVariableName
};

struct NameStyleInfo {
Expand Down
27 changes: 26 additions & 1 deletion CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,23 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
PushStyleCheck(NameDefineType::ModuleDefineName, name);
break;
}
PushStyleCheck(NameDefineType::LocalVariableName, name);
// check for a separate namestyle if a non-special variable is marked with <const>
bool matchConstRule = false;
auto attribute = name.GetNextSibling(t);
if (attribute.GetSyntaxKind(t) == LuaSyntaxNodeKind::Attribute) {
auto attributes = attribute.GetChildTokens(TK_NAME, t);
for (auto a: attributes) {
if (a.GetText(t) == "const") {
PushStyleCheck(NameDefineType::ConstVariableName, name);
matchConstRule = true;
break;
}
}
}

if (!matchConstRule) {
PushStyleCheck(NameDefineType::LocalVariableName, name);
}
}
}

Expand Down Expand Up @@ -434,6 +450,15 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
}
break;
}
case NameDefineType::ConstVariableName: {
if (!matcher.Match(n, t, state.GetDiagnosticStyle().const_variable_name_style)) {
d.PushDiagnostic(DiagnosticType::NameStyle,
n.GetTextRange(t),
MakeDiagnosticInfo("ConstVariableName", n, t,
state.GetDiagnosticStyle().const_variable_name_style));
}
break;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/name_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* module_name_style
* require_module_name_style
* class_name_style
* const_variable_name_style

每一个可配置项的格式都是相同的, 每个可配置项可配置的值支持如下格式:
* 单字符串 例如:
Expand Down
1 change: 1 addition & 0 deletions docs/name_style_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The configurable items are:
* module_name_style
* require_module_name_style
* class_name_style
* const_variable_name_style

The format of each configurable item is the same, and the configurable value of each configurable item supports the following formats:
* Single string Example:
Expand Down
Loading