Skip to content

Instantly share code, notes, and snippets.

@bkhanale
Created July 22, 2019 12:29
Show Gist options
  • Save bkhanale/204a11b3dd2cbbb581c3fddbe66ae75f to your computer and use it in GitHub Desktop.
Save bkhanale/204a11b3dd2cbbb581c3fddbe66ae75f to your computer and use it in GitHub Desktop.
import re
from queue import Queue
from sarge import run, Capture
from contextlib import suppress
from bears.general.AnnotationBear import AnnotationBear
from coalib.bears.LocalBear import LocalBear
from coalib.results.Result import Result
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting
from coalib.testing.LocalBearTestHelper import execute_bear
from dependency_management.requirements.PipRequirement import PipRequirement
class RegexLintBear(LocalBear):
LANGUAGES = {'All'}
REQUIREMENTS = {PipRequirement('regexlint', '1.6')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Formatting'}
def run(self, filename, file, language: str):
"""
Bear for linting regex through regexlint.
:param language:
The programming language of the file(s).
"""
section = Section('')
section.append(Setting('language', language))
bear = AnnotationBear(section, Queue())
with execute_bear(bear, filename, file) as result:
for src_range in result[0].contents['strings']:
src_line = src_range.affected_source({filename: file})[0]
regex = src_line[src_range.start.column:src_range.end.column-1]
with suppress(re.error):
re.compile(regex)
out = run('regexlint --regex "{}"'.format(regex),
stdout=Capture()).stdout.text
if out[-3:-1] != 'OK':
yield Result.from_values(
origin=self,
message=out,
file=filename,
line=src_range.start.line,
column=src_range.start.column,
end_line=src_range.end.line,
end_column=src_range.end.column,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment