Debian Codegraph @

go/analysis/passes/timeformat/timeformat.go

raw symbols SCIP
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 _ "embed"
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 "golang.org/x/tools/internal/analysis/analyzerutil"
22 "golang.org/x/tools/internal/typesinternal"
23 )
24
25 const badFormat = "2006-02-01"
26 const goodFormat = "2006-01-02"
27
28 //go:embed doc.go
29 var doc string
30
31 var Analyzer = &analysis.Analyzer{
32 Name: "timeformat",
33 Doc: analyzerutil.MustExtractDoc(doc, "timeformat"),
34 URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/timeformat",
35 Requires: []*analysis.Analyzer{inspect.Analyzer},
36 Run: run,
37 }
38
39 func run(pass *analysis.Pass) (any, error) {
40 // Note: (time.Time).Format is a method and can be a typeutil.Callee
41 // without directly importing "time". So we cannot just skip this package
42 // when !analysis.Imports(pass.Pkg, "time").
43 // TODO(taking): Consider using a prepass to collect typeutil.Callees.
44
45 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
46
47 nodeFilter := []ast.Node{
48 (*ast.CallExpr)(nil),
49 }
50 inspect.Preorder(nodeFilter, func(n ast.Node) {
51 call := n.(*ast.CallExpr)
52 obj := typeutil.Callee(pass.TypesInfo, call)
53 if !typesinternal.IsMethodNamed(obj, "time", "Time", "Format") &&
54 !typesinternal.IsFunctionNamed(obj, "time", "Parse") {
55 return
56 }
57 if len(call.Args) > 0 {
58 arg := call.Args[0]
59 badAt := badFormatAt(pass.TypesInfo, arg)
60
61 if badAt > -1 {
62 // Check if it's a literal string, otherwise we can't suggest a fix.
63 if _, ok := arg.(*ast.BasicLit); ok {
64 pos := int(arg.Pos()) + badAt + 1 // +1 to skip the " or `
65 end := pos + len(badFormat)
66
67 pass.Report(analysis.Diagnostic{
68 Pos: token.Pos(pos),
69 End: token.Pos(end),
70 Message: badFormat + " should be " + goodFormat,
71 SuggestedFixes: []analysis.SuggestedFix{{
72 Message: "Replace " + badFormat + " with " + goodFormat,
73 TextEdits: []analysis.TextEdit{{
74 Pos: token.Pos(pos),
75 End: token.Pos(end),
76 NewText: []byte(goodFormat),
77 }},
78 }},
79 })
80 } else {
81 pass.Reportf(arg.Pos(), badFormat+" should be "+goodFormat)
82 }
83 }
84 }
85 })
86 return nil, nil
87 }
88
89 // badFormatAt return the start of a bad format in e or -1 if no bad format is found.
90 func badFormatAt(info *types.Info, e ast.Expr) int {
91 tv, ok := info.Types[e]
92 if !ok { // no type info, assume good
93 return -1
94 }
95
96 t, ok := tv.Type.(*types.Basic) // sic, no unalias
97 if !ok || t.Info()&types.IsString == 0 {
98 return -1
99 }
100
101 if tv.Value == nil {
102 return -1
103 }
104
105 return strings.Index(constant.StringVal(tv.Value), badFormat)
106 }