Search in sources :

Example 1 with RelatedInformation

use of com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation in project bsl-language-server by 1c-syntax.

the class DuplicateRegionDiagnostic method visitFile.

@Override
public ParseTree visitFile(BSLParser.FileContext ctx) {
    List<RegionSymbol> regions = documentContext.getSymbolTree().getModuleLevelRegions();
    // анализировать модуль без областей не будем
    if (regions.isEmpty()) {
        return ctx;
    }
    // считаем дубли с учетом альтернативных имен для стандартных областей
    regions.stream().collect(Collectors.groupingBy(regionSymbol -> regionNames.getOrDefault(regionSymbol.getName(), regionSymbol.getName()))).forEach((String name, List<RegionSymbol> regionsList) -> {
        if (regionsList.size() > 1) {
            List<DiagnosticRelatedInformation> relatedInformation = new ArrayList<>();
            RegionSymbol currentRegion = regionsList.get(0);
            Range currentRange = currentRegion.getStartRange();
            regionsList.stream().map(region -> RelatedInformation.create(documentContext.getUri(), region.getStartRange(), "+1")).collect(Collectors.toCollection(() -> relatedInformation));
            diagnosticStorage.addDiagnostic(currentRange, info.getMessage(currentRegion.getName()), relatedInformation);
        }
    });
    return ctx;
}
Also used : RelatedInformation(com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range) DiagnosticType(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType) DiagnosticTag(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) DiagnosticSeverity(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity) Keywords(com.github._1c_syntax.bsl.languageserver.utils.Keywords) List(java.util.List) DiagnosticMetadata(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata) TreeMap(java.util.TreeMap) DiagnosticCompatibilityMode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCompatibilityMode) Map(java.util.Map) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RegionSymbol(com.github._1c_syntax.bsl.languageserver.context.symbol.RegionSymbol) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) Range(org.eclipse.lsp4j.Range)

Example 2 with RelatedInformation

use of com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation in project bsl-language-server by 1c-syntax.

the class SarifReporter method createResult.

private static Result createResult(FileInfo fileInfo, Diagnostic diagnostic) {
    var uri = Absolute.uri(fileInfo.getPath().toUri()).toString();
    var message = new Message().withText(diagnostic.getMessage());
    var ruleId = DiagnosticCode.getStringValue(diagnostic.getCode());
    var level = severityToResultLevel.get(diagnostic.getSeverity());
    var analysisTarget = new ArtifactLocation().withUri(uri);
    var locations = List.of(createLocation(diagnostic.getMessage(), uri, diagnostic.getRange()));
    var relatedLocations = Optional.ofNullable(diagnostic.getRelatedInformation()).stream().flatMap(Collection::stream).skip(1).map(relatedInformation -> createLocation(relatedInformation.getMessage(), relatedInformation.getLocation().getUri(), relatedInformation.getLocation().getRange())).collect(Collectors.toSet());
    return new Result().withMessage(message).withRuleId(ruleId).withLevel(level).withAnalysisTarget(analysisTarget).withLocations(locations).withRelatedLocations(relatedLocations);
}
Also used : ArtifactLocation(com.contrastsecurity.sarif.ArtifactLocation) ToolComponent(com.contrastsecurity.sarif.ToolComponent) SneakyThrows(lombok.SneakyThrows) ServerInfo(org.eclipse.lsp4j.ServerInfo) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) ReportingDescriptor(com.contrastsecurity.sarif.ReportingDescriptor) Message(com.contrastsecurity.sarif.Message) Absolute(com.github._1c_syntax.utils.Absolute) ArrayList(java.util.ArrayList) Region(com.contrastsecurity.sarif.Region) HashSet(java.util.HashSet) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ConfigurationOverride(com.contrastsecurity.sarif.ConfigurationOverride) Run(com.contrastsecurity.sarif.Run) URI(java.net.URI) Invocation(com.contrastsecurity.sarif.Invocation) Path(java.nio.file.Path) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) Location(com.contrastsecurity.sarif.Location) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) ReportingDescriptorReference(com.contrastsecurity.sarif.ReportingDescriptorReference) PropertyBag(com.contrastsecurity.sarif.PropertyBag) Tool(com.contrastsecurity.sarif.Tool) Optional(java.util.Optional) ReportingConfiguration(com.contrastsecurity.sarif.ReportingConfiguration) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) MultiformatMessageString(com.contrastsecurity.sarif.MultiformatMessageString) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Result(com.contrastsecurity.sarif.Result) SarifSchema210(com.contrastsecurity.sarif.SarifSchema210) Message(com.contrastsecurity.sarif.Message) ArtifactLocation(com.contrastsecurity.sarif.ArtifactLocation) Result(com.contrastsecurity.sarif.Result)

Example 3 with RelatedInformation

use of com.github._1c_syntax.bsl.languageserver.utils.RelatedInformation in project sonar-bsl-plugin-community by 1c-syntax.

