Skip to content

Commit

Permalink
Add module check (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
5nord committed Dec 30, 2020
1 parent f82e74e commit 1ea4040
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/cmds/lint/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ func (e errUsageExceedsLimit) Error() string {
e.fset.Position(e.node.Pos()), identName(e.node), e.limit, e.text)
}

type errUnusedModule struct {
file string
}

func (e errUnusedModule) Error() string {
return fmt.Sprintf("%s: error: unused module", e.file)
}

func isSilent(n ast.Node, checks ...string) bool {
scanner := bufio.NewScanner(strings.NewReader(ast.FirstToken(n).Comments()))
for scanner.Scan() {
Expand Down
77 changes: 77 additions & 0 deletions internal/cmds/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lint

import (
"fmt"
"path/filepath"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -72,6 +73,12 @@ When TTCN-3 code is refactored incrementally, it happens that references to
legacy code are faster added than one can remove them. This check helps with a
warning, as soon as the usage of a symbol exceed a defined limit.
Unused Symbols
unused.modules Checks for unused modules
Example configuration file:
aligned_braces: true
Expand All @@ -83,6 +90,9 @@ Example configuration file:
limit: 12
text: Use "bar" instead.
unused:
modules: true
complexity:
max: 15
ignore_guards: true
Expand Down Expand Up @@ -122,6 +132,9 @@ For information on writing new checks, see <TBD>.
regexes = make(map[string]*regexp.Regexp)
issues = 0

usedModules = make(map[string]Import)
usedModuleMu sync.Mutex

style = struct {
MaxLines int `yaml:"max_lines"`
AlignedBraces bool `yaml:"aligned_braces"`
Expand Down Expand Up @@ -157,9 +170,20 @@ For information on writing new checks, see <TBD>.
Limit int
count int
}
Unused struct {
Modules bool
}
}{}
)

type Import struct {
Fset *loc.FileSet
Node *ast.ImportDecl
Path string
From string
Imported string
}

func init() {
Command.PersistentFlags().StringVarP(&config, "config", "c", "", "path to YAML formatted file containing linter configuration")
}
Expand Down Expand Up @@ -229,6 +253,7 @@ func lint(cmd *cobra.Command, args []string) error {
switch n := n.(type) {
case *ast.Ident:
checkUsage(fset, n)

case *ast.Module:
checkNaming(fset, n, style.Naming.Modules)
checkBraces(fset, n.LBrace, n.RBrace)
Expand Down Expand Up @@ -322,6 +347,7 @@ func lint(cmd *cobra.Command, args []string) error {
checkBraces(fset, n.LBrace, n.RBrace)
case *ast.ImportDecl:
checkBraces(fset, n.LBrace, n.RBrace)
checkImport(fset, n, mod.Module)
case *ast.GroupDecl:
checkBraces(fset, n.LBrace, n.RBrace)
case *ast.WithSpec:
Expand Down Expand Up @@ -378,6 +404,9 @@ func lint(cmd *cobra.Command, args []string) error {
}

wg.Wait()

checkSuite(suite)

switch issues {
case 0:
return nil
Expand Down Expand Up @@ -498,6 +527,54 @@ func checkUsage(fset *loc.FileSet, n *ast.Ident) {
}
}

func checkImport(fset *loc.FileSet, n *ast.ImportDecl, mod *ast.Module) {
if !style.Unused.Modules {
return
}

imported := identName(n.Module)
importing := identName(mod.Name)

usedModuleMu.Lock()
usedModules[imported] = Import{
Node: n,
Fset: fset,
Path: fset.Position(n.Pos()).Filename,
From: importing,
Imported: imported,
}
usedModuleMu.Unlock()

}

func checkSuite(suite *ntt.Suite) {

if !style.Unused.Modules {
return
}

pkgs, _ := suite.Imports()
for _, pkg := range pkgs {
files, _ := filepath.Glob(pkg.Path() + "/*.ttcn3")
for _, file := range files {
if isWhiteListed(style.Ignore.Files, file) {
continue
}

mod := filepath.Base(file)
mod = strings.TrimSuffix(mod, filepath.Ext(mod))

if isWhiteListed(style.Ignore.Modules, mod) {
return
}

if _, found := usedModules[mod]; !found {
report(&errUnusedModule{file: file})
}
}
}
}

func matchAny(patterns []string, s string) bool {
for _, p := range patterns {

Expand Down

0 comments on commit 1ea4040

Please sign in to comment.