-
Notifications
You must be signed in to change notification settings - Fork 1.7k
require: fix invalid examples in doc comments (codegen) #1780
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
base: master
Are you sure you want to change the base?
Conversation
| return strings.Replace(f.Comment(), search, replace, -1) | ||
| } | ||
|
|
||
| func requireCommentParseIf(s string) string { |
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.
This method should be tested
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.
This is codegen. Tests are not strictly necessary: checking the generated output is enough.
However, tests would still be helpful to document what the function expects and does. Tests will also be useful for future refactorings.
Even more helpful would be some godoc for the function because the require prefix is misleading: on a first read I got it as verb while it is about the require package.
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.
An issue is that it's unclear from the function name or the code; what is this function is supposed to do. A test or a good comment before the function would help.
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.
That was my point, indeed
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.
#1780 (comment)
I wasn’t familiar with the reply feature, so I didn’t realize multiple conversations were happening. 😅
Considering tests, would it be better to separate this method and handle it individually?
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.
@PCloud63514 Please start by adding a comment block to document the function.
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.
Added a doc comment for requireCommentParseIf to clarify its purpose.requireCommentParseIf
| for _, line := range lines { | ||
| trim := strings.TrimSpace(line) | ||
|
|
||
| if !inIf { | ||
| slash := strings.Index(line, "//") | ||
| if slash < 0 { | ||
| out = append(out, line) | ||
| continue | ||
| } | ||
|
|
||
| trimmed := strings.TrimSpace(line[slash+2:]) | ||
|
|
||
| if strings.HasPrefix(trimmed, "if require.") && strings.HasSuffix(trimmed, "{") { | ||
| commentPrefix = line[:slash] + "//\t " | ||
|
|
||
| h := strings.TrimPrefix(trimmed, "if ") | ||
| h = strings.TrimSpace(h) | ||
| if strings.HasSuffix(h, "{") { | ||
| h = strings.TrimSuffix(h, "{") | ||
| } | ||
| h = strings.TrimSpace(h) | ||
|
|
||
| out = append(out, commentPrefix+strings.TrimLeft(h, "\t")) | ||
| inIf = true | ||
| continue | ||
| } | ||
| out = append(out, line) | ||
| } else { | ||
| if strings.HasPrefix(trim, "//") { | ||
| body := strings.TrimSpace(strings.TrimPrefix(trim, "//")) | ||
| if body == "}" { | ||
| inIf = false | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| slash := strings.Index(line, "//") | ||
| if slash >= 0 { | ||
| body := strings.TrimLeft(line[slash+2:], " \t") | ||
| out = append(out, commentPrefix+body) | ||
| } else { | ||
| out = append(out, line) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return strings.Join(out, "\n") | ||
| } |
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.
I'm worried about a badly indented comment.
I'm unsure how to handle this correctly.
|
@ccoVeille Thanks, I also felt the code was a bit messy, so I tried to simplify it further. Regarding your comment about tests: since this is only used for code generation, |
_codegen/main.go
Outdated
| commentPrefix := rePrefix.FindString(line) | ||
| comment := strings.TrimSpace(line[2:]) | ||
|
|
||
| if strings.HasSuffix(comment, "SomeFunction()") { |
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.
I don't like the explicit reference to SomeFunction(). This should instead look for the first line of an example block by looking at indent. But maybe there is a reason why this wasn't enough?
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.
Hi @dolmen
I’ve removed the explicit SomeFunction() reference.
That logic was originally added considering the type S struct { comment,
but now the handling is simplified to focus only on if require. cases.
|
Is there anything more I should do, or anything I might be misunderstanding? |
Summary
Correct invalid examples in require package doc comments.
require does not return a boolean value; its responsibility is to fail immediately.
Changes
Updated examples to reflect proper require usage without if.
Added a new helper requireCommentParseIf to sanitize and transform invalid doc comments in bulk, ensuring consistency across the package.
Removed conditional forms such as:
Updated examples to demonstrate proper usage of require:
Motivation
Unlike assert, require functions do not return a boolean value.
The previous examples suggested patterns such as if require.NoError(t, err) { ... }, which are invalid.
This PR fixes those examples and introduces an automated parser (requireCommentParseIf) to prevent such mistakes from persisting in the future.
Related issues
Closes #1776