Skip to content

Commit

Permalink
Ensure closing characters in strings are ignored (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler authored Jun 5, 2019
1 parent 643bb57 commit c528784
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
60 changes: 42 additions & 18 deletions flake8_multiline_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,24 @@ class MultilineContainers:
# The column where the last line that opened started
last_starts_at = attr.ib(factory=list)

def _check_opening(
self,
open_character: str,
close_character: str,
line_number: int,
line: str,
error_code: ErrorCodes,
):
"""Implementation for JS101.
def _number_of_matches_in_line(
self,
open_character: str,
close_character: str,
line: str) -> tuple:
"""Scan line and check how many times each character appears.
If open_character and close_character don't appear the same number of
times on the line, then open_character should be last character in the
line.
Characters inside strings are ignored.
Arguments:
open_character: Opening character for the container.
close_character: Closing character for the container.
line_number: The number of the line. Reported back to flake8.
line: The line to check.
error_code: The error to report if the validation fails.
Returns:
tuple
"""
# Scan the line for parts which are a string.
open_matches_in_string = 0
close_matches_in_string = 0

Expand All @@ -86,6 +81,34 @@ def _check_opening(
open_times -= open_matches_in_string
close_times -= close_matches_in_string

return open_times, close_times

def _check_opening(
self,
open_character: str,
close_character: str,
line_number: int,
line: str,
error_code: ErrorCodes,
):
"""Implementation for JS101.
If open_character and close_character don't appear the same number of
times on the line, then open_character should be last character in the
line.
Arguments:
open_character: Opening character for the container.
close_character: Closing character for the container.
line_number: The number of the line. Reported back to flake8.
line: The line to check.
error_code: The error to report if the validation fails.
"""
open_times, close_times = self._number_of_matches_in_line(
open_character, close_character, line,
)

if open_times >= 1 and open_times != close_times:
self.last_starts_at.append(get_left_pad(line))

Expand Down Expand Up @@ -116,10 +139,11 @@ def _check_closing(
error_code: The error to report if the validation fails.
"""
line_opens = True if open_character in line else False
line_closes = True if close_character in line else False
open_times, close_times = self._number_of_matches_in_line(
open_character, close_character, line,
)

if line_closes and not line_opens:
if close_times > 0 and open_times == 0:
for index, i in enumerate(line):
if i == close_character:
if index != self.last_starts_at[-1]:
Expand Down
6 changes: 6 additions & 0 deletions tests/dummy/string_examples.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
s = '{', '}', "foo={a\n", 'bar'
s = '{', '}', "foo=}a\n", 'bar'

string_only_opens_sq = '[foobar'
string_only_closes_sq = 'foobar]'

string_only_opens_dq = "[foobar"
string_only_closes_dq = "foobar]"

0 comments on commit c528784

Please sign in to comment.