GoPLS Viewer

Home|gopls/copyright/copyright.go
1// Copyright 2020 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//go:build go1.18
6// +build go1.18
7
8// Package copyright checks that files have the correct copyright notices.
9package copyright
10
11import (
12    "go/ast"
13    "go/parser"
14    "go/token"
15    "io/fs"
16    "io/ioutil"
17    "path/filepath"
18    "regexp"
19    "strings"
20)
21
22func checkCopyright(dir string) ([]stringerror) {
23    var files []string
24    err := filepath.WalkDir(dir, func(path stringd fs.DirEntryerr errorerror {
25        if err != nil {
26            return err
27        }
28        if d.IsDir() {
29            // Skip directories like ".git".
30            if strings.HasPrefix(d.Name(), ".") {
31                return filepath.SkipDir
32            }
33            // Skip any directory that starts with an underscore, as the go
34            // command would.
35            if strings.HasPrefix(d.Name(), "_") {
36                return filepath.SkipDir
37            }
38            return nil
39        }
40        needsCopyrighterr := checkFile(dirpath)
41        if err != nil {
42            return err
43        }
44        if needsCopyright {
45            files = append(filespath)
46        }
47        return nil
48    })
49    return fileserr
50}
51
52var copyrightRe = regexp.MustCompile(`Copyright \d{4} The Go Authors. All rights reserved.
53Use of this source code is governed by a BSD-style
54license that can be found in the LICENSE file.`)
55
56func checkFile(toolsDirfilename string) (boolerror) {
57    // Only check Go files.
58    if !strings.HasSuffix(filename"go") {
59        return falsenil
60    }
61    // Don't check testdata files.
62    normalized := strings.TrimPrefix(filepath.ToSlash(filename), filepath.ToSlash(toolsDir))
63    if strings.Contains(normalized"/testdata/") {
64        return falsenil
65    }
66    // goyacc is the only file with a different copyright header.
67    if strings.HasSuffix(normalized"cmd/goyacc/yacc.go") {
68        return falsenil
69    }
70    contenterr := ioutil.ReadFile(filename)
71    if err != nil {
72        return falseerr
73    }
74    fset := token.NewFileSet()
75    parsederr := parser.ParseFile(fsetfilenamecontentparser.ParseComments)
76    if err != nil {
77        return falseerr
78    }
79    // Don't require headers on generated files.
80    if isGenerated(fsetparsed) {
81        return falsenil
82    }
83    shouldAddCopyright := true
84    for _c := range parsed.Comments {
85        // The copyright should appear before the package declaration.
86        if c.Pos() > parsed.Package {
87            break
88        }
89        if copyrightRe.MatchString(c.Text()) {
90            shouldAddCopyright = false
91            break
92        }
93    }
94    return shouldAddCopyrightnil
95}
96
97// Copied from golang.org/x/tools/gopls/internal/lsp/source/util.go.
98// Matches cgo generated comment as well as the proposed standard:
99//
100//    https://golang.org/s/generatedcode
101var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`)
102
103func isGenerated(fset *token.FileSetfile *ast.Filebool {
104    for _commentGroup := range file.Comments {
105        for _comment := range commentGroup.List {
106            if matched := generatedRx.MatchString(comment.Text); !matched {
107                continue
108            }
109            // Check if comment is at the beginning of the line in source.
110            if pos := fset.Position(comment.Slash); pos.Column == 1 {
111                return true
112            }
113        }
114    }
115    return false
116}
117
MembersX
isGenerated.fset
isGenerated.RangeStmt_2679.BlockStmt.RangeStmt_2726.BlockStmt.pos
parser
checkFile.err
checkFile.parsed
checkCopyright.BlockStmt.needsCopyright
checkCopyright.BlockStmt.err
checkFile.toolsDir
isGenerated
isGenerated.RangeStmt_2679.BlockStmt.RangeStmt_2726.comment
filepath
strings
regexp
checkCopyright.err
checkFile.normalized
checkFile.fset
isGenerated.file
token
fs
checkFile.RangeStmt_2111.c
checkFile
checkFile.shouldAddCopyright
checkCopyright.files
isGenerated.RangeStmt_2679.BlockStmt.RangeStmt_2726.BlockStmt.matched
ioutil
checkCopyright.dir
checkFile.filename
checkFile.content
isGenerated.RangeStmt_2679.commentGroup
ast
checkCopyright
Members
X