• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 12857631973

19 Jan 2025 10:15PM CUT coverage: 57.418% (-0.4%) from 57.843%
12857631973

Pull #72

github

web-flow
Merge d3be17dbe into 30c661797
Pull Request #72: Check for name conflicts in re-exports

18 of 33 new or added lines in 3 files covered. (54.55%)

4 existing lines in 1 file now uncovered.

836 of 1456 relevant lines covered (57.42%)

8.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.49
/document/filter.go
1
package document
2

3
import (
4
        "fmt"
5
        "strings"
6
)
7

8
// Filters and re-structures docs for package re-exports.
9
//
10
// Also collects a lookup, mapping from original to altered cross-refs.
11
func (proc *Processor) filterPackages() error {
6✔
12
        proc.linkExports = map[string]string{}
6✔
13
        proc.linkExportsReverse = map[string]*exportError{}
6✔
14
        anyExports, err := proc.collectExports(proc.Docs.Decl, nil)
6✔
15
        if err != nil {
6✔
16
                return err
×
17
        }
×
18

19
        if !proc.Config.UseExports {
9✔
20
                proc.ExportDocs = &Docs{
3✔
21
                        Version: proc.Docs.Version,
3✔
22
                        Decl:    proc.Docs.Decl,
3✔
23
                }
3✔
24
                return nil
3✔
25
        }
3✔
26

27
        if !anyExports {
3✔
28
                return fmt.Errorf("no package re-exports found. Given flag '--exports', there will be no output.\n       Add exports or run without flag '--exports'")
×
29
        }
×
30

31
        proc.ExportDocs = &Docs{
3✔
32
                Version: proc.Docs.Version,
3✔
33
                Decl:    proc.Docs.Decl.linkedCopy(),
3✔
34
        }
3✔
35
        proc.addLinkExport([]string{proc.Docs.Decl.Name}, []string{proc.Docs.Decl.Name})
3✔
36
        proc.filterPackage(proc.Docs.Decl, proc.ExportDocs.Decl, nil, nil)
3✔
37

3✔
38
        errs := []*exportError{}
3✔
39
        for _, expErr := range proc.linkExportsReverse {
31✔
40
                if len(expErr.OldPaths) > 1 {
28✔
NEW
41
                        errs = append(errs, expErr)
×
NEW
42
                }
×
43
        }
44
        if len(errs) == 0 {
6✔
45
                return nil
3✔
46
        }
3✔
NEW
47
        msg := strings.Builder{}
×
NEW
48
        msg.WriteString(fmt.Sprintln("Name collisions in package re-exports:"))
×
NEW
49
        for _, expErr := range errs {
×
NEW
50
                msg.WriteString(fmt.Sprintf(" - %s (", expErr.NewPath))
×
NEW
51
                for i, pOld := range expErr.OldPaths {
×
NEW
52
                        if i > 0 {
×
NEW
53
                                msg.WriteString(", ")
×
NEW
54
                        }
×
NEW
55
                        msg.WriteString(pOld)
×
56
                }
NEW
57
                msg.WriteString(")\n")
×
58
        }
59

NEW
60
        return fmt.Errorf("%s", msg.String())
×
61
}
62

63
type exportError struct {
64
        NewPath  string
65
        OldPaths []string
66
}
67

68
func (proc *Processor) filterPackage(src, rootOut *Package, oldPath, newPath []string) {
5✔
69
        rootExports := map[string]*members{}
5✔
70
        collectExportsPackage(src, rootExports)
5✔
71

5✔
72
        oldPath = appendNew(oldPath, src.Name)
5✔
73
        newPath = appendNew(newPath, src.Name)
5✔
74

5✔
75
        for _, mod := range src.Modules {
13✔
76
                if mems, ok := rootExports[mod.Name]; ok {
16✔
77
                        proc.collectModuleExports(mod, rootOut, oldPath, newPath, mems)
8✔
78
                }
8✔
79
        }
80

81
        for _, pkg := range src.Packages {
7✔
82
                if mems, ok := rootExports[pkg.Name]; ok {
4✔
83
                        proc.collectPackageExports(pkg, rootOut, oldPath, newPath, mems)
2✔
84
                }
2✔
85
        }
86
}
87

