diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-10-16 11:18:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-10-16 11:18:13 +0000 |
commit | 535187ceaf88786aaf22560790564f1f6965ba30 (patch) | |
tree | 82456c5d0bc77961759812ddd85414435ba89127 /pre_commit_hooks/string_fixer.py | |
parent | Adding upstream version 4.4.0+dfsg. (diff) | |
download | pre-commit-hooks-535187ceaf88786aaf22560790564f1f6965ba30.tar.xz pre-commit-hooks-535187ceaf88786aaf22560790564f1f6965ba30.zip |
Adding upstream version 4.5.0+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit_hooks/string_fixer.py')
-rw-r--r-- | pre_commit_hooks/string_fixer.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py index 0ef9bc7..d1b1c4a 100644 --- a/pre_commit_hooks/string_fixer.py +++ b/pre_commit_hooks/string_fixer.py @@ -3,9 +3,16 @@ from __future__ import annotations import argparse import io import re +import sys import tokenize from typing import Sequence +if sys.version_info >= (3, 12): # pragma: >=3.12 cover + FSTRING_START = tokenize.FSTRING_START + FSTRING_END = tokenize.FSTRING_END +else: # pragma: <3.12 cover + FSTRING_START = FSTRING_END = -1 + START_QUOTE_RE = re.compile('^[a-zA-Z]*"') @@ -40,11 +47,17 @@ def fix_strings(filename: str) -> int: # Basically a mutable string splitcontents = list(contents) + fstring_depth = 0 + # Iterate in reverse so the offsets are always correct tokens_l = list(tokenize.generate_tokens(io.StringIO(contents).readline)) tokens = reversed(tokens_l) for token_type, token_text, (srow, scol), (erow, ecol), _ in tokens: - if token_type == tokenize.STRING: + if token_type == FSTRING_START: # pragma: >=3.12 cover + fstring_depth += 1 + elif token_type == FSTRING_END: # pragma: >=3.12 cover + fstring_depth -= 1 + elif fstring_depth == 0 and token_type == tokenize.STRING: new_text = handle_match(token_text) splitcontents[ line_offsets[srow] + scol: |