Skip to content

Commit

Permalink
Error on name conflicts in re-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jan 19, 2025
1 parent 30c6617 commit ac2b74b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

* Adds CLI flag `--strict` to break with an error instead of warnings (#68)
* Adds CLI flag `--dry-run` to run without file output (#71)
* Checks package re-exports, giving warning or error on fail (#68)
* Allows for cross-refs to aliases in modules and structs (#69)

### Documentation
Expand All @@ -16,6 +15,11 @@
* Don't trim docstrings during extraction and removal of exports section (#60)
* Fixes broken links when referencing parents or the root package (#62)

### Other

* Checks package re-exports for correct paths (#68)
* Checks package re-exports for resulting name conflicts (#72)

## [[v0.6.0]](https://github.com/mlange-42/modo/compare/v0.5.0...v0.6.0)

### Features
Expand Down
5 changes: 2 additions & 3 deletions document/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package document

import (
"bufio"
"fmt"
"strings"
)

Expand Down Expand Up @@ -38,9 +39,7 @@ func (proc *Processor) collectExports(p *Package, elems []string) (bool, error)
}
for _, ex := range p.exports {
if _, ok := proc.allPaths[strings.Join(ex.Long, ".")]; !ok {
if err := proc.warnOrError("Unresolved package re-export '%s' in %s", strings.Join(ex.Long, "."), strings.Join(newElems, ".")); err != nil {
return anyExports, err
}
return anyExports, fmt.Errorf("unresolved package re-export '%s' in %s", strings.Join(ex.Long, "."), strings.Join(newElems, "."))
}
}
return anyExports, nil
Expand Down
32 changes: 31 additions & 1 deletion document/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package document

import (
"fmt"
"strings"
)

// Filters and re-structures docs for package re-exports.
//
// Also collects a lookup, mapping from original to altered cross-refs.
func (proc *Processor) filterPackages() error {
proc.linkExports = map[string]string{}
proc.linkExportsReverse = map[string]*exportError{}
anyExports, err := proc.collectExports(proc.Docs.Decl, nil)
if err != nil {
return err
Expand All @@ -32,7 +34,35 @@ func (proc *Processor) filterPackages() error {
}
proc.addLinkExport([]string{proc.Docs.Decl.Name}, []string{proc.Docs.Decl.Name})
proc.filterPackage(proc.Docs.Decl, proc.ExportDocs.Decl, nil, nil)
return nil

errs := []*exportError{}
for _, expErr := range proc.linkExportsReverse {
if len(expErr.OldPaths) > 1 {
errs = append(errs, expErr)
}
}
if len(errs) == 0 {
return nil
}
msg := strings.Builder{}
msg.WriteString(fmt.Sprintln("Name collisions in package re-exports:"))
for _, expErr := range errs {
msg.WriteString(fmt.Sprintf(" - %s (", expErr.NewPath))
for i, pOld := range expErr.OldPaths {
if i > 0 {
msg.WriteString(", ")
}
msg.WriteString(pOld)
}
msg.WriteString(")\n")
}

return fmt.Errorf("%s", msg.String())
}

type exportError struct {
NewPath string
OldPaths []string
}

func (proc *Processor) filterPackage(src, rootOut *Package, oldPath, newPath []string) {
Expand Down
31 changes: 21 additions & 10 deletions document/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ type Config struct {
}

type Processor struct {
Config *Config
Template *template.Template
Formatter Formatter
Docs *Docs
ExportDocs *Docs
allPaths map[string]bool
linkTargets map[string]elemPath
linkExports map[string]string
writer func(file, text string) error
Config *Config
Template *template.Template
Formatter Formatter
Docs *Docs
ExportDocs *Docs
allPaths map[string]bool
linkTargets map[string]elemPath
linkExports map[string]string
linkExportsReverse map[string]*exportError
writer func(file, text string) error
}

func NewProcessor(docs *Docs, f Formatter, t *template.Template, config *Config) *Processor {
Expand Down Expand Up @@ -83,7 +84,17 @@ func (proc *Processor) warnOrError(pattern string, args ...any) error {
}

func (proc *Processor) addLinkExport(oldPath, newPath []string) {
proc.linkExports[strings.Join(oldPath, ".")] = strings.Join(newPath, ".")
pNew := strings.Join(newPath, ".")
pOld := strings.Join(oldPath, ".")
if present, ok := proc.linkExportsReverse[pNew]; ok {
present.OldPaths = append(present.OldPaths, pOld)
} else {
proc.linkExportsReverse[pNew] = &exportError{
NewPath: pNew,
OldPaths: []string{pOld},
}
}
proc.linkExports[pOld] = pNew
}

func (proc *Processor) addLinkTarget(elPath, filePath []string, kind string, isSection bool) {
Expand Down

0 comments on commit ac2b74b

Please sign in to comment.