gofactor

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 14 Imported by: 0

README

gofactor

Advanced utility for golang refactor based on DSL transformations provided by bblfsh/sdk

Usage example

Imagine you have a piece of code

package main

import "fmt"

func main() {
	var (
		i int
		X int
		j int
	)

	if i%2 == 0 {
		i = 5
	}

	if X%2 == 0 {
		X = 5
	}

	fmt.Println(i)

	if i%2 == 0 {
		i = 5
	}

	if j%2 == 0 {
		j = 5
	}
}

func a(i, X int) {
	if i%2 == 0 {
		i = 5
	}

	if X%2 == 0 {
		X = 5
	}

	fmt.Println(i)

	if i%2 == 0 {
		i = 5
	}

	if X%2 == 0 {
		X = 5
	}
}

And you want to replace all code patterns like

if i%2 == 0 {
    i = 5
}

if X%2 == 0 {
    X = 5
}

to

if i%2 == 1 {
    i = 1
} else {
    X = 1
}

Here's where refactor library comes for help.

  1. Init refactor object
refactor, err := gofactor.NewRefactor(beforeSnippet, afterSnippet)
if err != nil {
    log.Error(err)
    os.Exit(1)
}
  1. call Prepare function to generate transformations mappings
if err := refactor.Prepare(); err != nil {
    log.Error(err)
    os.Exit(1)
}
  1. Apply generated transformations to the desired code
code, err := refactor.Apply(desiredCode)
if err != nil {
    log.Error(err)
    os.Exit(1)
}

Result

package main

import "fmt"

func main() {
	var (
		i int
		X int
		j int
	)
	if i%2 == 1 {
		i = 1
	} else {
		X = 1
	}
	fmt.Println(i)
	if i%2 == 1 {
		i = 1
	} else {
		j = 1
	}
}
func a(i, X int) {
	if i%2 == 1 {
		i = 1
	} else {
		X = 1
	}
	fmt.Println(i)
	if i%2 == 1 {
		i = 1
	} else {
		X = 1
	}
}

Supported cases

See fixtures

Under the hood

  1. both input and output patterns are converted to go AST nodes
  2. both input and output nodes converted to bblfsh uast.Nodes
  3. define mapping of transformation operations from input to output node
  4. apply transformation mapping to the desired code: traverse over the uast.Nodes tree and transform matching nodes
  5. convert transformed tree back to golang AST
  6. convert golang AST to string

Roadmap

  • currently library cannot be build because of bblfsh/go-driver dependency issue, fix this part
  • support functions refactor
  • handle cases with cascade ifs, switches and tail recursions
  • during the transformations we are forced to drop nodes positions, need to investigate the possibilities of preserving/reconstructing them(probably using DST nodes could help, related issue https://github.com/dave/dst/issues/38)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Refactor

type Refactor struct {
	// contains filtered or unexported fields
}

func NewRefactor

func NewRefactor(before, after string) (*Refactor, error)

func (*Refactor) Apply

func (r *Refactor) Apply(code string) (string, error)

func (*Refactor) Prepare

func (r *Refactor) Prepare() error

Directories

Path Synopsis
fixtures
old
transform

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL