gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/engine/strategy_spellcheck.go (about)

     1  package engine
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/360EntSecGroup-Skylar/goreporter/linters/spellcheck"
     8  	"github.com/360EntSecGroup-Skylar/goreporter/utils"
     9  )
    10  
    11  type StrategySpellCheck struct {
    12  	Sync *Synchronizer `inject:""`
    13  }
    14  
    15  func (s *StrategySpellCheck) GetName() string {
    16  	return "SpellCheck"
    17  }
    18  
    19  func (s *StrategySpellCheck) GetDescription() string {
    20  	return "Check the project variables, functions, etc. naming spelling is wrong."
    21  }
    22  
    23  func (s *StrategySpellCheck) GetWeight() float64 {
    24  	return 0.05
    25  }
    26  
    27  func (s *StrategySpellCheck) Compute(parameters StrategyParameter) (summaries *Summaries) {
    28  	summaries = NewSummaries()
    29  
    30  	spelltips := spellcheck.SpellCheck(parameters.ProjectPath, parameters.ExceptPackages)
    31  	sumProcessNumber := int64(10)
    32  	processUnit := utils.GetProcessUnit(sumProcessNumber, len(spelltips))
    33  
    34  	for _, simpleTip := range spelltips {
    35  		simpleTips := strings.Split(simpleTip, ":")
    36  		if len(simpleTips) == 4 {
    37  			packageName := utils.PackageNameFromGoPath(simpleTips[0])
    38  			line, _ := strconv.Atoi(simpleTips[1])
    39  			erroru := Error{
    40  				LineNumber:  line,
    41  				ErrorString: utils.AbsPath(simpleTips[0]) + ":" + strings.Join(simpleTips[1:], ":"),
    42  			}
    43  			summaries.Lock()
    44  			if summarie, ok := summaries.Summaries[packageName]; ok {
    45  				summarie.Errors = append(summarie.Errors, erroru)
    46  				summaries.Summaries[packageName] = summarie
    47  			} else {
    48  				summarie := Summary{
    49  					Name:   utils.PackageAbsPathExceptSuffix(simpleTips[0]),
    50  					Errors: make([]Error, 0),
    51  				}
    52  				summarie.Errors = append(summarie.Errors, erroru)
    53  				summaries.Summaries[packageName] = summarie
    54  			}
    55  			summaries.Unlock()
    56  		}
    57  		if sumProcessNumber > 0 {
    58  			s.Sync.LintersProcessChans <- processUnit
    59  			sumProcessNumber = sumProcessNumber - processUnit
    60  		}
    61  	}
    62  	return
    63  }
    64  
    65  func (s *StrategySpellCheck) Percentage(summaries *Summaries) float64 {
    66  	summaries.RLock()
    67  	defer summaries.RUnlock()
    68  	return utils.CountPercentage(len(summaries.Summaries))
    69  }