88
func (proc *Processor) collectPackageExports(src, rootOut *Package, oldPath, newPath []string, rootMembers *members) {
2✔
89
        selfIncluded, toCrawl := collectExportMembers(rootMembers)
2✔
90

2✔
91
        if selfIncluded {
4✔
92
                newPkg := src.linkedCopy()
2✔
93
                proc.filterPackage(src, newPkg, oldPath, newPath)
2✔
94
                rootOut.Packages = append(rootOut.Packages, newPkg)
2✔
95

2✔
96
                tempOldPath := appendNew(oldPath, src.Name)
2✔
97
                tempNewPath := appendNew(newPath, src.Name)
2✔
98
                proc.addLinkExport(tempOldPath, tempNewPath)
2✔
99
        }
2✔
100
        oldPath = appendNew(oldPath, src.Name)
2✔
101

2✔
102
        for _, mod := range src.Modules {
4✔
103
                if mems, ok := toCrawl[mod.Name]; ok {
4✔
104
                        proc.collectModuleExports(mod, rootOut, oldPath, newPath, mems)
2✔
105
                }
2✔
106
        }
107

108
        for _, pkg := range src.Packages {
2✔
109
                if mems, ok := toCrawl[pkg.Name]; ok {
×
110
                        proc.collectPackageExports(pkg, rootOut, oldPath, newPath, mems)
×
111
                }
×
112
        }
113

114
        for _, elem := range src.Structs {
2✔
115
                proc.collectExportsStruct(elem, oldPath, newPath)
×
116
        }
×
117
        for _, elem := range src.Traits {
2✔
118
                proc.collectExportsTrait(elem, oldPath, newPath)
×
119
        }
×
120
        for _, elem := range src.Functions {
2✔
121
                proc.collectExportsFunction(elem, oldPath, newPath)
×
122
        }
×
123
}
124

125
func (proc *Processor) collectModuleExports(src *Module, rootOut *Package, oldPath, newPath []string, rootMembers *members) {
10✔
126
        selfIncluded, toCrawl := collectExportMembers(rootMembers)
10✔
127

10✔
128
        oldPath = appendNew(oldPath, src.Name)
10✔
129
        if selfIncluded {
15✔
130
                rootOut.Modules = append(rootOut.Modules, src)
5✔
131

5✔
132
                tempNewPath := appendNew(newPath, src.Name)
5✔
133
                proc.addLinkExport(oldPath, tempNewPath)
5✔
134

5✔
135
                collectExportsList(proc, src.Aliases, oldPath, tempNewPath)
5✔
136
                for _, elem := range src.Structs {
10✔
137
                        proc.collectExportsStruct(elem, oldPath, tempNewPath)
5✔
138
                }
5✔
139
                for _, elem := range src.Traits {
6✔
140
                        proc.collectExportsTrait(elem, oldPath, tempNewPath)
1✔
141
                }
1✔
142
                for _, elem := range src.Functions {
6✔
143
                        proc.collectExportsFunction(elem, oldPath, tempNewPath)
1✔
144
                }
1✔
145
        }
146

147
        for _, elem := range src.Structs {
22✔
148
                if _, ok := toCrawl[elem.Name]; ok {
19✔
149
                        rootOut.Structs = append(rootOut.Structs, elem)
7✔
150
                        proc.collectExportsStruct(elem, oldPath, newPath)
7✔
151
                }
7✔
152
        }
153
        for _, elem := range src.Traits {
13✔
154
                if _, ok := toCrawl[elem.Name]; ok {
3✔
155
                        rootOut.Traits = append(rootOut.Traits, elem)
×
156
                        proc.collectExportsTrait(elem, oldPath, newPath)
×
157
                }
×
158
        }
159
        for _, elem := range src.Functions {
13✔
160
                if _, ok := toCrawl[elem.Name]; ok {
5✔
161
                        rootOut.Functions = append(rootOut.Functions, elem)
2✔
162
                        proc.collectExportsFunction(elem, oldPath, newPath)
2✔
163
                }
2✔
164
        }
165
}
166

