Skip to content

Commit 144a1a5

Browse files
committed
取消snake_case和camel_case对下划线前缀的支持, snake_case混合数字
1 parent edf4641 commit 144a1a5

File tree

5 files changed

+35
-79
lines changed

5 files changed

+35
-79
lines changed

CodeService/src/Diagnostic/NameStyle/NameStyleChecker.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ std::set<std::string, std::less<>> NameStyleChecker::TableFieldSpecialName = {
1515
std::set<std::string, std::less<>> NameStyleChecker::GlobalSpecialName = {
1616
"_G", "_ENV"};
1717

18+
std::set<std::string, std::less<>> NameStyleChecker::LocalSpecialName = {
19+
"_", "_ENV"};
20+
21+
1822
NameStyleChecker::NameStyleChecker() {
1923
}
2024

@@ -154,12 +158,17 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
154158
defineExpr = defineExpr.GetChildToken(TK_NAME, t);
155159
} else {
156160
// table field define
157-
auto children = defineExpr.GetChildren(t);
158-
if (!children.empty()) {
159-
defineExpr = children.back().GetChildToken(TK_NAME, t);
161+
auto lastExpr = defineExpr.GetLastChildSyntaxNode(LuaSyntaxMultiKind::Expression, t);
162+
auto lastExprKind = lastExpr.GetSyntaxKind(t);
163+
if (lastExprKind == LuaSyntaxNodeKind::IndexExpression) {
164+
defineExpr = lastExpr.GetChildToken(TK_NAME, t);
160165
}
161166
}
162167

168+
if (defineExpr.IsNull(t)) {
169+
continue;
170+
}
171+
163172
bool matchSpecialRule = false;
164173
if (i < exprs.size()) {
165174
auto expr = exprs[i];
@@ -202,6 +211,7 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
202211
auto nameList = forList.GetChildSyntaxNode(LuaSyntaxNodeKind::NameDefList, t);
203212
for (auto name: nameList.GetChildTokens(TK_NAME, t)) {
204213
RecordLocalVariable(name, t);
214+
PushStyleCheck(NameDefineType::LocalVariableName, name);
205215
}
206216

207217
forBody = forList.GetChildSyntaxNode(LuaSyntaxNodeKind::ForBody, t);
@@ -327,6 +337,9 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
327337
auto n = LuaSyntaxNode(nameStyle.Index);
328338
switch (nameStyle.Type) {
329339
case NameDefineType::LocalVariableName: {
340+
if (LocalSpecialName.count(n.GetText(t))) {
341+
continue;
342+
}
330343
if (!matcher.Match(n, t, state.GetDiagnosticStyle().local_name_style)) {
331344
d.PushDiagnostic(DiagnosticType::NameStyle,
332345
n.GetTextRange(t),
@@ -346,7 +359,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
346359
}
347360
case NameDefineType::LocalFunctionName: {
348361
if (TableFieldSpecialName.count(n.GetText(t))) {
349-
return;
362+
continue;
350363
}
351364
if (!matcher.Match(n, t, state.GetDiagnosticStyle().local_function_name_style)) {
352365
d.PushDiagnostic(DiagnosticType::NameStyle,
@@ -358,7 +371,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
358371
}
359372
case NameDefineType::GlobalVariableDefineName: {
360373
if (GlobalSpecialName.count(n.GetText(t))) {
361-
return;
374+
continue;
362375
}
363376
if (!matcher.Match(n, t, state.GetDiagnosticStyle().global_variable_name_style)) {
364377
d.PushDiagnostic(DiagnosticType::NameStyle,
@@ -397,7 +410,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
397410
}
398411
case NameDefineType::FunctionDefineName: {
399412
if (TableFieldSpecialName.count(n.GetText(t))) {
400-
return;
413+
continue;
401414
}
402415

403416
if (!matcher.Match(n, t, state.GetDiagnosticStyle().function_name_style)) {
@@ -411,7 +424,7 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
411424
case NameDefineType::TableFieldDefineName: {
412425
// 忽略元方法
413426
if (TableFieldSpecialName.count(n.GetText(t))) {
414-
return;
427+
continue;
415428
}
416429
if (!matcher.Match(n, t, state.GetDiagnosticStyle().table_field_name_style)) {
417430
d.PushDiagnostic(DiagnosticType::NameStyle,

CodeService/src/Diagnostic/NameStyle/NameStyleRuleMatcher.cpp

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ bool NameStyleRuleMatcher::Match(LuaSyntaxNode &n, const LuaSyntaxTree &t, const
77
return true;
88
}
99

10+
auto text = n.GetText(t);
11+
if (text.empty()) {
12+
return true;
13+
}
1014
for (auto &rule: rules) {
11-
auto text = n.GetText(t);
1215
switch (rule.Type) {
1316
case NameStyleType::SnakeCase: {
1417
if (SnakeCase(text)) {
@@ -38,7 +41,7 @@ bool NameStyleRuleMatcher::Match(LuaSyntaxNode &n, const LuaSyntaxTree &t, const
3841
auto data = rule.Data;
3942
if (data) {
4043
auto ignoreData = std::dynamic_pointer_cast<IgnoreNameStyleData>(data);
41-
if (ignoreData->Param.count(text)){
44+
if (ignoreData->Param.count(text)) {
4245
return true;
4346
}
4447
}
@@ -65,11 +68,8 @@ bool NameStyleRuleMatcher::Match(LuaSyntaxNode &n, const LuaSyntaxTree &t, const
6568
bool NameStyleRuleMatcher::SnakeCase(std::string_view source) {
6669
enum class ParseState {
6770
None,
68-
PrefixUnderscore,
69-
PrefixUnderscore2,
70-
Underscore,
7171
Letter,
72-
EndDigit
72+
Underscore
7373
} state = ParseState::None;
7474

7575
for (std::size_t index = 0; index != source.size(); index++) {
@@ -80,26 +80,6 @@ bool NameStyleRuleMatcher::SnakeCase(std::string_view source) {
8080

8181
switch (state) {
8282
case ParseState::None: {
83-
if (ch == '_') {
84-
state = ParseState::PrefixUnderscore;
85-
} else if (::islower(ch)) {
86-
state = ParseState::Letter;
87-
} else {
88-
return false;
89-
}
90-
break;
91-
}
92-
case ParseState::PrefixUnderscore: {
93-
if (ch == '_') {
94-
state = ParseState::PrefixUnderscore2;
95-
} else if (::islower(ch)) {
96-
state = ParseState::Letter;
97-
} else {
98-
return false;
99-
}
100-
break;
101-
}
102-
case ParseState::PrefixUnderscore2: {
10383
if (::islower(ch)) {
10484
state = ParseState::Letter;
10585
} else {
@@ -117,23 +97,15 @@ bool NameStyleRuleMatcher::SnakeCase(std::string_view source) {
11797
break;
11898
}
11999
case ParseState::Letter: {
120-
if (::islower(ch)) {
100+
if (::islower(ch) || ::isdigit(ch)) {
121101
// ignore
122102
} else if (ch == '_') {
123103
state = ParseState::Underscore;
124-
} else if (::isdigit(ch)) {
125-
state = ParseState::EndDigit;
126104
} else {
127105
return false;
128106
}
129107
break;
130108
}
131-
case ParseState::EndDigit: {
132-
if (!::isdigit(ch)) {
133-
return false;
134-
}
135-
break;
136-
}
137109
}
138110
}
139111
return true;
@@ -144,7 +116,6 @@ bool NameStyleRuleMatcher::UpperSnakeCase(std::string_view text) {
144116
None,
145117
Underscore,
146118
Letter,
147-
EndDigit
148119
} state = ParseState::None;
149120

150121
auto source = text;
@@ -173,23 +144,15 @@ bool NameStyleRuleMatcher::UpperSnakeCase(std::string_view text) {
173144
break;
174145
}
175146
case ParseState::Letter: {
176-
if (::isupper(ch)) {
147+
if (::isupper(ch) || ::isdigit(ch)) {
177148
// ignore
178149
} else if (ch == '_') {
179150
state = ParseState::Underscore;
180-
} else if (::isdigit(ch)) {
181-
state = ParseState::EndDigit;
182151
} else {
183152
return false;
184153
}
185154
break;
186155
}
187-
case ParseState::EndDigit: {
188-
if (!::isdigit(ch)) {
189-
return false;
190-
}
191-
break;
192-
}
193156
}
194157
}
195158
return true;
@@ -198,8 +161,6 @@ bool NameStyleRuleMatcher::UpperSnakeCase(std::string_view text) {
198161
bool NameStyleRuleMatcher::CamelCase(std::string_view text) {
199162
enum class ParseState {
200163
None,
201-
PrefixUnderscore,
202-
PrefixUnderscore2,
203164
LowerLetter,
204165
UpperLetter
205166
} state = ParseState::None;
@@ -213,26 +174,6 @@ bool NameStyleRuleMatcher::CamelCase(std::string_view text) {
213174

214175
switch (state) {
215176
case ParseState::None: {
216-
if (ch == '_') {
217-
state = ParseState::PrefixUnderscore;
218-
} else if (::islower(ch)) {
219-
state = ParseState::LowerLetter;
220-
} else {
221-
return false;
222-
}
223-
break;
224-
}
225-
case ParseState::PrefixUnderscore: {
226-
if (ch == '_') {
227-
state = ParseState::PrefixUnderscore2;
228-
} else if (::islower(ch)) {
229-
state = ParseState::LowerLetter;
230-
} else {
231-
return false;
232-
}
233-
break;
234-
}
235-
case ParseState::PrefixUnderscore2: {
236177
if (::islower(ch)) {
237178
state = ParseState::LowerLetter;
238179
} else {

LuaParser/src/Ast/LuaSyntaxNode.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#include "Util/Utf8.h"
44

55
LuaSyntaxNode::LuaSyntaxNode(std::size_t index)
6-
: _index(index) {
7-
6+
: _index(index) {
87
}
98

109
TextRange LuaSyntaxNode::GetTextRange(const LuaSyntaxTree &t) const {
1110
if (IsToken(t)) {
1211
return t.GetTokenRange(_index);
12+
} else if (IsNull(t)) {
13+
return TextRange(0, 0);
1314
}
1415

1516
auto start = t.GetStartOffset(_index);
@@ -276,5 +277,3 @@ std::size_t LuaSyntaxNode::CountNodeChild(LuaSyntaxNodeKind kind, const LuaSynta
276277
bool LuaSyntaxNode::IsEmpty(const LuaSyntaxTree &t) const {
277278
return t.GetFirstChild(_index) == 0;
278279
}
279-
280-

Test2/src/FormatTest2.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
int main() {
1010
std::string buffer = R"(
11-
local empty = function () end
11+
--Some comment about the file
1212
13+
14+
tbl[i], tbl[j] = tbl[j], tbl[i]
1315
)";
1416

1517
auto file = std::make_shared<LuaFile>(std::move(buffer));

include/CodeService/Diagnostic/NameStyle/NameStyleChecker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class NameStyleChecker {
1919
private:
2020
static std::set<std::string, std::less<>> TableFieldSpecialName;
2121
static std::set<std::string, std::less<>> GlobalSpecialName;
22+
static std::set<std::string, std::less<>> LocalSpecialName;
2223

2324
void Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t);
2425

0 commit comments

Comments
 (0)