Skip to content

Commit 5559b0a

Browse files
committed
更为细致的命名风格检测,施工中...
1 parent 517a27d commit 5559b0a

File tree

9 files changed

+136
-27
lines changed

9 files changed

+136
-27
lines changed

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void LanguageClient::SendNotification(std::string_view method, std::shared_ptr<v
4545

4646
void LanguageClient::CacheFile(std::string_view uri, std::shared_ptr<LuaParser> parser)
4747
{
48+
std::filesystem::path path(uri);
49+
parser->SetFilename(path.filename().string());
4850
_fileMap[std::string(uri)] = parser;
4951
}
5052

CodeService/src/NameStyle/NameStyleChecker.cpp

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "CodeService/NameStyle/NameStyleChecker.h"
22
#include <algorithm>
3+
#include "Util/format.h"
4+
#include "CodeService/LanguageTranslator.h"
35

46
NameStyleChecker::NameStyleChecker(DiagnosisContext& ctx)
57
: _ctx(ctx)
@@ -8,6 +10,21 @@ NameStyleChecker::NameStyleChecker(DiagnosisContext& ctx)
810

911
std::vector<LuaDiagnosisInfo> NameStyleChecker::Analysis()
1012
{
13+
for (auto checkElement : _nameStyleCheckVector)
14+
{
15+
if (checkElement->Type == NameDefineType::ModuleDefineName)
16+
{
17+
auto parser = _ctx.GetParser();
18+
19+
auto filename = parser->GetFilename();
20+
21+
if (checkElement->Node->GetText() != filename)
22+
{
23+
_ctx.PushDiagnosis(format(LText("module name must be {}"), filename),
24+
checkElement->Node->GetTextRange());
25+
}
26+
}
27+
}
1128
// if (ctx.GetOptions().enable_name_style_check)
1229
// {
1330
// switch (_textDefineType)
@@ -95,7 +112,9 @@ void NameStyleChecker::VisitLocalStatement(const std::shared_ptr<LuaAstNode>& lo
95112
auto nameIdentify = nameVector[index];
96113
if (index >= expressionVector.size())
97114
{
98-
_nameStyleCheckVector.push_back({NameDefineType::LocalVariableName, nameIdentify});
115+
auto checkElement = std::make_shared<CheckElement>(NameDefineType::LocalVariableName, nameIdentify);
116+
_nameStyleCheckVector.push_back(checkElement);
117+
RecordLocalVariable(checkElement);
99118
continue;
100119
}
101120
auto expression = expressionVector[index];
@@ -110,20 +129,27 @@ void NameStyleChecker::VisitLocalStatement(const std::shared_ptr<LuaAstNode>& lo
110129
if (methodNameNode->GetText() == "require" || methodNameNode->GetText() == "import")
111130
{
112131
auto callArgsList = callExpression->FindFirstOf(LuaAstNodeType::CallArgList);
113-
_nameStyleCheckVector.push_back({NameDefineType::ImportModuleName, nameIdentify, callArgsList});
132+
133+
auto checkElement = std::make_shared<CheckElement>(
134+
NameDefineType::ImportModuleName, nameIdentify, callArgsList);
135+
_nameStyleCheckVector.push_back(checkElement);
136+
RecordLocalVariable(checkElement);
114137
continue;
115138
}
116139
else if (methodNameNode->GetText() == "Class" || methodNameNode->GetText() == "class")
117140
{
118141
auto callArgsList = callExpression->FindFirstOf(LuaAstNodeType::CallArgList);
119-
_nameStyleCheckVector.push_back({
120-
NameDefineType::ClassVariableName, nameIdentify, callArgsList
121-
});
142+
auto checkElement = std::make_shared<CheckElement>(
143+
NameDefineType::ClassVariableName, nameIdentify, callArgsList);
144+
_nameStyleCheckVector.push_back(checkElement);
145+
RecordLocalVariable(checkElement);
122146
continue;
123147
}
124148
}
125149
}
126-
_nameStyleCheckVector.push_back({NameDefineType::LocalVariableName, nameIdentify});
150+
auto checkElement = std::make_shared<CheckElement>(NameDefineType::LocalVariableName, nameIdentify);
151+
_nameStyleCheckVector.push_back(checkElement);
152+
RecordLocalVariable(checkElement);
127153
}
128154
}
129155
else if (nameDefineList)
@@ -132,7 +158,9 @@ void NameStyleChecker::VisitLocalStatement(const std::shared_ptr<LuaAstNode>& lo
132158
{
133159
if (nameIdentify->GetType() == LuaAstNodeType::Identify)
134160
{
135-
_nameStyleCheckVector.push_back({NameDefineType::LocalVariableName, nameIdentify});
161+
auto checkElement = std::make_shared<CheckElement>(NameDefineType::LocalVariableName, nameIdentify);
162+
_nameStyleCheckVector.push_back(checkElement);
163+
RecordLocalVariable(checkElement);
136164
}
137165
}
138166
}
@@ -144,7 +172,7 @@ void NameStyleChecker::VisitParamList(const std::shared_ptr<LuaAstNode>& paramLi
144172
{
145173
if (param->GetType() == LuaAstNodeType::Identify)
146174
{
147-
_nameStyleCheckVector.push_back({NameDefineType::ParamName, param});
175+
_nameStyleCheckVector.push_back(std::make_shared<CheckElement>(NameDefineType::ParamName, param));
148176
}
149177
}
150178
}
@@ -155,7 +183,7 @@ void NameStyleChecker::VisitLocalFunctionStatement(const std::shared_ptr<LuaAstN
155183
{
156184
if (child->GetType() == LuaAstNodeType::Identify)
157185
{
158-
_nameStyleCheckVector.push_back({NameDefineType::LocalFunctionName, child});
186+
_nameStyleCheckVector.push_back(std::make_shared<CheckElement>(NameDefineType::LocalFunctionName, child));
159187
break;
160188
}
161189
}
@@ -184,7 +212,8 @@ void NameStyleChecker::VisitFunctionStatement(const std::shared_ptr<LuaAstNode>&
184212
if (lastElement && lastElement->GetType() == LuaAstNodeType::Identify
185213
|| lastElement->GetType() == LuaAstNodeType::PrimaryExpression)
186214
{
187-
_nameStyleCheckVector.push_back({NameDefineType::FunctionDefineName, lastElement});
215+
_nameStyleCheckVector.push_back(
216+
std::make_shared<CheckElement>(NameDefineType::FunctionDefineName, lastElement));
188217
}
189218
}
190219
}
@@ -203,7 +232,8 @@ void NameStyleChecker::VisitAssignment(const std::shared_ptr<LuaAstNode>& assign
203232
{
204233
if (IsGlobal(expressionFirstChild))
205234
{
206-
_nameStyleCheckVector.push_back({NameDefineType::GlobalVariableDefineName, expressionFirstChild});
235+
_nameStyleCheckVector.push_back(
236+
std::make_shared<CheckElement>(NameDefineType::GlobalVariableDefineName, expressionFirstChild));
207237
}
208238
}
209239
else if (expressionFirstChild->GetType() == LuaAstNodeType::IndexExpression)
@@ -224,7 +254,8 @@ void NameStyleChecker::VisitAssignment(const std::shared_ptr<LuaAstNode>& assign
224254

225255
if (lastElement && lastElement->GetType() == LuaAstNodeType::Identify)
226256
{
227-
_nameStyleCheckVector.push_back({NameDefineType::FunctionDefineName, lastElement});
257+
_nameStyleCheckVector.push_back(
258+
std::make_shared<CheckElement>(NameDefineType::FunctionDefineName, lastElement));
228259
}
229260
}
230261
}
@@ -237,7 +268,7 @@ void NameStyleChecker::VisitTableField(const std::shared_ptr<LuaAstNode>& tableF
237268

238269
if (identify)
239270
{
240-
_nameStyleCheckVector.push_back({NameDefineType::TableFieldDefineName, identify});
271+
_nameStyleCheckVector.push_back(std::make_shared<CheckElement>(NameDefineType::TableFieldDefineName, identify));
241272
}
242273
}
243274

