Skip to content

Improve linter with --format json #3

@maxim

Description

@maxim

Hi!

I made a slightly improved erb linter, here's the py file:

import json
import re
from SublimeLinter.lint import util, Linter, LintMatch

class ERBLint(Linter):
    cmd = 'bundle exec erblint ${args} -f json ${temp_file}'
    regex = None
    multiline = True
    tempfile_suffix = 'erb'
    error_stream = util.STREAM_STDOUT
    defaults = {
        'selector': 'text.html.ruby, text.html.rails'
    }

    def get_code_and_message(self, s):
        return re.match(r'^(?:(?P<code>\w+\/\w+):\s)?(?P<message>.*)$', s)

    def find_errors(self, output):
        content = json.loads(output)

        for entry in content['files']:
            if not entry['offenses']:
                continue

            for offense in entry['offenses']:
                match = self.get_code_and_message(offense['message'])

                yield LintMatch(
                    match=offense,
                    line=offense['location']['start_line'] - 1,
                    col=offense['location']['start_column'],
                    end_line=offense['location']['last_line'] - 1,
                    end_col=offense['location']['last_column'],
                    error_type='warning',
                    code=match.group('code'),
                    message=match.group('message')
                )

This has the following improvements over the current linter:

  1. More specific highlights within each line
  2. Separates rubocop's prefixes as "code" from the message
  3. Adds selector text.html.rails
  4. Uses bundle exec (but probably should be made optional)

It's been working well for me so far, so decided to share.

If you want to use it, create a dir under your Sublime's "Packages" dir, like Packages/sublimelinter-custom-erblint, throw this file (name it linter.py), and if you have SublimeLinter installed, this should immediately start working.

Hopefully this will make its way into SublimeLinter-contrib-erblint eventually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions