Search in sources :

Example 1 with RuleDetails

use of org.sonarsource.sonarlint.core.client.api.common.RuleDetails in project sonarlint-core by SonarSource.

the class SonarLintLanguageServer method executeCommand.

@Override
public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
    switch(params.getCommand()) {
        case SONARLINT_OPEN_RULE_DESCRIPTION_COMMAND:
            List<Object> args = params.getArguments();
            if (args.size() != 1) {
                warn("Expecting 1 argument");
            } else {
                String ruleKey = args.get(0).toString();
                RuleDetails ruleDetails = engine.getRuleDetails(ruleKey);
                String ruleName = ruleDetails.getName();
                String htmlDescription = ruleDetails.getHtmlDescription();
                String type = ruleDetails.getType();
                String severity = ruleDetails.getSeverity();
                client.openRuleDescription(RuleDescription.of(ruleKey, ruleName, htmlDescription, type, severity));
            }
            break;
        default:
            warn("Unimplemented command: " + params.getCommand());
    }
    return CompletableFuture.completedFuture(new Object());
}
Also used : RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails)

Example 2 with RuleDetails

use of org.sonarsource.sonarlint.core.client.api.common.RuleDetails in project sonarlint-core by SonarSource.

the class Main method main.

public static void main(String[] args) throws MalformedURLException {
    String version = args[0];
    List<Path> pluginPaths = new ArrayList<>();
    for (int i = 1; i < args.length; i++) {
        pluginPaths.add(Paths.get(args[i]));
    }
    StandaloneGlobalConfiguration.Builder builder = StandaloneGlobalConfiguration.builder().setLogOutput(new LogOutput() {

        @Override
        public void log(String formattedMessage, Level level) {
        // Ignore
        }
    });
    for (Path path : pluginPaths) {
        builder.addPlugin(path.toUri().toURL());
    }
    StandaloneSonarLintEngineImpl client = new StandaloneSonarLintEngineImpl(builder.build());
    client.start();
    Table<String, String, RuleDetails> rulesByKeyAndLanguage = TreeBasedTable.create();
    for (String ruleKeyStr : ((StandaloneGlobalContainer) client.getGlobalContainer()).getActiveRuleKeys()) {
        RuleDetails ruleDetails = client.getRuleDetails(ruleKeyStr);
        RuleKey ruleKey = RuleKey.parse(ruleKeyStr);
        rulesByKeyAndLanguage.put(ruleKey.rule(), ruleDetails.getLanguage(), ruleDetails);
    }
    try {
        System.out.print("{");
        System.out.print("\"version\": \"");
        System.out.print(version);
        System.out.print("\",");
        System.out.print("\"rules\": [");
        boolean first = true;
        for (String ruleKey : rulesByKeyAndLanguage.rowKeySet()) {
            if (!first) {
                System.out.print(",");
            }
            first = false;
            System.out.print("{");
            System.out.print("\"key\": \"");
            System.out.print(ruleKey);
            System.out.print("\",");
            System.out.print("\"title\": \"");
            System.out.print(escapeJson(rulesByKeyAndLanguage.row(ruleKey).values().iterator().next().getName()));
            System.out.print("\",");
            Set<String> mergedTags = new HashSet<>();
            for (RuleDetails rule : rulesByKeyAndLanguage.row(ruleKey).values()) {
                mergedTags.addAll(Arrays.asList(rule.getTags()));
            }
            writeTags(mergedTags);
            System.out.print(",");
            System.out.print("\"implementations\": [");
            boolean firstLang = true;
            for (Map.Entry<String, RuleDetails> detailPerLanguage : rulesByKeyAndLanguage.row(ruleKey).entrySet()) {
                if (!firstLang) {
                    System.out.print(",");
                }
                firstLang = false;
                RuleDetails ruleDetails = detailPerLanguage.getValue();
                System.out.print("{");
                System.out.print("\"key\": \"");
                System.out.print(ruleDetails.getKey());
                System.out.print("\",");
                System.out.print("\"language\": \"");
                System.out.print(languageLabel(detailPerLanguage.getKey()));
                System.out.print("\",");
                System.out.print("\"title\": \"");
                System.out.print(escapeJson(ruleDetails.getName()));
                System.out.print("\",");
                System.out.print("\"description\": \"");
                System.out.print(escapeJson(ruleDetails.getHtmlDescription()));
                System.out.print("\",");
                System.out.print("\"severity\": \"");
                System.out.print(StringUtils.capitalize(ruleDetails.getSeverity().toLowerCase()));
                System.out.print("\",");
                String[] tags = ruleDetails.getTags();
                writeTags(Arrays.asList(tags));
                System.out.print("}");
            }
            System.out.print("]");
            System.out.print("}");
        }
        System.out.print("]");
        System.out.print("}");
    } finally {
        client.stop();
    }
}
Also used : Path(java.nio.file.Path) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) StandaloneGlobalContainer(org.sonarsource.sonarlint.core.container.standalone.StandaloneGlobalContainer) LogOutput(org.sonarsource.sonarlint.core.client.api.common.LogOutput) StandaloneSonarLintEngineImpl(org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl) StandaloneGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) HashSet(java.util.HashSet)

