WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 90d6775

Browse files
committed
Update internal types and fields from 'reference' to 'usage' terminology
- Rename AnalyzeReferences() to AnalyzeUsage() - Rename findReferencesInFile() to findUsagesInFile() - Rename GroupReferencesByFile() to GroupUsagesByFile() - Rename runReferences() to runUsage() - Update all type names: - ReferenceAnalysis -> UsageAnalysis - FileReference -> FileUsage - ReferenceNode -> UsageNode - GroupedFileReference -> GroupedFileUsage - Update all field names: - ReferencingFiles -> UsingFiles - ReferenceTree -> UsageTree - TotalReferences -> TotalUsages - ReferencePath -> UsagePath - References -> Usages - Update JSON field names for consistency: - total_references -> total_usages - referencing_files -> using_files - reference_path -> usage_path - Update all test functions and test data - Update all comments and documentation All tests pass. No backward compatibility concerns per user request.
1 parent 321d967 commit 90d6775

File tree

5 files changed

+234
-234
lines changed

5 files changed

+234
-234
lines changed

audit-cli/commands/analyze/usage/analyzer.go

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"github.com/mongodb/code-example-tooling/audit-cli/internal/rst"
1313
)
1414

15-
// AnalyzeReferences finds all files that reference the target file.
15+
// AnalyzeUsage finds all files that use the target file.
1616
//
1717
// This function searches through all RST files (.rst, .txt) and YAML files (.yaml, .yml)
18-
// in the source directory to find files that reference the target file using include,
18+
// in the source directory to find files that use the target file through include,
1919
// literalinclude, or io-code-block directives. YAML files are included because extract
2020
// and release files contain RST directives within their content blocks.
2121
//
@@ -29,9 +29,9 @@ import (
2929
// - excludePattern: Glob pattern for paths to exclude (empty string means no exclusion)
3030
//
3131
// Returns:
32-
// - *ReferenceAnalysis: The analysis results
32+
// - *UsageAnalysis: The analysis results
3333
// - error: Any error encountered during analysis
34-
func AnalyzeReferences(targetFile string, includeToctree bool, verbose bool, excludePattern string) (*ReferenceAnalysis, error) {
34+
func AnalyzeUsage(targetFile string, includeToctree bool, verbose bool, excludePattern string) (*UsageAnalysis, error) {
3535
// Check if target file exists
3636
if _, err := os.Stat(targetFile); os.IsNotExist(err) {
3737
return nil, fmt.Errorf("target file does not exist: %s\n\nPlease check:\n - The file path is correct\n - The file hasn't been moved or deleted\n - You have permission to access the file", targetFile)
@@ -50,10 +50,10 @@ func AnalyzeReferences(targetFile string, includeToctree bool, verbose bool, exc
5050
}
5151

5252
// Initialize analysis result
53-
analysis := &ReferenceAnalysis{
54-
TargetFile: absTargetFile,
55-
SourceDir: sourceDir,
56-
ReferencingFiles: []FileReference{},
53+
analysis := &UsageAnalysis{
54+
TargetFile: absTargetFile,
55+
SourceDir: sourceDir,
56+
UsingFiles: []FileUsage{},
5757
}
5858

5959
// Track if we found any RST/YAML files
@@ -62,7 +62,7 @@ func AnalyzeReferences(targetFile string, includeToctree bool, verbose bool, exc
6262

6363
// Show progress message if verbose
6464
if verbose {
65-
fmt.Fprintf(os.Stderr, "Scanning for references in %s...\n", sourceDir)
65+
fmt.Fprintf(os.Stderr, "Scanning for usages in %s...\n", sourceDir)
6666
}
6767

6868
// Walk through all RST and YAML files in the source directory
@@ -103,16 +103,16 @@ func AnalyzeReferences(targetFile string, includeToctree bool, verbose bool, exc
103103
fmt.Fprintf(os.Stderr, "Processed %d files...\n", filesProcessed)
104104
}
105105

106-
// Search for references in this file
107-
refs, err := findReferencesInFile(path, absTargetFile, sourceDir, includeToctree)
106+
// Search for usages in this file
107+
usages, err := findUsagesInFile(path, absTargetFile, sourceDir, includeToctree)
108108
if err != nil {
109109
// Log error but continue processing other files
110110
fmt.Fprintf(os.Stderr, "Warning: failed to process %s: %v\n", path, err)
111111
return nil
112112
}
113113

114-
// Add any found references
115-
analysis.ReferencingFiles = append(analysis.ReferencingFiles, refs...)
114+
// Add any found usages
115+
analysis.UsingFiles = append(analysis.UsingFiles, usages...)
116116

117117
return nil
118118
})
@@ -132,16 +132,16 @@ func AnalyzeReferences(targetFile string, includeToctree bool, verbose bool, exc
132132
}
133133

134134
// Update total counts
135-
analysis.TotalReferences = len(analysis.ReferencingFiles)
136-
analysis.TotalFiles = countUniqueFiles(analysis.ReferencingFiles)
135+
analysis.TotalUsages = len(analysis.UsingFiles)
136+
analysis.TotalFiles = countUniqueFiles(analysis.UsingFiles)
137137

138138
return analysis, nil
139139
}
140140

141-
// findReferencesInFile searches a single file for references to the target file.
141+
// findUsagesInFile searches a single file for usages of the target file.
142142
//
143143
// This function scans through the file line by line looking for include,
144-
// literalinclude, and io-code-block directives that reference the target file.
144+
// literalinclude, and io-code-block directives that use the target file.
145145
// If includeToctree is true, also searches for toctree entries.
146146
//
147147
// Parameters:
@@ -151,16 +151,16 @@ func AnalyzeReferences(targetFile string, includeToctree bool, verbose bool, exc
151151
// - includeToctree: If true, include toctree entries in the search
152152
//
153153
// Returns:
154-
// - []FileReference: List of references found in this file
154+
// - []FileUsage: List of usages found in this file
155155
// - error: Any error encountered during processing
156-
func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree bool) ([]FileReference, error) {
156+
func findUsagesInFile(filePath, targetFile, sourceDir string, includeToctree bool) ([]FileUsage, error) {
157157
file, err := os.Open(filePath)
158158
if err != nil {
159159
return nil, err
160160
}
161161
defer file.Close()
162162

163-
var references []FileReference
163+
var usages []FileUsage
164164
scanner := bufio.NewScanner(file)
165165
lineNum := 0
166166
inIOCodeBlock := false
@@ -201,10 +201,10 @@ func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree
201201
if matches := rst.IncludeDirectiveRegex.FindStringSubmatch(trimmedLine); matches != nil {
202202
refPath := strings.TrimSpace(matches[1])
203203
if referencesTarget(refPath, targetFile, sourceDir, filePath) {
204-
references = append(references, FileReference{
204+
usages = append(usages, FileUsage{
205205
FilePath: filePath,
206206
DirectiveType: "include",
207-
ReferencePath: refPath,
207+
UsagePath: refPath,
208208
LineNumber: lineNum,
209209
})
210210
}
@@ -215,10 +215,10 @@ func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree
215215
if matches := rst.LiteralIncludeDirectiveRegex.FindStringSubmatch(trimmedLine); matches != nil {
216216
refPath := strings.TrimSpace(matches[1])
217217
if referencesTarget(refPath, targetFile, sourceDir, filePath) {
218-
references = append(references, FileReference{
218+
usages = append(usages, FileUsage{
219219
FilePath: filePath,
220220
DirectiveType: "literalinclude",
221-
ReferencePath: refPath,
221+
UsagePath: refPath,
222222
LineNumber: lineNum,
223223
})
224224
}
@@ -231,10 +231,10 @@ func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree
231231
if matches := rst.InputDirectiveRegex.FindStringSubmatch(trimmedLine); matches != nil {
232232
refPath := strings.TrimSpace(matches[1])
233233
if referencesTarget(refPath, targetFile, sourceDir, filePath) {
234-
references = append(references, FileReference{
234+
usages = append(usages, FileUsage{
235235
FilePath: filePath,
236236
DirectiveType: "io-code-block",
237-
ReferencePath: refPath,
237+
UsagePath: refPath,
238238
LineNumber: ioCodeBlockStartLine,
239239
})
240240
}
@@ -245,10 +245,10 @@ func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree
245245
if matches := rst.OutputDirectiveRegex.FindStringSubmatch(trimmedLine); matches != nil {
246246
refPath := strings.TrimSpace(matches[1])
247247
if referencesTarget(refPath, targetFile, sourceDir, filePath) {
248-
references = append(references, FileReference{
248+
usages = append(usages, FileUsage{
249249
FilePath: filePath,
250250
DirectiveType: "io-code-block",
251-
ReferencePath: refPath,
251+
UsagePath: refPath,
252252
LineNumber: ioCodeBlockStartLine,
253253
})
254254
}
@@ -267,10 +267,10 @@ func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree
267267
// Document names can be relative or absolute (starting with /)
268268
docName := trimmedLine
269269
if referencesToctreeTarget(docName, targetFile, sourceDir, filePath) {
270-
references = append(references, FileReference{
270+
usages = append(usages, FileUsage{
271271
FilePath: filePath,
272272
DirectiveType: "toctree",
273-
ReferencePath: docName,
273+
UsagePath: docName,
274274
LineNumber: toctreeStartLine,
275275
})
276276
}
@@ -281,7 +281,7 @@ func findReferencesInFile(filePath, targetFile, sourceDir string, includeToctree
281281
return nil, err
282282
}
283283

284-
return references, nil
284+
return usages, nil
285285
}
286286

287287
// referencesTarget checks if a reference path points to the target file.
@@ -345,83 +345,83 @@ func referencesToctreeTarget(docName, targetFile, sourceDir, currentFile string)
345345
return resolvedPath == targetFile
346346
}
347347

348-
// FilterByDirectiveType filters the analysis results to only include references
348+
// FilterByDirectiveType filters the analysis results to only include usages
349349
// of the specified directive type.
350350
//
351351
// Parameters:
352352
// - analysis: The original analysis results
353353
// - directiveType: The directive type to filter by (include, literalinclude, io-code-block)
354354
//
355355
// Returns:
356-
// - *ReferenceAnalysis: A new analysis with filtered results
357-
func FilterByDirectiveType(analysis *ReferenceAnalysis, directiveType string) *ReferenceAnalysis {
358-
filtered := &ReferenceAnalysis{
359-
TargetFile: analysis.TargetFile,
360-
SourceDir: analysis.SourceDir,
361-
ReferencingFiles: []FileReference{},
362-
ReferenceTree: analysis.ReferenceTree,
356+
// - *UsageAnalysis: A new analysis with filtered results
357+
func FilterByDirectiveType(analysis *UsageAnalysis, directiveType string) *UsageAnalysis {
358+
filtered := &UsageAnalysis{
359+
TargetFile: analysis.TargetFile,
360+
SourceDir: analysis.SourceDir,
361+
UsingFiles: []FileUsage{},
362+
UsageTree: analysis.UsageTree,
363363
}
364364

365-
// Filter references
366-
for _, ref := range analysis.ReferencingFiles {
367-
if ref.DirectiveType == directiveType {
368-
filtered.ReferencingFiles = append(filtered.ReferencingFiles, ref)
365+
// Filter usages
366+
for _, usage := range analysis.UsingFiles {
367+
if usage.DirectiveType == directiveType {
368+
filtered.UsingFiles = append(filtered.UsingFiles, usage)
369369
}
370370
}
371371

372372
// Update counts
373-
filtered.TotalReferences = len(filtered.ReferencingFiles)
374-
filtered.TotalFiles = countUniqueFiles(filtered.ReferencingFiles)
373+
filtered.TotalUsages = len(filtered.UsingFiles)
374+
filtered.TotalFiles = countUniqueFiles(filtered.UsingFiles)
375375

376376
return filtered
377377
}
378378

379-
// countUniqueFiles counts the number of unique files in the reference list.
379+
// countUniqueFiles counts the number of unique files in the usage list.
380380
//
381381
// Parameters:
382-
// - refs: List of file references
382+
// - usages: List of file usages
383383
//
384384
// Returns:
385385
// - int: Number of unique files
386-
func countUniqueFiles(refs []FileReference) int {
386+
func countUniqueFiles(usages []FileUsage) int {
387387
uniqueFiles := make(map[string]bool)
388-
for _, ref := range refs {
389-
uniqueFiles[ref.FilePath] = true
388+
for _, usage := range usages {
389+
uniqueFiles[usage.FilePath] = true
390390
}
391391
return len(uniqueFiles)
392392
}
393393

394-
// GroupReferencesByFile groups references by file path and directive type.
394+
// GroupUsagesByFile groups usages by file path and directive type.
395395
//
396-
// This function takes a flat list of references and groups them by file,
397-
// counting how many times each file references the target.
396+
// This function takes a flat list of usages and groups them by file,
397+
// counting how many times each file uses the target.
398398
//
399399
// Parameters:
400-
// - refs: List of file references
400+
// - usages: List of file usages
401401
//
402402
// Returns:
403-
// - []GroupedFileReference: List of grouped references, sorted by file path
404-
func GroupReferencesByFile(refs []FileReference) []GroupedFileReference {
403+
// - []GroupedFileUsage: List of grouped usages, sorted by file path
404+
func GroupUsagesByFile(usages []FileUsage) []GroupedFileUsage {
405405
// Group by file path and directive type
406406
type groupKey struct {
407407
filePath string
408408
directiveType string
409409
}
410-
groups := make(map[groupKey][]FileReference)
410+
groups := make(map[groupKey][]FileUsage)
411411

412-
for _, ref := range refs {
413-
key := groupKey{ref.FilePath, ref.DirectiveType}
414-
groups[key] = append(groups[key], ref)
412+
for _, usage := range usages {
413+
key := groupKey{usage.FilePath, usage.DirectiveType}
414+
groups[key] = append(groups[key], usage)
415415
}
416416

417417
// Convert to slice
418-
var grouped []GroupedFileReference
419-
for key, refs := range groups {
420-
grouped = append(grouped, GroupedFileReference{
418+
var grouped []GroupedFileUsage
419+
for key, usages := range groups {
420+
grouped = append(grouped, GroupedFileUsage{
421421
FilePath: key.filePath,
422422
DirectiveType: key.directiveType,
423-
References: refs,
424-
Count: len(refs),
423+
Usages: usages,
424+
Count: len(usages),
425425
})
426426
}
427427

0 commit comments

Comments
 (0)