@@ -89,14 +89,18 @@ gopls. Use this standalone command to run it on your code:
8989const ignoreStruct = "betteralign:ignore"
9090
9191var (
92- unsafePointerTyp = types .Unsafe .Scope ().Lookup ("Pointer" ).(* types.TypeName ).Type ()
93- apply bool
94- testFiles bool
95- generatedFiles bool
96- excludeFiles StringArrayFlag
97- excludeDirs StringArrayFlag
92+ // flags
93+ fApply bool
94+ fTestFiles bool
95+ fGeneratedFiles bool
96+ fExcludeFiles StringArrayFlag
97+ fExcludeDirs StringArrayFlag
98+
99+ // default test and generated suffixes
98100 testSuffixes = []string {"_test.go" }
99101 generatedSuffixes = []string {"_generated.go" , "_gen.go" , ".gen.go" , ".pb.go" , ".pb.gw.go" }
102+
103+ // errors
100104 ErrStatFile = errors .New ("unable to stat the file" )
101105 ErrNotRegularFile = errors .New ("not a regular file, skipping" )
102106 ErrWriteFile = errors .New ("unable to write to file" )
@@ -122,11 +126,11 @@ var Analyzer = &analysis.Analyzer{
122126}
123127
124128func InitAnalyzer (analyzer * analysis.Analyzer ) {
125- analyzer .Flags .BoolVar (& apply , "apply" , false , "apply suggested fixes" )
126- analyzer .Flags .BoolVar (& testFiles , "test_files" , false , "also check and fix test files" )
127- analyzer .Flags .BoolVar (& generatedFiles , "generated_files" , false , "also check and fix generated files" )
128- analyzer .Flags .Var (& excludeFiles , "exclude_files" , "exclude files matching a pattern" )
129- analyzer .Flags .Var (& excludeDirs , "exclude_dirs" , "exclude directories matching a pattern" )
129+ analyzer .Flags .BoolVar (& fApply , "apply" , false , "apply suggested fixes" )
130+ analyzer .Flags .BoolVar (& fTestFiles , "test_files" , false , "also check and fix test files" )
131+ analyzer .Flags .BoolVar (& fGeneratedFiles , "generated_files" , false , "also check and fix generated files" )
132+ analyzer .Flags .Var (& fExcludeFiles , "exclude_files" , "exclude files matching a pattern" )
133+ analyzer .Flags .Var (& fExcludeDirs , "exclude_dirs" , "exclude directories matching a pattern" )
130134}
131135
132136func init () {
@@ -135,7 +139,7 @@ func init() {
135139
136140func run (pass * analysis.Pass ) (interface {}, error ) {
137141 if a := pass .Analyzer .Flags .Lookup ("fix" ); a != nil && a .Value .String () == "true" {
138- apply = true
142+ fApply = true
139143 }
140144
141145 inspect := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
@@ -157,15 +161,15 @@ func run(pass *analysis.Pass) (interface{}, error) {
157161 inspect .Preorder (nodeFilter , func (node ast.Node ) {
158162 fn := pass .Fset .File (node .Pos ()).Name ()
159163
160- if ! testFiles && hasSuffixes (testFset , fn , testSuffixes ) {
164+ if ! fTestFiles && hasSuffixes (testFset , fn , testSuffixes ) {
161165 return
162166 }
163167
164- if ! generatedFiles && hasSuffixes (generatedFset , fn , generatedSuffixes ) {
168+ if ! fGeneratedFiles && hasSuffixes (generatedFset , fn , generatedSuffixes ) {
165169 return
166170 }
167171
168- if len (excludeDirs ) > 0 || len (excludeFiles ) > 0 {
172+ if len (fExcludeDirs ) > 0 || len (fExcludeFiles ) > 0 {
169173 wd , err := os .Getwd ()
170174 if err != nil {
171175 fmt .Fprintf (os .Stderr , "%v %s: %v" , ErrPreFilterFiles , fn , err )
@@ -177,7 +181,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
177181 return
178182 }
179183 dir := filepath .Dir (relfn )
180- for _ , excludeDir := range excludeDirs {
184+ for _ , excludeDir := range fExcludeDirs {
181185 rel , err := filepath .Rel (excludeDir , dir )
182186 if err != nil {
183187 fmt .Fprintf (os .Stderr , "%v %s: %v" , ErrPreFilterFiles , fn , err )
@@ -187,7 +191,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
187191 return
188192 }
189193 }
190- for _ , excludeFile := range excludeFiles {
194+ for _ , excludeFile := range fExcludeFiles {
191195 match , err := filepath .Match (excludeFile , relfn )
192196 if err != nil {
193197 fmt .Fprintf (os .Stderr , "%v %s: %v" , ErrPreFilterFiles , fn , err )
@@ -203,7 +207,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
203207 aFile = f
204208 dFile , _ = dec .DecorateFile (aFile )
205209
206- if ! generatedFiles && hasGeneratedComment (generatedFset , fn , aFile ) {
210+ if ! fGeneratedFiles && hasGeneratedComment (generatedFset , fn , aFile ) {
207211 return
208212 }
209213
@@ -237,7 +241,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
237241 }
238242 })
239243
240- if ! apply {
244+ if ! fApply {
241245 return nil , nil
242246 }
243247
@@ -253,6 +257,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
253257func betteralign (pass * analysis.Pass , aNode * ast.StructType , typ * types.Struct , dec * decorator.Decorator ,
254258 dFile * dst.File , fixOps map [string ][]byte , fn string ,
255259) {
260+ unsafePointerTyp := types .Unsafe .Scope ().Lookup ("Pointer" ).(* types.TypeName ).Type ()
261+
256262 wordSize := pass .TypesSizes .Sizeof (unsafePointerTyp )
257263 maxAlign := pass .TypesSizes .Alignof (unsafePointerTyp )
258264
0 commit comments