Skip to content

Commit 46bbdae

Browse files
committed
SONARPY-201 Remove PythonFile.file()
1 parent c8de0ec commit 46bbdae

9 files changed

Lines changed: 14 additions & 24 deletions

File tree

python-checks/src/main/java/org/sonar/python/checks/MissingNewlineAtEndOfFileCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class MissingNewlineAtEndOfFileCheck extends PythonCheck {
4141
public void visitFile(@Nullable AstNode astNode) {
4242
String fileContent = getContext().pythonFile().content();
4343
if (fileContent.length() > 0 && !fileContent.endsWith("\n") && !fileContent.endsWith("\r")){
44-
addFileIssue(String.format(MESSAGE, getContext().pythonFile().file().getName()));
44+
addFileIssue(String.format(MESSAGE, getContext().pythonFile().fileName()));
4545
}
4646
}
4747

python-checks/src/main/java/org/sonar/python/checks/ModuleNameCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class ModuleNameCheck extends PythonCheck {
4949

5050
@Override
5151
public void visitFile(@Nullable AstNode astNode) {
52-
String fileName = getContext().pythonFile().file().getName();
52+
String fileName = getContext().pythonFile().fileName();
5353
int dotIndex = fileName.lastIndexOf('.');
5454
if (dotIndex > 0) {
5555
String moduleName = fileName.substring(0, dotIndex);

python-checks/src/main/java/org/sonar/python/checks/TooManyLinesInFileCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void visitNode(AstNode astNode) {
6060

6161
if (lines > maximum) {
6262
String message = "File \"{0}\" has {1} lines, which is greater than {2} authorized. Split it into smaller files.";
63-
addFileIssue(MessageFormat.format(message, getContext().pythonFile().file().getName(), lines, maximum));
63+
addFileIssue(MessageFormat.format(message, getContext().pythonFile().fileName(), lines, maximum));
6464
}
6565
}
6666
}

python-squid/src/main/java/org/sonar/python/PythonFile.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
*/
2020
package org.sonar.python;
2121

22-
import java.io.File;
23-
2422
public interface PythonFile {
2523

2624
String content();
2725

28-
@Deprecated
29-
File file();
26+
String fileName();
3027

3128
}

python-squid/src/main/java/org/sonar/python/TestPythonVisitorRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public String content() {
7171
}
7272

7373
@Override
74-
public File file() {
75-
return file;
74+
public String fileName() {
75+
return file.getName();
7676
}
7777

7878
}

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonHighlighter.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public class PythonHighlighter extends PythonVisitor {
8686

8787
private NewHighlighting newHighlighting;
8888

89-
private final SensorContext context;
9089
private Set<Token> docStringTokens;
9190

92-
public PythonHighlighter(SensorContext context) {
93-
this.context = context;
91+
public PythonHighlighter(SensorContext context, InputFile inputFile) {
92+
docStringTokens = new HashSet<>();
93+
newHighlighting = context.newHighlighting();
94+
newHighlighting.onFile(inputFile);
9495
}
9596

9697
@Override
@@ -102,14 +103,6 @@ public Set<AstNodeType> subscribedKinds() {
102103
);
103104
}
104105

105-
@Override
106-
public void visitFile(@Nullable AstNode astNode) {
107-
docStringTokens = new HashSet<>();
108-
newHighlighting = context.newHighlighting();
109-
InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().is(getContext().pythonFile().file().getAbsoluteFile()));
110-
newHighlighting.onFile(inputFile);
111-
}
112-
113106
@Override
114107
public void visitNode(AstNode astNode) {
115108
if (astNode.is(PythonGrammar.FILE_INPUT)) {

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonScanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void scanFile(InputFile inputFile) {
9999
saveIssues(inputFile, check, check.scanFileForIssues(visitorContext));
100100
}
101101

102-
new PythonHighlighter(context).scanFile(visitorContext);
102+
new PythonHighlighter(context, inputFile).scanFile(visitorContext);
103103
}
104104

105105
private void saveIssues(InputFile inputFile, PythonCheck check, List<PreciseIssue> issues) {

sonar-python-plugin/src/main/java/org/sonar/plugins/python/SonarQubePythonFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public String content() {
4646
}
4747

4848
@Override
49-
public File file() {
50-
return file;
49+
public String fileName() {
50+
return file.getName();
5151
}
5252

5353
}

sonar-python-plugin/src/test/java/org/sonar/plugins/python/PythonHighlighterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void scanFile() {
4949
context = SensorContextTester.create(new File(dir));
5050
context.fileSystem().add(inputFile);
5151

52-
PythonHighlighter pythonHighlighter = new PythonHighlighter(context);
52+
PythonHighlighter pythonHighlighter = new PythonHighlighter(context, inputFile);
5353
TestPythonVisitorRunner.scanFile(file, pythonHighlighter);
5454
}
5555

0 commit comments

Comments
 (0)