MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

src/resolution/frameworks/react.ts:162–206  ·  view source on GitHub ↗
(filePath, content)

Source from the content-addressed store, hash-verified

160 },
161
162 extract(filePath, content) {
163 const nodes: Node[] = [];
164 const references: UnresolvedRef[] = [];
165 const now = Date.now();
166
167 // Components and custom hooks are NOT extracted here. The tree-sitter
168 // extractor already emits them natively across .ts/.tsx/.js/.jsx — function
169 // and arrow components as `function` nodes, HOC-wrapped components
170 // (`forwardRef`/`memo`/`styled`) as `component` nodes (#841), and `useX`
171 // hooks as `function` nodes. Re-deriving them here with regex only ran on
172 // .ts/.js anyway (this resolver's `languages` didn't include the 'tsx'/'jsx'
173 // grammars), and it DUPLICATED those tree-sitter nodes (e.g. a `useAuth`
174 // ended up as two `function` nodes). This `extract` now contributes only
175 // what tree-sitter can't: route nodes (React Router + Next.js conventions),
176 // which is why 'tsx'/'jsx' are now in `languages` — `<Route>`/`element={<X/>}`
177 // routes live in JSX files and were previously skipped entirely.
178
179 // Read only each opening tag's own attributes, including expression values.
180 const declarations = scanRouteDeclarations(content, allowsJsx(filePath));
181 for (const route of declarations) {
182 const { path: routePath, parts, at } = route;
183 const line = content.slice(0, at).split('\n').length;
184 const routeNode: Node = {
185 id: `route:${filePath}:${line}:${routePath}`,
186 kind: 'route',
187 name: routePath,
188 qualifiedName: `${filePath}::route:${routePath}`,
189 filePath,
190 startLine: line,
191 endLine: line,
192 startColumn: 0,
193 endColumn: 0,
194 language: filePath.endsWith('.tsx') ? 'tsx' : 'jsx',
195 updatedAt: now,
196 // A path built from a constant (`paths.app.root.path`) is named in postExtract.
197 ...(parts.some((p) => p.expr) ? { signature: ROUTE_PARTS_PREFIX + JSON.stringify(parts) } : {}),
198 };
199 nodes.push(routeNode);
200 references.push(...routeReferences(route, routeNode.id, filePath, line));
201 }
202
203 // Next.js pages and route handlers are `frameworks/nextjs.ts`'s.
204
205 return { nodes, references };
206 },
207
208 /**
209 * Name the routes whose path is built from a constant — bulletproof-react's

Callers

nothing calls this directly

Calls 4

scanRouteDeclarationsFunction · 0.85
allowsJsxFunction · 0.85
routeReferencesFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected