33
33
34
34
import javax .annotation .Nullable ;
35
35
import java .util .Collection ;
36
+ import java .util .List ;
36
37
import java .util .Objects ;
37
38
import java .util .Optional ;
38
39
import java .util .regex .Pattern ;
@@ -54,10 +55,13 @@ public class UsageWriteLogEventDiagnostic extends AbstractVisitorDiagnostic {
54
55
private static final Pattern PATTERN_DETAIL_ERROR_DESCRIPTION = CaseInsensitivePattern .compile (
55
56
"подробноепредставлениеошибки|detailerrordescription"
56
57
);
58
+ private static final Pattern PATTERN_BRIEF_ERROR_DESCRIPTION = CaseInsensitivePattern .compile (
59
+ "краткоепредставлениеошибки|brieferrordescription"
60
+ );
57
61
private static final Pattern PATTERN_ERROR_INFO = CaseInsensitivePattern .compile (
58
62
"информацияобошибке|errorinfo"
59
63
);
60
- private static final Pattern PATTERN_BRIEF_ERROR_DESCRIPTION = CaseInsensitivePattern .compile (
64
+ private static final Pattern PATTERN_SIMPLE_ERROR_DESCRIPTION = CaseInsensitivePattern .compile (
61
65
"описаниеошибки|errordescription"
62
66
);
63
67
private static final Pattern PATTERN_EVENT_LOG_LEVEL = CaseInsensitivePattern .compile (
@@ -68,58 +72,73 @@ public class UsageWriteLogEventDiagnostic extends AbstractVisitorDiagnostic {
68
72
);
69
73
70
74
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" ;
71
78
public static final String WRONG_NUMBER_MESSAGE = "wrongNumberMessage" ;
72
79
public static final String NO_SECOND_PARAMETER = "noSecondParameter" ;
73
80
public static final String NO_COMMENT = "noComment" ;
74
- public static final String NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK = "noErrorLogLevelInsideExceptBlock" ;
75
- public static final String NO_DETAIL_ERROR_DESCRIPTION = "noDetailErrorDescription" ;
76
81
77
82
@ Override
78
- public ParseTree visitGlobalMethodCall (BSLParser .GlobalMethodCallContext ctx ) {
83
+ public ParseTree visitGlobalMethodCall (BSLParser .GlobalMethodCallContext context ) {
79
84
80
- if (!checkMethodName (ctx )) {
81
- return super .defaultResult ( );
85
+ if (!checkMethodName (context )) {
86
+ return super .visitGlobalMethodCall ( context );
82
87
}
83
88
84
- final var callParams = ctx .doCall ().callParamList ().callParam ();
85
- if (callParams .size () < WRITE_LOG_EVENT_METHOD_PARAMS_COUNT ) {
86
- fireIssue (ctx , WRONG_NUMBER_MESSAGE );
87
- return super .defaultResult ();
88
- }
89
+ checkParams (context );
89
90
90
- final BSLParser .CallParamContext secondParamCtx = callParams .get (1 );
91
- if (secondParamCtx .getChildCount () == 0 ) {
92
- fireIssue (ctx , NO_SECOND_PARAMETER );
93
- return super .defaultResult ();
94
- }
91
+ return super .defaultResult ();
92
+ }
95
93
96
- final BSLParser .CallParamContext commentCtx = callParams . get ( 4 );
97
- if ( commentCtx . getChildCount () == 0 ) {
98
- fireIssue ( ctx , NO_COMMENT );
99
- return super . defaultResult () ;
94
+ private void checkParams ( BSLParser .GlobalMethodCallContext context ) {
95
+ final var callParams = context . doCall (). callParamList (). callParam ();
96
+ if (! checkFirstParams ( context , callParams )){
97
+ return ;
100
98
}
101
99
102
- if (isInsideExceptBlock (ctx )){
103
- if (!hasErrorLogLevel (secondParamCtx )) {
104
- fireIssue (ctx , NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK );
105
- return super .defaultResult ();
100
+ if (isInsideExceptBlock (context )) {
101
+
102
+ final var logLevelCtx = callParams .get (1 );
103
+ if (!hasErrorLogLevel (logLevelCtx )) {
104
+ fireIssue (context , NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK );
105
+ return ;
106
106
}
107
107
108
+ final var commentCtx = callParams .get (COMMENTS_PARAM_INDEX );
108
109
if (!isCommentCorrect (commentCtx )) {
109
- fireIssue (ctx , NO_DETAIL_ERROR_DESCRIPTION );
110
+ fireIssue (context , NO_DETAIL_ERROR_DESCRIPTION );
110
111
}
111
112
}
113
+ }
112
114
113
- return super .defaultResult ();
115
+ private boolean checkFirstParams (BSLParser .GlobalMethodCallContext context , List <? extends BSLParser .CallParamContext > callParams ) {
116
+ if (callParams .size () < WRITE_LOG_EVENT_METHOD_PARAMS_COUNT ) {
117
+ fireIssue (context , WRONG_NUMBER_MESSAGE );
118
+ return false ;
119
+ }
120
+
121
+ final BSLParser .CallParamContext secondParamCtx = callParams .get (1 );
122
+ if (secondParamCtx .getChildCount () == 0 ) {
123
+ fireIssue (context , NO_SECOND_PARAMETER );
124
+ return false ;
125
+ }
126
+
127
+ final BSLParser .CallParamContext commentCtx = callParams .get (COMMENTS_PARAM_INDEX );
128
+ if (commentCtx .getChildCount () == 0 ) {
129
+ fireIssue (context , NO_COMMENT );
130
+ return false ;
131
+ }
132
+ return true ;
114
133
}
115
134
116
- private static boolean checkMethodName (BSLParser .GlobalMethodCallContext ctx ) {
117
- return WRITELOGEVENT .matcher (ctx .methodName ().getText ()).matches ();
135
+ private static boolean checkMethodName (BSLParser .GlobalMethodCallContext context ) {
136
+ return WRITELOGEVENT .matcher (context .methodName ().getText ()).matches ();
118
137
}
119
138
120
- private void fireIssue (BSLParser .GlobalMethodCallContext ctx , String messageKey ) {
139
+ private void fireIssue (BSLParser .GlobalMethodCallContext context , String messageKey ) {
121
140
var diagnosticMessage = info .getResourceString (messageKey );
122
- diagnosticStorage .addDiagnostic (ctx , diagnosticMessage );
141
+ diagnosticStorage .addDiagnostic (context , diagnosticMessage );
123
142
}
124
143
125
144
private static boolean hasErrorLogLevel (BSLParser .CallParamContext callParamContext ) {
@@ -186,7 +205,7 @@ private static boolean isValidExpression(
186
205
if (isErrorDescriptionCallCorrect (assignmentGlobalCalls )) {
187
206
return true ;
188
207
}
189
- if (hasBriefErrorDescription (assignmentGlobalCalls )) {
208
+ if (hasSimpleErrorDescription ( assignmentGlobalCalls ) || hasBriefErrorDescription (assignmentGlobalCalls )) {
190
209
return false ;
191
210
}
192
211
}
@@ -195,44 +214,50 @@ private static boolean isValidExpression(
195
214
196
215
private static boolean isErrorDescriptionCallCorrect (Collection <ParseTree > globalCalls ) {
197
216
return globalCalls .stream ()
198
- .filter (ctx -> ctx instanceof BSLParser .GlobalMethodCallContext )
217
+ .filter (context -> context instanceof BSLParser .GlobalMethodCallContext )
199
218
.map (BSLParser .GlobalMethodCallContext .class ::cast )
200
- .filter (ctx -> isAppropriateName (ctx , PATTERN_DETAIL_ERROR_DESCRIPTION ))
219
+ .filter (context -> isAppropriateName (context , PATTERN_DETAIL_ERROR_DESCRIPTION ))
201
220
.anyMatch (UsageWriteLogEventDiagnostic ::hasFirstDescendantGlobalCall );
202
221
}
203
222
204
223
private static boolean isAppropriateName (
205
- BSLParser .GlobalMethodCallContext ctx ,
224
+ BSLParser .GlobalMethodCallContext context ,
206
225
Pattern patternDetailErrorDescription
207
226
) {
208
- return patternDetailErrorDescription .matcher (ctx .methodName ().getText ()).matches ();
227
+ return patternDetailErrorDescription .matcher (context .methodName ().getText ()).matches ();
209
228
}
210
229
211
230
private static boolean hasFirstDescendantGlobalCall (BSLParser .GlobalMethodCallContext globalCallCtx ) {
212
231
return Trees .findAllRuleNodes (globalCallCtx , BSLParser .RULE_globalMethodCall ).stream ()
213
232
.map (BSLParser .GlobalMethodCallContext .class ::cast )
214
- .anyMatch (ctx -> isAppropriateName (ctx , PATTERN_ERROR_INFO ));
233
+ .anyMatch (context -> isAppropriateName (context , PATTERN_ERROR_INFO ));
234
+ }
235
+
236
+ private static boolean hasSimpleErrorDescription (Collection <ParseTree > globalCalls ) {
237
+ return globalCalls .stream ()
238
+ .filter (context -> context instanceof BSLParser .GlobalMethodCallContext )
239
+ .anyMatch (context -> isAppropriateName ((BSLParser .GlobalMethodCallContext ) context , PATTERN_SIMPLE_ERROR_DESCRIPTION ));
215
240
}
216
241
217
242
private static boolean hasBriefErrorDescription (Collection <ParseTree > globalCalls ) {
218
243
return globalCalls .stream ()
219
- .filter (ctx -> ctx instanceof BSLParser .GlobalMethodCallContext )
220
- .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 ));
221
246
}
222
247
223
- private static boolean isValidExpression (BSLParser .ExpressionContext ctx , BSLParser .CodeBlockContext codeBlock ,
248
+ private static boolean isValidExpression (BSLParser .ExpressionContext context , BSLParser .CodeBlockContext codeBlock ,
224
249
boolean checkPrevAssignment ) {
225
- return ctx .member ().stream ()
250
+ return context .member ().stream ()
226
251
.allMatch (memberContext -> isValidExpression (memberContext , codeBlock , checkPrevAssignment ));
227
252
}
228
253
229
- private static boolean isValidExpression (BSLParser .MemberContext ctx , BSLParser .CodeBlockContext codeBlock ,
254
+ private static boolean isValidExpression (BSLParser .MemberContext context , BSLParser .CodeBlockContext codeBlock ,
230
255
boolean checkPrevAssignment ) {
231
- if (ctx .constValue () != null ) {
256
+ if (context .constValue () != null ) {
232
257
return false ;
233
258
}
234
259
if (checkPrevAssignment ) {
235
- final var complexIdentifier = ctx .complexIdentifier ();
260
+ final var complexIdentifier = context .complexIdentifier ();
236
261
if (complexIdentifier != null ) {
237
262
return isValidVarAssignment (complexIdentifier , codeBlock );
238
263
}
@@ -241,10 +266,10 @@ private static boolean isValidExpression(BSLParser.MemberContext ctx, BSLParser.
241
266
}
242
267
243
268
private static boolean isValidVarAssignment (
244
- BSLParser .ComplexIdentifierContext varContext ,
269
+ BSLParser .ComplexIdentifierContext identifierContext ,
245
270
BSLParser .CodeBlockContext codeBlock
246
271
) {
247
- String varName = varContext .getText ();
272
+ String varName = identifierContext .getText ();
248
273
return getAssignment (varName , codeBlock )
249
274
.map (BSLParser .AssignmentContext ::expression )
250
275
.map (expression -> isValidExpression (codeBlock , expression , false ))
@@ -263,8 +288,8 @@ private static Optional<BSLParser.AssignmentContext> getAssignment(
263
288
.filter (assignmentContext -> assignmentContext .lValue ().getText ().equalsIgnoreCase (varName ));
264
289
}
265
290
266
- private static boolean isInsideExceptBlock (BSLParserRuleContext ctx ) {
267
- return Trees .getRootParent (ctx , BSLParser .RULE_exceptCodeBlock ) != null ;
291
+ private static boolean isInsideExceptBlock (BSLParserRuleContext context ) {
292
+ return Trees .getRootParent (context , BSLParser .RULE_exceptCodeBlock ) != null ;
268
293
}
269
294
270
295
}
0 commit comments