-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportEdgeBuilder.cs
More file actions
92 lines (75 loc) · 3.24 KB
/
ImportEdgeBuilder.cs
File metadata and controls
92 lines (75 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Text.RegularExpressions;
namespace CodeAtlas;
internal static partial class ImportEdgeBuilder
{
[GeneratedRegex(@"^from\s+(\S+)\s+import", RegexOptions.Compiled)]
private static partial Regex PythonImportFrom();
[GeneratedRegex(@"^import\s+(\S+)", RegexOptions.Compiled)]
private static partial Regex PythonImport();
[GeneratedRegex(@"from\s+['""]([^'""]+)['""]", RegexOptions.Compiled)]
private static partial Regex TsEsImport();
[GeneratedRegex(@"require\(['""]([^'""]+)['""]\)", RegexOptions.Compiled)]
private static partial Regex RequireCall();
internal static void Build(MrGraph graph, IEnumerable<DiffFile> diffFiles)
{
var fileIndex = BuildFileIndex(graph);
var seen = new HashSet<string>();
foreach (var diffFile in diffFiles.Where(f => !f.IsDeleted))
{
var source = graph.Files.FirstOrDefault(f => f.FilePath == diffFile.Path);
if (source is null) continue;
foreach (var import in ExtractImports(diffFile))
{
var target = ResolveImport(import, fileIndex);
if (target is null || target.Id == source.Id) continue;
var key = $"{source.Id}->{target.Id}";
if (!seen.Add(key)) continue;
graph.Edges.Add(new MrEdge
{
FromFileId = source.Id,
ToFileId = target.Id,
InterfaceName = "imports",
Type = "imports"
});
}
}
}
private static Dictionary<string, MrFileNode> BuildFileIndex(MrGraph graph)
{
var index = new Dictionary<string, MrFileNode>(StringComparer.OrdinalIgnoreCase);
foreach (var f in graph.Files)
{
index.TryAdd(f.FilePath, f);
index.TryAdd(f.FileName, f);
index.TryAdd(Path.GetFileNameWithoutExtension(f.FileName), f);
}
return index;
}
private static MrFileNode? ResolveImport(string import, Dictionary<string, MrFileNode> index)
{
if (index.TryGetValue(import, out var hit)) return hit;
if (index.TryGetValue(Path.GetFileName(import), out hit)) return hit;
if (index.TryGetValue(Path.GetFileNameWithoutExtension(import), out hit)) return hit;
var segments = import.Split('.', '/');
index.TryGetValue(segments[^1], out hit);
return hit;
}
private static List<string> ExtractImports(DiffFile file)
{
var imports = new List<string>();
foreach (var line in file.Hunks.SelectMany(h => h.Lines).Select(l => l.Text))
{
var t = line.TrimStart();
Match m;
m = PythonImportFrom().Match(t);
if (m.Success) { imports.Add(m.Groups[1].Value); continue; }
m = PythonImport().Match(t);
if (m.Success) { imports.Add(m.Groups[1].Value); continue; }
m = TsEsImport().Match(t);
if (m.Success && m.Groups[1].Value.StartsWith('.')) { imports.Add(m.Groups[1].Value); continue; }
m = RequireCall().Match(t);
if (m.Success && m.Groups[1].Value.StartsWith('.')) { imports.Add(m.Groups[1].Value); }
}
return imports;
}
}