Skip to content

Commit 7e9774a

Browse files
Reverted noreferences policy to now warn only
1 parent f7c385a commit 7e9774a

File tree

4 files changed

+16
-63
lines changed

4 files changed

+16
-63
lines changed

docs/linters.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,13 @@ lintersConfig:
629629
- **Allows** fields containing 'Ref' or 'Refs' without reporting errors
630630

631631
**Strict mode (policy: NoReferences):**
632-
- Reports errors for any reference-related words in field names
633-
- Suggests removing 'Ref', 'Refs', 'Reference', or 'References' entirely from the beginning or end of field names
634-
- In this strict mode, the goal is to avoid all reference-related words in field names
632+
- **Warns** about any reference-related words ('Ref', 'Refs', 'Reference', or 'References') in field names
633+
- Does not provide automatic fixes - serves as an informational warning
634+
- In this strict mode, the goal is to inform developers about reference-related words in field names
635635

636636
### Fixes
637637

638-
The `noreferences` linter can automatically fix field names based on the policy:
638+
The `noreferences` linter can automatically fix field names in **PreferAbbreviatedReference mode**:
639639

640640
**PreferAbbreviatedReference mode:**
641641
- Replaces 'Reference' with 'Ref' and 'References' with 'Refs' at the start or end of field names
@@ -647,15 +647,7 @@ The `noreferences` linter can automatically fix field names based on the policy:
647647
- `ReferencesCount``RefsCount` (start)
648648
- `PreferenceType` → no change (middle - not flagged)
649649

650-
**NoReferences mode:**
651-
- Removes all reference-related words (Ref/Refs/Reference/References) from the beginning or end of field names
652-
- Like PreferAbbreviatedReference, this only matches at word boundaries to avoid false positives
653-
- Examples:
654-
- `RefNode``Node` (start)
655-
- `NodeRef``Node` (end)
656-
- `ReferenceName``Name` (start)
657-
- `ConfigReferences``Config` (end)
658-
- `ReferenceCount``Count` (start)
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.
659651

660652
## SSATags
661653

pkg/analysis/noreferences/analyzer.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ func buildConventions(policy Policy) []namingconventions.Convention {
7878
switch policy {
7979
case PolicyPreferAbbreviatedReference:
8080
// Replace "Reference" or "References" with "Ref" or "Refs"
81-
// At start: [Rr]eference(s?) matches Reference/References (case insensitive R)
82-
// At end: Reference(s?)$ matches Reference/References (capital R only to avoid "*preference")
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)
8387
return []namingconventions.Convention{
8488
{
8589
Name: "reference-to-ref",
@@ -91,17 +95,14 @@ func buildConventions(policy Policy) []namingconventions.Convention {
9195
}
9296

9397
case PolicyNoReferences:
94-
// Drop any reference-related words from field names
95-
// At start: matches [Rr]ef(erence)?s? followed by uppercase letter, preserving that letter ($3)
96-
// At end: matches Ref(erence)?s? at end (capital R to avoid "*preference")
97-
// When at start, $3 captures and preserves the next uppercase letter
98-
// When at end, $3 is empty, effectively dropping the text
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")
99101
return []namingconventions.Convention{
100102
{
101103
Name: "no-references",
102104
ViolationMatcher: "^[Rr]ef(erence)?(s?)([A-Z])|Ref(erence)?(s?)$",
103-
Operation: namingconventions.OperationReplacement,
104-
Replacement: "$3",
105+
Operation: namingconventions.OperationInform,
105106
Message: "field names should not contain reference-related words",
106107
},
107108
}

pkg/analysis/noreferences/testdata/src/b/b.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package b
22

33
// TestWithPolicyNoReferences tests the linter with PolicyNoReferences (strict mode)
4-
// In this mode, all reference-related words (Reference/References/Ref/Refs) are removed
4+
// In this mode, all reference-related words (Reference/References/Ref/Refs) are warned about but not removed
55
type TestWithPolicyNoReferences struct {
66
// Fields ending with Reference should be flagged
77
NodeReference string `json:"nodeReference"` // want `naming convention "no-references": field NodeReference: field names should not contain reference-related words`

pkg/analysis/noreferences/testdata/src/b/b.go.golden

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)