safechange

package module
v0.0.0-...-0316702 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2021 License: CC0-1.0 Imports: 7 Imported by: 0

README

Description

This tool returns exitcode 0 if two Go files has equivalent source code (which is expected to have the same machine code after compilation), and returns exitcode 1 if not.

This is supposed to be used to check if a change after a code review requires an additional review. For example:

  • Often in review one may accept a change and propose to optionally add a comment. In this case the author of the change has to choose between adding a comment (which leads to resetting the code review approval) or deliver the code as is. To avoid this dilemma this tool allows validating if the code change is safe (for example a change in comments).

Install

go get github.com/xaionaro-go/safechange/cmd/safechange

Run

safechange <file1> <file2>

Example

/tmp/1.go:

package pkg
import (
	"fmt"

	"io"
)

import "io/ioutil"

import "errors"

func main() {
	// some comment
}

/tmp/2.go:

package pkg
import (
	"errors"
	"fmt"
	"io/ioutil"
	"io"
)

func main() {
}

/tmp/3.go:

package pkg
import (
	"errors"
	"fmt"
	"io/ioutil"
	"io"
)

func main() {
	fmt.Println("hello!")
}

Files 1.go and 2.go are equivalent:

$ safechange /tmp/1.go /tmp/2.go
$ echo $?
0

Files 1.go and 3.go are not equivalent (because 3.go has fmt.Println, while 1.go hasn't):

$ safechange /tmp/1.go /tmp/3.go
$ echo $?
1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AstFile

type AstFile struct {
	*ast.File
}

AstFile is a wrapper for *ast.File which extends with additional methods.

func NewAstFile

func NewAstFile(file *ast.File) *AstFile

NewAstFile wraps an *ast.File to AstFile.

func (*AstFile) Copy

func (file *AstFile) Copy() *AstFile

Copy returns a deep copy.

func (*AstFile) Equals

func (file *AstFile) Equals(cmp *AstFile) bool

Equals returns true if ASTs has exactly the same content.

func (*AstFile) EquivalentTo

func (file *AstFile) EquivalentTo(cmp *AstFile) bool

EquivalentTo returns true if ASTs are expected to be compiled into the same machine code.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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