167
func (proc *Processor) collectExportsStruct(s *Struct, oldPath []string, newPath []string) {
12✔
168
        oldPath = appendNew(oldPath, s.Name)
12✔
169
        newPath = appendNew(newPath, s.Name)
12✔
170

12✔
171
        proc.addLinkExport(oldPath, newPath)
12✔
172

12✔
173
        collectExportsList(proc, s.Aliases, oldPath, newPath)
12✔
174
        collectExportsList(proc, s.Parameters, oldPath, newPath)
12✔
175
        collectExportsList(proc, s.Fields, oldPath, newPath)
12✔
176
        collectExportsList(proc, s.Functions, oldPath, newPath)
12✔
177
}
12✔
178

179
func (proc *Processor) collectExportsTrait(s *Trait, oldPath []string, newPath []string) {
1✔
180
        oldPath = appendNew(oldPath, s.Name)
1✔
181
        newPath = appendNew(newPath, s.Name)
1✔
182

1✔
183
        proc.addLinkExport(oldPath, newPath)
1✔
184

1✔
185
        collectExportsList(proc, s.Fields, oldPath, newPath)
1✔
186
        collectExportsList(proc, s.Functions, oldPath, newPath)
1✔
187
}
1✔
188

189
func (proc *Processor) collectExportsFunction(s *Function, oldPath []string, newPath []string) {
3✔
190
        oldPath = appendNew(oldPath, s.Name)
3✔
191
        newPath = appendNew(newPath, s.Name)
3✔
192

3✔
193
        proc.addLinkExport(oldPath, newPath)
3✔
194
}
3✔
195

196
func collectExportsList[T Named](proc *Processor, sl []T, oldPath, newPath []string) {
55✔
197
        for _, elem := range sl {
57✔
198
                oldPath := appendNew(oldPath, elem.GetName())
2✔
199
                newPath := appendNew(newPath, elem.GetName())
2✔
200
                proc.addLinkExport(oldPath, newPath)
2✔
201
        }
2✔
202
}
203

204
type members struct {
205
        Members []member
206
}
207

208
type member struct {
209
        Include bool
210
        RelPath []string
211
}
212

213
func collectExportMembers(parentMember *members) (selfIncluded bool, toCrawl map[string]*members) {
12✔
214
        selfIncluded = false
12✔
215
        toCrawl = map[string]*members{}
12✔
216

12✔
217
        for _, mem := range parentMember.Members {
32✔
218
                if mem.Include {
27✔
219
                        selfIncluded = true
7✔
220
                        continue
7✔
221
                }
222
                var newMember member
13✔
223
                if len(mem.RelPath) == 1 {
24✔
224
                        newMember = member{Include: true}
11✔
225
                } else {
13✔
226
                        newMember = member{RelPath: mem.RelPath[1:]}
2✔
227
                }
2✔
228
                if members, ok := toCrawl[mem.RelPath[0]]; ok {
15✔
229
                        members.Members = append(members.Members, newMember)
2✔
230
                        continue
2✔
231
                }
232
                toCrawl[mem.RelPath[0]] = &members{Members: []member{newMember}}
11✔
233
        }
234

235
        return
12✔
236
}
237

238
func collectExportsPackage(p *Package, out map[string]*members) {
5✔
239
        for _, ex := range p.exports {
21✔
240
                var newMember member
16✔
241
                if len(ex.Short) == 1 {
21✔
242
                        newMember = member{Include: true}
5✔
243
                } else {
16✔
244
                        newMember = member{RelPath: ex.Short[1:]}
11✔
245
                }
11✔
246
                if members, ok := out[ex.Short[0]]; ok {
22✔
247
                        members.Members = append(members.Members, newMember)
6✔
248
                        continue
6✔
249
                }
250
                out[ex.Short[0]] = &members{Members: []member{newMember}}
10✔
251
        }
252
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc