use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class LiteralLikeTest method literal_nested_as_only_child.
@Test
void literal_nested_as_only_child() throws Exception {
UastNode literal = Uast.from(new StringReader("{ \"kinds\": [], " + "\"children\": [{ \"kinds\": [\"LITERAL\"], \"token\": {\"value\": \"foo\" , \"line\": 1, \"column\": 1 } }]" + "}"));
LiteralLike literalLike = LiteralLike.from(literal);
assertThat(literalLike).isNotNull();
assertThat(literalLike.value()).isEqualTo("foo");
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class UastGeneratorWrapper method createUast.
UastNode createUast(InputStream source) throws IOException, InterruptedException {
Process process = processBuilder.start();
errorConsumer.consumeStream(process.getErrorStream(), LOG::debug);
try (OutputStream out = process.getOutputStream();
InputStream in = process.getInputStream()) {
copy(source, process.getOutputStream());
out.close();
UastNode uastNode = Uast.from(new InputStreamReader(in, StandardCharsets.UTF_8));
boolean exited = process.waitFor(5, TimeUnit.SECONDS);
if (exited && process.exitValue() != 0) {
throw new IllegalStateException("Parser returned non-zero exit value: " + process.exitValue());
}
if (process.isAlive()) {
process.destroyForcibly();
throw new IllegalStateException("Took too long to parse. External process killed forcibly");
}
return uastNode;
}
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class CpdVisitorTest method test.
@Test
void test() throws IOException {
String filename = "lets.go";
String code = readTestResource(getClass(), filename);
String codeJson = readTestResource(getClass(), filename + ".uast.json");
InputFile inputFile = createInputFile("lets.go", code);
sensorContext.fileSystem().add(inputFile);
CpdVisitor cpdVisitor = new CpdVisitor(sensorContext, inputFile);
UastNode node = Uast.from(new StringReader(codeJson));
cpdVisitor.scan(node);
cpdVisitor.save();
List<TokensLine> tokensLines = sensorContext.cpdTokens("module:" + inputFile.filename());
assertThat(tokensLines).isNotNull().hasSize(5);
assertThat(tokensLines).extracting("value").isEqualTo(Arrays.asList("packagemain", "funcfun()string{", "a:=LITERAL", "returna", "}"));
assertThat(tokensLines).extracting("startLine").isEqualTo(Arrays.asList(1, 3, 4, 5, 6));
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class TooManyParametersCheck method visitNode.
@Override
public void visitNode(UastNode node) {
FunctionLike function = FunctionLike.from(node);
if (function == null) {
return;
}
List<UastNode> parameters = function.parameters();
int parameterCount = 0;
for (UastNode parameter : parameters) {
List<UastNode> identifiers = new ArrayList<>();
parameter.getDescendants(UastNode.Kind.IDENTIFIER, identifiers::add, UastNode.Kind.TYPE);
parameterCount += identifiers.size();
}
if (parameterCount > maximum) {
reportIssue(function.name(), String.format("Function has %d parameters, which is more than %d authorized.", parameterCount, maximum));
}
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class WrongAssignmentOperatorCheck method visitNode.
@Override
public void visitNode(UastNode node) {
AssignmentLike assignmentLike = AssignmentLike.from(node);
if (assignmentLike == null) {
return;
}
if (assignmentLike.isMultiple()) {
List<AssignmentLike> couples = assignmentLike.assignmentsTuples();
if (couples.size() != 1) {
// not relevant for multiple assignment form
return;
}
assignmentLike = couples.get(0);
}
UastNode.Token variableLastToken = assignmentLike.target().lastToken();
UastNode operator = assignmentLike.operator();
UastNode expression = assignmentLike.value();
UastNode.Token expressionFirstToken = expression.firstToken();
if (noSpacingBetween(operator.lastToken(), expressionFirstToken) && spacingBetween(variableLastToken, operator.firstToken()) && expression.is(UastNode.Kind.UNARY_MINUS, UastNode.Kind.UNARY_PLUS, UastNode.Kind.LOGICAL_COMPLEMENT)) {
String msg;
if (expression.is(UastNode.Kind.LOGICAL_COMPLEMENT)) {
msg = "Add a space between \"=\" and \"!\" to avoid confusion.";
} else {
msg = String.format("Was \"%s=\" meant instead?", expressionFirstToken.value);
}
reportIssue(assignmentLike.node(), msg);
}
}
Aggregations