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

Add diagnostic capability #39

Merged
merged 9 commits into from
Dec 7, 2024
Merged

Add diagnostic capability #39

merged 9 commits into from
Dec 7, 2024

Conversation

uudashr
Copy link
Owner

@uudashr uudashr commented Dec 7, 2024

TL;DR:

  • Add flag -d to binary cli to show diagnostics
  • Add functions to support diagnostics to the API
    • ComplexityStatsWithDiagnostic(...) []Stat
    • ScanComplexity(...) ScanResult

Diagnostic enable us to understand the details how the complexity increase.

Example source code:

package prime

func SumOfPrimes(max int) int {
        var total int

OUT:
        for i := 1; i < max; i++ { // +1
                for j := 2; j < i; j++ { // +2 (nesting = 1)
                        if i%j == 0 { // +3 (nesting = 2)
                                continue OUT // +1
                        }
                }
                total += i
        }

        return total
} // Cognitive complexity = 7

By supplying -d flag on the command line ex: gocognit -json -d ., it will show diagnostics information:

[
    {
        "PkgName": "prime",
        "FuncName": "SumOfPrimes",
        "Complexity": 7,
        "Pos": {
            "Filename": "prime.go",
            "Offset": 15,
            "Line": 3,
            "Column": 1
        },
        "Diagnostics": [
            {
                "Inc": 1,
                "Text": "for",
                "Pos": {
                    "Offset": 69,
                    "Line": 7,
                    "Column": 2
                }
            },
            {
                "Inc": 2,
                "Nesting": 1,
                "Text": "for",
                "Pos": {
                    "Offset": 104,
                    "Line": 8,
                    "Column": 3
                }
            },
            {
                "Inc": 3,
                "Nesting": 2,
                "Text": "if",
                "Pos": {
                    "Offset": 152,
                    "Line": 9,
                    "Column": 4
                }
            },
            {
                "Inc": 1,
                "Text": "continue",
                "Pos": {
                    "Offset": 190,
                    "Line": 10,
                    "Column": 5
                }
            }
        ]
    }
]

@uudashr uudashr requested a review from Copilot December 7, 2024 01:45
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 suggestion.

Comments skipped due to low confidence (3)

README.md:201

  • The type 'Diagnostics' should be 'Diagnostic' to match the type definition.
Diagnostics []Diagnostics

gocognit.go:20

  • The field name 'Diagnostics' should be singular to match the type name 'Diagnostic'.
Diagnostics []Diagnostic `json:",omitempty"`

gocognit.go:103

  • [nitpick] The variable name 'tracePos' should be renamed to 'diagPos' to better reflect its purpose.
func generateDiagnostics(fset *token.FileSet, diags []diagnostic) []Diagnostic {

gocognit.go Outdated Show resolved Hide resolved
@uudashr uudashr merged commit 2881c26 into master Dec 7, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant