mirror of
https://github.com/imjasonh/ctxhttp-analyzer
synced 2026-07-06 22:12:38 +00:00
Go analyzer that detects net/http calls without context and rewrites them to use http.NewRequestWithContext. Handles http.Get/Post/Head/PostForm, client methods, and injects context at function boundaries (main, tests, HTTP handlers). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1,006 B
1,006 B
Lessons
analysistest.RunWithSuggestedFixescompares fixed output against.goldenfiles. The// wantannotation comments survive text edits and appear in golden files.go/analysis.SuggestedFixtext edits can replace arbitrary ranges, not just expressions. We use this to replace entire statements for multi-statement rewrites (e.g.,http.Get→ request creation + error check +.Do).printer.Fprintcan print AST nodes to get their source text, which is useful for preserving original argument expressions.- When a file has a single
import "pkg"(no parens), adding a second import creates a separateimportdeclaration rather than a grouped one. This is cosmetically suboptimal but functional. - The
injectedmap keyed by*ast.FuncDeclcorrectly deduplicates context injection when multiple HTTP calls exist in the same function. assign.Tok.String()is the correct way to get:=vs=as a string; printing a token as an AST node doesn't work withprinter.Fprint.