镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Add warning for regular expression with [\/] (#2043) - #2053

Merged
asottile merged 1 commit into
pre-commit:masterfrom
radek-sprta:master
Sep 22, 2021
Merged

asottile merged 1 commit into
pre-commit:masterfrom
radek-sprta:master

Conversation

@radek-sprta

Copy link
Copy Markdown
Contributor

Add warning about unnecessary slash regex as per #2043

Comment thread pre_commit/clientlib.py
Comment on lines +163 to +174
if r'[\/]' in dct.get(self.key, ''):
logger.warning(
f'Pre-commit normalizes the slashes in {self.key!r} field in '
f"hook {dct.get('id')!r} to forward slashes, so you can use "
f'/ instead of [\\/]',
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add these to the existing SensibleRegex checks

also this should check for both r'[\/]' and r'[/\]' since those are both valid spellings of "either slash"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've merged the check into the existing functions. However, r[/\] is not a valid regex, \] is an escape sequence to enable things like [...\]].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/\\] is though, with the same issue

@radek-sprta radek-sprta Sep 20, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll add it. Although it seems unlikely to me that someone would use [/\\] instead of just [\/].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've kept it in three ifs. If you feel looping through the regexes in for loop would be more readable, let me know.

Comment thread pre_commit/clientlib.py Outdated

if r'[\/]' in dct.get(self.key, ''):
logger.warning(
f'Pre-commit normalizes the slashes in {self.key!r} field in '

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-commit is not capitalized

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I capitalized it as a sentence, even though it wasn't one. Fixed.

Comment thread pre_commit/clientlib.py Outdated
logger.warning(
f'Pre-commit normalizes the slashes in the top-level '
f'{self.key!r} field to forward slashes, so you can use / '
f'instead of [\\/]',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use fr'...' for a raw f-string by the way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@asottile asottile left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile
asottile merged commit ef7b126 into pre-commit:master Sep 22, 2021
@callmesangio

Copy link
Copy Markdown

@asottile @radek-sprta Hi! Thanks for this feature. I just wanted to point out that there's one more permutation that's currently not addressed and you migth want to consider, [\\/]. I have it in one of my .pre-commit-config.yaml files and it didn't get caught by pre-commit 2.16. Cheers!

@asottile

asottile commented Dec 1, 2021

Copy link
Copy Markdown
Member

@sanjioh can you make a new issue please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants