-
Notifications
You must be signed in to change notification settings - Fork 4
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
+299
−11
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
src/main/java/org/eolang/lints/LtUnlintNonExistingDefect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
.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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/resources/org/eolang/motives/misc/unlint-non-existing-defect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
126
src/test/java/org/eolang/lints/LtUnlintNonExistingDefectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofXPath
There was a problem hiding this comment.
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:Or you meant we should use
xml.element()
instead?There was a problem hiding this comment.
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