Skip to content

Commit aad43de

Browse files
Fixed edge cases and updated regexes to match replacement criteria
1 parent 7e9774a commit aad43de

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

docs/linters.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -638,16 +638,15 @@ lintersConfig:
638638
The `noreferences` linter can automatically fix field names in **PreferAbbreviatedReference mode**:
639639

640640
**PreferAbbreviatedReference mode:**
641-
- Replaces 'Reference' with 'Ref' and 'References' with 'Refs' at the start or end of field names
642-
- This avoids false positives for words like "Preference" where "eference" appears in the middle
641+
- Replaces 'Reference' with 'Ref' and 'References' with 'Refs' anywhere in field names
642+
- Case insensitive matching
643643
- Examples:
644-
- `NodeReference``NodeRef` (end)
645-
- `ReferenceNode``RefNode` (start)
646-
- `NodeReferences``NodeRefs` (end)
647-
- `ReferencesCount``RefsCount` (start)
648-
- `PreferenceType` → no change (middle - not flagged)
644+
- `NodeReference``NodeRef`
645+
- `ReferenceNode``RefNode`
646+
- `NodeReferences``NodeRefs`
649647

650-
**Note:** NoReferences policy does not provide automatic fixes. It only reports warnings to inform developers about the presence of reference-related words in field names.
648+
**Note:**
649+
- The `NoReferences` mode only reports warnings without providing fixes, allowing developers to choose appropriate field names manually.
651650

652651
## SSATags
653652

pkg/analysis/noreferences/analyzer.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,23 @@ func newAnalyzer(cfg *Config) *analysis.Analyzer {
7777
func buildConventions(policy Policy) []namingconventions.Convention {
7878
switch policy {
7979
case PolicyPreferAbbreviatedReference:
80-
// Replace "Reference" or "References" with "Ref" or "Refs"
81-
// Single regex: matches Reference(s) at start or end, preserving the 's' if present
82-
// Pattern breakdown:
83-
// ^[Rr]eference(s?) - matches Reference/References at start, $1 captures optional 's'
84-
// | - OR
85-
// Reference(s?)$ - matches Reference/References at end, $2 captures optional 's'
86-
// Replacement: Ref$1$2 - keeps whichever 's' was captured (only one will be non-empty)
80+
// Replace "Reference" or "References" with "Ref" or "Refs" anywhere in field name
81+
// Case insensitive to handle lowercase in JSON tags
82+
// Note: May produce false positives like "Preference" → "PRef", but this is acceptable
8783
return []namingconventions.Convention{
8884
{
8985
Name: "reference-to-ref",
90-
ViolationMatcher: "^[Rr]eference(s?)|Reference(s?)$",
86+
ViolationMatcher: "^[Rr]eference|Reference(s?)$",
9187
Operation: namingconventions.OperationReplacement,
92-
Replacement: "Ref$1$2",
88+
Replacement: "Ref$1",
9389
Message: "field names should use 'Ref' instead of 'Reference'",
9490
},
9591
}
9692

9793
case PolicyNoReferences:
98-
// Warn about reference words without modifying them
99-
// At start: [Rr]ef(erence)?(s?)([A-Z]) - matches Ref/Reference at start
100-
// At end: Ref(erence)?(s?)$ - matches Ref/Reference at end (capital R to avoid "*preference")
94+
// Warn about reference words anywhere in field name without providing fixes
95+
// Case insensitive to handle all variations
96+
// Note: May flag false positives like "Preference", but user is warned to manually review
10197
return []namingconventions.Convention{
10298
{
10399
Name: "no-references",

0 commit comments

Comments
 (0)