@VisibleForTesting
 protected void analyseFiles(
     SensorContext context,
     List<TreeVisitor> treeVisitors,
     Iterable<InputFile> inputFiles,
     ProgressReport progressReport) {
   boolean success = false;
   try {
     for (InputFile inputFile : inputFiles) {
       // check for cancellation of the analysis (by SonarQube or SonarLint). See SONARJS-761.
       if (context.getSonarQubeVersion().isGreaterThanOrEqual(V6_0) && context.isCancelled()) {
         throw new CancellationException(
             "Analysis interrupted because the SensorContext is in cancelled state");
       }
       if (!isExcluded(inputFile.file())) {
         analyse(context, inputFile, treeVisitors);
       }
       progressReport.nextFile();
     }
     success = true;
   } catch (CancellationException e) {
     // do not propagate the exception
     LOG.debug(e.toString());
   } finally {
     stopProgressReport(progressReport, success);
   }
 }
  private void scanFile(
      SensorContext sensorContext,
      InputFile inputFile,
      List<TreeVisitor> visitors,
      ScriptTree scriptTree) {
    JavaScriptVisitorContext context =
        new JavaScriptVisitorContext(scriptTree, inputFile.file(), sensorContext.settings());

    highlightSymbols(sensorContext.newSymbolTable().onFile(inputFile), context);

    List<Issue> fileIssues = new ArrayList<>();

    for (TreeVisitor visitor : visitors) {
      if (visitor instanceof CharsetAwareVisitor) {
        ((CharsetAwareVisitor) visitor).setCharset(fileSystem.encoding());
      }

      if (visitor instanceof JavaScriptCheck) {
        fileIssues.addAll(((JavaScriptCheck) visitor).scanFile(context));

      } else {
        visitor.scanTree(context);
      }
    }

    saveFileIssues(sensorContext, fileIssues, inputFile);
  }
Esempio n. 3
0
  /** Create issue report according to issue list generated during SonarQube analysis. */
  public static SonarQubeIssuesReport extractIssueReport(
      ProjectIssues projectIssues, InputFileCache inputFileCache, File projectBaseDir) {
    SonarQubeIssuesReport result = new SonarQubeIssuesReport();

    for (Issue issue : projectIssues.issues()) {
      if (!issue.isNew()) {
        LOGGER.debug("Issue {} is not a new issue and so, not added to the report", issue.key());
      } else {
        String key = issue.key();
        String severity = issue.severity();
        String rule = issue.ruleKey().toString();
        String message = issue.message();

        int line = 0;
        if (issue.line() != null) {
          line = issue.line();
        }

        InputFile inputFile = inputFileCache.getInputFile(issue.componentKey());
        if (inputFile == null) {
          LOGGER.debug("Issue {} is not linked to a file, not added to the report", issue.key());
        } else {
          String path = new PathResolver().relativePath(projectBaseDir, inputFile.file());

          // Create the issue and Add to report
          SonarQubeIssue stashIssue = new SonarQubeIssue(key, severity, message, rule, path, line);
          result.add(stashIssue);
        }
      }
    }

    return result;
  }
Esempio n. 4
0
  /**
   * Check if the xml file starts with a prolog "&lt?xml version="1.0" ?&gt" if so, check if there
   * is any characters prefixing it.
   */
  private void checkForCharactersBeforeProlog(FileSystem fileSystem) {
    try {
      int lineNb = 1;
      Pattern firstTagPattern = Pattern.compile("<[a-zA-Z?]+");
      boolean hasBOM = false;

      for (String line : Files.readLines(inputFile.file(), fileSystem.encoding())) {
        if (lineNb == 1 && line.startsWith(BOM_CHAR)) {
          hasBOM = true;
          characterDeltaForHighlight = -1;
        }

        Matcher m = firstTagPattern.matcher(line);
        if (m.find()) {
          int column = line.indexOf(m.group());

          if (XML_PROLOG_START_TAG.equals(m.group()) && !isFileBeginning(lineNb, column, hasBOM)) {
            hasCharsBeforeProlog = true;
          }
          break;
        }
        lineNb++;
      }

      if (hasCharsBeforeProlog) {
        processCharBeforePrologInFile(fileSystem, lineNb);
      }
    } catch (IOException e) {
      LOG.warn("Unable to analyse file {}", inputFile.absolutePath(), e);
    }
  }
Esempio n. 5
0
 @Override
 public boolean accept(InputFile inputFile) {
   if (filters.length > 0) {
     DeprecatedContext context = new DeprecatedContext(inputFile);
     for (FileSystemFilter filter : filters) {
       if (!filter.accept(inputFile.file(), context)) {
         return false;
       }
     }
   }
   return true;
 }
Esempio n. 6
0
  /**
   * Create a temporary file without any character before the prolog and update the following
   * attributes in order to correctly report issues:
   *
   * <ul>
   *   <li>lineDeltaForIssue
   *   <li>file
   */
  private void processCharBeforePrologInFile(FileSystem fileSystem, int lineDelta) {
    try {
      String content = Files.toString(inputFile.file(), fileSystem.encoding());
      File tempFile = new File(fileSystem.workDir(), inputFile.file().getName());

      int index = content.indexOf(XML_PROLOG_START_TAG);
      Files.write(content.substring(index), tempFile, fileSystem.encoding());

      noCharBeforePrologFile = tempFile;

      if (index != -1) {
        characterDeltaForHighlight += index;
      }

      if (lineDelta > 1) {
        lineDeltaForIssue = lineDelta - 1;
      }

    } catch (IOException e) {
      LOG.warn("Unable to analyse file {}", inputFile.absolutePath(), e);
    }
  }
Esempio n. 7
0
 public File getIOFile() {
   return noCharBeforePrologFile != null ? noCharBeforePrologFile : inputFile.file();
 }