Skip to content

Commit e4028f6

Browse files
authored
Merge pull request #177 from AlWoSp/awa/module-local-names
Add namestyle for local variables in the outermost scope `module_local_name_style`
2 parents 864af38 + bc976bc commit e4028f6

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaDiagnosticStyle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ class LuaDiagnosticStyle {
4848
std::vector<NameStyleRule> const_variable_name_style = {
4949
NameStyleRule(NameStyleType::SnakeCase),
5050
NameStyleRule(NameStyleType::UpperSnakeCase)};
51+
52+
std::vector<NameStyleRule> module_local_name_style = {NameStyleRule(NameStyleType::SnakeCase)};
5153
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ enum class NameDefineType {
1010
ImportModuleName,
1111
ModuleDefineName,
1212
TableFieldDefineName,
13-
ConstVariableName
13+
ConstVariableName,
14+
ModuleLocalVariableName
1415
};
1516

1617
struct NameStyleInfo {

CodeFormatCore/src/Config/LuaDiagnosticStyle.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
105105
{module_name_style, "module_name_style" },
106106
{require_module_name_style, "require_module_name_style" },
107107
{class_name_style, "class_name_style" },
108-
{const_variable_name_style, "const_variable_name_style" }
108+
{const_variable_name_style, "const_variable_name_style" },
109+
{module_local_name_style, "module_local_name_style" }
109110
};
110111
for (auto &pair: name_styles) {
111112
if (auto n = root.GetValue(pair.second); !n.IsNull()) {
@@ -119,4 +120,18 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
119120
}
120121
}
121122
}
123+
// module_local_name_style should fallback on local_name_style if not defined
124+
if (root.GetValue("module_local_name_style").IsNull() && !root.GetValue("local_name_style").IsNull()) {
125+
auto moduleLocalNameStyle = std::find_if(name_styles.begin(), name_styles.end(), [&](const std::pair<std::vector<NameStyleRule> &, std::string> &pair) {
126+
return pair.second == "module_local_name_style";
127+
});
128+
auto localNameStyle = std::find_if(name_styles.begin(), name_styles.end(), [&](const std::pair<std::vector<NameStyleRule> &, std::string> &pair) {
129+
return pair.second == "local_name_style";
130+
});
131+
132+
// overwrite the namestyle of module_local_name_style with the namestyle of local_name_style
133+
if (moduleLocalNameStyle != name_styles.end() && localNameStyle != name_styles.end()) {
134+
moduleLocalNameStyle->first = localNameStyle->first;
135+
}
136+
}
122137
}

CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
152152
}
153153

154154
if (!matchConstRule) {
155+
// check for non-special, non-const variables that are in the outermost scope
156+
if (_scopeStack.size() == 1) {
157+
PushStyleCheck(NameDefineType::ModuleLocalVariableName, name);
158+
break;
159+
}
160+
155161
PushStyleCheck(NameDefineType::LocalVariableName, name);
156162
}
157163
}
@@ -459,6 +465,15 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
459465
}
460466
break;
461467
}
468+
case NameDefineType::ModuleLocalVariableName: {
469+
if (!matcher.Match(n, t, state.GetDiagnosticStyle().module_local_name_style)) {
470+
d.PushDiagnostic(DiagnosticType::NameStyle,
471+
n.GetTextRange(t),
472+
MakeDiagnosticInfo("ModuleLocalVariableName", n, t,
473+
state.GetDiagnosticStyle().module_local_name_style));
474+
}
475+
break;
476+
}
462477
}
463478
}
464479
}

docs/name_style.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* require_module_name_style
3232
* class_name_style
3333
* const_variable_name_style
34+
* module_local_name_style (没有额外作用域的变量,如果没有设置默认值,则将其默认值设置为 `local_name_style`)
3435

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

docs/name_style_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The configurable items are:
3131
* require_module_name_style
3232
* class_name_style
3333
* const_variable_name_style
34+
* module_local_name_style (variables without additional scope, setting defaults to the one of `local_name_style` if not set)
3435

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

0 commit comments

Comments
 (0)