Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#322): unlint-non-existing-defect lint #339

Merged
merged 3 commits into from
Feb 20, 2025
Merged
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
135 changes: 135 additions & 0 deletions src/main/java/org/eolang/lints/LtUnlintNonExistingDefect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2025 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.lints;

import com.github.lombrozo.xnav.Xnav;
import com.jcabi.xml.XML;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Set;
import java.util.stream.Collectors;
import org.cactoos.io.ResourceOf;
import org.cactoos.list.ListOf;
import org.cactoos.text.IoCheckedText;
import org.cactoos.text.TextOf;

/**
* Lints.
*
* @since 0.0.40
*/
final class LtUnlintNonExistingDefect implements Lint<XML> {

/**
* Lints.
*/
private final Iterable<Lint<XML>> lints;

/**
* Ctor.
*
* @param lnts Lints
*/
LtUnlintNonExistingDefect(final Iterable<Lint<XML>> lnts) {
this.lints = lnts;
}

@Override
public String name() {
return "unlint-non-existing-defect";
}

@Override
public Collection<Defect> defects(final XML xmir) throws IOException {
final Collection<Defect> defects = new LinkedList<>();
final Collection<String> present = this.existingDefects(xmir);
final Xnav xml = new Xnav(xmir.inner());
final Set<String> unlints = xml.path("/program/metas/meta[head='unlint']/tail")
.map(xnav -> xnav.text().get())
.collect(Collectors.toSet());
unlints.stream()
.filter(unlint -> !present.contains(unlint))
.forEach(
unlint ->
xml.path(
String.format(
"program/metas/meta[head='unlint' and tail='%s']/@line", unlint
Copy link
Member

Choose a reason for hiding this comment

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

@h1alexbel let's try to use Xnav here instead of XPath

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@maxonfjvipon actually we are using Xnav here:

final Xnav xml = new Xnav(xmir.inner());
xml.path(..);

Or you meant we should use xml.element() instead?

Copy link
Member

Choose a reason for hiding this comment

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

@h1alexbel sorry, didn't notice it. Then it's okay

)
)
.map(xnav -> xnav.text().get())
.collect(Collectors.toList())
.forEach(
line ->
defects.add(
new Defect.Default(
this.name(),
Severity.WARNING,
xml.element("program")
.attribute("name")
.text()
.orElse("unknown"),
Integer.parseInt(line),
String.format(
"Unlinting rule '%s' doesn't make sense, since there are no defects with it",
unlint
)
)
)
)
);
return defects;
}

@Override
public String motive() throws IOException {
return new IoCheckedText(
new TextOf(
new ResourceOf(
String.format(
"org/eolang/motives/misc/%s.md", this.name()
)
)
)
).asString();
}

private Collection<String> existingDefects(final XML xmir) {
final Collection<String> existing = new ListOf<>();
this.lints.forEach(
lint -> {
try {
existing.addAll(
lint.defects(xmir).stream()
.map(Defect::rule)
.collect(Collectors.toList())
);
} catch (final IOException exception) {
throw new IllegalStateException(exception);
}
}
);
return existing;
}
}
24 changes: 14 additions & 10 deletions src/main/java/org/eolang/lints/PkMono.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ final class PkMono extends IterableEnvelope<Lint<XML>> {
* All XML-based lints.
*/
private static final Iterable<Lint<XML>> LINTS = new Shuffled<>(
new Mapped<Lint<XML>>(
LtUnlint::new,
new Joined<Lint<XML>>(
new PkByXsl(),
List.of(
new LtAsciiOnly()
)
new Joined<Lint<XML>>(
new PkByXsl(),
List.of(
new LtAsciiOnly()
)
)
);
Expand All @@ -68,14 +65,21 @@ final class PkMono extends IterableEnvelope<Lint<XML>> {
PkMono() {
super(
new Joined<Lint<XML>>(
PkMono.LINTS,
new Mapped<Lint<XML>>(
LtUnlint::new,
new Joined<Lint<XML>>(
PkMono.LINTS,
List.of(new LtUnlintNonExistingDefect(PkMono.LINTS))
)
),
List.of(
new LtIncorrectUnlint(
new Mapped<>(
Lint::name,
new Joined<Lint<?>>(
new Joined<>(
new PkWpa(),
PkMono.LINTS
PkMono.LINTS,
List.of(new LtUnlintNonExistingDefect(PkMono.LINTS))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `+unlint` Of Non-Existing Defect

Special `+unlint` meta should be used only to suppress an existing defects.

Incorrect (since there is no duplicate metas):

```eo
+unlint duplicate-metas

[] > foo
42 > @
```

Correct:

```eo
+unlint duplicate-metas
+architect jeff
+architect foo

[] > foo
42 > @
```
126 changes: 126 additions & 0 deletions src/test/java/org/eolang/lints/LtUnlintNonExistingDefectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2025 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.lints;

import java.io.IOException;
import java.util.stream.Collectors;
import org.cactoos.list.ListOf;
import org.eolang.parser.EoSyntax;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link LtUnlintNonExistingDefect}.
*
* @since 0.0.40
*/
final class LtUnlintNonExistingDefectTest {

@Test
void reportsDefects() throws IOException {
MatcherAssert.assertThat(
"Defects are empty, but they should not",
new LtUnlintNonExistingDefect(
new ListOf<>(new LtAsciiOnly())
).defects(
new EoSyntax(
String.join(
"\n",
"+unlint ascii-only",
"# first",
"# second",
"[] > bar"
)
).parsed()
),
Matchers.hasSize(Matchers.greaterThan(0))
);
}

@Test
void reportsDefectsForEachUnlintWithDifferentLines() throws IOException {
MatcherAssert.assertThat(
"Defects should be reported for each line with unlint, but it's not",
new LtUnlintNonExistingDefect(
new ListOf<>(new LtAsciiOnly())
).defects(
new EoSyntax(
String.join(
"\n",
"+unlint ascii-only",
"+unlint ascii-only",
"# first",
"# second",
"[] > bar"
)
).parsed()
).stream()
.map(Defect::line)
.collect(Collectors.toList()),
Matchers.equalTo(
new ListOf<>(1, 2)
)
);
}

@Test
void allowsUnlintingOfExistingDefects() throws IOException {
MatcherAssert.assertThat(
"Defects are not empty, but they should",
new LtUnlintNonExistingDefect(
new ListOf<>(new LtAsciiOnly())
).defects(
new EoSyntax(
String.join(
"\n",
"+unlint ascii-only",
"# 程式分析是我的熱愛",
"[] > bar"
)
).parsed()
),
Matchers.emptyIterable()
);
}

@Test
void allowsNoUnlints() throws IOException {
MatcherAssert.assertThat(
"Defects are not empty, but they should",
new LtUnlintNonExistingDefect(
new ListOf<>(new LtAsciiOnly())
).defects(
new EoSyntax(
String.join(
"\n",
"# тук-тук",
"[] > bar"
)
).parsed()
),
Matchers.emptyIterable()
);
}
}
1 change: 1 addition & 0 deletions src/test/java/org/eolang/lints/ProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void suppressesManyLints() throws IOException {
new InputOf(
String.join(
"\n",
"+unlint unlint-non-existing-defect",
"+unlint object-does-not-match-filename",
"+unlint empty-object",
"+unlint mandatory-home",
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/org/eolang/lints/canonical.eo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
+architect yegor256@gmail.com
+home https://www.eolang.org
+package canonical
+unlint empty-object
+unlint object-does-not-match-filename
+version 0.0.0

Expand Down
Loading