Exemple #1
0
 def unexpectedError(self, filename, msg):
     loc = types.SourceLocation(line=0, column=0)
     severity = types.Diagnostic.Severity.ERROR
     self.diagnostics.append(
         types.Diagnostic(severity=severity,
                          locations=[loc.to_range()],
                          message=str(msg)))
Exemple #2
0
 def syntaxError(self, filename, msg, lineno, offset, text):
     severity = types.Diagnostic.Severity.ERROR
     col = int(offset) + 1
     loc = types.SourceLocation(line=lineno, column=col)
     self.diagnostics.append(
         types.Diagnostic(severity=severity,
                          locations=[loc.to_range()],
                          message=str(msg)))
Exemple #3
0
 def flake(self, message):
     loc = types.SourceLocation(line=message.lineno,
                                column=int(message.col) + 1)
     severity = types.Diagnostic.Severity.WARNING
     text = message.message % message.message_args
     self.diagnostics.append(
         types.Diagnostic(severity=severity,
                          locations=[loc.to_range()],
                          message=text))
Exemple #4
0
    def parse(self, doc, options):
        doc.diagnostics = []

        try:
            p = subprocess.Popen(['shellcheck', '-f', 'gcc', doc.data_path],
                                 stdout=subprocess.PIPE)
            for line in iter(p.stdout.readline, ''):
                if not line:
                    break
                line = line.decode()
                result = re.search(':(\d*):(\d*): (\w*):(.*)', line).groups()
                loc = types.SourceLocation(line=result[0], column=result[1])
                if result[2] == 'error':
                    severity = types.Diagnostic.Severity.ERROR
                elif result[2] == 'warning':
                    severity = types.Diagnostic.Severity.WARNING
                else:
                    severity = types.Diagnostic.Severity.INFO
                doc.diagnostics.append(
                    types.Diagnostic(severity=severity,
                                     locations=[loc.to_range()],
                                     message=result[3]))
        except FileNotFoundError:
            # shellcheck is not installed. Check with bash dry run mode instead.
            try:
                p = subprocess.Popen(["/bin/bash", "-n", doc.data_path],
                                     stdout=DEVNULL,
                                     stderr=subprocess.PIPE)

                for l in iter(p.stderr.readline, ''):
                    if not l:
                        break

                    m = Service.pattern.match(l.decode())
                    if m:
                        loc = types.SourceLocation(line=m.group(1))

                        doc.diagnostics.append(
                            types.Diagnostic(
                                severity=types.Diagnostic.Severity.ERROR,
                                locations=[loc.to_range()],
                                message=m.group(2)))
            except Exception as e:
                pass
Exemple #5
0
 def error(self, line_number, offset, text, check):
     errorcode = super().error(line_number, offset, text, check)
     if errorcode:
         loc = types.SourceLocation(line=line_number,
                                    column=offset + 1)
         severity = types.Diagnostic.Severity.INFO
         self.diagnostics.append(
             types.Diagnostic(severity=severity,
                              locations=[loc.to_range()],
                              message=text))
Exemple #6
0
    def parse(self, doc, options):
        doc.diagnostics = []

        try:
            with open(doc.data_path) as f:
                json.load(f)

        except json.JSONDecodeError as e:
            start = types.SourceLocation(line=e.lineno, column=e.colno)

            if not e.endlineno is None:
                end = types.SourceLocation(line=e.endlineno, column=e.endcolno)
            else:
                end = None

            severity = types.Diagnostic.Severity.ERROR
            doc.diagnostics = [types.Diagnostic(severity=severity, locations=[types.SourceRange(start=start, end=end)], message=e.msg)]
    def _map_cdiagnostic(self, d):
        loc = self._map_csource_location(d.location)
        severity = self._map_cseverity(d.severity)

        ranges = [self._map_csource_range(r) for r in d.ranges]
        ranges = list(filter(lambda x: not x is None, ranges))
        ranges.insert(0, loc.to_range())

        fixits = [self._map_cfixit(f) for f in d.fixits]
        fixits = list(filter(lambda x: not x is None, fixits))

        message = d.spelling

        return types.Diagnostic(severity=severity,
                                locations=ranges,
                                fixits=fixits,
                                message=message)
