Skip to content

Commit

Permalink
refactor: Operator wrapping on end of line (#4510)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne authored Sep 21, 2024
1 parent 9355812 commit 25d374b
Show file tree
Hide file tree
Showing 92 changed files with 385 additions and 385 deletions.
4 changes: 2 additions & 2 deletions rewrite-core/src/main/java/org/openrewrite/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ public String toString() {
.map(t -> t instanceof Tree ?
t.getClass().getSimpleName() :
t.toString())
.collect(Collectors.joining("->"))
+ "}";
.collect(Collectors.joining("->")) +
"}";
}

public Cursor dropParentUntil(Predicate<Object> valuePredicate) {
Expand Down
10 changes: 5 additions & 5 deletions rewrite-core/src/main/java/org/openrewrite/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ private static boolean matchesGlob(String pattern, String path) {
if (!StringUtils.matchesGlob(pathTokens[pathIdxEnd], pattTokens[pattIdxEnd])) {
return false;
}
if (pattIdxEnd == (pattTokens.length - 1)
&& (isFileSeparator(pattern.charAt(pattern.length() - 1)) ^ isFileSeparator(path.charAt(path.length() - 1)))) {
if (pattIdxEnd == (pattTokens.length - 1) &&
(isFileSeparator(pattern.charAt(pattern.length() - 1)) ^ isFileSeparator(path.charAt(path.length() - 1)))) {
return false;
}
pattIdxEnd--;
Expand Down Expand Up @@ -293,8 +293,8 @@ private static boolean isFileSeparator(char ch) {

@SuppressWarnings("SameParameterValue")
private static boolean isFileSeparator(boolean strict, char ch) {
return strict
? ch == File.separatorChar
: ch == UNIX_SEPARATOR || ch == WINDOWS_SEPARATOR;
return strict ?
ch == File.separatorChar :
ch == UNIX_SEPARATOR || ch == WINDOWS_SEPARATOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ private static boolean matchesGlob(String pattern, String str, boolean caseSensi
if (ch == '*') {
break;
}
if (ch != '?'
&& different(caseSensitive, ch, str.charAt(strIdxStart))) {
if (ch != '?' &&
different(caseSensitive, ch, str.charAt(strIdxStart))) {
return false; // Character mismatch
}
patIdxStart++;
Expand Down Expand Up @@ -554,9 +554,9 @@ private static boolean allStars(String chars, int start, int end) {
}

private static boolean different(boolean caseSensitive, char ch, char other) {
return caseSensitive
? ch != other
: Character.toUpperCase(ch) != Character.toUpperCase(other);
return caseSensitive ?
ch != other :
Character.toUpperCase(ch) != Character.toUpperCase(other);
}

public static String indent(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public Response send(Request request) {
String methodValue = method.toString();
if (entity.length > 0) {
String contentType = request.getRequestHeaders().get("Content-Type");
MediaType mediaType = contentType != null
? MediaType.get(contentType + "; charset=utf-8")
: MEDIA_TYPE_APPLICATION_JSON;
MediaType mediaType = contentType != null ?
MediaType.get(contentType + "; charset=utf-8") :
MEDIA_TYPE_APPLICATION_JSON;
RequestBody body = RequestBody.create(entity, mediaType);
requestBuilder.method(methodValue, body);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ public GitProvenance(UUID id,
if (environment instanceof JenkinsBuildEnvironment) {
JenkinsBuildEnvironment jenkinsBuildEnvironment = (JenkinsBuildEnvironment) environment;
try (Repository repository = new RepositoryBuilder().findGitDir(projectDir.toFile()).build()) {
String branch = jenkinsBuildEnvironment.getLocalBranch() != null
? jenkinsBuildEnvironment.getLocalBranch()
: localBranchName(repository, jenkinsBuildEnvironment.getBranch());
String branch = jenkinsBuildEnvironment.getLocalBranch() != null ?
jenkinsBuildEnvironment.getLocalBranch() :
localBranchName(repository, jenkinsBuildEnvironment.getBranch());
return fromGitConfig(repository, branch, getChangeset(repository), gitRemoteParser);
} catch (IllegalArgumentException | GitAPIException e) {
// Silently ignore if the project directory is not a git repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public static BitbucketBuildEnvironment build(UnaryOperator<String> environment)

@Override
public GitProvenance buildGitProvenance() throws IncompleteGitConfigException {
if (StringUtils.isBlank(httpOrigin)
|| StringUtils.isBlank(branch)
|| StringUtils.isBlank(sha)) {
if (StringUtils.isBlank(httpOrigin) ||
StringUtils.isBlank(branch) ||
StringUtils.isBlank(sha)) {
throw new IncompleteGitConfigException();
} else {
return new GitProvenance(UUID.randomUUID(), httpOrigin, branch, sha,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public interface BuildEnvironment extends Marker {
if (environment.apply("GITLAB_CI") != null) {
return GitlabBuildEnvironment.build(environment);
}
if (environment.apply("CI") != null && environment.apply("GITHUB_ACTION") != null
&& environment.apply("GITHUB_RUN_ID") != null) {
if (environment.apply("CI") != null && environment.apply("GITHUB_ACTION") != null &&
environment.apply("GITHUB_RUN_ID") != null) {
return GithubActionsBuildEnvironment.build(environment);
}
if (environment.apply("DRONE") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public static CustomBuildEnvironment build(UnaryOperator<String> environment) {

@Override
public GitProvenance buildGitProvenance() throws IncompleteGitConfigException {
if (StringUtils.isBlank(cloneURL)
|| StringUtils.isBlank(ref)
|| StringUtils.isBlank(sha)) {
if (StringUtils.isBlank(cloneURL) ||
StringUtils.isBlank(ref) ||
StringUtils.isBlank(sha)) {
throw new IncompleteGitConfigException();
} else {
return new GitProvenance(UUID.randomUUID(), cloneURL, ref, sha,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public static DroneBuildEnvironment build(UnaryOperator<String> environment) {

@Override
public GitProvenance buildGitProvenance() throws IncompleteGitConfigException {
if (StringUtils.isBlank(remoteURL)
|| (StringUtils.isBlank(branch) && StringUtils.isBlank(tag))
|| StringUtils.isBlank(commitSha)) {
if (StringUtils.isBlank(remoteURL) ||
(StringUtils.isBlank(branch) && StringUtils.isBlank(tag)) ||
StringUtils.isBlank(commitSha)) {
throw new IncompleteGitConfigException();
}
return new GitProvenance(UUID.randomUUID(), remoteURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ public GitProvenance buildGitProvenance() throws IncompleteGitConfigException {
} else {
gitRef = gitRef.replaceFirst("refs/heads/", "");
}
if (StringUtils.isBlank(ghRef)
|| StringUtils.isBlank(host)
|| StringUtils.isBlank(repository)
|| StringUtils.isBlank(sha)) {
if (StringUtils.isBlank(ghRef) ||
StringUtils.isBlank(host) ||
StringUtils.isBlank(repository) ||
StringUtils.isBlank(sha)) {
throw new IncompleteGitConfigException(
String.format("Invalid GitHub environment with host: %s, branch: %s, " +
"repository: %s, sha: %s", host, ghRef, repository, sha));
}

return new GitProvenance(UUID.randomUUID(), host + "/" + getRepository()
+ ".git", gitRef, getSha(), null, null, emptyList());
return new GitProvenance(UUID.randomUUID(), host + "/" + getRepository() +
".git", gitRef, getSha(), null, null, emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public static GitlabBuildEnvironment build(UnaryOperator<String> environment) {

@Override
public GitProvenance buildGitProvenance() throws IncompleteGitConfigException {
if (StringUtils.isBlank(ciRepositoryUrl)
|| StringUtils.isBlank(ciCommitRefName)
|| StringUtils.isBlank(ciCommitSha)) {
if (StringUtils.isBlank(ciRepositoryUrl) ||
StringUtils.isBlank(ciCommitRefName) ||
StringUtils.isBlank(ciCommitSha)) {
throw new IncompleteGitConfigException();
}
return new GitProvenance(UUID.randomUUID(), ciRepositoryUrl, ciCommitRefName, ciCommitSha,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public static Validated<DependencyMatcher> build(String pattern) {
}

public boolean matches(@Nullable String groupId, String artifactId, String version) {
return StringUtils.matchesGlob(groupId, groupPattern)
&& StringUtils.matchesGlob(artifactId, artifactPattern)
&& (versionComparator == null || versionComparator.isValid(null, version));
return StringUtils.matchesGlob(groupId, groupPattern) &&
StringUtils.matchesGlob(artifactId, artifactPattern) &&
(versionComparator == null || versionComparator.isValid(null, version));
}

public boolean matches(@Nullable String groupId, String artifactId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public class AppendToTextFile extends ScanningRecipe<AtomicBoolean> {
@Nullable Boolean appendNewline;

@Option(displayName = "Existing file strategy",
description = "Determines behavior if a file exists at this location prior to Rewrite execution.\n\n"
+ "- `Continue`: append new content to existing file contents. If existing file is not plaintext, recipe does nothing.\n"
+ "- `Replace`: remove existing content from file.\n"
+ "- `Leave`: *(default)* do nothing. Existing file is fully preserved.\n\n"
+ "Note: this only affects the first interaction with the specified file per Rewrite execution.\n"
+ "Subsequent instances of this recipe in the same Rewrite execution will always append.",
description = "Determines behavior if a file exists at this location prior to Rewrite execution.\n\n" +
"- `Continue`: append new content to existing file contents. If existing file is not plaintext, recipe does nothing.\n" +
"- `Replace`: remove existing content from file.\n" +
"- `Leave`: *(default)* do nothing. Existing file is fully preserved.\n\n" +
"Note: this only affects the first interaction with the specified file per Rewrite execution.\n" +
"Subsequent instances of this recipe in the same Rewrite execution will always append.",
valid = {"Continue", "Replace", "Leave"},
required = false)
@Nullable Strategy existingFileStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m) {
if (strings.size() >= 2 &&
strings.get(0) instanceof J.Literal) {
Dependency dependency = DependencyStringNotationConverter.parse((String) requireNonNull(((J.Literal) strings.get(0)).getValue()));
if (dependency != null && !newArtifactId.equals(dependency.getArtifactId())
&& depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) {
if (dependency != null && !newArtifactId.equals(dependency.getArtifactId()) &&
depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) {
Dependency newDependency = dependency.withArtifactId(newArtifactId);
updatedDependencies.put(dependency.getGav().asGroupArtifact(), newDependency.getGav().asGroupArtifact());
String replacement = newDependency.toStringNotation();
Expand Down Expand Up @@ -196,9 +196,9 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m) {
version = valueValue;
}
}
if (groupId == null || artifactId == null
|| (version == null && !depMatcher.matches(groupId, artifactId))
|| (version != null && !depMatcher.matches(groupId, artifactId, version))) {
if (groupId == null || artifactId == null ||
(version == null && !depMatcher.matches(groupId, artifactId)) ||
(version != null && !depMatcher.matches(groupId, artifactId, version))) {
return m;
}
String delimiter = versionStringDelimiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
}
index++;
}
if (groupId == null || artifactId == null
|| (version == null && !depMatcher.matches(groupId, artifactId))
|| (version != null && !depMatcher.matches(groupId, artifactId, version))
|| Objects.equals(newClassifier, classifier)) {
if (groupId == null || artifactId == null ||
(version == null && !depMatcher.matches(groupId, artifactId)) ||
(version != null && !depMatcher.matches(groupId, artifactId, version)) ||
Objects.equals(newClassifier, classifier)) {
return m;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
extension = valueValue;
}
}
if (groupId == null || artifactId == null
|| (version == null && !depMatcher.matches(groupId, artifactId))
|| (version != null && !depMatcher.matches(groupId, artifactId, version))
|| extension == null) {
if (groupId == null || artifactId == null ||
(version == null && !depMatcher.matches(groupId, artifactId)) ||
(version != null && !depMatcher.matches(groupId, artifactId, version)) ||
extension == null) {
return m;
}
String delimiter = extensionStringDelimiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m) {
if (strings.size() >= 2 &&
strings.get(0) instanceof J.Literal) {
Dependency dependency = DependencyStringNotationConverter.parse((String) requireNonNull(((J.Literal) strings.get(0)).getValue()));
if (dependency != null && !newGroupId.equals(dependency.getGroupId())
&& depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) {
if (dependency != null && !newGroupId.equals(dependency.getGroupId()) &&
depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) {
Dependency newDependency = dependency.withGroupId(newGroupId);
updatedDependencies.put(dependency.getGav().asGroupArtifact(), newDependency.getGav().asGroupArtifact());
String replacement = newDependency.toStringNotation();
Expand Down Expand Up @@ -196,9 +196,9 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m) {
version = valueValue;
}
}
if (groupId == null || artifactId == null
|| (version == null && !depMatcher.matches(groupId, artifactId))
|| (version != null && !depMatcher.matches(groupId, artifactId, version))) {
if (groupId == null || artifactId == null ||
(version == null && !depMatcher.matches(groupId, artifactId)) ||
(version != null && !depMatcher.matches(groupId, artifactId, version))) {
return m;
}
String delimiter = versionStringDelimiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public J.Assignment visitAssignment(J.Assignment as, ExecutionContext ctx) {
if(!Objects.equals(key, var.getSimpleName())) {
return as;
}
if((var.getTarget() instanceof J.Identifier && ((J.Identifier) var.getTarget()).getSimpleName().equals("ext"))
|| (var.getTarget() instanceof J.FieldAccess && ((J.FieldAccess) var.getTarget()).getSimpleName().equals("ext")) ) {
if((var.getTarget() instanceof J.Identifier && ((J.Identifier) var.getTarget()).getSimpleName().equals("ext")) ||
(var.getTarget() instanceof J.FieldAccess && ((J.FieldAccess) var.getTarget()).getSimpleName().equals("ext")) ) {
as = updateAssignment(as);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ

private static boolean isInDependenciesBlock(Cursor cursor) {
Cursor c = cursor.dropParentUntil(value ->
value == Cursor.ROOT_VALUE
|| (value instanceof J.MethodInvocation && DEPENDENCIES_DSL_MATCHER.matches((J.MethodInvocation) value)));
value == Cursor.ROOT_VALUE ||
(value instanceof J.MethodInvocation && DEPENDENCIES_DSL_MATCHER.matches((J.MethodInvocation) value)));
return c.getValue() instanceof J.MethodInvocation;
}

private static boolean isEachDependency(J.MethodInvocation m) {
return "eachDependency".equals(m.getSimpleName())
&& (m.getSelect() instanceof J.Identifier
&& "resolutionStrategy".equals(((J.Identifier) m.getSelect()).getSimpleName()));
return "eachDependency".equals(m.getSimpleName()) &&
(m.getSelect() instanceof J.Identifier &&
"resolutionStrategy".equals(((J.Identifier) m.getSelect()).getSimpleName()));
}

private static boolean predicateRelatesToGav(J.If iff, GroupArtifactVersionBecause groupArtifactVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ private J.MethodInvocation forGString(J.MethodInvocation m) {
// Supporting all possible GString interpolations is impossible
// Supporting all probable GString interpolations is difficult
// This focuses on the most common case: When only the version number is interpolated
if (g.getStrings().size() != 2 || !(g.getStrings().get(0) instanceof J.Literal)
|| !(g.getStrings().get(1) instanceof G.GString.Value)) {
if (g.getStrings().size() != 2 || !(g.getStrings().get(0) instanceof J.Literal) ||
!(g.getStrings().get(1) instanceof G.GString.Value)) {
return m;
}
J.Literal arg1 = (J.Literal)g.getStrings().get(0);
Expand Down
Loading

0 comments on commit 25d374b

Please sign in to comment.