Skip to content

Commit 03527f1

Browse files
committed
Merge branch 'master' of github.com:CppCXY/EmmyLuaCodeStyle
2 parents 76d5678 + c9bbaa4 commit 03527f1

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaDiagnosticStyle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ class LuaDiagnosticStyle {
4444
std::vector<NameStyleRule> class_name_style = {
4545
NameStyleRule(NameStyleType::SnakeCase),
4646
NameStyleRule(NameStyleType::PascalCase)};
47+
48+
std::vector<NameStyleRule> const_variable_name_style = {
49+
NameStyleRule(NameStyleType::SnakeCase),
50+
NameStyleRule(NameStyleType::UpperSnakeCase)};
4751
};

CodeFormatCore/include/CodeFormatCore/Diagnostic/NameStyle/NameDefineType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum class NameDefineType {
1010
ImportModuleName,
1111
ModuleDefineName,
1212
TableFieldDefineName,
13+
ConstVariableName
1314
};
1415

1516
struct NameStyleInfo {

CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,23 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
137137
PushStyleCheck(NameDefineType::ModuleDefineName, name);
138138
break;
139139
}
140-
PushStyleCheck(NameDefineType::LocalVariableName, name);
140+
// check for a separate namestyle if a non-special variable is marked with <const>
141+
bool matchConstRule = false;
142+
auto attribute = name.GetNextSibling(t);
143+
if (attribute.GetSyntaxKind(t) == LuaSyntaxNodeKind::Attribute) {
144+
auto attributes = attribute.GetChildTokens(TK_NAME, t);
145+
for (auto a: attributes) {
146+
if (a.GetText(t) == "const") {
147+
PushStyleCheck(NameDefineType::ConstVariableName, name);
148+
matchConstRule = true;
149+
break;
150+
}
151+
}
152+
}
153+
154+
if (!matchConstRule) {
155+
PushStyleCheck(NameDefineType::LocalVariableName, name);
156+
}
141157
}
142158
}
143159

@@ -434,6 +450,15 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
434450
}
435451
break;
436452
}
453+
case NameDefineType::ConstVariableName: {
454+
if (!matcher.Match(n, t, state.GetDiagnosticStyle().const_variable_name_style)) {
455+
d.PushDiagnostic(DiagnosticType::NameStyle,
456+
n.GetTextRange(t),
457+
MakeDiagnosticInfo("ConstVariableName", n, t,
458+
state.GetDiagnosticStyle().const_variable_name_style));
459+
}
460+
break;
461+
}
437462
}
438463
}
439464
}

docs/name_style.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* module_name_style
3131
* require_module_name_style
3232
* class_name_style
33+
* const_variable_name_style
3334

3435
每一个可配置项的格式都是相同的, 每个可配置项可配置的值支持如下格式:
3536
* 单字符串 例如:

docs/name_style_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The configurable items are:
3030
* module_name_style
3131
* require_module_name_style
3232
* class_name_style
33+
* const_variable_name_style
3334

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

0 commit comments

Comments
 (0)