Skip to content

Commit 2f5a5a9

Browse files
authored
Add fix for tab-oriented confusion (#369)
This result for the `AddClarifyingBracesCodemod` was confusing, because it shouldn't act on the statement. There's no visual confusion in the single statement within the if block. The math the codemod does for calculating the offsets involved and deciding to change is based on column number. This makes sense if the code is written with spaces, not tabs. With tabs, the "visual layout" of the code depends on the viewer, and the offsets would require different math at a minimum. Our workaround is to ignore cases that involve tabs for now until we get a better design to accommodate tabs. <img width="434" alt="image" src="https://github.com/pixee/codemodder-java/assets/911610/9de959b0-d952-42a9-b673-54716a513a47">
1 parent 2438833 commit 2f5a5a9

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

core-codemods/src/main/java/io/codemodder/codemods/AddClarifyingBracesCodemod.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.javaparser.ast.stmt.IfStmt;
1010
import com.github.javaparser.ast.stmt.Statement;
1111
import com.github.javaparser.ast.stmt.WhileStmt;
12+
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
1213
import io.codemodder.*;
1314
import io.codemodder.javaparser.ChangesResult;
1415
import io.codemodder.providers.sarif.pmd.PmdScan;
@@ -76,6 +77,13 @@ private ChangesResult handleUnbracedStmt(final UnbracedStatement stmt) {
7677
Range onlyInnerStatementRange = stmt.getExistingSingleStatementRange();
7778
int onlyInnerStatementColumn = onlyInnerStatementRange.begin.column;
7879

80+
String concreteStatementCode = LexicalPreservingPrinter.print(stmt.getStatement());
81+
if (concreteStatementCode.contains("\t")) {
82+
// if the statement contains tabs, we don't want to touch it -- tabs confuse the visual layout
83+
// because we can't go by "column" -- it will depend on the viewer
84+
return ChangesResult.noChanges;
85+
}
86+
7987
if (nextChildNodeBeginColumn >= onlyInnerStatementColumn) {
8088
stmt.addBraces();
8189
return ChangesResult.changesApplied;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.acme;
2+
3+
abstract class Foo {
4+
5+
private void loadApplications() {
6+
if(m_authProvider.isTokenExpired())
7+
return;
8+
9+
m_applications = new HashMap<String, String>();
10+
String url = m_authProvider.getServer() + ASE_APPS+"?columns=name";
11+
}
12+
}

0 commit comments

Comments
 (0)