Example 3 with RuleDetails

use of org.sonarsource.sonarlint.core.client.api.common.RuleDetails in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTest method simpleJavaScript.

@Test
public void simpleJavaScript() throws Exception {
    RuleDetails ruleDetails = sonarlint.getRuleDetails("javascript:UnusedVariable");
    assertThat(ruleDetails.getName()).isEqualTo("Unused local variables and functions should be removed");
    assertThat(ruleDetails.getLanguage()).isEqualTo("js");
    assertThat(ruleDetails.getSeverity()).isEqualTo("MINOR");
    assertThat(ruleDetails.getTags()).containsOnly("unused");
    assertThat(ruleDetails.getHtmlDescription()).contains("<p>", "If a local variable or a local function is declared but not used");
    String content = "function foo() {\n" + "  var x;\n" + "  var y; //NOSONAR\n" + "}";
    ClientInputFile inputFile = prepareInputFile("foo.js", content, false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of()), i -> issues.add(i), null, null);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path").containsOnly(tuple("javascript:UnusedVariable", 2, inputFile.getPath()));
    // SLCORE-160
    inputFile = prepareInputFile("node_modules/foo.js", content, false);
    issues.clear();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of()), i -> issues.add(i), null, null);
    assertThat(issues).isEmpty();
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ArrayList(java.util.ArrayList) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) Test(org.junit.Test)

Example 4 with RuleDetails

use of org.sonarsource.sonarlint.core.client.api.common.RuleDetails in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTest method simpleTypeScript.

@Test
public void simpleTypeScript() throws Exception {
    RuleDetails ruleDetails = sonarlint.getRuleDetails("typescript:S1764");
    assertThat(ruleDetails.getName()).isEqualTo("Identical expressions should not be used on both sides of a binary operator");
    assertThat(ruleDetails.getLanguage()).isEqualTo("ts");
    assertThat(ruleDetails.getSeverity()).isEqualTo("MAJOR");
    assertThat(ruleDetails.getTags()).containsOnly("cert");
    assertThat(ruleDetails.getHtmlDescription()).contains("<p>", "Using the same value on either side of a binary operator is almost always a mistake");
    final File tsConfig = new File(baseDir, "tsconfig.json");
    FileUtils.write(tsConfig, "{}", StandardCharsets.UTF_8);
    ClientInputFile inputFile = prepareInputFile("foo.ts", "function foo() {\n" + "  if(bar() && bar()) { return 42; }\n" + "}", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of()), i -> issues.add(i), null, null);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path").containsOnly(tuple("typescript:S1764", 2, inputFile.getPath()));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ArrayList(java.util.ArrayList) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) File(java.io.File) RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) Test(org.junit.Test)

Example 5 with RuleDetails

use of org.sonarsource.sonarlint.core.client.api.common.RuleDetails in project sonarlint-intellij by SonarSource.

the class StandaloneSonarLintFacadeTest method should_get_description.

@Test
public void should_get_description() {
    RuleDetails ruleDetails = mock(RuleDetails.class);
    when(ruleDetails.getExtendedDescription()).thenReturn("desc");
    when(ruleDetails.getHtmlDescription()).thenReturn("html");
    when(engine.getRuleDetails("rule1")).thenReturn(ruleDetails);
    assertThat(facade.getDescription("rule1")).isEqualTo("html<br/><br/>desc");
    assertThat(facade.getDescription("invalid")).isNull();
}
Also used : RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) Test(org.junit.Test)

Aggregations

RuleDetails (org.sonarsource.sonarlint.core.client.api.common.RuleDetails)9 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 ClientInputFile (org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)3 Issue (org.sonarsource.sonarlint.core.client.api.common.analysis.Issue)3 TestClientInputFile (org.sonarsource.sonarlint.core.TestClientInputFile)2 StandaloneAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 RuleKey (org.sonar.api.rule.RuleKey)1 StandaloneSonarLintEngineImpl (org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl)1 LogOutput (org.sonarsource.sonarlint.core.client.api.common.LogOutput)1 ConnectedAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.connected.ConnectedAnalysisConfiguration)1 StandaloneGlobalConfiguration (org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration)1 StandaloneGlobalContainer (org.sonarsource.sonarlint.core.container.standalone.StandaloneGlobalContainer)1