bugfix(patterns): word-boundary slug match — fix false positive on short slugs#6
Open
CryptoJones wants to merge 1 commit into
Open
bugfix(patterns): word-boundary slug match — fix false positive on short slugs#6CryptoJones wants to merge 1 commit into
CryptoJones wants to merge 1 commit into
Conversation
…ort slugs
`_slug_in_project` did a naive substring search:
if slug.lower() in file_text.lower(): return True
That false-positives on any short slug whose letters appear inside a
longer word. Concrete cases:
slug "auth" matches "author", "authentic", "authority"
slug "api" matches "apiary", "rapidly", "tropical"
slug "db" matches "subdivision", "subdued"
slug "validate-numbers" matches "validate-numbers-attempt"
When that happens, the pattern is reported as "used elsewhere" and
silently does NOT appear in the UNUSED findings — masking genuinely
unused patterns and undermining the whole point of the report.
Fix: compile a word-boundary regex with custom boundary chars
(`\w` + `-`) so kebab-case slugs match as complete tokens but not as
prefixes/suffixes of longer kebab identifiers, and short slugs don't
match inside longer words.
(?<![\w-]){re.escape(slug)}(?![\w-])
Tests added (3):
- slug `auth` in a project that contains `author` -> still UNUSED
- slug `validate-numbers` in a project that contains
`validate-numbers-attempt` -> still UNUSED
- positive control: exact `validate-numbers` mention -> NOT unused
150/150 tests pass; ruff + mypy clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_slug_in_projectdid a naive substring search:That false-positives on any short slug whose letters appear inside a
longer word. Concrete cases:
slug "auth" matches "author", "authentic", "authority"
slug "api" matches "apiary", "rapidly", "tropical"
slug "db" matches "subdivision", "subdued"
slug "validate-numbers" matches "validate-numbers-attempt"
When that happens, the pattern is reported as "used elsewhere" and
silently does NOT appear in the UNUSED findings — masking genuinely
unused patterns and undermining the whole point of the report.
Fix: compile a word-boundary regex with custom boundary chars
(
\w+-) so kebab-case slugs match as complete tokens but not asprefixes/suffixes of longer kebab identifiers, and short slugs don't
match inside longer words.
(?<![\w-]){re.escape(slug)}(?![\w-])
Tests added (3):
authin a project that containsauthor-> still UNUSEDvalidate-numbersin a project that containsvalidate-numbers-attempt-> still UNUSEDvalidate-numbersmention -> NOT unused150/150 tests pass; ruff + mypy clean.