Skip to content

Commit

Permalink
JSONFormatBear.py: Implement max_line_length
Browse files Browse the repository at this point in the history
The max_line_length for JSON is infinity by default.
It can be modified if a user is sure about the line length.

Closes coala#2743
  • Loading branch information
bkhanale authored and ZamaSharik committed Nov 15, 2018
1 parent dba2cd0 commit 1d23525
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
31 changes: 27 additions & 4 deletions bears/js/JSONFormatBear.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import sys
from collections import OrderedDict
from re import match

Expand All @@ -23,18 +24,24 @@ class JSONFormatBear(LocalBear):
@deprecate_settings(indent_size='tab_width',
escape_unicode=('keep_unicode', negate))
def run(self, filename, file,
max_line_length: int = 0,
json_sort: bool = False,
indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH,
escape_unicode: bool = True,
):
"""
Raises issues for any deviations from the pretty-printed JSON.
:param json_sort: Whether or not keys should be sorted.
:param indent_size: Number of spaces per indentation level.
:param escape_unicode: Whether or not to escape unicode values using
ASCII.
:param max_line_length: Maximum number of characters for a line.
When set to 0 allows infinite line length.
:param json_sort: Whether or not keys should be sorted.
:param indent_size: Number of spaces per indentation level.
:param escape_unicode: Whether or not to escape unicode values using
ASCII.
"""
if not max_line_length:
max_line_length = sys.maxsize

# Output a meaningful message if empty file given as input
if len(file) == 0:
yield Result.from_values(self,
Expand All @@ -56,6 +63,22 @@ def run(self, filename, file,
column=int(err_content.group(3)))
return

for line_number, line in enumerate(file):
line = line.expandtabs(indent_size)
if len(line) > max_line_length + 1:
yield Result.from_values(
origin=self,
message='Line is longer than allowed.'
' ({actual} > {maximum})'.format(
actual=len(line)-1,
maximum=max_line_length),
file=filename,
line=line_number + 1,
column=max_line_length + 1,
end_line=line_number + 1,
end_column=len(line),
)

corrected = json.dumps(json_content,
sort_keys=json_sort,
indent=indent_size,
Expand Down
23 changes: 23 additions & 0 deletions tests/js/JSONFormatBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
}"""


# This will generate a line with 80 characters
max_line_length_file1 = '{\n "string": "' + 'a' * 64 + '"\n}'

# This will generate a line with 79 characters
max_line_length_file2 = '{\n "string": "' + 'a' * 63 + '"\n}'


class JSONTest(LocalBearTestHelper):

def setUp(self):
Expand Down Expand Up @@ -91,3 +98,19 @@ def test_exception_empty_file(self):
invalid_files=(),
settings={'escape_unicode':
'false'})


JSONFormatBearInfLineLengthTest = verify_local_bear(JSONFormatBear,
valid_files=(
max_line_length_file1,
max_line_length_file2),
invalid_files=())


JSONFormatBearLineLengthTest = verify_local_bear(JSONFormatBear,
valid_files=(
max_line_length_file2,),
invalid_files=(
max_line_length_file1,),
settings={'max_line_length':
'79'})

0 comments on commit 1d23525

Please sign in to comment.