Skip to content

Commit 9754d51

Browse files
committed
initial commit for markerscope linter
Signed-off-by: nayuta-ai <nayuta723@gmail.com>
1 parent 1dea13b commit 9754d51

File tree

14 files changed

+1199
-0
lines changed

14 files changed

+1199
-0
lines changed

docs/linters.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [ForbiddenMarkers](#forbiddenmarkers) - Checks that no forbidden markers are present on types/fields.
88
- [Integers](#integers) - Validates usage of supported integer types
99
- [JSONTags](#jsontags) - Ensures proper JSON tag formatting
10+
- [MarkerScope](#markerscope) - Validates that markers are applied in the correct scope
1011
- [MaxLength](#maxlength) - Checks for maximum length constraints on strings and arrays
1112
- [NamingConventions](#namingconventions) - Ensures field names adhere to user-defined naming conventions
1213
- [NoBools](#nobools) - Prevents usage of boolean types
@@ -289,6 +290,44 @@ lintersConfig:
289290
jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # Provide a custom regex, which the json tag must match.
290291
```
291292

293+
## MarkerScope
294+
295+
The `markerscope` linter validates that markers are applied in the correct scope. It ensures that markers are placed on appropriate Go language constructs (types, fields) according to their intended usage.
296+
297+
The linter defines different scope types for markers:
298+
299+
- **Field-only markers**: Can only be applied to struct fields (e.g., `required`, `kubebuilder:validation:Required`)
300+
- **Type-only markers**: Can only be applied to type definitions
301+
- **Type or Map/Slice fields**: Can be applied to type definitions, map fields, or slice fields (e.g., `kubebuilder:validation:MinProperties`)
302+
- **Field or Type markers**: Can be applied to either fields or type definitions
303+
304+
### Default Scope Rules
305+
306+
By default, the linter enforces these scope rules:
307+
308+
- `required` and `kubebuilder:validation:Required`: Field-only
309+
- `kubebuilder:validation:MinProperties`: Type definitions, map fields, or slice fields only
310+
311+
### Configuration
312+
313+
```yaml
314+
lintersConfig:
315+
markerscope:
316+
policy: SuggestFix | Warn # The policy for marker scope violations. Defaults to `SuggestFix`.
317+
```
318+
319+
### Fixes
320+
321+
The `markerscope` linter can automatically fix scope violations when `policy` is set to `SuggestFix`:
322+
323+
1. **Remove incorrect markers**: Suggests removing markers that are in the wrong scope
324+
2. **Move markers to correct locations**:
325+
- Move field-only markers from types to appropriate fields
326+
- Move type-only markers from fields to their corresponding type definitions
327+
3. **Preserve marker values**: When moving markers like `kubebuilder:validation:MinProperties=1`
328+
329+
**Note**: This linter is not enabled by default and must be explicitly enabled in the configuration.
330+
292331
## MaxLength
293332

294333
The `maxlength` linter checks that string and array fields in the API are bounded by a maximum length.

0 commit comments

Comments
 (0)