Skip to content

Commit 15ccbc8

Browse files
refactor(#5183): simplify logOne method and extract baseRule helper
1 parent e7d147f commit 15ccbc8

1 file changed

Lines changed: 50 additions & 71 deletions

File tree

eo-maven-plugin/src/main/java/org/eolang/maven/Linting.java

Lines changed: 50 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import org.eolang.lints.Defect;
2626
import org.eolang.lints.Severity;
2727
import org.eolang.lints.Source;
28-
import org.eolang.wpa.Program;
2928
import org.eolang.parser.OnDefault;
3029
import org.eolang.parser.OnDetailed;
30+
import org.eolang.wpa.Program;
3131
import org.w3c.dom.Node;
3232
import org.xembly.Directives;
3333
import org.xembly.Xembler;
@@ -301,7 +301,7 @@ private int lintOne(
301301
).apply(source, target, base.relativize(target));
302302
} else {
303303
new Saved(
304-
this.linted( xmir, unlints).toString(),
304+
this.linted(xmir, unlints).toString(),
305305
target
306306
).value();
307307
}
@@ -313,7 +313,10 @@ private int lintOne(
313313
seen.add(
314314
Linting.format(tojo.identifier(), defect.rule(), defect.line(), defect.text())
315315
);
316-
Linting.logOne(tojo.identifier(), defect);
316+
Linting.logOne(
317+
defect.severity().mnemo(),
318+
Linting.format(tojo.identifier(), defect.rule(), defect.line(), defect.text())
319+
);
317320
}
318321
}
319322
tojo.withLinted(target);
@@ -404,7 +407,12 @@ private List<org.eolang.wpa.Defect> wpa(final Map<String, XML> pkg) {
404407
).applyQuietly(node);
405408
if (Linting.notSuppressed(new Xnav(node), defect)) {
406409
defects.add(defect);
407-
Linting.logOne(defect.object(), defect);
410+
Linting.logOne(
411+
defect.severity().mnemo(),
412+
Linting.format(
413+
defect.object(), defect.rule(), defect.line(), defect.text()
414+
)
415+
);
408416
}
409417
}
410418
);
@@ -418,10 +426,7 @@ private List<org.eolang.wpa.Defect> wpa(final Map<String, XML> pkg) {
418426
* @return XML after linting
419427
* @checkstyle ParameterNumberCheck (40 lines)
420428
*/
421-
private XML linted(
422-
final XML xmir,
423-
final String... unlints
424-
) {
429+
private XML linted(final XML xmir, final String... unlints) {
425430
final Node node = xmir.inner();
426431
final Collection<Defect> defects = Linting.existing(new Xnav(node));
427432
final Collection<Defect> found = new Source(xmir)
@@ -445,66 +450,15 @@ private XML linted(
445450
}
446451

447452
/**
448-
* Log one defect.
449-
* @param object Program identifier
450-
* @param defect The defect to log
451-
*/
452-
private static void logOne(final String object, final Defect defect) {
453-
final StringBuilder message = new StringBuilder()
454-
.append(object)
455-
.append(':').append(defect.line())
456-
.append(' ')
457-
.append(defect.text())
458-
.append(" (")
459-
.append(defect.rule())
460-
.append(')');
461-
switch (defect.severity()) {
462-
case WARNING:
463-
Logger.warn(Linting.class, message.toString());
464-
break;
465-
case ERROR:
466-
case CRITICAL:
467-
Logger.error(Linting.class, message.toString());
468-
break;
469-
default:
470-
throw new IllegalArgumentException(
471-
String.format(
472-
"Not yet supported severity: %s",
473-
defect.severity()
474-
)
475-
);
476-
}
477-
}
478-
479-
/**
480-
* Log one WPA defect.
481-
* @param object Program identifier
482-
* @param defect The WPA defect to log
453+
* Log one defect message.
454+
* @param severity Severity mnemo (e.g. "warning", "error")
455+
* @param message Formatted defect message
483456
*/
484-
private static void logOne(final String object, final org.eolang.wpa.Defect defect) {
485-
final StringBuilder message = new StringBuilder()
486-
.append(object)
487-
.append(':').append(defect.line())
488-
.append(' ')
489-
.append(defect.text())
490-
.append(" (")
491-
.append(defect.rule())
492-
.append(')');
493-
switch (defect.severity()) {
494-
case WARNING:
495-
Logger.warn(Linting.class, message.toString());
496-
break;
497-
case ERROR:
498-
case CRITICAL:
499-
Logger.error(Linting.class, message.toString());
500-
break;
501-
default:
502-
throw new IllegalArgumentException(
503-
String.format(
504-
"Not yet supported severity: %s",
505-
defect.severity()
506-
)
507-
);
457+
private static void logOne(final String severity, final String message) {
458+
if (Severity.WARNING.mnemo().equals(severity)) {
459+
Logger.warn(Linting.class, message);
460+
} else {
461+
Logger.error(Linting.class, message);
508462
}
509463
}
510464

@@ -644,6 +598,15 @@ private static boolean notSuppressed(
644598
).findAny().isEmpty();
645599
}
646600

601+
/**
602+
* Format a defect for display.
603+
* @param object Program or object identifier
604+
* @param rule Rule name
605+
* @param line Line number
606+
* @param text Defect message
607+
* @return Formatted string
608+
* @checkstyle ParameterNumberCheck (5 lines)
609+
*/
647610
private static String format(
648611
final String object, final String rule, final int line, final String text
649612
) {
@@ -676,11 +639,27 @@ private static List<org.eolang.wpa.Defect> read(final Path path) {
676639
* @return TRUE if not suppressed
677640
*/
678641
private static boolean notSuppressed(final Xnav xnav, final Defect defect) {
679-
final String rule = defect.rule();
680-
final int slash = rule.lastIndexOf('/');
681-
final String base = slash >= 0 ? rule.substring(0, slash) : rule;
682642
return xnav.path(
683-
String.format("/object/metas/meta[head='unlint' and tail='%s']", base)
643+
String.format(
644+
"/object/metas/meta[head='unlint' and tail='%s']",
645+
Linting.baseRule(defect.rule())
646+
)
684647
).findAny().isEmpty();
685648
}
649+
650+
/**
651+
* Strip scope suffix (e.g. {@code /S}, {@code /W}) from a scoped rule name.
652+
* @param rule Rule name, possibly scoped
653+
* @return Base rule name without scope suffix
654+
*/
655+
private static String baseRule(final String rule) {
656+
final int slash = rule.lastIndexOf('/');
657+
final String result;
658+
if (slash >= 0) {
659+
result = rule.substring(0, slash);
660+
} else {
661+
result = rule;
662+
}
663+
return result;
664+
}
686665
}

0 commit comments

Comments
 (0)