-
Notifications
You must be signed in to change notification settings - Fork 17
Added linter for warning about unnecessary usage of ^ in code #754
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
67b0b68
3bb5795
b28765e
f85cda0
a392722
b1880ed
26ada99
0f2b0eb
55bef02
dc746e2
3775eb8
01a2191
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.eolang.parser.OnDefault; | ||
|
|
||
| /** | ||
| * Lint that warns if a redundant {@code ^} is used. | ||
| * | ||
| * @since 0.0.60 | ||
| */ | ||
| final class LtRedundantHat implements Lint<XML> { | ||
|
||
|
|
||
| @Override | ||
| public Collection<Defect> defects(final XML xmir) throws IOException { | ||
| final Collection<Defect> defects = new ArrayList<>(0); | ||
| final Xnav xml = new Xnav(xmir.inner()); | ||
| final List<Xnav> objects = xml.path("//o[@base='^']").collect(Collectors.toList()); | ||
| for (final Xnav object : objects) { | ||
| final String name = object.attribute("name").text().get(); | ||
| final List<Xnav> parents = object.path(String.format("ancestor::o[.//o[@name='%s']]", name)) | ||
| .collect(Collectors.toList()); | ||
| if (parents.isEmpty()) { | ||
| defects.add( | ||
| new Defect.Default( | ||
| this.name(), | ||
| Severity.WARNING, | ||
| new OnDefault(xmir).get(), | ||
| Integer.parseInt(object.attribute("line").text().orElse("0")), | ||
| String.format( | ||
| "Redundant '^' notation: '%s' can be accessed without it", | ||
| name | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| return defects; | ||
| } | ||
|
|
||
| @Override | ||
| public String motive() throws IOException { | ||
| return "The '^' notation is used when there are no naming conflicts, please omitted for brevity."; | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return "redundant-hat"; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.eolang.lints; | ||
|
|
||
| import com.jcabi.xml.XML; | ||
| import com.jcabi.xml.XMLDocument; | ||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import org.hamcrest.MatcherAssert; | ||
| import org.hamcrest.Matchers; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Test case for {@link LtRedundantHat}. | ||
| * | ||
| * @since 0.0.60 | ||
| */ | ||
| final class LtRedundantHatTest { | ||
|
|
||
| @Test | ||
| void reportsNoDefectsForNecessaryHat() throws IOException { | ||
| final XML xml = new XMLDocument( | ||
Snehakb1219-christ marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "<program><object name='foo' line='10'><o name='+test' line='12'></o></object></program>" | ||
| ); | ||
| final LtRedundantHat lint = new LtRedundantHat(); | ||
| final Collection<Defect> defects = lint.defects(xml); | ||
| MatcherAssert.assertThat( | ||
| "Should not report a defect when '^' is necessary to resolve ambiguity", | ||
| defects, | ||
| Matchers.empty() | ||
| ); | ||
| } | ||
Snehakb1219-christ marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| void containsGuidanceInMotive() throws IOException { | ||
| final LtRedundantHat lint = new LtRedundantHat(); | ||
| MatcherAssert.assertThat( | ||
| "Motive must explain when to omit redundant '^'", | ||
| lint.motive(), | ||
| Matchers.containsString("notation is used when there are no naming conflicts") | ||
| ); | ||
| } | ||
Snehakb1219-christ marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| void namesStableId() { | ||
| final LtRedundantHat lint = new LtRedundantHat(); | ||
Snehakb1219-christ marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MatcherAssert.assertThat( | ||
| "Rule id must be 'redundant-hat'", | ||
| lint.name(), | ||
| Matchers.equalTo("redundant-hat") | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.