|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package timeformat defines an Analyzer that checks for the use |
| 6 | +// of time.Format or time.Parse calls with a bad format. |
| 7 | +package timeformat |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "go/ast" |
| 12 | + "go/constant" |
| 13 | + "go/token" |
| 14 | + "go/types" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "golang.org/x/tools/go/analysis" |
| 18 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 19 | + "golang.org/x/tools/go/ast/inspector" |
| 20 | + "golang.org/x/tools/go/types/typeutil" |
| 21 | +) |
| 22 | + |
| 23 | +const badFormat = "2006-02-01" |
| 24 | +const goodFormat = "2006-01-02" |
| 25 | + |
| 26 | +const Doc = `check for calls of (time.Time).Format or time.Parse with 2006-02-01 |
| 27 | +
|
| 28 | +The timeformat checker looks for time formats with the 2006-02-01 (yyyy-dd-mm) |
| 29 | +format. Internationally, "yyyy-dd-mm" does not occur in common calendar date |
| 30 | +standards, and so it is more likely that 2006-01-02 (yyyy-mm-dd) was intended. |
| 31 | +` |
| 32 | + |
| 33 | +var Analyzer = &analysis.Analyzer{ |
| 34 | + Name: "timeformat", |
| 35 | + Doc: Doc, |
| 36 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 37 | + Run: run, |
| 38 | +} |
| 39 | + |
| 40 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 41 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 42 | + |
| 43 | + nodeFilter := []ast.Node{ |
| 44 | + (*ast.CallExpr)(nil), |
| 45 | + } |
| 46 | + inspect.Preorder(nodeFilter, func(n ast.Node) { |
| 47 | + call := n.(*ast.CallExpr) |
| 48 | + fn, ok := typeutil.Callee(pass.TypesInfo, call).(*types.Func) |
| 49 | + if !ok { |
| 50 | + return |
| 51 | + } |
| 52 | + if !isTimeDotFormat(fn) && !isTimeDotParse(fn) { |
| 53 | + return |
| 54 | + } |
| 55 | + if len(call.Args) > 0 { |
| 56 | + arg := call.Args[0] |
| 57 | + badAt := badFormatAt(pass.TypesInfo, arg) |
| 58 | + |
| 59 | + if badAt > -1 { |
| 60 | + // Check if it's a literal string, otherwise we can't suggest a fix. |
| 61 | + if _, ok := arg.(*ast.BasicLit); ok { |
| 62 | + fmt.Printf("%#v\n", arg) |
| 63 | + pos := int(arg.Pos()) + badAt + 1 // +1 to skip the " or ` |
| 64 | + end := pos + len(badFormat) |
| 65 | + |
| 66 | + pass.Report(analysis.Diagnostic{ |
| 67 | + Pos: token.Pos(pos), |
| 68 | + End: token.Pos(end), |
| 69 | + Message: badFormat + " should be " + goodFormat, |
| 70 | + SuggestedFixes: []analysis.SuggestedFix{{ |
| 71 | + Message: "Replace " + badFormat + " with " + goodFormat, |
| 72 | + TextEdits: []analysis.TextEdit{{ |
| 73 | + Pos: token.Pos(pos), |
| 74 | + End: token.Pos(end), |
| 75 | + NewText: []byte(goodFormat), |
| 76 | + }}, |
| 77 | + }}, |
| 78 | + }) |
| 79 | + } else { |
| 80 | + pass.Reportf(arg.Pos(), badFormat+" should be "+goodFormat) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + return nil, nil |
| 86 | +} |
| 87 | + |
| 88 | +func isTimeDotFormat(f *types.Func) bool { |
| 89 | + if f.Name() != "Format" || f.Pkg().Path() != "time" { |
| 90 | + return false |
| 91 | + } |
| 92 | + sig, ok := f.Type().(*types.Signature) |
| 93 | + if !ok { |
| 94 | + return false |
| 95 | + } |
| 96 | + // Verify that the receiver is time.Time. |
| 97 | + recv := sig.Recv() |
| 98 | + if recv == nil { |
| 99 | + return false |
| 100 | + } |
| 101 | + named, ok := recv.Type().(*types.Named) |
| 102 | + return ok && named.Obj().Name() == "Time" |
| 103 | +} |
| 104 | + |
| 105 | +func isTimeDotParse(f *types.Func) bool { |
| 106 | + if f.Name() != "Parse" || f.Pkg().Path() != "time" { |
| 107 | + return false |
| 108 | + } |
| 109 | + // Verify that there is no receiver. |
| 110 | + sig, ok := f.Type().(*types.Signature) |
| 111 | + return ok && sig.Recv() == nil |
| 112 | +} |
| 113 | + |
| 114 | +// badFormatAt return the start of a bad format in e or -1 if no bad format is found. |
| 115 | +func badFormatAt(info *types.Info, e ast.Expr) int { |
| 116 | + tv, ok := info.Types[e] |
| 117 | + if !ok { // no type info, assume good |
| 118 | + return -1 |
| 119 | + } |
| 120 | + |
| 121 | + t, ok := tv.Type.(*types.Basic) |
| 122 | + if !ok || t.Info()&types.IsString == 0 { |
| 123 | + return -1 |
| 124 | + } |
| 125 | + |
| 126 | + if tv.Value == nil { |
| 127 | + return -1 |
| 128 | + } |
| 129 | + |
| 130 | + return strings.Index(constant.StringVal(tv.Value), badFormat) |
| 131 | +} |
0 commit comments