Skip to content

Commit

Permalink
fix: check for modifier existence before refactoring (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Oct 30, 2022
1 parent df2f6f8 commit d46079e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Unit Tests
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
- 'releases/*'

jobs:
junit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- uses: actions/setup-java@v3
with:
java-version: 17
distribution: microsoft
- name: Run Tests
run: |
gradle test
1 change: 0 additions & 1 deletion code-transformation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
dependencies {
// implementation 'fr.inria.gforge.spoon:spoon-core:10.0.1-beta-2'
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '6.3.0.202209071007-r'
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.ssh.apache', version: '6.3.0.202209071007-r'
testImplementation "com.google.truth:truth:1.1.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public void refactor(ChangeListener listener, CtType<?> type) {
}
for (CtElement match : PositionScanner.findLineOnly(type, result.position())) {
if (match instanceof CtModifiable ctModifierHandler) {
if (!hasModifier(ctModifierHandler, modifier)) {
continue;
}
var modifiers = new HashSet<>(ctModifierHandler.getModifiers());
modifiers.removeIf(v -> v.toString().equals(modifier.toLowerCase()));
ctModifierHandler.setModifiers(modifiers);
Expand All @@ -59,6 +62,11 @@ public void refactor(ChangeListener listener, CtType<?> type) {
}
}

private boolean hasModifier(CtModifiable ctModifierHandler, String modifier) {
return ctModifierHandler.getModifiers().stream()
.anyMatch(v -> v.toString().equalsIgnoreCase(modifier));
}

@Override
public List<BadSmell> getHandledBadSmells() {
return List.of(UNNECESSARY_MODIFIER);
Expand Down
1 change: 0 additions & 1 deletion github-bot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dependencies {
implementation("io.quarkus:quarkus-oidc")
implementation("io.quarkus:quarkus-keycloak-authorization")
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.23.1'
implementation 'io.vertx:vertx-junit5:+'
}

group 'io.github.martinwitt'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private String toYaml(AnalyzerResult analyzerResult) {
public String printRepairedIssues(Collection<? extends Change> changes) {
StringBuilder sb = new StringBuilder();
sb.append("# Repairing Code Style Issues\n");
sb.append("<!-- laughing-train-refactor -->");
sb.append("<!-- laughing-train-refactor -->\n");
changes.stream().map(Change::getBadSmell).distinct().forEach(v -> sb.append(
"## " + v.getName().asText() + "\n")
.append(v.getDescription().asMarkdown())
Expand Down

0 comments on commit d46079e

Please sign in to comment.