2929import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticMetadata ;
3030import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticParameterInfo ;
3131import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticSeverity ;
32+ import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticTag ;
3233import com .github ._1c_syntax .bsl .languageserver .diagnostics .metadata .DiagnosticType ;
3334import com .github ._1c_syntax .bsl .sonar .BSLCommunityProperties ;
3435import com .github ._1c_syntax .utils .StringInterner ;
3536import com .google .common .reflect .ClassPath ;
3637import lombok .SneakyThrows ;
3738import lombok .extern .slf4j .Slf4j ;
39+ import org .apache .commons .lang3 .tuple .Pair ;
3840import org .commonmark .ext .autolink .AutolinkExtension ;
3941import org .commonmark .ext .gfm .tables .TablesExtension ;
4042import org .commonmark .ext .heading .anchor .HeadingAnchorExtension ;
4143import org .commonmark .parser .Parser ;
4244import org .commonmark .renderer .html .HtmlRenderer ;
4345import org .sonar .api .config .Configuration ;
46+ import org .sonar .api .issue .impact .Severity ;
47+ import org .sonar .api .issue .impact .SoftwareQuality ;
48+ import org .sonar .api .rules .CleanCodeAttribute ;
4449import org .sonar .api .rules .RuleType ;
4550import org .sonar .api .server .rule .RuleParamType ;
4651import org .sonar .api .server .rule .RulesDefinition ;
4954import javax .annotation .CheckForNull ;
5055import java .util .Arrays ;
5156import java .util .EnumMap ;
57+ import java .util .HashMap ;
5258import java .util .List ;
5359import java .util .Locale ;
5460import java .util .Map ;
@@ -58,10 +64,13 @@ public class BSLLanguageServerRuleDefinition implements RulesDefinition {
5864
5965 public static final String REPOSITORY_KEY = "bsl-language-server" ;
6066 public static final String PARAMETERS_TAG_NAME = "parameters" ;
61- private static final String REPOSITORY_NAME = "BSL Language Server" ;
67+ public static final String REPOSITORY_NAME = "BSL Language Server" ;
6268
63- private static final Map <DiagnosticSeverity , String > SEVERITY_MAP = createDiagnosticSeverityMap ();
64- private static final Map <DiagnosticType , RuleType > RULE_TYPE_MAP = createRuleTypeMap ();
69+ public static final Map <DiagnosticSeverity , String > OLD_SEVERITY_MAP = createOldDiagnosticSeverityMap ();
70+ public static final Map <DiagnosticType , RuleType > RULE_TYPE_MAP = createRuleTypeMap ();
71+ public static final Map <DiagnosticTag , CleanCodeAttribute > CLEAN_CODE_ATTRIBUTE_MAP = createCleanCodeAttributeMap ();
72+
73+ public static final Map <DiagnosticTag , Pair <SoftwareQuality , Severity >> IMPACTS_MAP = createImpactsMap ();
6574
6675 private final Configuration config ;
6776 private final Parser markdownParser ;
@@ -124,7 +133,7 @@ private void setUpNewRule(NewRule newRule) {
124133 .setName (diagnosticInfo .getName ())
125134 .setHtmlDescription (getHtmlDescription (diagnosticInfo .getDescription ()))
126135 .setType (RULE_TYPE_MAP .get (diagnosticInfo .getType ()))
127- .setSeverity (SEVERITY_MAP .get (diagnosticInfo .getSeverity ()))
136+ .setSeverity (OLD_SEVERITY_MAP .get (diagnosticInfo .getSeverity ())) // "старая" серьезность, на всю диагностику
128137 .setActivatedByDefault (diagnosticInfo .isActivatedByDefault ())
129138 ;
130139
@@ -142,6 +151,19 @@ private void setUpNewRule(NewRule newRule) {
142151 newRule .addTags (tagsName );
143152 }
144153
154+ // установим атрибут clean code
155+ // переводим теги в соответствующий атрибут, сортируем и берем первый из списка
156+ // пока так
157+ diagnosticInfo .getTags ().stream ()
158+ .map (CLEAN_CODE_ATTRIBUTE_MAP ::get )
159+ .distinct ()
160+ .sorted ()
161+ .findFirst ()
162+ .ifPresent (newRule ::setCleanCodeAttribute );
163+
164+ // заполним влияние
165+ computeImpact (diagnosticInfo .getTags ()).forEach (newRule ::addDefaultImpact );
166+
145167 if (diagnosticInfo .getExtraMinForComplexity () > 0 ) {
146168 newRule .setDebtRemediationFunction (
147169 newRule .debtRemediationFunctions ().linearWithOffset (
@@ -186,6 +208,18 @@ private Language createDiagnosticLanguage() {
186208 return Language .valueOf (diagnosticLanguageCode .toUpperCase (Locale .ENGLISH ));
187209 }
188210
211+ private static Map <SoftwareQuality , Severity > computeImpact (List <DiagnosticTag > tags ) {
212+ Map <SoftwareQuality , Severity > map = new HashMap <>();
213+ tags .forEach ((DiagnosticTag tag ) -> {
214+ var impact = IMPACTS_MAP .get (tag );
215+ var value = map .get (impact .getLeft ());
216+ if (value == null || impact .getRight ().compareTo (value ) > 0 ) {
217+ map .put (impact .getLeft (), impact .getRight ());
218+ }
219+ });
220+ return map ;
221+ }
222+
189223 @ CheckForNull
190224 private static RuleParamType getRuleParamType (Class <?> type ) {
191225
@@ -206,7 +240,7 @@ private static RuleParamType getRuleParamType(Class<?> type) {
206240 return ruleParamType ;
207241 }
208242
209- private static Map <DiagnosticSeverity , String > createDiagnosticSeverityMap () {
243+ private static Map <DiagnosticSeverity , String > createOldDiagnosticSeverityMap () {
210244 Map <DiagnosticSeverity , String > map = new EnumMap <>(DiagnosticSeverity .class );
211245 map .put (DiagnosticSeverity .INFO , org .sonar .api .rule .Severity .INFO );
212246 map .put (DiagnosticSeverity .MINOR , org .sonar .api .rule .Severity .MINOR );
@@ -227,6 +261,44 @@ private static Map<DiagnosticType, RuleType> createRuleTypeMap() {
227261 return map ;
228262 }
229263
264+ private static Map <DiagnosticTag , CleanCodeAttribute > createCleanCodeAttributeMap () {
265+ Map <DiagnosticTag , CleanCodeAttribute > map = new EnumMap <>(DiagnosticTag .class );
266+ map .put (DiagnosticTag .BADPRACTICE , CleanCodeAttribute .FOCUSED );
267+ map .put (DiagnosticTag .BRAINOVERLOAD , CleanCodeAttribute .CLEAR );
268+ map .put (DiagnosticTag .ERROR , CleanCodeAttribute .LOGICAL );
269+ map .put (DiagnosticTag .CLUMSY , CleanCodeAttribute .CLEAR );
270+ map .put (DiagnosticTag .DEPRECATED , CleanCodeAttribute .MODULAR );
271+ map .put (DiagnosticTag .DESIGN , CleanCodeAttribute .MODULAR );
272+ map .put (DiagnosticTag .LOCALIZE , CleanCodeAttribute .CONVENTIONAL );
273+ map .put (DiagnosticTag .LOCKINOS , CleanCodeAttribute .LOGICAL );
274+ map .put (DiagnosticTag .PERFORMANCE , CleanCodeAttribute .EFFICIENT );
275+ map .put (DiagnosticTag .SQL , CleanCodeAttribute .EFFICIENT );
276+ map .put (DiagnosticTag .STANDARD , CleanCodeAttribute .CONVENTIONAL );
277+ map .put (DiagnosticTag .SUSPICIOUS , CleanCodeAttribute .LOGICAL );
278+ map .put (DiagnosticTag .UNPREDICTABLE , CleanCodeAttribute .LOGICAL );
279+ map .put (DiagnosticTag .UNUSED , CleanCodeAttribute .LOGICAL );
280+ return map ;
281+ }
282+
283+ private static Map <DiagnosticTag , Pair <SoftwareQuality , Severity >> createImpactsMap () {
284+ Map <DiagnosticTag , Pair <SoftwareQuality , Severity >> map = new EnumMap <>(DiagnosticTag .class );
285+ map .put (DiagnosticTag .BADPRACTICE , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .MEDIUM ));
286+ map .put (DiagnosticTag .BRAINOVERLOAD , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .LOW ));
287+ map .put (DiagnosticTag .ERROR , Pair .of (SoftwareQuality .RELIABILITY , Severity .HIGH ));
288+ map .put (DiagnosticTag .CLUMSY , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .LOW ));
289+ map .put (DiagnosticTag .DEPRECATED , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .MEDIUM ));
290+ map .put (DiagnosticTag .DESIGN , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .MEDIUM ));
291+ map .put (DiagnosticTag .LOCALIZE , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .LOW ));
292+ map .put (DiagnosticTag .LOCKINOS , Pair .of (SoftwareQuality .RELIABILITY , Severity .LOW ));
293+ map .put (DiagnosticTag .PERFORMANCE , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .MEDIUM ));
294+ map .put (DiagnosticTag .SQL , Pair .of (SoftwareQuality .RELIABILITY , Severity .HIGH ));
295+ map .put (DiagnosticTag .STANDARD , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .LOW ));
296+ map .put (DiagnosticTag .SUSPICIOUS , Pair .of (SoftwareQuality .RELIABILITY , Severity .LOW ));
297+ map .put (DiagnosticTag .UNPREDICTABLE , Pair .of (SoftwareQuality .RELIABILITY , Severity .MEDIUM ));
298+ map .put (DiagnosticTag .UNUSED , Pair .of (SoftwareQuality .MAINTAINABILITY , Severity .INFO ));
299+ return map ;
300+ }
301+
230302 @ SneakyThrows
231303 @ SuppressWarnings ("unchecked" )
232304 private static List <DiagnosticInfo > getDiagnosticInfo (LanguageServerConfiguration configuration ) {
0 commit comments