@@ -249,9 +280,23 @@ void NameStyleChecker::VisitReturnStatement(const std::shared_ptr<LuaAstNode>& r
249280
if (expressionList && !expressionList->GetChildren().empty())
250281
{
251282
auto firstReturnExpression = expressionList->GetChildren().front();
252-
if (firstReturnExpression->GetType() == LuaAstNodeType::PrimaryExpression)
283+
if (firstReturnExpression->GetType() == LuaAstNodeType::Expression)
253284
{
254-
auto block = returnStatement->GetParent();
285+
auto primaryReturnIdentify = firstReturnExpression->FindFirstOf(LuaAstNodeType::PrimaryExpression);
286+
if (primaryReturnIdentify)
287+
{
288+
auto block = returnStatement->GetParent();
289+
290+
if (_scopeMap.count(block))
291+
{
292+
auto& scope = _scopeMap[block];
293+
auto it = scope.LocalVariableMap.find(primaryReturnIdentify->GetText());
294+
if (it != scope.LocalVariableMap.end())
295+
{
296+
it->second->Type = NameDefineType::ModuleDefineName;
297+
}
298+
}
299+
}
255300
}
256301
}
257302
}
@@ -262,8 +307,23 @@ void NameStyleChecker::VisitBlock(const std::shared_ptr<LuaAstNode>& block)
262307
_scopeMap.insert({block, Scope()});
263308
}
264309

