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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
<class>org.eolang.lints.PkMonoTest</class>
<class>org.eolang.lints.LtByXslTest</class>
<class>org.eolang.lints.PkWpaTest</class>
<class>org.eolang.lints.ProgramTest</class>
<class>org.eolang.lints.EoPackageTest</class>
<class>org.eolang.lints.LtUnlintNonExistingDefectWpaTest</class>
<class>org.eolang.lints.LtTestNotVerbTest</class>
<class>org.eolang.lints.WpaLintsTest</class>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import org.eolang.lints.Source;
import java.util.Map;
import org.eolang.lints.Program;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -22,7 +23,12 @@ final class LintsItTest {
void lintsSource() throws IOException {
MatcherAssert.assertThat(
"passes with no exceptions",
new Source(new XMLDocument("<object/>")).defects(),
new Program(
Map.of(
Copy link
Member

Choose a reason for hiding this comment

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

@Marat-Tim let's use new MapOf<>() from org.cactoos for better consistency in the codebase

"main.eo",
new XMLDocument("<object/>")
)
).defects(),
Matchers.notNullValue()
);
}
Expand Down
163 changes: 163 additions & 0 deletions src/main/java/org/eolang/lints/EoPackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.cactoos.iterable.Sticky;
import org.cactoos.list.ListOf;
import org.cactoos.list.Synced;

/**
* Whole EO package, as collection of XMIR sources to analyze.
* @since 0.1.0
Copy link
Member

Choose a reason for hiding this comment

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

@Marat-Tim let's place correct version of the future release

*/
final class EoPackage {

/**
* Collection of wpa lints, preloaded on JVM start.
*/
private static final Iterable<Lint<Map<String, XML>>> WPA = new Synced<>(
new ListOf<>(
new Sticky<>(
new PkWpa()
)
)
);

/**
* Lints to use.
*/
private final Iterable<Lint<Map<String, XML>>> lints;

/**
* The package of XMIR files.
*/
private final Map<String, XML> pkg;

/**
* Ctor.
* @param dirs The directory
* @throws IOException If fails
*/
EoPackage(final Path... dirs) throws IOException {
this(Arrays.asList(dirs));
}

/**
* Ctor.
*
* <p>Pay attention, it's important to use {@link Collection} as a type
* of argument, because {@link Path} implements {@link Iterable}.</p>
*
* @param dirs The directory
* @throws IOException If fails
*/
EoPackage(final Collection<Path> dirs) throws IOException {
this(EoPackage.discover(dirs));
}

/**
* Ctor.
* @param map The map with them
*/
EoPackage(final Map<String, XML> map) {
this(map, EoPackage.WPA);
}

/**
* Ctor.
* @param map The map with them
* @param list The lints
*/
EoPackage(final Map<String, XML> map, final Iterable<Lint<Map<String, XML>>> list) {
this.pkg = Collections.unmodifiableMap(map);
this.lints = list;
}

/**
* Package with disabled lints.
* @param names Lint names
* @return Package analysis without specifics names
*/
public EoPackage without(final String... names) {
return new EoPackage(this.pkg, new WpaWithout(names));
}

/**
* Find all possible defects in the EO package.
* @return All defects found
*/
public Collection<Defect> defects() {
final Collection<Defect> messages = new ArrayList<>(0);
for (final Lint<Map<String, XML>> lint : this.lints) {
try {
messages.addAll(new ScopedDefects(lint.defects(this.pkg), "WPA"));
} catch (final IOException exception) {
throw new IllegalStateException(
String.format(
"Failed to find defects in the '%s' package with '%s' lint",
this.pkg,
lint
),
exception
);
}
}
return messages;
}

/**
* Discover all XMIR files in the directory.
* @param dirs The directories to search for XMIR files in (recursively)
* @return Map of XMIR files
* @throws IOException If fails
*/
private static Map<String, XML> discover(final Iterable<Path> dirs) throws IOException {
final Map<String, XML> map = new HashMap<>(0);
for (final Path dir : dirs) {
map.putAll(EoPackage.discover(dir));
}
return map;
}

/**
* Discover all XMIR files in the directory.
* @param dir The directories to search for XMIR files in (recursively)
* @return Map of XMIR files
* @throws IOException If fails
*/
private static Map<String, XML> discover(final Path dir) throws IOException {
try (Stream<Path> walk = Files.walk(dir)) {
return walk
.filter(Files::isRegularFile)
.collect(
Collectors.toMap(
path -> new XmirKey(path, dir).asString(),
path -> {
try {
return new XMLDocument(path);
} catch (final FileNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
}
)
);
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/eolang/lints/PkWpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.cactoos.list.ListOf;

/**
* A collection of lints for Whole Program Analysis (WPA),
* provided by the {@link Program} class.
* A collection of lints for Whole Package Analysis (WPA),
Copy link
Member

Choose a reason for hiding this comment

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

@Marat-Tim here, we should stay with "Whole Program Analysis"

* provided by the {@link EoPackage} class.
*
* <p>This class is thread-safe.</p>
*
Expand Down
Loading
Loading