diff --git a/ast/ast.go b/ast/ast.go index 0a5191d..6a7abea 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -295,6 +295,21 @@ func ByFieldNumber(_, ni, nj *Node, isWholeSlice bool) bool { return ni.Name < nj.Name } +// Formatter is a function that can format nodes in the AST. +type Formatter func([]*Node) error + +var extraFormatters []Formatter + +// RegisterFormatter registers an extra formatter that will be called after parsing. +func RegisterFormatter(f Formatter) { + extraFormatters = append(extraFormatters, f) +} + +// GetFormatters returns all registered formatters. +func GetFormatters() []Formatter { + return extraFormatters +} + // getChildValue returns the Value of the child with the given field name, // or nil if no single such child exists. func (n *Node) getChildValue(field string) *Value { diff --git a/impl/impl.go b/impl/impl.go index ea21ea2..22422c9 100644 --- a/impl/impl.go +++ b/impl/impl.go @@ -188,6 +188,11 @@ func ParseWithMetaCommentConfig(in []byte, c config.Config) ([]*ast.Node, error) if p.index < p.length { return nil, fmt.Errorf("parser didn't consume all input. Stopped at %s", p.errorContext()) } + for _, f := range ast.GetFormatters() { + if err := f(nodes); err != nil { + return nil, err + } + } if err := wrap.Strings(nodes, 0, c); err != nil { return nil, err } @@ -201,7 +206,7 @@ func ParseWithMetaCommentConfig(in []byte, c config.Config) ([]*ast.Node, error) // have the equal sign. Currently there are only two MetaComments that are in the former format: // // "sort_repeated_fields_by_subfield": If this appears multiple times, then they will all be added -// to the config and the order is perserved. +// to the config and the order is preserved. // "wrap_strings_at_column": The is expected to be an integer. If it is not, then it will be // ignored. If this appears multiple times, only the last one saved. func addToConfig(metaComment string, c *config.Config) error {