-
Notifications
You must be signed in to change notification settings - Fork 298
refactor: modifies linting machinery to use Failure as a mean to signal erros in rules application #1178
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
refactor: modifies linting machinery to use Failure as a mean to signal erros in rules application #1178
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,11 @@ type AddConstantRule struct { | |
|
||
// Apply applies the rule to given file. | ||
func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||
r.configureOnce.Do(func() { r.configure(arguments) }) | ||
var configureErr error | ||
r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) | ||
if configureErr != nil { | ||
return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} | ||
} | ||
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This idea is great, but it doesn't address this But it could be addressed separately There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I know. It is not my intention to address this problem right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree Maybe linter could call configure if there is a Configure interface on the Rule But, some tests will need to be rewritten There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've studied before the alternative of adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My point was to add an interface for Configure only. type Configure interface { If detected, you call it after a type assertion. But it's maybe part of what you considered There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok @chavacava nice concept, can you deploy that changes for lint/* files without add_constant.go rule, and in #1126 I do the rest of job (add |
||
|
||
var failures []lint.Failure | ||
|
||
|
@@ -201,15 +205,16 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool { | |
return ok | ||
} | ||
|
||
func (r *AddConstantRule) configure(arguments lint.Arguments) { | ||
func (r *AddConstantRule) configure(arguments lint.Arguments) error { | ||
println(">>>> configuring") | ||
r.strLitLimit = defaultStrLitLimit | ||
r.allowList = newAllowList() | ||
if len(arguments) == 0 { | ||
return | ||
return nil | ||
} | ||
args, ok := arguments[0].(map[string]any) | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting a k,v map. Got %T", arguments[0])) | ||
return fmt.Errorf("invalid argument to the add-constant rule, expecting a k,v map. Got %T", arguments[0]) | ||
} | ||
for k, v := range args { | ||
kind := "" | ||
|
@@ -228,39 +233,41 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) { | |
} | ||
list, ok := v.(string) | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v)) | ||
fmt.Errorf("invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v) | ||
} | ||
r.allowList.add(kind, list) | ||
case "maxLitCount": | ||
sl, ok := v.(string) | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v)) | ||
fmt.Errorf("invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v) | ||
} | ||
|
||
limit, err := strconv.Atoi(sl) | ||
if err != nil { | ||
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v)) | ||
fmt.Errorf("invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v) | ||
} | ||
r.strLitLimit = limit | ||
case "ignoreFuncs": | ||
excludes, ok := v.(string) | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v)) | ||
fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v) | ||
} | ||
|
||
for _, exclude := range strings.Split(excludes, ",") { | ||
exclude = strings.Trim(exclude, " ") | ||
if exclude == "" { | ||
panic("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.") | ||
fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.") | ||
} | ||
|
||
exp, err := regexp.Compile(exclude) | ||
if err != nil { | ||
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err)) | ||
fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err) | ||
} | ||
|
||
r.ignoreFunctions = append(r.ignoreFunctions, exp) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using
https://pkg.go.dev/golang.org/x/sync/errgroup
The problems I see with your code are:
I would have said why not, if each errors was reported or returned
Of course, this feedback is based on my understanding of the code. I can be wrong 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback
(I've edited your comment to add numbers to the list of problems)
My meta-feedback:
On errgroup:
(each
>>>> linting ...
is a goroutine)To resume, errgroup is (subjectively) simpler but still shares the same drawbacks of explicitly using channels. (and to be fair, these drawbacks are also present in the current -with panics- implementation of the linting machinery)
Anyway, the single significant point of my proposal is: use the already available failure mechanism instead of adding an error return value to rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your detailed reply and feedback.
I agree with your conclusion
👍