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

Function exportedTable

src/resolution/frameworks/react.ts:2101–2134  ·  view source on GitHub ↗

The literals `file` exports as `exported`: `export default [ … ]`, `export default AppRoutes`, `export const routes = [ … ]`, re-exports followed.

(file: string, exported: string, call: boolean, context: ResolutionContext, depth: number)

Source from the content-addressed store, hash-verified

2099
2100/** The literals `file` exports as `exported`: `export default [ … ]`, `export default AppRoutes`, `export const routes = [ … ]`, re-exports followed. */
2101function exportedTable(file: string, exported: string, call: boolean, context: ResolutionContext, depth: number): TableLiteral[] {
2102 const source = context.readFile(file);
2103 if (!source) return [];
2104 const safe = stripCommentsForRegex(source, 'typescript');
2105 let local = exported;
2106 if (exported === 'default') {
2107 const m = /\bexport\s+default\s+/.exec(safe);
2108 if (m) {
2109 const after = m.index + m[0].length;
2110 // `export default [ … ]`, `export default buildRoutes()`, `export default function routes() { … }`.
2111 const inline = call ? returnedTables(safe, after) : heldTable(safe, after);
2112 if (inline.length > 0) return tablesAt(inline, file, context, depth);
2113 local = /^([A-Za-z_$][\w$]*)\s*(?:;|\n|$)/.exec(safe.slice(after))?.[1] ?? '';
2114 } else local = /\bexport\s*\{[^}]*?\b([A-Za-z_$][\w$]*)\s+as\s+default\b/.exec(safe)?.[1] ?? '';
2115 } else {
2116 // `export { appRoutes as routes }`
2117 const alias = new RegExp(`\\bexport\\s*\\{[^}]*?\\b([A-Za-z_$][\\w$]*)\\s+as\\s+${escapeRegExp(exported)}\\b`).exec(safe);
2118 if (alias) local = alias[1]!;
2119 }
2120 // What a module exports is declared at its top level.
2121 const declared = local ? declaredTable(safe, local, call, visibleAt(undefined, safe, file, context)) : [];
2122 if (declared.length > 0) return tablesAt(declared, file, context, depth);
2123 if (depth > 3) return [];
2124 // `export { routes } from './routes'`, `export { default as routes } from './routes'`, `export * from './routes'`.
2125 const language = scriptLanguage(file);
2126 for (const re of context.getReExports?.(file, language) ?? []) {
2127 if (re.kind === 'named' && re.exportedName !== exported) continue;
2128 const target = resolveImportPath(re.source, file, language, context);
2129 if (!target) continue;
2130 const found = exportedTable(target, re.kind === 'named' ? re.originalName : exported, call, context, depth + 1);
2131 if (found.length > 0) return found;
2132 }
2133 return [];
2134}
2135
2136/**
2137 * Where `name`'s table literal opens, for `const name = [ … ]` (or the tables

Callers 1

tableNamedFunction · 0.85

Calls 12

stripCommentsForRegexFunction · 0.90
escapeRegExpFunction · 0.90
resolveImportPathFunction · 0.90
returnedTablesFunction · 0.85
heldTableFunction · 0.85
tablesAtFunction · 0.85
declaredTableFunction · 0.85
visibleAtFunction · 0.85
scriptLanguageFunction · 0.85
readFileMethod · 0.80
getReExportsMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected