diff --git a/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java b/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java index 18f2c8a20..7d3993f94 100644 --- a/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java +++ b/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.LinkedList; import java.util.Map; +import java.util.regex.Pattern; import org.cactoos.io.ResourceOf; import org.cactoos.text.TextOf; import org.eolang.lints.Defect; @@ -39,6 +40,11 @@ */ public final class LtIncorrectAlias implements Lint> { + /** + * Pattern contains dot. + */ + private static final Pattern DOT_PATTERN = Pattern.compile("\\."); + @Override public String name() { return "incorrect-alias"; @@ -54,11 +60,18 @@ public Collection defects(final Map pkg) { continue; } final String pointer = alias.xpath("text()").get(0); - final String lookup = String.format( - "%s/%s", - xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0), - pointer - ); + final String lookup; + if (Boolean.parseBoolean(alias.xpath("contains(text(), ' ')").get(0))) { + lookup = LtIncorrectAlias.DOT_PATTERN.matcher( + alias.xpath("substring-after(text(), ' ')").get(0) + ).replaceAll("/"); + } else { + lookup = String.format( + "%s/%s", + xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0), + pointer + ); + } if (!pkg.containsKey(lookup)) { defects.add( new Defect.Default( @@ -69,7 +82,7 @@ public Collection defects(final Map pkg) { xmir.xpath("/program/metas/meta[head='alias'][1]/@line").get(0) ), String.format( - "Incorrect pointing alias '%s', there is no %s", + "Incorrect pointing alias '%s', since there is no %s", pointer, lookup ) diff --git a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java index 92f97fcdc..7818ca1f7 100644 --- a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java +++ b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java @@ -119,6 +119,29 @@ void ignoresProgram(final String name) throws IOException { ); } + @Test + void scansSecondPartInLongerAlias() throws IOException { + MatcherAssert.assertThat( + "Defects aren't empty, but they should", + new LtIncorrectAlias().defects( + new MapOf( + new MapEntry<>( + "longer-alias", + new EoSyntax( + new ResourceOf( + "org/eolang/lints/critical/incorrect-alias/longer-alias.eo" + ) + ).parsed() + ), + new MapEntry<>( + "org/eolang/io/stdout", new XMLDocument("") + ) + ) + ), + Matchers.emptyIterable() + ); + } + @Test @ExtendWith(MktmpResolver.class) void acceptsValidDirectory(@Mktmp final Path dir) throws IOException { @@ -140,4 +163,37 @@ void acceptsValidDirectory(@Mktmp final Path dir) throws IOException { Matchers.emptyIterable() ); } + + @Test + @ExtendWith(MktmpResolver.class) + void acceptsValidDirectoryWithLongerAlias(@Mktmp final Path dir) throws IOException { + Files.write( + dir.resolve("main.xmir"), + new EoSyntax( + new ResourceOf( + "org/eolang/lints/critical/incorrect-alias/longer-alias.eo" + ) + ).parsed().toString().getBytes() + ); + Files.write(dir.resolve("main-test.xmir"), "".getBytes()); + Files.createDirectory( + Files.createDirectory( + Files.createDirectory( + dir.resolve("org") + ).resolve("eolang") + ).resolve("io") + ); + Files.write( + dir.resolve("org/eolang/io/stdout.xmir"), "".getBytes() + ); + Files.write( + dir.resolve("org/eolang/io/stdout-test.xmir"), + "".getBytes() + ); + MatcherAssert.assertThat( + "Defects are not empty, but should be", + new Programs(dir).defects(), + Matchers.emptyIterable() + ); + } } diff --git a/src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo b/src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo new file mode 100644 index 000000000..60083a381 --- /dev/null +++ b/src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo @@ -0,0 +1,28 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 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. + ++alias stdout org.eolang.io.stdout ++package foo + +[] > main + stdout > @ + "hi!"