Skip to content

Commit c51f035

Browse files
committed
исправил замечания из ревью
1 parent 13b7b1e commit c51f035

File tree

2 files changed

+43
-105
lines changed

2 files changed

+43
-105
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UsageWriteLogEventDiagnostic.java

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -72,67 +72,73 @@ public class UsageWriteLogEventDiagnostic extends AbstractVisitorDiagnostic {
7272
);
7373

7474
private static final int WRITE_LOG_EVENT_METHOD_PARAMS_COUNT = 5;
75+
public static final int COMMENTS_PARAM_INDEX = 4;
76+
public static final String NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK = "noErrorLogLevelInsideExceptBlock";
77+
public static final String NO_DETAIL_ERROR_DESCRIPTION = "noDetailErrorDescription";
78+
public static final String WRONG_NUMBER_MESSAGE = "wrongNumberMessage";
79+
public static final String NO_SECOND_PARAMETER = "noSecondParameter";
80+
public static final String NO_COMMENT = "noComment";
7581

7682
@Override
77-
public ParseTree visitGlobalMethodCall(BSLParser.GlobalMethodCallContext ctx) {
83+
public ParseTree visitGlobalMethodCall(BSLParser.GlobalMethodCallContext context) {
7884

79-
if (!checkMethodName(ctx)) {
80-
return super.visitGlobalMethodCall(ctx);
85+
if (!checkMethodName(context)) {
86+
return super.visitGlobalMethodCall(context);
8187
}
8288

83-
checkParams(ctx);
89+
checkParams(context);
8490

85-
return super.visitGlobalMethodCall(ctx);
91+
return super.defaultResult();
8692
}
8793

88-
private void checkParams(BSLParser.GlobalMethodCallContext ctx) {
89-
final var callParams = ctx.doCall().callParamList().callParam();
90-
if (!checkFirstParams(ctx, callParams)){
94+
private void checkParams(BSLParser.GlobalMethodCallContext context) {
95+
final var callParams = context.doCall().callParamList().callParam();
96+
if (!checkFirstParams(context, callParams)){
9197
return;
9298
}
9399

94-
if (isInsideExceptBlock(ctx)) {
100+
if (isInsideExceptBlock(context)) {
95101

96102
final var logLevelCtx = callParams.get(1);
97103
if (!hasErrorLogLevel(logLevelCtx)) {
98-
fireIssue(ctx, "noErrorLogLevelInsideExceptBlock");
104+
fireIssue(context, NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK);
99105
return;
100106
}
101107

102-
final var commentCtx = callParams.get(4);
108+
final var commentCtx = callParams.get(COMMENTS_PARAM_INDEX);
103109
if (!isCommentCorrect(commentCtx)) {
104-
fireIssue(ctx, "noDetailErrorDescription");
110+
fireIssue(context, NO_DETAIL_ERROR_DESCRIPTION);
105111
}
106112
}
107113
}
108114

109-
private boolean checkFirstParams(BSLParser.GlobalMethodCallContext ctx, List<? extends BSLParser.CallParamContext> callParams) {
115+
private boolean checkFirstParams(BSLParser.GlobalMethodCallContext context, List<? extends BSLParser.CallParamContext> callParams) {
110116
if (callParams.size() < WRITE_LOG_EVENT_METHOD_PARAMS_COUNT) {
111-
fireIssue(ctx, "wrongNumberMessage");
117+
fireIssue(context, WRONG_NUMBER_MESSAGE);
112118
return false;
113119
}
114120

115121
final BSLParser.CallParamContext secondParamCtx = callParams.get(1);
116122
if (secondParamCtx.getChildCount() == 0) {
117-
fireIssue(ctx, "noSecondParameter");
123+
fireIssue(context, NO_SECOND_PARAMETER);
118124
return false;
119125
}
120126

121-
final BSLParser.CallParamContext commentCtx = callParams.get(4);
127+
final BSLParser.CallParamContext commentCtx = callParams.get(COMMENTS_PARAM_INDEX);
122128
if (commentCtx.getChildCount() == 0) {
123-
fireIssue(ctx, "noComment");
129+
fireIssue(context, NO_COMMENT);
124130
return false;
125131
}
126132
return true;
127133
}
128134

129-
private static boolean checkMethodName(BSLParser.GlobalMethodCallContext ctx) {
130-
return WRITELOGEVENT.matcher(ctx.methodName().getText()).matches();
135+
private static boolean checkMethodName(BSLParser.GlobalMethodCallContext context) {
136+
return WRITELOGEVENT.matcher(context.methodName().getText()).matches();
131137
}
132138

133-
private void fireIssue(BSLParser.GlobalMethodCallContext ctx, String messageKey) {
139+
private void fireIssue(BSLParser.GlobalMethodCallContext context, String messageKey) {
134140
var diagnosticMessage = info.getResourceString(messageKey);
135-
diagnosticStorage.addDiagnostic(ctx, diagnosticMessage);
141+
diagnosticStorage.addDiagnostic(context, diagnosticMessage);
136142
}
137143

138144
private static boolean hasErrorLogLevel(BSLParser.CallParamContext callParamContext) {
@@ -208,50 +214,50 @@ private static boolean isValidExpression(
208214

209215
private static boolean isErrorDescriptionCallCorrect(Collection<ParseTree> globalCalls) {
210216
return globalCalls.stream()
211-
.filter(ctx -> ctx instanceof BSLParser.GlobalMethodCallContext)
217+
.filter(context -> context instanceof BSLParser.GlobalMethodCallContext)
212218
.map(BSLParser.GlobalMethodCallContext.class::cast)
213-
.filter(ctx -> isAppropriateName(ctx, PATTERN_DETAIL_ERROR_DESCRIPTION))
219+
.filter(context -> isAppropriateName(context, PATTERN_DETAIL_ERROR_DESCRIPTION))
214220
.anyMatch(UsageWriteLogEventDiagnostic::hasFirstDescendantGlobalCall);
215221
}
216222

217223
private static boolean isAppropriateName(
218-
BSLParser.GlobalMethodCallContext ctx,
224+
BSLParser.GlobalMethodCallContext context,
219225
Pattern patternDetailErrorDescription
220226
) {
221-
return patternDetailErrorDescription.matcher(ctx.methodName().getText()).matches();
227+
return patternDetailErrorDescription.matcher(context.methodName().getText()).matches();
222228
}
223229

224230
private static boolean hasFirstDescendantGlobalCall(BSLParser.GlobalMethodCallContext globalCallCtx) {
225231
return Trees.findAllRuleNodes(globalCallCtx, BSLParser.RULE_globalMethodCall).stream()
226232
.map(BSLParser.GlobalMethodCallContext.class::cast)
227-
.anyMatch(ctx -> isAppropriateName(ctx, PATTERN_ERROR_INFO));
233+
.anyMatch(context -> isAppropriateName(context, PATTERN_ERROR_INFO));
228234
}
229235

230236
private static boolean hasSimpleErrorDescription(Collection<ParseTree> globalCalls) {
231237
return globalCalls.stream()
232-
.filter(ctx -> ctx instanceof BSLParser.GlobalMethodCallContext)
233-
.anyMatch(ctx -> isAppropriateName((BSLParser.GlobalMethodCallContext) ctx, PATTERN_SIMPLE_ERROR_DESCRIPTION));
238+
.filter(context -> context instanceof BSLParser.GlobalMethodCallContext)
239+
.anyMatch(context -> isAppropriateName((BSLParser.GlobalMethodCallContext) context, PATTERN_SIMPLE_ERROR_DESCRIPTION));
234240
}
235241

236242
private static boolean hasBriefErrorDescription(Collection<ParseTree> globalCalls) {
237243
return globalCalls.stream()
238-
.filter(ctx -> ctx instanceof BSLParser.GlobalMethodCallContext)
239-
.anyMatch(ctx -> isAppropriateName((BSLParser.GlobalMethodCallContext) ctx, PATTERN_BRIEF_ERROR_DESCRIPTION));
244+
.filter(context -> context instanceof BSLParser.GlobalMethodCallContext)
245+
.anyMatch(context -> isAppropriateName((BSLParser.GlobalMethodCallContext) context, PATTERN_BRIEF_ERROR_DESCRIPTION));
240246
}
241247

242-
private static boolean isValidExpression(BSLParser.ExpressionContext ctx, BSLParser.CodeBlockContext codeBlock,
248+
private static boolean isValidExpression(BSLParser.ExpressionContext context, BSLParser.CodeBlockContext codeBlock,
243249
boolean checkPrevAssignment) {
244-
return ctx.member().stream()
250+
return context.member().stream()
245251
.allMatch(memberContext -> isValidExpression(memberContext, codeBlock, checkPrevAssignment));
246252
}
247253

248-
private static boolean isValidExpression(BSLParser.MemberContext ctx, BSLParser.CodeBlockContext codeBlock,
254+
private static boolean isValidExpression(BSLParser.MemberContext context, BSLParser.CodeBlockContext codeBlock,
249255
boolean checkPrevAssignment) {
250-
if (!isInsideExceptBlock(codeBlock) && ctx.constValue() != null) {
256+
if (!isInsideExceptBlock(codeBlock) && context.constValue() != null) {
251257
return true;
252258
}
253259
if (checkPrevAssignment) {
254-
final var complexIdentifier = ctx.complexIdentifier();
260+
final var complexIdentifier = context.complexIdentifier();
255261
if (complexIdentifier != null) {
256262
return isValidVarAssignment(complexIdentifier, codeBlock);
257263
}
@@ -282,8 +288,8 @@ private static Optional<BSLParser.AssignmentContext> getAssignment(
282288
.filter(assignmentContext -> assignmentContext.lValue().getText().equalsIgnoreCase(varName));
283289
}
284290

285-
private static boolean isInsideExceptBlock(BSLParserRuleContext ctx) {
286-
return Trees.getRootParent(ctx, BSLParser.RULE_exceptCodeBlock) != null;
291+
private static boolean isInsideExceptBlock(BSLParserRuleContext context) {
292+
return Trees.getRootParent(context, BSLParser.RULE_exceptCodeBlock) != null;
287293
}
288294

289295
}

src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UsageWriteLogEventDiagnosticTest.java

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
*/
2222
package com.github._1c_syntax.bsl.languageserver.diagnostics;
2323

24-
import com.github._1c_syntax.bsl.languageserver.util.TestUtils;
2524
import org.eclipse.lsp4j.Diagnostic;
2625
import org.junit.jupiter.api.Test;
2726

@@ -56,72 +55,5 @@ void test() {
5655
.hasRange(212, 6, 214,22)
5756
.hasSize(13)
5857
;
59-
60-
}
61-
62-
@Test
63-
void testBriefDescriptionWithoutFullDesc() {
64-
var sample =
65-
" Попытка\n" +
66-
" // клиентский код, приводящий к вызову исключения\n" +
67-
" СоздатьФайлНаДиске();\n" +
68-
" Исключение\n" +
69-
" //ТекстСообщения = КраткоеПредставлениеОшибки(ИнформацияОбОшибке());\n" +
70-
" ПоказатьПредупреждение(,НСтр(\"ru = 'Операция не может быть выполнена по причине:'\") + Символы.ПС + ТекстСообщения);\n" +
71-
" ЗаписьЖурналаРегистрации(НСтр(\"ru = 'Выполнение операции'\"),\n" +
72-
" УровеньЖурналаРегистрации.Ошибка,,,\n" +
73-
" КраткоеПредставлениеОшибки(ИнформацияОбОшибке()));\n" +
74-
" КонецПопытки;\n";
75-
76-
var documentContext = TestUtils.getDocumentContext(sample);
77-
var diagnostics = getDiagnostics(documentContext);
78-
79-
assertThat(diagnostics).hasSize(1);
80-
}
81-
82-
@Test
83-
void testSimpleWriteLogEventWithoutFullDesc() {
84-
var sample =
85-
"Процедура ОбычнаяЗаписьВЖР(Знач ИмяСобытия, Знач ОписаниеОшибки, Знач Ответ, Знач СсылкаНаДанные = Неопределено) Экспорт\n" +
86-
" ТекстЗаписи = ТекстОтвета(ОписаниеОшибки, Ответ);\n" +
87-
" ЗаписьЖурналаРегистрации(\n" +
88-
" ИмяСобытия,\n" +
89-
" УровеньЖурналаРегистрации.Ошибка,\n" +
90-
" ,\n" +
91-
" СсылкаНаДанные,\n" +
92-
" ТекстЗаписи); // не ошибка\n" +
93-
"КонецПроцедуры";
94-
95-
var documentContext = TestUtils.getDocumentContext(sample);
96-
var diagnostics = getDiagnostics(documentContext);
97-
98-
assertThat(diagnostics).isEmpty();
99-
}
100-
101-
@Test
102-
void testHardLogEventInsideException() {
103-
var sample =
104-
"Процедура СложныйМетодСИспользованиемПодробногоПредставленияВнутриИсключения(Знач ИмяСобытия, Знач ОписаниеОшибки, Знач Ответ, Знач СсылкаНаДанные = Неопределено) Экспорт\n" +
105-
"\t\tПопытка\n" +
106-
"\t\t\tБлокировка.Заблокировать();\n" +
107-
"\t\tИсключение\n" +
108-
" ТекстСообщения = СтроковыеФункцииКлиентСервер.ПодставитьПараметрыВСтроку(\n" +
109-
" НСтр(\"ru = 'Не удалось обработать график работы по причине:\n" +
110-
" |%2'\"), \n" +
111-
" ПодробноеПредставлениеОшибки(ИнформацияОбОшибке()));\n" +
112-
" \n" +
113-
" ЗаписьЖурналаРегистрации(\n" +
114-
" ИмяСобытия,\n" +
115-
" УровеньЖурналаРегистрации.Ошибка,\n" +
116-
" ,\n" +
117-
" СсылкаНаДанные,\n" +
118-
" ТекстСообщения); // ошибка\n" +
119-
"\t\tКонецПопытки;\n" +
120-
"КонецПроцедуры\n";
121-
122-
var documentContext = TestUtils.getDocumentContext(sample);
123-
var diagnostics = getDiagnostics(documentContext);
124-
125-
assertThat(diagnostics).isEmpty();
12658
}
12759
}

0 commit comments

Comments
 (0)