From 05c5fd5509754ccae36ef5ce5be6f2989f529634 Mon Sep 17 00:00:00 2001 From: AndreaWalchshoferSCCH Date: Fri, 2 Feb 2024 18:41:53 +0100 Subject: [PATCH] Add additional namestyle configuration for const variables --- .../Config/LuaDiagnosticStyle.h | 4 +++ .../Diagnostic/NameStyle/NameDefineType.h | 1 + .../Diagnostic/NameStyle/NameStyleChecker.cpp | 27 ++++++++++++++++++- docs/name_style.md | 1 + docs/name_style_EN.md | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CodeFormatCore/include/CodeFormatCore/Config/LuaDiagnosticStyle.h b/CodeFormatCore/include/CodeFormatCore/Config/LuaDiagnosticStyle.h index b4b5838b..16cf8534 100644 --- a/CodeFormatCore/include/CodeFormatCore/Config/LuaDiagnosticStyle.h +++ b/CodeFormatCore/include/CodeFormatCore/Config/LuaDiagnosticStyle.h @@ -44,4 +44,8 @@ class LuaDiagnosticStyle { std::vector class_name_style = { NameStyleRule(NameStyleType::SnakeCase), NameStyleRule(NameStyleType::PascalCase)}; + + std::vector const_variable_name_style = { + NameStyleRule(NameStyleType::SnakeCase), + NameStyleRule(NameStyleType::UpperSnakeCase)}; }; diff --git a/CodeFormatCore/include/CodeFormatCore/Diagnostic/NameStyle/NameDefineType.h b/CodeFormatCore/include/CodeFormatCore/Diagnostic/NameStyle/NameDefineType.h index 8f10c5cb..b0489883 100644 --- a/CodeFormatCore/include/CodeFormatCore/Diagnostic/NameStyle/NameDefineType.h +++ b/CodeFormatCore/include/CodeFormatCore/Diagnostic/NameStyle/NameDefineType.h @@ -10,6 +10,7 @@ enum class NameDefineType { ImportModuleName, ModuleDefineName, TableFieldDefineName, + ConstVariableName }; struct NameStyleInfo { diff --git a/CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp b/CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp index 08150cb9..e81d35d9 100644 --- a/CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp +++ b/CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp @@ -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 + 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); + } } } @@ -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; + } } } } diff --git a/docs/name_style.md b/docs/name_style.md index cc938e39..8ef41898 100644 --- a/docs/name_style.md +++ b/docs/name_style.md @@ -30,6 +30,7 @@ * module_name_style * require_module_name_style * class_name_style +* const_variable_name_style 每一个可配置项的格式都是相同的, 每个可配置项可配置的值支持如下格式: * 单字符串 例如: diff --git a/docs/name_style_EN.md b/docs/name_style_EN.md index d65ee5a3..977290ad 100644 --- a/docs/name_style_EN.md +++ b/docs/name_style_EN.md @@ -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: