blizzy78 / varnamelen

Go analyzer checking that the length of a variable's name matches its usage scope
MIT License
20 stars 2 forks source link

http handler context #2

Closed 1995parham closed 3 years ago

1995parham commented 3 years ago

some HTTP frameworks like echo have context which is different from go context and sometimes we need to have them both so using the variable name c is a pattern for them.

// e.GET("/users/:id", getUser)
func getUser(c echo.Context) error {
    // User ID from path `users/:id`
    id := c.Param("id")
    return c.String(http.StatusOK, id)
}

I don't know how, but it would be great if we can consider this for popular frameworks.

blizzy78 commented 3 years ago

We already have the -ignoreNames flag, but I think for cases like this it's not quite enough. It would be better to have a new flag that also allows to specify the exact type of the variable to ignore.

blizzy78 commented 3 years ago

I've added the -ignoreDecls flag that allows to ignore arbitrary variable declarations, for example:

-ignoreDecls 'c echo.Context, f *foo.Bar, t testing.T'

Please note that this does not work if the analyzer can't directly determine a variable's type, for example in this statement:

c := context.Background()

I think it should be possible to analyze type information if the analyzer is running inside golangci-lint, but I have not worked on that yet.

blizzy78 commented 2 years ago

varnamelen 0.6.0 can now infer types of expressions (see #10). For example, for a statement like this

c := context.Background()

it is now able to infer the type of c.