Exemple #8
0
    def format_error(self, prefix, error, line=1, column=1):
        if type(error) is etree._LogEntry:
            # specially handle the case where line is 0 since docs start at line 1
            if error.line != 0:
                line = error.line
            if error.column != 0:
                column = error.column

            msg = error.message
        else:
            # it is probably a string or an Exception
            msg = str(error)

        msg = prefix + ': ' + msg
        loc = types.SourceLocation(line=line, column=column)
        severity = types.Diagnostic.Severity.ERROR

        return types.Diagnostic(severity=severity,
                                message=msg,
                                locations=[loc.to_range()])
Exemple #9
0
    def parse(self, doc, options):
        doc.diagnostics = []
        use_pylint = HAS_PYLINT and "pylint" in options and options["pylint"]

        with open(doc.data_path) as f:
            source = f.read()

        if not use_pylint and not HAS_PYFLAKES:
            # both pylint and pyflakes warn about syntax errors, so only need
            # ast.parse() if we can't use either of them
            try:
                ast.parse(source, doc.path)
            except SyntaxError as e:
                loc = types.SourceLocation(line=e.lineno, column=e.offset)
                severity = types.Diagnostic.Severity.ERROR

                doc.diagnostics.append(
                    types.Diagnostic(severity=severity,
                                     locations=[loc.to_range()],
                                     message=e.msg))

        # Pycodestyle / PEP8 checks
        if HAS_PYCODESTYLE or True:
            pycodestyle_checker = PyCodeStyle(source, doc.path)
            for diagnostic in pycodestyle_checker.run():
                doc.diagnostics.append(diagnostic)

        # Pylint checks (if present and enabled)
        if use_pylint:
            pylint = PyLint(doc.data_path)
            diagnostics = pylint.run()

            for diag in diagnostics:
                doc.diagnostics.append(diag)

        # Pyflakes check (if present)
        if HAS_PYFLAKES:
            pyflakes = Pyflakes(doc.data_path)
            diagnostics = pyflakes.run()
            for diag in diagnostics:
                doc.diagnostics.append(diag)
Exemple #10
0
    def parse(self, doc, options):
        doc.diagnostics = []

        # load the yamllint config file, if exists
        config = get_yamllint_config(doc.path)

        with open(doc.data_path) as f:
            for problem in linter.run(f, config, doc.data_path):
                loc = types.SourceLocation(line=problem.line,
                                           column=problem.column)

                severity = types.Diagnostic.Severity.INFO
                if problem.level == 'warning':
                    severity = types.Diagnostic.Severity.WARNING
                elif problem.level == 'error':
                    severity = types.Diagnostic.Severity.ERROR

                doc.diagnostics.append(
                    types.Diagnostic(severity=severity,
                                     locations=[loc.to_range()],
                                     message=problem.message))
Exemple #11
0
    def write(self, st):
        if st != "\n" and not st.startswith("*") and ":" in st:
            result = st.split(":")
            col = int(result[1]) + 1
            loc = types.SourceLocation(line=result[0], column=col)
            """
            * (C) convention, for programming standard violation
            * (R) refactor, for bad code smell
            * (W) warning, for python specific problems
            * (E) error, for much probably bugs in the code
            * (F) fatal, if an error occurred which prevented pylint from doing
            further processing.
            """
            if result[2] == "C" or result[2] == "R" or result[2] == "W":
                severity = types.Diagnostic.Severity.INFO
            else:
                severity = types.Diagnostic.Severity.ERROR

            self.diagnostics.append(
                types.Diagnostic(severity=severity,
                                 locations=[loc.to_range()],
                                 message=result[3]))