265-
void NameStyleChecker::RecordLocalVariable(std::shared_ptr<LuaAstNode> identify)
310+
void NameStyleChecker::RecordLocalVariable(std::shared_ptr<CheckElement> checkElement)
266311
{
312+
auto node = checkElement->Node;
313+
auto parent = node->GetParent();
314+
while (parent)
315+
{
316+
if (parent->GetType() == LuaAstNodeType::Block)
317+
{
318+
if (_scopeMap.count(parent))
319+
{
320+
auto& scope = _scopeMap[parent];
321+
scope.LocalVariableMap.insert({std::string(node->GetText()), checkElement});
322+
}
323+
break;
324+
}
325+
parent = parent->GetParent();
326+
}
267327
}
268328

269329
bool NameStyleChecker::InTopBlock(std::shared_ptr<LuaAstNode> chunkBlockStatement)
@@ -279,6 +339,26 @@ bool NameStyleChecker::InTopBlock(std::shared_ptr<LuaAstNode> chunkBlockStatemen
279339

280340
bool NameStyleChecker::IsGlobal(std::shared_ptr<LuaAstNode> node)
281341
{
342+
auto identifyText = node->GetText();
343+
344+
auto parent = node->GetParent();
345+
while (parent)
346+
{
347+
if (parent->GetType() == LuaAstNodeType::Block)
348+
{
349+
if (_scopeMap.count(parent))
350+
{
351+
auto& scope = _scopeMap[parent];
352+
auto it = scope.LocalVariableMap.find(identifyText);
353+
if (it != scope.LocalVariableMap.end())
354+
{
355+
return false;
356+
}
357+
}
358+
}
359+
parent = parent->GetParent();
360+
}
361+
282362
return true;
283363
}
284364

LuaParser/src/LuaAstNode.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include "LuaParser/LuaAstVisitor.h"
66

77
LuaAstNode::LuaAstNode(LuaAstNodeType type, const char* source)
8-
: _parent(),
9-
_type(type),
8+
: _type(type),
109
_text(source, 0),
1110
_source(source),
1211
_textRange(0, 0)
@@ -170,7 +169,7 @@ void LuaAstNode::AddLeafChild(std::shared_ptr<LuaAstNode> child)
170169

171170
std::shared_ptr<LuaAstNode> LuaAstNode::GetParent()
172171
{
173-
if(!_parent.expired())
172+
if (!_parent.expired())
174173
{
175174
return _parent.lock();
176175
}

LuaParser/src/LuaParser.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ std::shared_ptr<LuaParser> LuaParser::LoadFromFile(std::string_view filename)
2020
{
2121
std::stringstream s;
2222
s << fin.rdbuf();
23-
return LoadFromBuffer(s.str());
23+
auto parser = LoadFromBuffer(s.str());
24+
parser->SetFilename(filePath.filename().string());
25+
return parser;
2426
}
2527

2628
return nullptr;
@@ -94,6 +96,18 @@ bool LuaParser::IsEmptyLine(int line)
9496
return _tokenParser->IsEmptyLine(line);
9597
}
9698

99+
void LuaParser::SetFilename(std::string_view filename)
100+
{
101+
std::filesystem::path path(filename);
102+
103+
_filename = path.replace_extension().string();
104+
}
105+
106+
std::string_view LuaParser::GetFilename()
107+
{
108+
return _filename;
109+
}
110+
97111
void LuaParser::BuildAstWithComment()
98112
{
99113
BuildAst();

include/CodeService/NameStyle/NameStyleChecker.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class NameStyleChecker : public LuaAstVisitor
1919
class Scope
2020
{
2121
public:
22-
22+
std::map<std::string, std::shared_ptr<CheckElement>, std::less<>> LocalVariableMap;
2323
};
2424

2525
NameStyleChecker(DiagnosisContext& ctx);
@@ -36,7 +36,7 @@ class NameStyleChecker : public LuaAstVisitor
3636
void VisitReturnStatement(const std::shared_ptr<LuaAstNode>& node) override;
3737
void VisitBlock(const std::shared_ptr<LuaAstNode>& node) override;
3838
private:
39-
void RecordLocalVariable(std::shared_ptr<LuaAstNode> identify);
39+
void RecordLocalVariable(std::shared_ptr<CheckElement> checkElement);
4040
bool InTopBlock(std::shared_ptr<LuaAstNode> chunkBlockStatement);
4141
bool IsGlobal(std::shared_ptr<LuaAstNode> node);
4242
bool SnakeCase(std::string_view source);
@@ -47,7 +47,7 @@ class NameStyleChecker : public LuaAstVisitor
4747

4848
// block到作用域的映射
4949
std::map<std::shared_ptr<LuaAstNode>, Scope> _scopeMap;
50-
std::vector<CheckElement> _nameStyleCheckVector;
50+
std::vector<std::shared_ptr<CheckElement>> _nameStyleCheckVector;
5151
std::string_view _moduleName;
5252

5353
};

include/LuaParser/LuaAstNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ class LuaAstNode: public std::enable_shared_from_this<LuaAstNode>
4646
void addChildAfter(int index, std::shared_ptr<LuaAstNode> child);
4747
void addChildBefore(int index, std::shared_ptr<LuaAstNode> child);
4848

49-
std::weak_ptr<LuaAstNode> _parent;
5049
LuaAstNodeType _type;
5150
std::string_view _text;
5251

5352
const char* _source;
5453
TextRange _textRange;
5554
std::vector<std::shared_ptr<LuaAstNode>> _children;
5655
std::string _error;
56+
std::weak_ptr<LuaAstNode> _parent;
5757
};

