JavaScriptSensor { public void execute(SensorContext context) { checkDeprecatedEslintProperty(context); List<TreeVisitor> treeVisitors = new ArrayList<>(); treeVisitors.add(new SeChecksDispatcher(checks.seChecks())); treeVisitors.addAll(checks.visitorChecks()); Iterable<InputFile> inputFiles = getInputFiles(context); Collection<String> files = StreamSupport.stream(inputFiles.spliterator(), false) .map(InputFile::toString) .collect(Collectors.toList()); ProgressReport progressReport = new ProgressReport("Report about progress of Javascript analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(files); analyseFiles(context, treeVisitors, inputFiles, progressReport); } JavaScriptSensor(AbstractChecks checks); void execute(SensorContext context);  }
@Test public void should_only_log_debug_if_a_parsing_error() { String relativePath = "cpd/parsingError.js"; InputFile inputFile = inputFile(relativePath); String parsingErrorCheckKey = "ParsingError"; ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setName("ParsingError").setRuleKey(RuleKey.of(CheckList.JS_REPOSITORY_KEY, parsingErrorCheckKey)).build()) .build(); checkFactory = new CheckFactory(activeRules); context.setActiveRules(activeRules); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(0); assertThat(context.allAnalysisErrors()).hasSize(0); assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).isEqualTo("Unable to parse file with java-based frontend (some rules will not be executed): " + inputFile.toString()); assertThat(logTester.logs(LoggerLevel.DEBUG).get(1)).startsWith("Parse error at line 3 column 1:"); }
@Test public void should_not_add_error_to_context_when_analysis_raises_no_issue() { inputFile("file.js"); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(0); assertThat(context.allAnalysisErrors()).isEmpty(); }
@Test public void should_not_add_error_to_context_when_analysis_raises_issues() { inputFile("file.js"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.JS_REPOSITORY_KEY, "S1314")).build()) .build(); checkFactory = new CheckFactory(activeRules); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); assertThat(context.allAnalysisErrors()).isEmpty(); }
@Test public void should_save_issue() throws Exception { inputFile("file.js"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder() .setRuleKey(RuleKey.of(CheckList.JS_REPOSITORY_KEY, "S1314")) .build()) .build(); checkFactory = new CheckFactory(activeRules); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); assertThat(logTester.logs(LoggerLevel.WARN)).doesNotContain("SonarJS custom rules usage is deprecated. Consider using ESlint custom rules instead"); }
@Test public void should_run_custom_rule() throws Exception { inputFile("file.js"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("customKey", "key")).build()) .build(); checkFactory = new CheckFactory(activeRules); createSensorWithCustomRules().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); Map<Integer, Issue> issueByLine = issues.stream().collect(Collectors.toMap(issue -> issue.primaryLocation().textRange().start().line(), i -> i)); assertThat(issueByLine.keySet()).containsExactlyInAnyOrder(1); Issue issue = issueByLine.get(1); assertThat(issue.gap()).isEqualTo(42); assertThat(issue.primaryLocation().message()).isEqualTo("Message of custom rule"); assertThat(issue.primaryLocation().textRange()).isEqualTo(new DefaultTextRange(new DefaultTextPointer(1, 0), new DefaultTextPointer(1, 7))); }
@Test public void should_run_custom_rule_repository() throws Exception { inputFile("file.js"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("custom-repo", "key2")).build()) .build(); checkFactory = new CheckFactory(activeRules); createSensorWithCustomRuleRepository().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); Map<Integer, Issue> issueByLine = issues.stream().collect(Collectors.toMap(issue -> issue.primaryLocation().textRange().start().line(), i -> i)); assertThat(issueByLine.keySet()).containsExactlyInAnyOrder(2); Issue issue = issueByLine.get(2); assertThat(issue.gap()).isEqualTo(42); assertThat(issue.primaryLocation().message()).isEqualTo("Message of custom rule"); assertThat(issue.primaryLocation().textRange()).isEqualTo(new DefaultTextRange(new DefaultTextPointer(2, 0), new DefaultTextPointer(2, 5))); }
@Test public void should_log_deprecation_warning() throws Exception { JavaScriptSensor sensor = createSensor(CUSTOM_RULES, null); sensor.execute(context); assertThat(logTester.logs(LoggerLevel.WARN)).contains("JavaScript analyzer custom rules are deprecated. Consider using ESlint custom rules instead"); logTester.clear(); sensor = createSensor(null, CUSTOM_RULE_REPOSITORIES); sensor.execute(context); assertThat(logTester.logs(LoggerLevel.WARN)).doesNotContain("JavaScript analyzer custom rules are deprecated. Consider using ESlint custom rules instead"); }
@Test public void should_log_deprecated_property_used() throws Exception { context.settings().setProperty(DEPRECATED_ESLINT_PROPERTY, "eslint-report.json"); createSensor().execute(context); assertThat(logTester.logs(LoggerLevel.WARN)).contains("Property 'sonar.typescript.eslint.reportPaths' is deprecated, use 'sonar.eslint.reportPaths'."); }