From acbb8f05466a3d3bbb08c2f0baf60cbef5047b95 Mon Sep 17 00:00:00 2001 From: Alexander-N Date: Sun, 6 Aug 2017 05:01:01 +0200 Subject: [PATCH] JSStandardBear: Add fixing capabilities The corrected code is retrieved by piping to JSStandard. If there several issues in a single line, the messages are displayed together to make it easy to understand the proposed fix. Closes https://github.com/coala/coala-bears/issues/1952 --- bears/js/JSStandardBear.py | 71 ++++++++++++++++++++++++++++++++-- tests/js/JSStandardBearTest.py | 60 ++++++++++++++++++++++------ 2 files changed, 117 insertions(+), 14 deletions(-) diff --git a/bears/js/JSStandardBear.py b/bears/js/JSStandardBear.py index a68f999259..8818ed658c 100644 --- a/bears/js/JSStandardBear.py +++ b/bears/js/JSStandardBear.py @@ -1,11 +1,17 @@ +from collections import defaultdict +import re +from subprocess import Popen, PIPE +from typing import List, Iterable, Tuple + from coalib.bearlib.abstractions.Linter import linter from dependency_management.requirements.NpmRequirement import NpmRequirement +from coalib.results.Result import Result +from coalib.results.Diff import Diff @linter(executable='standard', - output_format='regex', - output_regex=r'\s*[^:]+:(?P\d+):(?P\d+):' - r'\s*(?P.+)') + use_stdin=True, + use_stderr=True) class JSStandardBear: """ One JavaScript Style to Rule Them All. @@ -24,5 +30,64 @@ class JSStandardBear: CAN_FIX = {'Formatting'} SEE_MORE = 'https://standardjs.com/rules.html' + issue_regex = re.compile( + r'\s*[^:]+:(?P\d+):(?P\d+):' + r'\s*(?P.+)') + def create_arguments(self, filename, file, config_file): return (filename, '--verbose') + + @staticmethod + def _get_corrected_code(old_code: List[str]) -> List[str]: + """ + Pipes the code to JSStandard and returns the corrected code. + """ + p = Popen( + ('standard', '--stdin', '--fix'), + stdin=PIPE, stdout=PIPE, stderr=PIPE) + p.stdin.write(bytes(''.join(old_code), 'UTF-8')) + out, err = p.communicate() + return out.decode('UTF-8').splitlines(True) + + def _get_issues(self, stdout: str) -> Iterable[Tuple[int, str]]: + """ + Gets the issues from the output of JSStandard. + + The issues get parsed with ``self.issue_regex`` and merged if they + concern the same line. + + :param stdout: Output from which the issues get parsed. + :return: List of tuples containing the line number and the message. + """ + match_objects = ( + self.issue_regex.match(line) for line in stdout.splitlines()) + issues = ( + match_object.groupdict() + for match_object in match_objects if match_object is not None) + line_number_to_messages = defaultdict(list) + for issue in issues: + line_number = int(issue['line']) + line_number_to_messages[line_number].append(issue['message']) + return ( + (line_number, '\n'.join(messages)) + for line_number, messages in line_number_to_messages.items()) + + def process_output(self, output, filename, file): + stdout, stderr = output + corrected_code = None + if '--fix' in stderr: + corrected_code = self._get_corrected_code(file) + if len(file) != len(corrected_code): + corrected_code = None + + for line_number, message in self._get_issues(stdout): + diff = None + if corrected_code: + diff = Diff(file) + diff.modify_line(line_number, corrected_code[line_number-1]) + yield Result.from_values( + origin=self, + message=message, + file=filename, + line=line_number, + diffs={filename: diff}) diff --git a/tests/js/JSStandardBearTest.py b/tests/js/JSStandardBearTest.py index 30b638de0b..635810e10c 100644 --- a/tests/js/JSStandardBearTest.py +++ b/tests/js/JSStandardBearTest.py @@ -1,8 +1,11 @@ -from coalib.testing.LocalBearTestHelper import verify_local_bear +from queue import Queue +from coalib.settings.Section import Section +from coalib.testing.BearTestHelper import generate_skip_decorator +from coalib.testing.LocalBearTestHelper import verify_local_bear +from coalib.testing.LocalBearTestHelper import LocalBearTestHelper from bears.js.JSStandardBear import JSStandardBear - good_file = """ var foo = { bar: 1, @@ -68,12 +71,47 @@ } """ -JSStandardBearTest = verify_local_bear(JSStandardBear, - valid_files=(good_file,), - invalid_files=(bad_file_indent, - bad_file_quote, - bad_file_semicolon, - bad_file_infix, - bad_file_undef, - bad_file_ifelse, - bad_file_func_name,)) + +JSStandardBearTest = verify_local_bear( + JSStandardBear, + valid_files=(good_file,), + invalid_files=(bad_file_indent, + bad_file_quote, + bad_file_semicolon, + bad_file_infix, + bad_file_undef, + bad_file_ifelse, + bad_file_func_name,)) + + +@generate_skip_decorator(JSStandardBear) +class JSStandardBearTestClass(LocalBearTestHelper): + bad_code = """ +(function () { + console.log('wrong indentation') + console.log('wrong indentation and semicolon'); +}()) +""".splitlines(True) + + def setUp(self): + section = Section('name') + self.js_standard_bear = JSStandardBear(section, Queue()) + + def test_messages(self): + results = self.check_invalidity( + self.js_standard_bear, + self.bad_code) + self.assertIn('(indent)', results[0].message) + # Check that messages for lines with multiple issues get merged. + self.assertIn('(indent)', results[1].message) + self.assertIn('(semi)', results[1].message) + + def test_corrected_code(self): + """ + Check that the corrected code is valid. + """ + corrected_code = self.js_standard_bear._get_corrected_code( + self.bad_code) + self.check_validity( + self.js_standard_bear, + corrected_code)