include/LuaParser/LuaParser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class LuaParser
3737

3838
bool IsEmptyLine(int line);
3939

40+
void SetFilename(std::string_view filename);
41+
42+
std::string_view GetFilename();
4043
private:
4144
bool BlockFollow(bool withUntil = false);
4245

@@ -162,4 +165,6 @@ class LuaParser
162165
std::shared_ptr<LuaAstNode> _chunkAstNode;
163166

164167
std::vector<LuaError> _errors;
168+
169+
std::string _filename;
165170
};

include/LuaParser/LuaTokenParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LuaTokenParser
4242
std::vector<LuaError>& GetErrors();
4343

4444
bool HasError() const;
45+
4546
private:
4647
static std::map<std::string, LuaTokenType, std::less<>> LuaReserved;
4748

lua.template.editorconfig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ insert_final_newline = true
4040

4141

4242
# the following is name style check rule
43-
# optional off/camel_case/snake_case/pascal_case
43+
# base option off/camel_case/snake_case/pascal_case
4444
enable_name_style_check = false
4545
local_name_define_style = snake_case
46-
function_name_define_style = off
47-
table_field_name_define_style = off
46+
function_name_define_style = snake_case
47+
table_field_name_define_style = snake_case
48+
global_variable_name_define_style = snake_case
49+
# the following has extra option: same:filename/first_param/'<const string>', snake_case/pascal_case/camel_case
50+
# for example:
51+
# same:first_param, snake_case
52+
# same:'m'
53+
module_name_define_style = same:filename, snake_case
54+
require_module_name_sytle = same:first_param, snake_case
55+
class_name_define_style = same:first_param

0 commit comments

Comments
 (0)