linters

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 7 Imported by: 0

README

keyStyle

[!IMPORTANT] This project is a first attempt at writing a custom linter and used for personal purposes mainly. I am happy if someone finds it useful, but please be aware that it is not necessarily production-ready or well-tested.

Map-key styling linter plugin for golangci-lint.

Example

// ...
someData = LogData{
  "camelCase":     "value1",
  "IncorrectKey2": "value2",
}
// ...

Linter output:

example.go:10:3: Key 'IncorrectKey2' style should be camelCase (keystyle)
  "IncorrectKey2": someValue,
  ^

Usage

Instructions for setting up a custom linter can be found in the golangci-lint documentation. Replace foo with keyStyle in the example (and the paths accordingly).

The following instructions are based on above documentation.

Linter plugin setup (.custom-gcl.yml):

version: v2.1.6
plugins:
  # Import keystyle linter for linting of map key style.
  - module: 'github.com/merschformann/keystyle'
    import: 'github.com/merschformann/keystyle'
    version: v0.0.1

Linter configuration (.golangci.yml):

version: "2"
linters:
  default: none
  enable:
    - keystyle
  settings:
    custom:
      keystyle:
        type: "module"
        description: Keystyle preference.
        settings:
          checks:
            - style: "camelCase"
              type-name: "LogData"
            - style: "custom"
              type-name: "OtherData"
              regex: "^[a-z][a-zA-Z0-9]*$"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(settings any) (register.LinterPlugin, error)

Types

type KeyStyle

type KeyStyle string

KeyStyle defines the style of keys in maps.

const (
	CamelCase  KeyStyle = "camelCase"
	PascalCase KeyStyle = "PascalCase"
	KebabCase  KeyStyle = "kebab-case"
	SnakeCase  KeyStyle = "snake_case"
	Custom     KeyStyle = "custom" // Custom style with a regex pattern.
)

type KeyStyleDefinition

type KeyStyleDefinition struct {
	Style    KeyStyle `json:"style"`     // The style of keys in maps, e.g., "camelCase", "PascalCase", "kebab-case", "snake_case", "custom".
	TypeName string   `json:"type-name"` // The name of the type of map to check, e.g., "LogData"
	Regex    string   `json:"regex"`     // Optional custom regex to match keys (if Style is "custom").
}

type KeyStyleSettings

type KeyStyleSettings struct {
	Checks []KeyStyleDefinition `json:"checks"` // List of key style definitions to check.
}

type PluginKeyStyle

type PluginKeyStyle struct {
	// contains filtered or unexported fields
}

func (*PluginKeyStyle) BuildAnalyzers

func (f *PluginKeyStyle) BuildAnalyzers() ([]*analysis.Analyzer, error)

func (*PluginKeyStyle) GetLoadMode

func (f *PluginKeyStyle) GetLoadMode() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL