Skip to content

Commit

Permalink
Ignore 'misspellings' due to string escapes (#2875)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD authored Jun 14, 2023
1 parent 44b540e commit 463ac02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,19 @@ def parse_file(
word = match.group()
lword = word.lower()
if lword in misspellings:
# Sometimes we find a 'misspelling' which is actually a valid word
# preceded by a string escape sequence. Ignore such cases as
# they're usually false alarms; see issue #17 among others.
char_before_idx = match.start() - 1
if (
char_before_idx >= 0
and line[char_before_idx] == "\\"
# bell, backspace, formfeed, newline, carriage-return, tab, vtab.
and word.startswith(("a", "b", "f", "n", "r", "t", "v"))
and lword[1:] not in misspellings
):
continue

context_shown = False
fix = misspellings[lword].fix
fixword = fix_case(word, misspellings[lword].data)
Expand Down
3 changes: 3 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def test_basic(
with fname.open("a") as f:
f.write("this is a test file\n")
assert cs.main(fname) == 0, "good"
with fname.open("a") as f:
f.write("var = '\\nDoes not error on newline'\n")
assert cs.main(fname) == 0, "with string escape"
with fname.open("a") as f:
f.write("abandonned\n")
assert cs.main(fname) == 1, "bad"
Expand Down

0 comments on commit 463ac02

Please sign in to comment.