misspell

Table of Contents

  1. 解決する問題
  2. 設定
  3. オプション
    1. extra-words の使い方
  4. サンプル
    1. 検出例
    2. 修正例
  5. 注意点
  6. 参考リンク

misspell はソースコード内の英単語のスペルミスを検出するツールです。

解決する問題

変数名、コメント、文字列リテラルに含まれるスペルミスは、コードの可読性を下げ、検索性を損ないます。特にエクスポートされた識別子やドキュメントコメントのスペルミスは、外部から参照される際に問題になります。

1
2
3
4
5
6
7
// Before: スペルミスがある
// Perfomance is a key factor in this module.
type Perfomance struct {
Paralell bool // 正: Parallel
Occurance int // 正: Occurrence
Enviroment string // 正: Environment
}
1
2
3
4
5
6
7
// After: 正しいスペル
// Performance is a key factor in this module.
type Performance struct {
Parallel bool
Occurrence int
Environment string
}

設定

公式ドキュメント

1
2
3
4
5
6
7
8
9
10
11
12
# .golangci.yml 設定例
version: "2"

linters:
enable:
- misspell
settings:
misspell:
locale: US
ignore-rules:
- colour
- organisation

オプション

オプション デフォルト 説明
locale string "" スペルのロケール。US で米国英語、UK で英国英語を強制
ignore-rules []string [] 無視する単語のリスト
extra-words []object [] 追加のスペルミスルール(typocorrection のペア)
mode string "" チェックモード。restricted でより厳密なチェック

extra-words の使い方

1
2
3
4
5
6
7
8
linters:
settings:
misspell:
extra-words:
- typo: "Kubernets"
correction: "Kubernetes"
- typo: "Promethues"
correction: "Prometheus"

サンプル

検出例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// misspell が警告するパターン

// 1. コメント内のスペルミス
// This function is responsable for user authentication.
// "responsable" is a misspelling of "responsible"
func Authenticate(user string) error {
return nil
}

// 2. 変数名のスペルミス
var seperate = "," // "seperate" is a misspelling of "separate"

// 3. 文字列リテラル内のスペルミス
func GetMessage() string {
return "Sucessfully completed" // "Sucessfully" → "Successfully"
}

// 4. ロケール指定時(locale: US)
var colour = "red" // US ロケールでは "color" を推奨

修正例

1
2
3
4
5
6
7
8
9
10
11
12
// This function is responsible for user authentication.
func Authenticate(user string) error {
return nil
}

var separate = ","

func GetMessage() string {
return "Successfully completed"
}

var color = "red"

注意点

  • デフォルトでは米国英語・英国英語の両方を受け入れる。locale: US または locale: UK で片方に統一できる
  • ignore-rules でプロジェクト固有の用語(ドメイン用語、製品名など)を除外できる
  • コメント、文字列リテラル、識別子のすべてがチェック対象
  • 自動修正(--fix)に対応しているため、大量のスペルミスも一括修正可能
  • mode: restricted ではより厳密なルールが適用されるが、誤検知が増える可能性がある

参考リンク