Skip to content

Commit 2a93aa3

Browse files
committed
[ref] Make 'normalize_strings' an argument to '__merge_first_string_group(...)'
1 parent f480203 commit 2a93aa3

File tree

1 file changed

+65
-59
lines changed

1 file changed

+65
-59
lines changed

black.py

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,18 +2579,78 @@ def _my_regexp(self) -> str:
25792579
return r"^[\s\S]*$"
25802580

25812581
def _do_transform(self, line: Line, _string_idx: Optional[int]) -> Iterator[Line]:
2582-
# Merge strings that were split across multiple lines using backslashes.
25832582
new_line = self.__remove_backslash_line_continuation_chars(line)
25842583

2585-
(new_line, line_was_changed) = self.__merge_first_string_group(new_line)
2584+
(new_line, line_was_changed) = self.__merge_first_string_group(
2585+
new_line, self.normalize_strings
2586+
)
25862587
while line_was_changed:
2587-
(new_line, line_was_changed) = self.__merge_first_string_group(new_line)
2588+
(new_line, line_was_changed) = self.__merge_first_string_group(
2589+
new_line, self.normalize_strings
2590+
)
25882591

25892592
new_line = self.__remove_bad_trailing_commas(new_line)
25902593

25912594
yield new_line
25922595

2593-
def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:
2596+
@staticmethod
2597+
def __remove_backslash_line_continuation_chars(line: Line) -> Line:
2598+
"""Merge strings that were split across multiple lines using backslashes."""
2599+
for leaf in line.leaves:
2600+
if (
2601+
leaf.type == token.STRING
2602+
and "\\\n" in leaf.value
2603+
and leaf.value.lstrip(STRING_PREFIX_CHARS)[:3] not in {'"""', "'''"}
2604+
):
2605+
break
2606+
else:
2607+
return line
2608+
2609+
new_line = line.clone()
2610+
new_line.comments = line.comments
2611+
append_leaves(new_line, line, line.leaves)
2612+
for leaf in new_line.leaves:
2613+
if leaf.type == token.STRING and leaf.value.lstrip(STRING_PREFIX_CHARS)[
2614+
:3
2615+
] not in {'"""', "'''"}:
2616+
leaf.value = leaf.value.replace("\\\n", "")
2617+
2618+
return new_line
2619+
2620+
@staticmethod
2621+
def __remove_bad_trailing_commas(line: Line) -> Line:
2622+
line_str = line_to_string(line)
2623+
if not re.match(r"^[A-Za-z0-9_]+\(\(?" + STRING_REGEXP + r"\)?,\)", line_str):
2624+
return line
2625+
2626+
already_seen_lpar = False
2627+
skip_next_rpar = False
2628+
2629+
new_line = line.clone()
2630+
for old_leaf in line.leaves:
2631+
if old_leaf.type == token.COMMA:
2632+
continue
2633+
2634+
if already_seen_lpar and old_leaf.type == token.LPAR:
2635+
skip_next_rpar = True
2636+
continue
2637+
2638+
if old_leaf.type == token.LPAR:
2639+
already_seen_lpar = True
2640+
2641+
if skip_next_rpar and old_leaf.type == token.RPAR:
2642+
skip_next_rpar = False
2643+
continue
2644+
2645+
new_leaf = Leaf(old_leaf.type, old_leaf.value)
2646+
replace_child(old_leaf, new_leaf)
2647+
new_line.append(new_leaf)
2648+
2649+
return new_line
2650+
2651+
def __merge_first_string_group(
2652+
self, line: Line, normalize_strings: bool
2653+
) -> Tuple[Line, bool]:
25942654
first_str_idx = self.__get_string_group_index(line)
25952655

25962656
if first_str_idx is None:
@@ -2655,7 +2715,7 @@ def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:
26552715
return (line, False)
26562716

26572717
temp_string_leaf = Leaf(token.STRING, string_value)
2658-
if self.normalize_strings:
2718+
if normalize_strings:
26592719
normalize_string_quotes(temp_string_leaf)
26602720

26612721
naked_string_value = temp_string_leaf.value[len(prefix) + 1 : -1]
@@ -2728,60 +2788,6 @@ def __get_string_group_index(line: Line) -> Optional[int]:
27282788

27292789
return None
27302790

2731-
@staticmethod
2732-
def __remove_backslash_line_continuation_chars(line: Line) -> Line:
2733-
for leaf in line.leaves:
2734-
if (
2735-
leaf.type == token.STRING
2736-
and "\\\n" in leaf.value
2737-
and leaf.value.lstrip(STRING_PREFIX_CHARS)[:3] not in {'"""', "'''"}
2738-
):
2739-
break
2740-
else:
2741-
return line
2742-
2743-
new_line = line.clone()
2744-
new_line.comments = line.comments
2745-
append_leaves(new_line, line, line.leaves)
2746-
for leaf in new_line.leaves:
2747-
if leaf.type == token.STRING and leaf.value.lstrip(STRING_PREFIX_CHARS)[
2748-
:3
2749-
] not in {'"""', "'''"}:
2750-
leaf.value = leaf.value.replace("\\\n", "")
2751-
2752-
return new_line
2753-
2754-
@staticmethod
2755-
def __remove_bad_trailing_commas(line: Line) -> Line:
2756-
line_str = line_to_string(line)
2757-
if not re.match(r"^[A-Za-z0-9_]+\(\(?" + STRING_REGEXP + r"\)?,\)", line_str):
2758-
return line
2759-
2760-
already_seen_lpar = False
2761-
skip_next_rpar = False
2762-
2763-
new_line = line.clone()
2764-
for old_leaf in line.leaves:
2765-
if old_leaf.type == token.COMMA:
2766-
continue
2767-
2768-
if already_seen_lpar and old_leaf.type == token.LPAR:
2769-
skip_next_rpar = True
2770-
continue
2771-
2772-
if old_leaf.type == token.LPAR:
2773-
already_seen_lpar = True
2774-
2775-
if skip_next_rpar and old_leaf.type == token.RPAR:
2776-
skip_next_rpar = False
2777-
continue
2778-
2779-
new_leaf = Leaf(old_leaf.type, old_leaf.value)
2780-
replace_child(old_leaf, new_leaf)
2781-
new_line.append(new_leaf)
2782-
2783-
return new_line
2784-
27852791

27862792
class StringSplitterMixin(StringTransformerMixin):
27872793
STRING_CHILD_IDX_MAP: ClassVar[Dict[int, Optional[int]]] = {}

0 commit comments

Comments
 (0)