the class IssuesLoaderTest method test_createIssue.

@Test
void test_createIssue() {
    var issueSeverity = DiagnosticSeverity.Information;
    var diagnosticName = "OneStatementPerLine";
    var ruleKey = RuleKey.of(BSLLanguageServerRuleDefinition.REPOSITORY_KEY, diagnosticName);
    var context = SensorContextTester.create(BASE_DIR);
    var activeRules = new ActiveRulesBuilder().addRule(new NewActiveRule.Builder().setRuleKey(ruleKey).setName(diagnosticName).build()).build();
    context.setActiveRules(activeRules);
    var inputFile = Tools.inputFileBSL(FILE_NAME, BASE_DIR);
    context.fileSystem().add(inputFile);
    var issuesLoader = new IssuesLoader(context);
    var diagnostic = new Diagnostic();
    diagnostic.setCode(diagnosticName);
    diagnostic.setSeverity(issueSeverity);
    diagnostic.setMessage("Check message OneStatementPerLine");
    diagnostic.setRange(new Range(new Position(0, 0), new Position(0, 1)));
    var uri = inputFile.uri().toString();
    var relatedInformation = List.of(new DiagnosticRelatedInformation(new Location(uri, new Range(new Position(11, 0), new Position(11, 8))), "+1"), new DiagnosticRelatedInformation(new Location(uri, new Range(new Position(12, 0), new Position(12, 5))), "+1"));
    diagnostic.setRelatedInformation(relatedInformation);
    issuesLoader.createIssue(inputFile, diagnostic);
    assertThat(context.allIssues()).hasSize(1);
    DefaultIssue issue = (DefaultIssue) context.allIssues().toArray()[0];
    assertThat(issue.ruleKey()).isEqualTo(ruleKey);
    assertThat(issue.flows()).hasSize(2).anySatisfy(flow -> assertThat(flow.locations()).hasSize(1).element(0).extracting(IssueLocation::textRange).isEqualTo(new DefaultTextRange(new DefaultTextPointer(12, 0), new DefaultTextPointer(12, 8)))).anySatisfy(flow -> assertThat(flow.locations()).hasSize(1).element(0).extracting(IssueLocation::textRange).isEqualTo(new DefaultTextRange(new DefaultTextPointer(13, 0), new DefaultTextPointer(13, 5))));
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) IssueLocation(org.sonar.api.batch.sensor.issue.IssueLocation) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) DefaultExternalIssue(org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue) DefaultIssue(org.sonar.api.batch.sensor.issue.internal.DefaultIssue) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Issue(org.sonar.api.batch.sensor.issue.Issue) File(java.io.File) Test(org.junit.jupiter.api.Test) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) List(java.util.List) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) Location(org.eclipse.lsp4j.Location) DefaultTextRange(org.sonar.api.batch.fs.internal.DefaultTextRange) RuleKey(org.sonar.api.rule.RuleKey) Position(org.eclipse.lsp4j.Position) BSLLanguageServerRuleDefinition(com.github._1c_syntax.bsl.sonar.language.BSLLanguageServerRuleDefinition) Position(org.eclipse.lsp4j.Position) Diagnostic(org.eclipse.lsp4j.Diagnostic) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) DefaultIssue(org.sonar.api.batch.sensor.issue.internal.DefaultIssue) Range(org.eclipse.lsp4j.Range) DefaultTextRange(org.sonar.api.batch.fs.internal.DefaultTextRange) DefaultTextRange(org.sonar.api.batch.fs.internal.DefaultTextRange) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) IssueLocation(org.sonar.api.batch.sensor.issue.IssueLocation) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) IssueLocation(org.sonar.api.batch.sensor.issue.IssueLocation) Location(org.eclipse.lsp4j.Location) Test(org.junit.jupiter.api.Test)

Aggregations

List (java.util.List)3 Range (org.eclipse.lsp4j.Range)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 ArtifactLocation (com.contrastsecurity.sarif.ArtifactLocation)1 ConfigurationOverride (com.contrastsecurity.sarif.ConfigurationOverride)1 Invocation (com.contrastsecurity.sarif.Invocation)1 Location (com.contrastsecurity.sarif.Location)1 Message (com.contrastsecurity.sarif.Message)1 MultiformatMessageString (com.contrastsecurity.sarif.MultiformatMessageString)1 PhysicalLocation (com.contrastsecurity.sarif.PhysicalLocation)1 PropertyBag (com.contrastsecurity.sarif.PropertyBag)1 Region (com.contrastsecurity.sarif.Region)1 ReportingConfiguration (com.contrastsecurity.sarif.ReportingConfiguration)1 ReportingDescriptor (com.contrastsecurity.sarif.ReportingDescriptor)1 ReportingDescriptorReference (com.contrastsecurity.sarif.ReportingDescriptorReference)1 Result (com.contrastsecurity.sarif.Result)1 Run (com.contrastsecurity.sarif.Run)1