diff --git a/internal/literals/literals.go b/internal/literals/literals.go index b5e93fc..c5ebae1 100644 --- a/internal/literals/literals.go +++ b/internal/literals/literals.go @@ -33,8 +33,8 @@ func randObfuscator() obfuscator { return obfuscators[randPos] } -// Obfuscate replace literals with obfuscated lambda functions -func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, ignoreObj map[types.Object]bool) []*ast.File { +// Obfuscate replaces literals with obfuscated anonymous functions. +func Obfuscate(file *ast.File, info *types.Info, fset *token.FileSet, ignoreObj map[types.Object]bool) *ast.File { pre := func(cursor *astutil.Cursor) bool { switch x := cursor.Node().(type) { case *ast.GenDecl: @@ -174,10 +174,7 @@ func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, ignoreO return true } - for i := range files { - files[i] = astutil.Apply(files[i], pre, post).(*ast.File) - } - return files + return astutil.Apply(file, pre, post).(*ast.File) } func obfuscateString(data string) *ast.CallExpr { diff --git a/main.go b/main.go index 478d017..2dce034 100644 --- a/main.go +++ b/main.go @@ -607,11 +607,6 @@ func transformCompile(args []string) ([]string, error) { tf.recordReflectArgs(files) - if opts.GarbleLiterals { - // TODO: use transformer here? - files = literals.Obfuscate(files, tf.info, fset, tf.ignoreObjects) - } - // Add our temporary dir to the beginning of -trimpath, so that we don't // leak temporary dirs. Needs to be at the beginning, since there may be // shorter prefixes later in the list, such as $PWD if TMPDIR=$PWD/tmp. @@ -964,7 +959,6 @@ func (tf *transformer) recordReflectArgs(files []*ast.File) { visit := func(node ast.Node) bool { if opts.GarbleLiterals { - // TODO: use transformer here? literals.RecordUsedAsConstants(node, tf.info, tf.ignoreObjects) } @@ -1014,8 +1008,12 @@ type transformer struct { ignoreObjects map[types.Object]bool } -// transformGo garbles the provided Go syntax node. +// transformGo obfuscates the provided Go syntax file. func (tf *transformer) transformGo(file *ast.File) *ast.File { + if opts.GarbleLiterals { + file = literals.Obfuscate(file, tf.info, fset, tf.ignoreObjects) + } + pre := func(cursor *astutil.Cursor) bool { node, ok := cursor.Node().(*ast.Ident) if !ok {