Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
214 lines
5.3 KiB
Go
214 lines
5.3 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package mlogFieldNaming
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
"go/constant"
|
|
"go/token"
|
|
"go/types"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
)
|
|
|
|
var Analyzer = &analysis.Analyzer{
|
|
Name: "mlogFieldNaming",
|
|
Doc: "check that mlog field keys are snake_case",
|
|
Run: run,
|
|
}
|
|
|
|
const mlogPkgPath = "github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
|
|
// keyedFieldConstructors are the mlog field constructors that take a field key
|
|
// as their first argument.
|
|
var keyedFieldConstructors = map[string]bool{
|
|
"Any": true,
|
|
"Array": true,
|
|
"Bool": true,
|
|
"Duration": true,
|
|
"Float": true,
|
|
"Int": true,
|
|
"Map": true,
|
|
"Millis": true,
|
|
"NamedErr": true,
|
|
"String": true,
|
|
"Stringer": true,
|
|
"Time": true,
|
|
"Uint": true,
|
|
}
|
|
|
|
var snakeCase = regexp.MustCompile(`^[a-z][a-z0-9]*(_[a-z0-9]+)*$`)
|
|
|
|
func run(pass *analysis.Pass) (interface{}, error) {
|
|
for _, file := range pass.Files {
|
|
ast.Inspect(file, func(node ast.Node) bool {
|
|
call, ok := node.(*ast.CallExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
|
|
fun, ok := calleeSelector(call.Fun)
|
|
if !ok || !keyedFieldConstructors[fun.Sel.Name] {
|
|
return true
|
|
}
|
|
|
|
if !isMlogPackage(pass, fun.X) {
|
|
return true
|
|
}
|
|
|
|
if len(call.Args) == 0 {
|
|
return true
|
|
}
|
|
|
|
arg := call.Args[0]
|
|
|
|
key, ok := constantString(pass, arg)
|
|
if !ok {
|
|
// The key isn't statically known, so there's nothing to check.
|
|
return true
|
|
}
|
|
|
|
if snakeCase.MatchString(key) {
|
|
return true
|
|
}
|
|
|
|
pass.Report(diagnostic(arg, key))
|
|
|
|
return true
|
|
})
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func diagnostic(arg ast.Expr, key string) analysis.Diagnostic {
|
|
// A fix can only rewrite the key in place when it is spelled out as a
|
|
// string literal. Keys that come from a named constant have to be renamed
|
|
// at the declaration instead, which is beyond what this analyzer offers.
|
|
lit, isLiteral := arg.(*ast.BasicLit)
|
|
isLiteral = isLiteral && lit.Kind == token.STRING
|
|
|
|
fixed, canFix := toSnakeCase(key)
|
|
if !isLiteral || !canFix {
|
|
return analysis.Diagnostic{
|
|
Pos: arg.Pos(),
|
|
End: arg.End(),
|
|
Message: fmt.Sprintf("mlog field key %q is not snake_case", key),
|
|
}
|
|
}
|
|
|
|
return analysis.Diagnostic{
|
|
Pos: arg.Pos(),
|
|
End: arg.End(),
|
|
Message: fmt.Sprintf("mlog field key %q is not snake_case, use %q", key, fixed),
|
|
SuggestedFixes: []analysis.SuggestedFix{{
|
|
Message: fmt.Sprintf("Rename mlog field key to %q", fixed),
|
|
TextEdits: []analysis.TextEdit{{
|
|
Pos: arg.Pos(),
|
|
End: arg.End(),
|
|
NewText: []byte(strconv.Quote(fixed)),
|
|
}},
|
|
}},
|
|
}
|
|
}
|
|
|
|
// toSnakeCase converts a field key to snake_case, reporting whether the result
|
|
// is a valid snake_case key. Keys that cannot be converted mechanically (an
|
|
// empty key, or one that would start with a digit) are left to the author.
|
|
func toSnakeCase(key string) (string, bool) {
|
|
runes := []rune(key)
|
|
|
|
var b strings.Builder
|
|
for i, r := range runes {
|
|
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
|
|
b.WriteRune('_')
|
|
continue
|
|
}
|
|
|
|
// Break before an uppercase rune that starts a new word, either
|
|
// following a lowercase rune or a digit ("userId" -> "user_id"), or
|
|
// ending a run of uppercase runes ("requestURLPath" ->
|
|
// "request_url_path").
|
|
if i > 0 && unicode.IsUpper(r) {
|
|
prev := runes[i-1]
|
|
startsWord := unicode.IsLower(prev) || unicode.IsDigit(prev)
|
|
endsAcronym := unicode.IsUpper(prev) && startsNewWord(runes, i+1)
|
|
if startsWord || endsAcronym {
|
|
b.WriteRune('_')
|
|
}
|
|
}
|
|
|
|
b.WriteRune(unicode.ToLower(r))
|
|
}
|
|
|
|
fixed := strings.Trim(b.String(), "_")
|
|
for strings.Contains(fixed, "__") {
|
|
fixed = strings.ReplaceAll(fixed, "__", "_")
|
|
}
|
|
|
|
return fixed, snakeCase.MatchString(fixed)
|
|
}
|
|
|
|
// startsNewWord reports whether the lowercase run beginning at i is a new word
|
|
// rather than a plural suffix on the acronym that precedes it, so that
|
|
// "requestURLPath" breaks before "Path" but "userIDs" keeps its trailing "s"
|
|
// ("user_ids", not "user_i_ds").
|
|
func startsNewWord(runes []rune, i int) bool {
|
|
if i >= len(runes) || !unicode.IsLower(runes[i]) {
|
|
return false
|
|
}
|
|
|
|
// A lone "s" pluralizes the acronym instead of starting a word.
|
|
if runes[i] == 's' && (i+1 == len(runes) || !unicode.IsLower(runes[i+1])) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// calleeSelector returns the pkg.Name selector a call expression resolves to.
|
|
// Most of the keyed constructors are generic, so an explicitly instantiated
|
|
// call such as mlog.Int[int64](...) reaches here as an index expression
|
|
// wrapping the selector rather than as the selector itself.
|
|
func calleeSelector(fun ast.Expr) (*ast.SelectorExpr, bool) {
|
|
switch expr := ast.Unparen(fun).(type) {
|
|
case *ast.IndexExpr:
|
|
fun = expr.X
|
|
case *ast.IndexListExpr:
|
|
fun = expr.X
|
|
}
|
|
|
|
sel, ok := ast.Unparen(fun).(*ast.SelectorExpr)
|
|
|
|
return sel, ok
|
|
}
|
|
|
|
func isMlogPackage(pass *analysis.Pass, expr ast.Expr) bool {
|
|
ident, ok := expr.(*ast.Ident)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
pkgName, ok := pass.TypesInfo.Uses[ident].(*types.PkgName)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
return pkgName.Imported().Path() == mlogPkgPath
|
|
}
|
|
|
|
func constantString(pass *analysis.Pass, expr ast.Expr) (string, bool) {
|
|
tv, ok := pass.TypesInfo.Types[expr]
|
|
if !ok || tv.Value == nil || tv.Value.Kind() != constant.String {
|
|
return "", false
|
|
}
|
|
|
|
return constant.StringVal(tv.Value), true
|
|
}
|