featmap 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. featmap-0.1.1.dist-info/METADATA +95 -0
  2. featmap-0.1.1.dist-info/RECORD +130 -0
  3. featmap-0.1.1.dist-info/WHEEL +5 -0
  4. featmap-0.1.1.dist-info/entry_points.txt +2 -0
  5. featmap-0.1.1.dist-info/licenses/LICENSE +24 -0
  6. featmap-0.1.1.dist-info/licenses/fmap/vendor/typescript/LICENSE.txt +55 -0
  7. featmap-0.1.1.dist-info/licenses/fmap/vendor/typescript/ThirdPartyNoticeText.txt +193 -0
  8. featmap-0.1.1.dist-info/top_level.txt +1 -0
  9. fmap/__init__.py +1 -0
  10. fmap/__main__.py +3 -0
  11. fmap/axum.py +485 -0
  12. fmap/cache.py +25 -0
  13. fmap/cli.py +907 -0
  14. fmap/goext/go.mod +3 -0
  15. fmap/goext/main.go +547 -0
  16. fmap/goext.py +43 -0
  17. fmap/graph.py +920 -0
  18. fmap/impact.py +171 -0
  19. fmap/index.py +66 -0
  20. fmap/pyx.py +1133 -0
  21. fmap/routes.py +163 -0
  22. fmap/rsx.py +381 -0
  23. fmap/scip.py +80 -0
  24. fmap/trace_site.py +63 -0
  25. fmap/tsx.cjs +1258 -0
  26. fmap/tsx.py +98 -0
  27. fmap/vendor/typescript/LICENSE.txt +55 -0
  28. fmap/vendor/typescript/ThirdPartyNoticeText.txt +193 -0
  29. fmap/vendor/typescript/lib/lib.d.ts +22 -0
  30. fmap/vendor/typescript/lib/lib.decorators.d.ts +384 -0
  31. fmap/vendor/typescript/lib/lib.decorators.legacy.d.ts +22 -0
  32. fmap/vendor/typescript/lib/lib.dom.asynciterable.d.ts +41 -0
  33. fmap/vendor/typescript/lib/lib.dom.d.ts +39429 -0
  34. fmap/vendor/typescript/lib/lib.dom.iterable.d.ts +571 -0
  35. fmap/vendor/typescript/lib/lib.es2015.collection.d.ts +147 -0
  36. fmap/vendor/typescript/lib/lib.es2015.core.d.ts +597 -0
  37. fmap/vendor/typescript/lib/lib.es2015.d.ts +28 -0
  38. fmap/vendor/typescript/lib/lib.es2015.generator.d.ts +77 -0
  39. fmap/vendor/typescript/lib/lib.es2015.iterable.d.ts +605 -0
  40. fmap/vendor/typescript/lib/lib.es2015.promise.d.ts +81 -0
  41. fmap/vendor/typescript/lib/lib.es2015.proxy.d.ts +128 -0
  42. fmap/vendor/typescript/lib/lib.es2015.reflect.d.ts +144 -0
  43. fmap/vendor/typescript/lib/lib.es2015.symbol.d.ts +46 -0
  44. fmap/vendor/typescript/lib/lib.es2015.symbol.wellknown.d.ts +326 -0
  45. fmap/vendor/typescript/lib/lib.es2016.array.include.d.ts +116 -0
  46. fmap/vendor/typescript/lib/lib.es2016.d.ts +21 -0
  47. fmap/vendor/typescript/lib/lib.es2016.full.d.ts +23 -0
  48. fmap/vendor/typescript/lib/lib.es2016.intl.d.ts +31 -0
  49. fmap/vendor/typescript/lib/lib.es2017.arraybuffer.d.ts +21 -0
  50. fmap/vendor/typescript/lib/lib.es2017.d.ts +26 -0
  51. fmap/vendor/typescript/lib/lib.es2017.date.d.ts +31 -0
  52. fmap/vendor/typescript/lib/lib.es2017.full.d.ts +23 -0
  53. fmap/vendor/typescript/lib/lib.es2017.intl.d.ts +44 -0
  54. fmap/vendor/typescript/lib/lib.es2017.object.d.ts +49 -0
  55. fmap/vendor/typescript/lib/lib.es2017.sharedmemory.d.ts +135 -0
  56. fmap/vendor/typescript/lib/lib.es2017.string.d.ts +45 -0
  57. fmap/vendor/typescript/lib/lib.es2017.typedarrays.d.ts +53 -0
  58. fmap/vendor/typescript/lib/lib.es2018.asyncgenerator.d.ts +77 -0
  59. fmap/vendor/typescript/lib/lib.es2018.asynciterable.d.ts +53 -0
  60. fmap/vendor/typescript/lib/lib.es2018.d.ts +24 -0
  61. fmap/vendor/typescript/lib/lib.es2018.full.d.ts +24 -0
  62. fmap/vendor/typescript/lib/lib.es2018.intl.d.ts +83 -0
  63. fmap/vendor/typescript/lib/lib.es2018.promise.d.ts +30 -0
  64. fmap/vendor/typescript/lib/lib.es2018.regexp.d.ts +37 -0
  65. fmap/vendor/typescript/lib/lib.es2019.array.d.ts +79 -0
  66. fmap/vendor/typescript/lib/lib.es2019.d.ts +24 -0
  67. fmap/vendor/typescript/lib/lib.es2019.full.d.ts +24 -0
  68. fmap/vendor/typescript/lib/lib.es2019.intl.d.ts +23 -0
  69. fmap/vendor/typescript/lib/lib.es2019.object.d.ts +33 -0
  70. fmap/vendor/typescript/lib/lib.es2019.string.d.ts +37 -0
  71. fmap/vendor/typescript/lib/lib.es2019.symbol.d.ts +24 -0
  72. fmap/vendor/typescript/lib/lib.es2020.bigint.d.ts +765 -0
  73. fmap/vendor/typescript/lib/lib.es2020.d.ts +27 -0
  74. fmap/vendor/typescript/lib/lib.es2020.date.d.ts +42 -0
  75. fmap/vendor/typescript/lib/lib.es2020.full.d.ts +24 -0
  76. fmap/vendor/typescript/lib/lib.es2020.intl.d.ts +474 -0
  77. fmap/vendor/typescript/lib/lib.es2020.number.d.ts +28 -0
  78. fmap/vendor/typescript/lib/lib.es2020.promise.d.ts +47 -0
  79. fmap/vendor/typescript/lib/lib.es2020.sharedmemory.d.ts +99 -0
  80. fmap/vendor/typescript/lib/lib.es2020.string.d.ts +44 -0
  81. fmap/vendor/typescript/lib/lib.es2020.symbol.wellknown.d.ts +41 -0
  82. fmap/vendor/typescript/lib/lib.es2021.d.ts +23 -0
  83. fmap/vendor/typescript/lib/lib.es2021.full.d.ts +24 -0
  84. fmap/vendor/typescript/lib/lib.es2021.intl.d.ts +166 -0
  85. fmap/vendor/typescript/lib/lib.es2021.promise.d.ts +48 -0
  86. fmap/vendor/typescript/lib/lib.es2021.string.d.ts +33 -0
  87. fmap/vendor/typescript/lib/lib.es2021.weakref.d.ts +78 -0
  88. fmap/vendor/typescript/lib/lib.es2022.array.d.ts +121 -0
  89. fmap/vendor/typescript/lib/lib.es2022.d.ts +25 -0
  90. fmap/vendor/typescript/lib/lib.es2022.error.d.ts +75 -0
  91. fmap/vendor/typescript/lib/lib.es2022.full.d.ts +24 -0
  92. fmap/vendor/typescript/lib/lib.es2022.intl.d.ts +145 -0
  93. fmap/vendor/typescript/lib/lib.es2022.object.d.ts +26 -0
  94. fmap/vendor/typescript/lib/lib.es2022.regexp.d.ts +39 -0
  95. fmap/vendor/typescript/lib/lib.es2022.string.d.ts +25 -0
  96. fmap/vendor/typescript/lib/lib.es2023.array.d.ts +924 -0
  97. fmap/vendor/typescript/lib/lib.es2023.collection.d.ts +21 -0
  98. fmap/vendor/typescript/lib/lib.es2023.d.ts +22 -0
  99. fmap/vendor/typescript/lib/lib.es2023.full.d.ts +24 -0
  100. fmap/vendor/typescript/lib/lib.es2023.intl.d.ts +56 -0
  101. fmap/vendor/typescript/lib/lib.es2024.arraybuffer.d.ts +65 -0
  102. fmap/vendor/typescript/lib/lib.es2024.collection.d.ts +29 -0
  103. fmap/vendor/typescript/lib/lib.es2024.d.ts +26 -0
  104. fmap/vendor/typescript/lib/lib.es2024.full.d.ts +24 -0
  105. fmap/vendor/typescript/lib/lib.es2024.object.d.ts +29 -0
  106. fmap/vendor/typescript/lib/lib.es2024.promise.d.ts +35 -0
  107. fmap/vendor/typescript/lib/lib.es2024.regexp.d.ts +25 -0
  108. fmap/vendor/typescript/lib/lib.es2024.sharedmemory.d.ts +68 -0
  109. fmap/vendor/typescript/lib/lib.es2024.string.d.ts +29 -0
  110. fmap/vendor/typescript/lib/lib.es5.d.ts +4601 -0
  111. fmap/vendor/typescript/lib/lib.es6.d.ts +23 -0
  112. fmap/vendor/typescript/lib/lib.esnext.array.d.ts +35 -0
  113. fmap/vendor/typescript/lib/lib.esnext.collection.d.ts +96 -0
  114. fmap/vendor/typescript/lib/lib.esnext.d.ts +29 -0
  115. fmap/vendor/typescript/lib/lib.esnext.decorators.d.ts +28 -0
  116. fmap/vendor/typescript/lib/lib.esnext.disposable.d.ts +193 -0
  117. fmap/vendor/typescript/lib/lib.esnext.error.d.ts +24 -0
  118. fmap/vendor/typescript/lib/lib.esnext.float16.d.ts +445 -0
  119. fmap/vendor/typescript/lib/lib.esnext.full.d.ts +24 -0
  120. fmap/vendor/typescript/lib/lib.esnext.intl.d.ts +21 -0
  121. fmap/vendor/typescript/lib/lib.esnext.iterator.d.ts +148 -0
  122. fmap/vendor/typescript/lib/lib.esnext.promise.d.ts +34 -0
  123. fmap/vendor/typescript/lib/lib.esnext.sharedmemory.d.ts +25 -0
  124. fmap/vendor/typescript/lib/lib.scripthost.d.ts +322 -0
  125. fmap/vendor/typescript/lib/lib.webworker.asynciterable.d.ts +41 -0
  126. fmap/vendor/typescript/lib/lib.webworker.d.ts +13150 -0
  127. fmap/vendor/typescript/lib/lib.webworker.importscripts.d.ts +23 -0
  128. fmap/vendor/typescript/lib/lib.webworker.iterable.d.ts +340 -0
  129. fmap/vendor/typescript/lib/typescript.js +200276 -0
  130. fmap/vendor/typescript/package.json +120 -0
fmap/axum.py ADDED
@@ -0,0 +1,485 @@
1
+ """Conservative axum registration scan; SCIP supplies symbol identity, not name guesses."""
2
+ import json
3
+ import re
4
+
5
+ TOKEN = re.compile(r'//[^\n]*|/\*[\s\S]*?\*/|r(\#*)"[\s\S]*?"\1|"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\\n])\'|\b[A-Za-z_]\w*\b|::|->|[^\s]')
6
+ ROUTER_METHODS = {"route", "route_service", "nest", "nest_service", "merge", "layer", "route_layer",
7
+ "with_state", "fallback", "fallback_service", "without_v07_checks"}
8
+ METHODS = {"get", "post", "put", "patch", "delete", "head", "options", "trace", "connect", "any"}
9
+ FRAMEWORK_WORD = re.compile(r"\b(?:axum|actix_web|rocket|warp)\b")
10
+ FRAMEWORKS = {"axum": "axum", "actix_web": "actix-web", "rocket": "rocket", "warp": "warp"}
11
+
12
+
13
+ def tokens(text):
14
+ result = []
15
+ line, line_start, previous = 1, 0, 0
16
+ for match in TOKEN.finditer(text):
17
+ start = match.start()
18
+ line += text.count("\n", previous, start)
19
+ last_newline = text.rfind("\n", previous, start)
20
+ if last_newline >= 0:
21
+ line_start = last_newline + 1
22
+ previous = start
23
+ if not match.group().startswith(("//", "/*")):
24
+ result.append((match.group(), line, start - line_start))
25
+ return result
26
+
27
+
28
+ def literal(value):
29
+ if value.startswith('r'):
30
+ match = re.fullmatch(r'r(\#*)"([\s\S]*)"\1', value)
31
+ return match[2] if match else None
32
+ try:
33
+ return json.loads(value) if value.startswith('"') else None
34
+ except ValueError:
35
+ return None
36
+
37
+
38
+ ITEM_WORDS = {"const", "static", "fn", "mod", "struct", "enum", "union", "type", "trait", "as", "!"}
39
+ ITEM_START = {"fn", "mod", "struct", "enum", "union", "type", "trait", "impl", "use", "const", "static",
40
+ "extern", "async", "unsafe", "macro_rules"}
41
+
42
+
43
+ def strip_cfg_test(stream):
44
+ """Drop test-only items: `#[cfg(test)]`, `#[cfg(all(test, ..))]` and `#[test]`/`#[x::test]` (absent from
45
+ production routers).
46
+
47
+ Returns the kept tokens, top-level `mod x;` names gated by cfg(test), plain top-level `mod x;` names
48
+ and whether the whole file is `#![cfg(test)]`."""
49
+ values = [token[0] for token in stream]
50
+ pairs, stack, depth = {}, [], [0] * len(values)
51
+ for index, value in enumerate(values):
52
+ depth[index] = len(stack)
53
+ if value in ("(", "{", "["):
54
+ stack.append(index)
55
+ elif value in (")", "}", "]") and stack:
56
+ pairs[stack.pop()] = index
57
+ opener = {close: open_ for open_, close in pairs.items()}
58
+
59
+ def test_only(start, close, inner=False):
60
+ """Attribute body values[start:close] is test-only: cfg(test), cfg(all(.., test, ..)) or a `..::test` path."""
61
+ body = values[start:close]
62
+ if body[:2] == ["cfg", "("]:
63
+ if body[2:] == ["test", ")"]:
64
+ return True
65
+ group = start + 3 # `all(` only: `any(test, ..)` and `not(test)` still compile outside tests
66
+ return (body[2:4] == ["all", "("] and pairs.get(group) == close - 2
67
+ and any(values[k] == "test" and depth[k] == depth[group] + 1 and values[k - 1] in ("(", ",")
68
+ and values[k + 1] in (",", ")") for k in range(group + 1, close - 2)))
69
+ path = body[:body.index("(")] if "(" in body else body
70
+ return (not inner and path[-1:] == ["test"] and all(t == "::" for t in path[1::2])
71
+ and all(re.fullmatch(r"[A-Za-z_]\w*", t) for t in path[::2]))
72
+
73
+ drop, gated, index = set(), set(), 0
74
+ while index < len(values):
75
+ if values[index:index + 2] != ["#", "["] or index + 1 not in pairs or not test_only(index + 2, pairs[index + 1]):
76
+ index += 1
77
+ continue
78
+ start, end, attrs = index, pairs[index + 1] + 1, []
79
+ while values[end:end + 2] == ["#", "["] and end + 1 in pairs:
80
+ attrs.append(values[end + 2])
81
+ end = pairs[end + 1] + 1
82
+ item = end
83
+ if values[item:item + 2] == ["pub", "("]:
84
+ item = pairs.get(item + 1, item) + 1
85
+ elif values[item:item + 1] == ["pub"]:
86
+ item += 1
87
+ keyword, attr_end, last, unclear = values[item:item + 1] and values[item] in ITEM_START, end, None, False
88
+ # An item ends at its `;` or body; a field/argument/variant ends at `,` or the enclosing closer.
89
+ while end < len(values) and values[end] not in ("{", ";", ",", ")", "}", "]"):
90
+ # A statement without `;` (`return`, `unreachable!()`) must not swallow the next line's statement.
91
+ unclear = (not keyword and last is not None and stream[end][1] > stream[last][1]
92
+ and re.match(r"\w", values[end]) and re.match(r"[\w\"')\]]", values[last]))
93
+ if unclear:
94
+ break
95
+ last = pairs.get(end, end)
96
+ end = last + 1
97
+ if unclear:
98
+ drop.update(range(start, attr_end)) # statement end unclear: drop only the attributes
99
+ index = attr_end
100
+ continue
101
+ if values[end:end + 1] == ["{"]:
102
+ end = pairs.get(end, end)
103
+ elif values[end:end + 1] not in ([";"], [","]):
104
+ end -= 1
105
+ if depth[start] == 0 and "path" not in attrs and values[end:end + 1] == [";"] and values[item:end - 1] == ["mod"]:
106
+ gated.add(values[end - 1])
107
+ drop.update(range(start, end + 1))
108
+ index = end + 1
109
+
110
+ def path_attr(i):
111
+ """`mod` at values[i] carries a `#[path]` attribute (its file is not the default child path)."""
112
+ if values[i - 1:i] == ["pub"]:
113
+ i -= 1
114
+ elif values[i - 1:i] == [")"] and values[opener.get(i - 1, 0) - 1:opener.get(i - 1, 0)] == ["pub"]:
115
+ i = opener[i - 1] - 1
116
+ while values[i - 1:i] == ["]"] and i - 1 in opener and values[opener[i - 1] - 1:opener[i - 1]] == ["#"]:
117
+ if values[opener[i - 1] + 1:opener[i - 1] + 2] == ["path"]:
118
+ return True
119
+ i = opener[i - 1] - 1
120
+ return False
121
+
122
+ plain = {values[i + 1] for i in range(len(values) - 2) if values[i] == "mod" and depth[i] == 0
123
+ and values[i + 2] == ";" and not path_attr(i) and i not in drop}
124
+ index = 0 # whole file: any `#![cfg(test)]` in the leading run of inner attributes
125
+ while values[index:index + 3] == ["#", "!", "["] and index + 2 in pairs:
126
+ if test_only(index + 3, pairs[index + 2], inner=True):
127
+ return [], gated, plain, True
128
+ index = pairs[index + 2] + 1
129
+ return [token for i, token in enumerate(stream) if i not in drop], gated, plain, False
130
+
131
+
132
+ def child_module(path, name):
133
+ """Files that may hold `mod name;` declared in `path` (no `#[path]`)."""
134
+ base = path.rsplit("/", 1)
135
+ directory = base[0] + "/" if len(base) == 2 else ""
136
+ if base[-1] not in ("mod.rs", "lib.rs", "main.rs"):
137
+ directory = path[:-3] + "/"
138
+ return (f"{directory}{name}.rs", f"{directory}{name}/mod.rs")
139
+
140
+
141
+ def normalize(path):
142
+ return "/".join("*" if re.fullmatch(r'\*\w+|\{\*\w+\}', part)
143
+ else re.sub(r'^\{(\w+)\}$', r':\1', part) for part in path.split("/"))
144
+
145
+
146
+ class Scanner:
147
+ def __init__(self, path, text, nodes, references, tokenized=None, owners=None):
148
+ self.path, self.tokens, self.nodes, self.references = path, tokens(text) if tokenized is None else tokenized, nodes, references
149
+ self.owners = owners if owners is not None else [(nid, node) for nid, node in nodes.items()
150
+ if nid.startswith(path + ":") and node["kind"] != "module"]
151
+ self.values = [token[0] for token in self.tokens]
152
+ self.imported = set()
153
+ for index in range(len(self.values) - 2):
154
+ if self.values[index:index + 3] == ["use", "axum", "::"]:
155
+ end = next((i for i in range(index, len(self.values)) if self.values[i] == ";"), index)
156
+ self.imported.update(self.values[index + 3:end])
157
+ self.pairs, self.reverse, stack, top = {}, {}, [], set()
158
+ for index, value in enumerate(self.values):
159
+ if not stack:
160
+ top.add(index)
161
+ if value in ("(", "{", "["):
162
+ stack.append(index)
163
+ elif value in (")", "}", "]") and stack:
164
+ start = stack.pop()
165
+ self.pairs[start] = index
166
+ self.reverse[index] = start
167
+ # A module-level `const|static NAME: &str = "...";` stands for its literal. Rust forbids let/parameter
168
+ # bindings that shadow it, so only another item or import named NAME in this file can; then it is unknown.
169
+ defs, imports, using = {}, set(), False
170
+ for index, value in enumerate(self.values):
171
+ using = value == "use" or using and value != ";"
172
+ if using:
173
+ imports.add(index)
174
+ for index in sorted(top):
175
+ if self.values[index] not in ("const", "static") or index + 1 >= len(self.values):
176
+ continue
177
+ rest = self.values[index + 2:index + 11]
178
+ for shape in ([":", "&", "str", "="], [":", "&", "'", "static", "str", "="]):
179
+ text = literal(rest[len(shape)]) if rest[:len(shape)] == shape and len(rest) > len(shape) + 1 else None
180
+ if text is not None and rest[len(shape) + 1] == ";":
181
+ defs.setdefault(self.values[index + 1], []).append((index + 1, text))
182
+ for index, value in enumerate(self.values):
183
+ if value in defs and index != defs[value][0][0] and (index in imports or self.values[index - 1] in ITEM_WORDS):
184
+ defs[value].append(None)
185
+ self.consts = {name: found[0][1] for name, found in defs.items() if len(found) == 1}
186
+ self.inline = []
187
+ for index in range(1, len(self.values) - 1):
188
+ if self.values[index - 1:index + 2] != [".", "nest", "("]:
189
+ continue
190
+ args = self.arguments(index + 1)
191
+ if len(args) == 2 and "Router" in self.values[args[1][0]:args[1][0] + 3]:
192
+ self.inline.append(args[1])
193
+ self.bindings, self.records = [], []
194
+ for index, value in enumerate(self.values):
195
+ if value != "let" or index + 2 >= len(self.values):
196
+ continue
197
+ name = index + 2 if self.values[index + 1] == "mut" else index + 1
198
+ end = self.statement_end(name)
199
+ equal = next((i for i in range(name, end) if self.values[i] == "="), None)
200
+ if equal is not None:
201
+ self.bindings.append((self.values[name], equal + 1, end, self.owner(index)))
202
+
203
+ def string(self, span):
204
+ """A path argument: one string literal token or a same-file `&str` const."""
205
+ if span[1] != span[0] + 1:
206
+ return None
207
+ value = self.values[span[0]]
208
+ return self.consts.get(value) if re.fullmatch(r"[A-Za-z_]\w*", value) else literal(value)
209
+
210
+ def statement_end(self, start):
211
+ while start < len(self.values) and self.values[start] != ";":
212
+ start = self.pairs.get(start, start) + 1
213
+ return start
214
+
215
+ def owner(self, index):
216
+ line = self.tokens[index][1]
217
+ candidates = [(node["end_line"] - node["line"], nid) for nid, node in self.owners
218
+ if node["line"] <= line <= node["end_line"]]
219
+ return min(candidates)[1] if candidates else self.path + ":<module>"
220
+
221
+ def app(self, index):
222
+ inline = [span for span in self.inline if span[0] <= index < span[1]]
223
+ if inline:
224
+ return f"{self.path}:inline:{max(inline)[0]}"
225
+ binding = next((b for b in self.bindings if b[1] <= index < b[2]), None)
226
+ return f"{self.path}:let:{binding[1]}" if binding else self.owner(index)
227
+
228
+ def resolve(self, start, end, binding=False):
229
+ values = self.values[start:end]
230
+ if binding and (start, end) in self.inline:
231
+ return f"{self.path}:inline:{start}"
232
+ if not values:
233
+ return None
234
+ if binding and len(values) == 1:
235
+ hits = [b for b in self.bindings if b[0] == values[0] and b[2] < start and b[3] == self.owner(start)]
236
+ if len(hits) == 1:
237
+ return f"{self.path}:let:{hits[0][1]}"
238
+ # Router factories retain their identity even when state is passed as an argument.
239
+ if binding:
240
+ opening = next((i for i in range(start, end) if self.values[i] == "("), None)
241
+ if opening is not None and self.pairs.get(opening) == end - 1:
242
+ values = self.values[start:opening]
243
+ if not values or not re.fullmatch(r'[A-Za-z_]\w*(::[A-Za-z_]\w*)*', "".join(values)):
244
+ return None
245
+ last = start + len(values) - 1
246
+ position = self.tokens[last][1:]
247
+ if position in self.references:
248
+ return self.references[position]
249
+ if len(values) == 1:
250
+ if any(b[0] == values[0] and b[2] < start and b[3] == self.owner(start) for b in self.bindings):
251
+ return None
252
+ hits = [nid for nid, node in self.nodes.items() if nid == self.path + ":" + values[0]
253
+ and node["kind"] == "function"]
254
+ return hits[0] if len(hits) == 1 else None
255
+ return None
256
+
257
+ def marker(self, index, expr):
258
+ self.records.append({"t": "edge", "from": self.owner(index), "to": "?", "kind": "?route",
259
+ "at": f"{self.path}:{self.tokens[index][1]}", "expr": expr})
260
+
261
+ def arguments(self, opening):
262
+ end, parts, start = self.pairs.get(opening, opening), [], opening + 1
263
+ index = start
264
+ while index < end:
265
+ if self.values[index] == ",":
266
+ parts.append((start, index))
267
+ start = index + 1
268
+ index = self.pairs.get(index, index) + 1
269
+ if start < end:
270
+ parts.append((start, end))
271
+ return parts
272
+
273
+ def receiver(self, index, seen=()):
274
+ """Walk a fluent receiver back to an axum constructor or an immutable binding."""
275
+ end = index - 1
276
+ start = end - 1
277
+ if start < 0:
278
+ return False
279
+ if self.values[start] == ")":
280
+ opening = self.reverse.get(start)
281
+ if opening is None or opening == 0:
282
+ return False
283
+ name = opening - 1
284
+ if name and self.values[name - 1] == ".":
285
+ return self.values[name] in ROUTER_METHODS and self.receiver(name, seen)
286
+ first = name
287
+ while first >= 2 and self.values[first - 1] == "::":
288
+ first -= 2
289
+ path = self.values[first:opening]
290
+ if path == ["axum", "::", "Router", "::", "new"]:
291
+ return True
292
+ if path == ["Router", "::", "new"] and "Router" in self.imported:
293
+ return self.path + ":Router" not in self.nodes
294
+ target = self.resolve(first, start + 1, binding=True)
295
+ return target is not None and any(r.get("app") == target for r in self.records)
296
+ hits = [b for b in self.bindings if b[0] == self.values[start] and b[2] < index and b[3] == self.owner(index)]
297
+ if len(hits) != 1 or hits[0][1] in seen:
298
+ return False
299
+ binding = hits[0]
300
+ # Reassignment makes the syntactic binding unsuitable for exact extraction.
301
+ if any(self.values[i:i + 2] == [binding[0], "="] for i in range(binding[2], index)):
302
+ return False
303
+ first = binding[1]
304
+ return "Router" in self.imported and self.values[first:first + 5] == ["Router", "::", "new", "(", ")"]
305
+
306
+ def method_range(self, start, end):
307
+ if end == start + 1:
308
+ hits = [b for b in self.bindings if b[0] == self.values[start] and b[2] < start and b[3] == self.owner(start)]
309
+ if len(hits) == 1:
310
+ return hits[0][1:3]
311
+ return start, end
312
+
313
+ def route(self, index, args):
314
+ path = self.string(args[0]) if args else None
315
+ if path is None or len(args) != 2:
316
+ self.marker(index, "route(non-literal path)")
317
+ return
318
+ start, end = self.method_range(*args[1])
319
+ handlers = self.handlers(start, end)
320
+ if handlers is None:
321
+ self.marker(index, f"route({path}: unresolved MethodRouter)")
322
+ return
323
+ for method, target in handlers:
324
+ if not target:
325
+ self.marker(index, f"route({path}, {method}: unresolved handler)")
326
+ continue
327
+ self.records.append({"t": "entry", "kind": "route", "value": normalize(path),
328
+ "method": "ALL" if method == "any" else method.upper(), "node": target,
329
+ "app": self.app(index), "at": f"{self.path}:{self.tokens[index][1]}"})
330
+
331
+ def closure_owner(self, start, end):
332
+ """`get(|| async { .. })`: an inline closure runs inside the router function, which owns its calls."""
333
+ i = start
334
+ while i < end and self.values[i] in ("move", "async"):
335
+ i += 1
336
+ return self.owner(start) if i < end and self.values[i] in ("|", "||") else None
337
+
338
+ def unlayer(self, start, end):
339
+ """`create.layer(mw)` / `.with_state(s)`: the wrapped handler still runs; strip the trailing calls."""
340
+ while end - start > 3 and self.values[end - 1] == ")":
341
+ opening = self.reverse.get(end - 1)
342
+ if opening is None or opening - 2 < start or self.values[opening - 2] != "." \
343
+ or self.values[opening - 1] not in {"layer", "with_state"}:
344
+ break
345
+ end = opening - 2
346
+ return start, end
347
+
348
+ def handlers(self, start, end):
349
+ handlers = []
350
+ is_first = True
351
+ while start < end:
352
+ name = start
353
+ while name + 2 < end and self.values[name + 1] == "::":
354
+ name += 2
355
+ method = self.values[name]
356
+ opening = name + 1
357
+ if opening >= end or self.values[opening] != "(" or opening not in self.pairs:
358
+ return None
359
+ args = self.arguments(opening)
360
+ if method in METHODS:
361
+ path = self.values[start:name + 1]
362
+ if is_first and not (path == ["axum", "::", "routing", "::", method]
363
+ or path == ["routing", "::", method] and "routing" in self.imported
364
+ or path == [method] and method in self.imported):
365
+ return None
366
+ # A project function named `get` is not axum's routing constructor.
367
+ if start == name and self.path + ":" + method in self.nodes:
368
+ return None
369
+ handlers.append((method, (self.closure_owner(*args[0]) or self.resolve(*self.unlayer(*args[0])))
370
+ if len(args) == 1 else None))
371
+ elif not (method == "new" and (self.values[start:name + 1] == ["axum", "::", "routing", "::", "MethodRouter", "::", "new"]
372
+ or self.values[start:name + 1] == ["MethodRouter", "::", "new"] and "MethodRouter" in self.imported)) and method not in {"layer", "route_layer", "with_state"}:
373
+ return None
374
+ is_first = False
375
+ start = self.pairs[opening] + 1
376
+ if start < end:
377
+ if self.values[start] != ".":
378
+ return None
379
+ start += 1
380
+ return handlers or None
381
+
382
+ def scan(self):
383
+ for index in range(len(self.values) - 4):
384
+ if self.values[index:index + 5] == ["Router", "::", "new", "(", ")"] and "Router" in self.imported:
385
+ self.records.append({"t": "router", "app": self.app(index)})
386
+ for index in range(1, len(self.values) - 1):
387
+ method = self.values[index]
388
+ if self.values[index - 1] != "." or self.values[index + 1] != "(" or method not in {"route", "nest", "merge"}:
389
+ continue
390
+ args = self.arguments(index + 1)
391
+ if not self.receiver(index):
392
+ self.marker(index, f"{method}(unresolved router receiver)")
393
+ continue
394
+ if method == "route":
395
+ self.route(index, args)
396
+ elif method in ("nest", "merge"):
397
+ prefix = "" if method == "merge" else (self.string(args[0]) if args else None)
398
+ child = self.resolve(*args[-1], binding=True) if args else None
399
+ if prefix is None or child is None:
400
+ if child:
401
+ self.records.append({"t": "blocked_mount", "child": child})
402
+ self.marker(index, f"{method}(unresolved router or prefix)")
403
+ else:
404
+ self.records.append({"t": "mount", "child": child, "parent": self.app(index),
405
+ "prefix": normalize(prefix), "at": f"{self.path}:{self.tokens[index][1]}"})
406
+ for name, start, end, owner in self.bindings:
407
+ child = self.resolve(start, end, binding=True)
408
+ if child:
409
+ self.records.append({"t": "router_alias", "child": child, "parent": f"{self.path}:let:{start}",
410
+ "prefix": "", "at": f"{self.path}:{self.tokens[start][1]}"})
411
+ node = self.nodes.get(owner)
412
+ if not node:
413
+ continue
414
+ tail = [i for i in range(end + 1, len(self.values)) if self.tokens[i][1] <= node["end_line"]]
415
+ if tail and self.values[tail[0]] == name and all(self.values[i] == "}" for i in tail[1:]):
416
+ self.records.append({"t": "mount", "child": f"{self.path}:let:{start}", "parent": owner,
417
+ "prefix": "", "at": f"{self.path}:{self.tokens[start][1]}"})
418
+ return self.records
419
+
420
+
421
+ def frameworks(sources, tokenized=None):
422
+ """Known framework paths in Rust code, excluding comments and strings."""
423
+ imports = set()
424
+ for path, text in sources.items():
425
+ if not FRAMEWORK_WORD.search(text):
426
+ continue
427
+ stream = tokens(text)
428
+ if tokenized is not None:
429
+ tokenized[path] = stream
430
+ values = [token[0] for token in stream]
431
+ for index, value in enumerate(values[:-1]):
432
+ if value in FRAMEWORKS and (values[index + 1] == "::" or index > 0 and values[index - 1] in {"use", "crate"}):
433
+ imports.add(value)
434
+ return imports
435
+
436
+
437
+ def extract(sources, nodes, references, framework_sources=None):
438
+ """Sources are one crate's indexed files; references map exact (line, column) sites to node ids."""
439
+ tokenized = {}
440
+ imports = frameworks(sources if framework_sources is None else framework_sources, tokenized)
441
+ records = [{"t": "fw", "name": FRAMEWORKS[name]} for name in sorted(imports - {"axum"})]
442
+ if "axum" in imports:
443
+ owners = {}
444
+ for nid, node in nodes.items():
445
+ if node["kind"] != "module":
446
+ owners.setdefault(nid.split(":", 1)[0], []).append((nid, node))
447
+ stripped, modules, pending = {}, {}, []
448
+ for path, text in sources.items():
449
+ stripped[path], gated, plain, whole = strip_cfg_test(tokenized[path] if path in tokenized else tokens(text))
450
+ modules[path] = [child for name in sorted(gated | plain) for child in child_module(path, name)]
451
+ pending += [path] if whole else [child for name in sorted(gated) for child in child_module(path, name)]
452
+ tests = set() # files compiled only under cfg(test), including their submodules
453
+ while pending:
454
+ path = pending.pop()
455
+ if path in sources and path not in tests:
456
+ tests.add(path)
457
+ pending += modules[path]
458
+ for path, text in sorted(sources.items()):
459
+ if path not in tests:
460
+ records += Scanner(path, text, nodes, references.get(path, {}), stripped[path], owners.get(path, ())).scan()
461
+ known = {r["app"] for r in records if r["t"] in {"entry", "router"}}
462
+ while True:
463
+ parents = {r["parent"] for r in records if r["t"] in {"mount", "router_alias"} and r["child"] in known}
464
+ if parents <= known:
465
+ break
466
+ known |= parents
467
+ records = [dict(r, t="mount") if r["t"] == "router_alias" else r for r in records
468
+ if r["t"] != "router_alias" or r["child"] in known]
469
+ for record in records[:]:
470
+ if record["t"] == "mount" and record["child"] not in known:
471
+ records.append({"t": "edge", "from": record["parent"] if record["parent"] in nodes else record["at"].rsplit(":", 1)[0] + ":<module>",
472
+ "to": "?", "kind": "?route", "at": record["at"], "expr": "unresolved router target"})
473
+ records = [r for r in records if r["t"] != "router"]
474
+ blocked = {r["child"] for r in records if r["t"] == "blocked_mount"}
475
+ mounted = {r["child"] for r in records if r["t"] == "mount"}
476
+ blocked -= mounted
477
+ while True:
478
+ children = {r["child"] for r in records if r["t"] == "mount" and r["parent"] in blocked}
479
+ if children <= blocked:
480
+ break
481
+ blocked |= children
482
+ records = [r for r in records if r["t"] != "blocked_mount" and r.get("app") not in blocked]
483
+ if "axum" in imports and not any(r["t"] == "entry" for r in records):
484
+ records.append({"t": "fw", "name": "axum"})
485
+ return records
fmap/cache.py ADDED
@@ -0,0 +1,25 @@
1
+ """Repository cache paths and file signatures."""
2
+ import hashlib
3
+ import os
4
+ import subprocess
5
+
6
+ HERE = os.path.dirname(os.path.abspath(__file__))
7
+
8
+
9
+ def cache_dir(root):
10
+ r = subprocess.run(["git", "rev-parse", "--git-common-dir"], cwd=root, capture_output=True, text=True)
11
+ d = os.path.join(root, r.stdout.strip(), "fmap", "cache")
12
+ os.makedirs(d, exist_ok=True)
13
+ return d
14
+
15
+
16
+ def file_sig(root, rel):
17
+ p = rel if os.path.isabs(rel) else os.path.join(root, rel)
18
+ try:
19
+ if "/node_modules/" in p or p.startswith(HERE):
20
+ st = os.stat(p) # shortcut: dependency files keyed by size+mtime, upgrade if installs keep mtimes
21
+ return f"{st.st_size}:{st.st_mtime_ns}"
22
+ with open(p, "rb") as fh:
23
+ return hashlib.sha256(fh.read()).hexdigest()
24
+ except OSError:
25
+ return "missing"