Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Sep 22, 2021

Conversation

radek-sprta
Copy link
Contributor

Add warning about unnecessary slash regex as per #2043

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
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
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
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

Copy link
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
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.


if r'[\/]' in dct.get(self.key, ''):
logger.warning(
f'Pre-commit normalizes the slashes in {self.key!r} field in '
Copy link
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
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.

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
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
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

@asottile asottile left a comment

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
@sanjioh
Copy link

sanjioh commented Dec 1, 2021

@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
Copy link
Member

asottile commented Dec 1, 2021

@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.

None yet

3 participants