Skip to content

Commit 700bf7c

Browse files
committed
Various fixes for tokenizer
1 parent ed9354d commit 700bf7c

File tree

11 files changed

+62
-42
lines changed

11 files changed

+62
-42
lines changed

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/PowershellPlugin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.sonar.api.Properties;
55
import org.sonar.api.PropertyType;
66
import org.sonar.api.config.PropertyDefinition;
7+
import org.sonar.plugins.powershell.sensors.ScriptAnalyzerSensor;
8+
import org.sonar.plugins.powershell.sensors.TokenizerSensor;
79

810
@Properties({})
911
public class PowershellPlugin implements Plugin {
@@ -13,8 +15,7 @@ public void define(final Context context) {
1315
.description("Flag whether to skip tokenizer").defaultValue("false").type(PropertyType.BOOLEAN)
1416
.build());
1517
context.addExtension(PropertyDefinition.builder(Constants.SKIP_PLUGIN).name("Skip plugin")
16-
.description("Flag whether to skip plugin").defaultValue("false").type(PropertyType.BOOLEAN)
17-
.build());
18+
.description("Flag whether to skip plugin").defaultValue("false").type(PropertyType.BOOLEAN).build());
1819
context.addExtension(PropertyDefinition.builder(Constants.PS_EXECUTABLE).name("Path to powershell executable")
1920
.description("Path to powershell executable").defaultValue("powershell.exe").type(PropertyType.STRING)
2021
.build());

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/ast/Tokens.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,15 @@ public void setComplexity(Integer value) {
169169
})
170170
public static class Token {
171171

172-
@XmlElement(name = "Text", required = true)
172+
@Override
173+
public String toString() {
174+
return "Token [text=" + text + ", value=" + value + ", tokenFlags=" + tokenFlags + ", kind=" + kind
175+
+ ", startLineNumber=" + startLineNumber + ", cType=" + cType + ", endLineNumber=" + endLineNumber
176+
+ ", startOffset=" + startOffset + ", endOffset=" + endOffset + ", startColumnNumber="
177+
+ startColumnNumber + ", endColumnNumber=" + endColumnNumber + "]";
178+
}
179+
180+
@XmlElement(name = "Text", required = true)
173181
protected String text;
174182
@XmlElement(name = "Value", required = true)
175183
protected String value;

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/fillers/CpdFiller.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public void fill(SensorContext context, InputFile f, Tokens tokens) {
3535

3636
private static void tryAddToken(final NewCpdTokens cpdTokens, final Token token) {
3737
try {
38-
cpdTokens.addToken(token.getStartLineNumber(), token.getStartColumnNumber(),
39-
token.getEndLineNumber(), token.getEndColumnNumber(), token.getText());
38+
cpdTokens.addToken(token.getStartLineNumber(), token.getStartColumnNumber()-1,
39+
token.getEndLineNumber(), token.getEndColumnNumber()-1, token.getText());
4040
} catch (final Throwable e) {
4141
if (isDebugEnabled) {
42-
LOGGER.debug("Exception while adding token", e);
42+
LOGGER.debug(String.format("Exception while adding token: %s", token), e);
4343
}
4444
}
4545
}

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/fillers/HighlightingFiller.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,33 @@ public void fill(final SensorContext context, final InputFile f, final Tokens to
2929
}
3030
}
3131

32-
@SuppressWarnings("deprecation")
3332
private static void highlightToken(final NewHighlighting highlighting, final Token token) {
3433
try {
3534
final List<String> kinds = Arrays.asList(token.getTokenFlags().toLowerCase().split(","));
36-
35+
int startLine = token.getStartLineNumber();
36+
int startLineOffset = token.getStartColumnNumber() - 1;
37+
int endLine = token.getEndLineNumber();
38+
int endLineOffset = token.getEndColumnNumber() - 1;
3739
if (check("comment", token, kinds)) {
38-
highlighting.highlight(token.getStartOffset(), token.getEndOffset(), TypeOfText.COMMENT);
40+
highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.COMMENT);
3941
return;
4042
}
41-
4243
if (check("keyword", token, kinds)) {
43-
highlighting.highlight(token.getStartOffset(), token.getEndOffset(), TypeOfText.KEYWORD);
44+
45+
highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.KEYWORD);
4446
return;
4547
}
4648
if (check("StringLiteral", token, kinds) || check("StringExpandable", token, kinds)) {
47-
highlighting.highlight(token.getStartOffset(), token.getEndOffset(), TypeOfText.STRING);
49+
highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.STRING);
4850
return;
4951
}
5052
if (check("Variable", token, kinds)) {
51-
highlighting.highlight(token.getStartOffset(), token.getEndOffset(), TypeOfText.KEYWORD_LIGHT);
53+
highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.KEYWORD_LIGHT);
5254
}
5355

5456
} catch (Throwable e) {
5557
if (LOGGER.isDebugEnabled()) {
56-
LOGGER.warn("Exception while adding highliting for: " + token, e);
58+
LOGGER.warn("Exception while adding highlighting for: " + token, e);
5759
}
5860
}
5961
}

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/fillers/IssuesFiller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void fill(final SensorContext context, final File sourceDir, final Object
3838
.inputFile(fileSystem.predicates().and(fileSystem.predicates().hasRelativePath(fsFile)));
3939

4040
if (file == null) {
41-
LOGGER.warn(String.format("File %s not found", fsFile));
41+
LOGGER.debug(String.format("File '%s' not found in system to add issue %s", initialFile, ruleKey));
4242
continue;
4343
}
4444
final NewIssue issue = context.newIssue().forRule(ruleKey);

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/BaseSensor.java renamed to sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/sensors/BaseSensor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.sonar.plugins.powershell;
1+
package org.sonar.plugins.powershell.sensors;
22

33
import java.io.BufferedReader;
44
import java.io.IOException;

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/ScriptAnalyzerSensor.java renamed to sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/sensors/ScriptAnalyzerSensor.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.sonar.plugins.powershell;
1+
package org.sonar.plugins.powershell.sensors;
22

33
import java.io.File;
44
import java.util.Arrays;
@@ -13,6 +13,8 @@
1313
import org.sonar.api.utils.TempFolder;
1414
import org.sonar.api.utils.log.Logger;
1515
import org.sonar.api.utils.log.Loggers;
16+
import org.sonar.plugins.powershell.Constants;
17+
import org.sonar.plugins.powershell.PowershellLanguage;
1618
import org.sonar.plugins.powershell.fillers.IssuesFiller;
1719
import org.sonar.plugins.powershell.issues.Objects;
1820

@@ -55,27 +57,36 @@ public void execute(final SensorContext context) {
5557
LOGGER.warn("Exception while copying tokenizer script", e1);
5658
return;
5759
}
60+
5861
final String scriptFile = parserFile.getAbsolutePath();
59-
final File resultsFile = folder.newFile();
62+
6063
final FileSystem fileSystem = context.fileSystem();
61-
final File sourceDir = fileSystem.baseDir().toPath().toFile();
64+
final File baseDir = fileSystem.baseDir();
65+
final String sourceDir = baseDir.toPath().toFile().getAbsolutePath();
66+
67+
final String outFile = folder.newFile().toPath().toFile().getAbsolutePath();
6268

63-
final String[] args = new String[] { powershellExecutable, scriptFile, "-inputDir",
64-
sourceDir.getAbsolutePath(), "-output", resultsFile.toPath().toFile().getAbsolutePath() };
65-
LOGGER.info(String.format("Starting running powershell analysis: %s", Arrays.toString(args)));
69+
final String[] args = new String[] { powershellExecutable, scriptFile, "-inputDir", sourceDir, "-output",
70+
outFile };
71+
72+
LOGGER.info(String.format("Starting Script-Analyzer using powershell: %s", Arrays.toString(args)));
6673
final Process process = new ProcessBuilder(args).start();
6774
final int pReturnValue = process.waitFor();
6875

6976
if (pReturnValue != 0) {
7077
LOGGER.info(String.format(
71-
"Error executing Powershell script analyzer. Maybe Script-Analyzer is not installed? Error was: %s",
78+
"Error executing Powershell Script-Analyzer analyzer. Maybe Script-Analyzer is not installed? Error was: %s",
7279
read(process)));
7380
return;
7481
}
7582

7683
final JAXBContext jaxbContext = JAXBContext.newInstance(Objects.class);
77-
final Objects issues = (Objects) jaxbContext.createUnmarshaller().unmarshal(resultsFile);
78-
this.issuesFiller.fill(context, sourceDir, issues);
84+
final Objects issues = (Objects) jaxbContext.createUnmarshaller().unmarshal(new File(outFile));
85+
this.issuesFiller.fill(context, baseDir, issues);
86+
87+
LOGGER.info(String.format("Script-Analyzer finished, found %s issues at %s", issues.getObject().size(),
88+
sourceDir));
89+
7990
} catch (Throwable e) {
8091
LOGGER.warn("Unexpected exception while running analysis", e);
8192
}

sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/TokenizerSensor.java renamed to sonar-ps-plugin/src/main/java/org/sonar/plugins/powershell/sensors/TokenizerSensor.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
package org.sonar.plugins.powershell;
1+
package org.sonar.plugins.powershell.sensors;
22

3-
import java.io.BufferedReader;
43
import java.io.File;
5-
import java.io.IOException;
6-
import java.io.InputStreamReader;
74
import java.util.Arrays;
85

96
import javax.xml.bind.JAXBContext;
@@ -16,6 +13,8 @@
1613
import org.sonar.api.utils.TempFolder;
1714
import org.sonar.api.utils.log.Logger;
1815
import org.sonar.api.utils.log.Loggers;
16+
import org.sonar.plugins.powershell.Constants;
17+
import org.sonar.plugins.powershell.PowershellLanguage;
1918
import org.sonar.plugins.powershell.ast.Tokens;
2019
import org.sonar.plugins.powershell.fillers.CComplexityFiller;
2120
import org.sonar.plugins.powershell.fillers.CpdFiller;
@@ -83,9 +82,9 @@ public void execute(final SensorContext context) {
8382
for (final InputFile inputFile : inputFiles) {
8483
try {
8584

86-
final String analysisFile = inputFile.file().getAbsolutePath();
85+
final String analysisFile = String.format("'%s'", inputFile.file().getAbsolutePath());
8786
final String resultsFile = folder.newFile().toPath().toFile().getAbsolutePath();
88-
final String[] args = new String[] { powershellExecutable, scriptFile, "-inputFile", scriptFile,
87+
final String[] args = new String[] { powershellExecutable, scriptFile, "-inputFile", analysisFile,
8988
"-output", resultsFile
9089

9190
};
@@ -123,5 +122,4 @@ public void execute(final SensorContext context) {
123122

124123
}
125124

126-
127125
}

sonar-ps-plugin/src/main/resources/parser.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
param(
2-
[string]$inputFile,
3-
[string]$output,
4-
[int] $depth = 9999999 )
2+
[string]$inputFile,
3+
[string]$output,
4+
[int] $depth = 9999999
5+
)
56

6-
$text = Get-Content -Path "$inputFile" -Raw
7+
$text = (Get-Content $inputFile -Raw) -replace "\xEF\xBB\xBF", "";
78
$tokens = $null
89
$errors = $null
910
$ast = [Management.Automation.Language.Parser]::ParseInput($text , [ref]$tokens, [ref]$errors);

sonar-ps-plugin/src/test/java/org/sonar/plugins/powershell/ScriptAnalyzerSensorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.sonar.api.batch.fs.internal.DefaultInputFile;
1313
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
1414
import org.sonar.api.batch.sensor.internal.SensorContextTester;
15-
import org.sonar.api.batch.sensor.issue.Issue;
1615
import org.sonar.api.utils.internal.JUnitTempFolder;
16+
import org.sonar.plugins.powershell.sensors.ScriptAnalyzerSensor;
1717

1818
public class ScriptAnalyzerSensorTest {
1919

@@ -26,7 +26,6 @@ public class ScriptAnalyzerSensorTest {
2626
@Test
2727
public void testExecute() throws IOException {
2828

29-
3029
SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());
3130
ctxTester.settings().setProperty(Constants.PS_EXECUTABLE, "powershell.exe");
3231
File baseFile = folder.newFile("test.ps1");
@@ -37,7 +36,7 @@ public void testExecute() throws IOException {
3736

3837
ScriptAnalyzerSensor s = new ScriptAnalyzerSensor(temp);
3938
s.execute(ctxTester);
40-
39+
4140
Assert.assertEquals(4, ctxTester.allIssues().size());
4241

4342
}

0 commit comments

Comments
 (0)