codemod

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package codemod rewrites project code when an incompatible change of kit arrives.

A codemod belongs to the platform version that brings the change. apply runs the ones newer than the version the project was last applied with, which platformgo.lock records, so an upgrade over several versions replays every step in order. A codemod must be idempotent: code that has nothing left to rewrite stays as it is.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	Path     string   // relative to the project root
	Codemods []string // names of the codemods that changed it
	Content  []byte
}

Change is a file a run rewrote.

func Run

func Run(dir string, mods []Codemod) ([]Change, error)

Run applies the codemods to the Go files of the project without writing anything and returns the files that change.

type Codemod

type Codemod struct {
	Version     string // the platform version that brings the change
	Name        string
	Description string
	Fix         func(f *File) // rewrites one Go file through File.Replace
}

Codemod is one rewrite.

func All

func All() []Codemod

All are the codemods of the platform, oldest first. Add one with the change of kit that needs it, under the version that will be released with it:

RenameSymbol("v0.3.0", "github.com/aidarbn/platform-go/kit/modules/riverx", "NewQueue", "Queue"),

func Pending

func Pending(all []Codemod, applied string) []Codemod

Pending returns the codemods newer than the version the project was applied with, in version order. An empty or invalid version means before codemods existed: all of them.

func RenameSymbol

func RenameSymbol(version, pkg, from, to string) Codemod

RenameSymbol is the common codemod: an exported name of a kit package changes.

type File

type File struct {
	Path string
	Fset *token.FileSet
	AST  *ast.File
	// contains filtered or unexported fields
}

File is a Go file a codemod rewrites. Edits replace source ranges, so comments and layout outside them stay as the developer wrote them.

func (*File) ImportName

func (f *File) ImportName(path string) (string, bool)

ImportName returns the name a package is known by in the file, when it is imported.

func (*File) Replace

func (f *File) Replace(n ast.Node, text string)

Replace replaces the source of a node.

func (*File) Source

func (f *File) Source(n ast.Node) string

Source returns the source of a node.

Jump to

Keyboard shortcuts

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