From b4ed621eed01b63777cf2e92399ac061b8f5803c Mon Sep 17 00:00:00 2001 From: lu4p Date: Mon, 13 Jul 2020 04:37:34 +0200 Subject: [PATCH] Shuffle the order of top-level definitions. (#60) --- main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/main.go b/main.go index fcbcb4b..9329583 100644 --- a/main.go +++ b/main.go @@ -723,6 +723,26 @@ func transformGo(file *ast.File, info *types.Info, blacklist map[types.Object]st } } + if len(file.Comments) == 0 { + // Shuffle top level declarations, + // if the file contains no "//go:" compiler directives. + // TODO: Also allow files with compiler directives. + mathrand.Shuffle(len(file.Decls), func(i, j int) { + decl1 := file.Decls[i] + decl2 := file.Decls[j] + + gendecl1, ok1 := decl1.(*ast.GenDecl) + gendecl2, ok2 := decl2.(*ast.GenDecl) + if (ok1 && gendecl1.Tok == token.IMPORT) || + (ok2 && gendecl2.Tok == token.IMPORT) { + // Don't move import statements. + return + } + + file.Decls[i], file.Decls[j] = decl2, decl1 + }) + } + pre := func(cursor *astutil.Cursor) bool { node, ok := cursor.Node().(*ast.Ident) if !ok {