Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: display files counter for JS #1427

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions internal/report/output/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Output struct {
DetailedContext string `json:"detailed_context,omitempty" yaml:"detailed_context,omitempty"`
}

var javascriptLanguages = []string{"JSX", "TypeScript", "JavaScript"}

func AddReportData(
reportData *outputtypes.ReportData,
config settings.Config,
Expand Down Expand Up @@ -574,14 +576,11 @@ func writeRuleListToString(

tbl := table.New("Language", "Default Rules", "Custom Rules", "Files").WithWriter(reportStr)

languageSlice := maps.Values(languages)[:]
sort.Slice(languageSlice, func(i, j int) bool {
return len(languageSlice[i].Files) > len(languageSlice[j].Files)
})
languagePairs := getLanguagePairs(languages)
unsupportedLanguages := make(map[string]bool)
for _, lang := range languageSlice {
for _, lang := range languagePairs {
if ruleCount, ok := ruleCountPerLang[lang.Name]; ok {
tbl.AddRow(lang.Name, ruleCount.DefaultRuleCount, ruleCount.CustomRuleCount, len(languages[lang.Name].Files))
tbl.AddRow(lang.Name, ruleCount.DefaultRuleCount, ruleCount.CustomRuleCount, len(lang.Files))
} else {
for _, reportedDependency := range reportedDependencies {
if reportedDependency.DetectorLanguage == lang.Name {
Expand Down Expand Up @@ -610,6 +609,46 @@ func writeRuleListToString(
return totalRuleCount
}

func getLanguagePairs(languages map[string]*gocloc.Language) []*gocloc.Language {
languageSlice := maps.Values(languages)[:]
groupedLanguageSlice := make(map[string]*gocloc.Language)

for _, language := range languageSlice {
var detectedLanguage string
var files []string
if slices.Contains(javascriptLanguages, language.Name) {
detectedLanguage = "JavaScript"
files = language.Files
} else {
detectedLanguage = language.Name
files = language.Files
}

if _, exists := groupedLanguageSlice[detectedLanguage]; !exists {
groupedLanguageSlice[detectedLanguage] = &gocloc.Language{
Name: detectedLanguage,
Files: files,
}
} else {
groupedLanguageSlice[detectedLanguage].Files = append(groupedLanguageSlice[detectedLanguage].Files, files...)
}
}

languagePairs := make([]*gocloc.Language, 0, len(groupedLanguageSlice))
for key := range groupedLanguageSlice {
languagePairs = append(languagePairs, &gocloc.Language{
Name: key,
Files: groupedLanguageSlice[key].Files,
})
}

sort.Slice(languagePairs, func(i, j int) bool {
return len(languagePairs[i].Files) > len(languagePairs[j].Files)
})

return languagePairs
}

func countRules(
rules map[string]*settings.Rule,
languages map[string]*gocloc.Language,
Expand Down
Loading