Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/java/org/eolang/lints/Defect.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.jcabi.manifests.Manifests;
import java.util.Objects;
import org.eolang.lints.fix.Fix;

/**
* A single defect found.
Expand Down Expand Up @@ -102,6 +103,13 @@ public interface Defect {
*/
boolean experimental();

/**
* Information on how to fix this defect.
*
* @return Fix info.
*/
Fix fix();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marat-Tim to be honest, it is hard to review Fix architecture without unit tests. For now, I don't really understand how it works. So, I suggest to create unit tests, or even test YAML stories (the way we did in LtByXslTest#testsAllLintsByEo and WpaLintsTest#testsAllLintsByEo) to see how it works in practice (EO before fix vs. EO after fix).


/**
* Default implementation of {@link Defect}.
* <p>
Expand Down Expand Up @@ -142,6 +150,11 @@ final class Default implements Defect {
*/
private final boolean experiment;

/**
* How to fix.
*/
private final Fix fix;

/**
* Ctor.
* @param rule Rule name
Expand All @@ -155,7 +168,7 @@ public Default(
final String rule, final Severity severity,
final String object, final int line, final String text
) {
this(rule, severity, object, line, text, false);
this(rule, severity, object, line, text, false, new Fix());
}

/**
Expand All @@ -175,14 +188,15 @@ public Default(
public Default(
final String rule, final Severity severity,
final String object, final int line, final String text,
final boolean exprmnt
final boolean exprmnt, final Fix fix
) {
this.rle = rule;
this.sev = severity;
this.oname = object;
this.lineno = line;
this.txt = text;
this.experiment = exprmnt;
this.fix = fix;
}

@Override
Expand Down Expand Up @@ -237,6 +251,11 @@ public boolean experimental() {
return this.experiment;
}

@Override
public Fix fix() {
return this.fix;
}

@Override
public boolean equals(final Object obj) {
final boolean result;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/eolang/lints/DfContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.eolang.lints;

import org.eolang.lints.fix.Fix;

/**
* Defect with context.
* @since 0.0.40
Expand Down Expand Up @@ -60,6 +62,11 @@ public String version() {
return this.origin.version();
}

@Override
public Fix fix() {
return this.origin.fix();
}

@Override
public String context() {
return this.ctxt.replace("&lt;", "<")
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/eolang/lints/LtByXsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.cactoos.io.ResourceOf;
import org.cactoos.text.IoCheckedText;
import org.cactoos.text.TextOf;
import org.eolang.lints.fix.Edit;
import org.eolang.lints.fix.File;
import org.eolang.lints.fix.Fix;
import org.eolang.parser.ObjectName;

/**
Expand Down Expand Up @@ -98,15 +101,26 @@ public Collection<Defect> defects(final XML xmir) {
String.format("No severity reported by %s", this.rule)
);
}
final Optional<String> message = xml.element("message").text();
defects.add(
new DfContext(
new Defect.Default(
this.rule,
Severity.parsed(sever.get()),
new ObjectName(xmir).get(),
this.lineno(xml),
xml.text().get(),
LtByXsl.experimental(xml)
message.orElseGet(() -> xml.text().get()),
LtByXsl.experimental(xml),
message.map(
ignored ->
new Fix(
new File(
xmir.xpath("/@source/text()").get(0),
new Edit(xml.element("edit"))
)
)
)
.orElseGet(Fix::new)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marat-Tim maybe we should throw an exception in this case? Fix::new only will make things more complicated

),
xml.attribute("context").text().orElse("")
)
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/org/eolang/lints/LtUnlintNonExistingDefect.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import org.cactoos.list.ListOf;
import org.cactoos.text.IoCheckedText;
import org.cactoos.text.TextOf;
import org.eolang.lints.fix.Edit;
import org.eolang.lints.fix.File;
import org.eolang.lints.fix.Fix;
import org.eolang.lints.fix.Position;
import org.eolang.parser.ObjectName;

/**
Expand Down Expand Up @@ -82,18 +86,29 @@ public Collection<Defect> defects(final XML xmir) throws IOException {
)
)
.map(xnav -> xnav.text().get())
.collect(Collectors.toList())
.map(Integer::parseInt)
.forEach(
line ->
defects.add(
new Defect.Default(
this.name(),
Severity.WARNING,
new ObjectName(xmir).get(),
Integer.parseInt(line),
line,
String.format(
"Unlinting rule '%s' doesn't make sense, since there are no defects with it",
unlint
),
false,
new Fix(
new File(
new ObjectSource(xmir).get(),
new Edit(
new Position(line, 0),
new Position(line + 1, 0),
""
)
)
)
)
)
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/eolang/lints/ObjectSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.lints;

import com.github.lombrozo.xnav.Xnav;
import com.jcabi.xml.XML;

final class ObjectSource {
private final Xnav xnav;

ObjectSource(XML xml) {
this.xnav = new Xnav(xml.inner());
}

String get() {
return xnav.attribute("source").text().orElse("");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marat-Tim XMIR does not have @source attribute anymore, you should use org.eolang.parser.ObjectName instead

}
}
3 changes: 2 additions & 1 deletion src/main/java/org/eolang/lints/ScopedDefects.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ final class ScopedDefects extends CollectionEnvelope<Defect> {
defect.object(),
defect.line(),
defect.text(),
defect.experimental()
defect.experimental(),
defect.fix()
),
defect.context()
),
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/eolang/lints/fix/Create.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.lints.fix;

import com.github.lombrozo.xnav.Xnav;

public class Create {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marat-Tim these classes should be package private. We don't want to expose them to the client

private final String path;

private final boolean overwrite;

private final boolean ignoreIfExists;

public Create(String path, boolean overwrite, boolean ignoreIfExists) {
this.path = path;
this.overwrite = overwrite;
this.ignoreIfExists = ignoreIfExists;
}

public Create(Xnav xnav) {
this(
xnav.element("path").text().get(),
Boolean.parseBoolean(xnav.element("overwrite").text().orElse("")),
Boolean.parseBoolean(xnav.element("ignoreIfExists").text().orElse(""))
);
}

public String path() {
return this.path;
}

public boolean overwrite() {
return this.overwrite;
}

public boolean ignoreIfExists() {
return this.ignoreIfExists;
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/eolang/lints/fix/Delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.lints.fix;

import com.github.lombrozo.xnav.Xnav;

public class Delete {
private final String path;

private final boolean recursive;

private final boolean ignoreIfNotExists;

public Delete(String path, boolean recursive, boolean ignoreIfNotExists) {
this.path = path;
this.recursive = recursive;
this.ignoreIfNotExists = ignoreIfNotExists;
}

public Delete(final Xnav xnav) {
this(
xnav.element("path").text().get(),
Boolean.parseBoolean(xnav.element("recursive").text().orElse("")),
Boolean.parseBoolean(xnav.element("ignoreIfNotExists").text().orElse(""))
);
}

public String path() {
return this.path;
}

public boolean recursive() {
return this.recursive;
}

public boolean ignoreIfNotExists() {
return this.ignoreIfNotExists;
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/eolang/lints/fix/Edit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.lints.fix;

import com.github.lombrozo.xnav.Xnav;

public class Edit {
private final Position start;

private final Position end;

private final String newText;

public Edit(final Position start, final Position end, final String newText) {
this.start = start;
this.end = end;
this.newText = newText;
}

public Edit(Xnav xnav) {
this(
new Position(xnav.element("start")),
new Position(xnav.element("end")),
xnav.element("newText").text().get()
);
}

public Position start() {
return this.start;
}

public Position end() {
return this.end;
}

public String newText() {
return this.newText;
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/eolang/lints/fix/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.lints.fix;

import com.github.lombrozo.xnav.Xnav;
import java.util.Arrays;
import java.util.Collection;

public class File {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marat-Tim File is a name of the well-known Java class. Let's rename this class to avoid confusion

private final String path;
private final Collection<Edit> edits;

public File(final String path, final Edit... edits) {
this.path = path;
this.edits = Arrays.asList(edits);
}

public File(final Xnav xnav) {
this(
xnav.attribute("path").text().get(),
xnav.path("edit").map(Edit::new).toArray(Edit[]::new)
);
}

public String path() {
return this.path;
}

public Collection<Edit> edits() {
return this.edits;
}
}
Loading
Loading