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

Function statedDefaultExport

src/resolution/import-resolver.ts:242–281  ·  view source on GitHub ↗

* What `filePath`'s own `export default` statement exports. The statement * says it outright, where the export index can only guess: * - `export default function Vans() {…}` / `export default class Store` — * the declaration the statement writes, found by position. A React Router * data-rout

(filePath: string, idx: FileExportIndex, context: ResolutionContext)

Source from the content-addressed store, hash-verified

240 * doesn't vouches for nothing, and every head counts.
241 */
242function statedDefaultExport(filePath: string, idx: FileExportIndex, context: ResolutionContext): StatedDefault {
243 if (idx.statedDefault !== undefined) return idx.statedDefault;
244 let stated: StatedDefault | undefined;
245 const source = JS_FAMILY_FILE.test(filePath) ? context.readFile(filePath) : null;
246 if (source && /\bexport\s+default\b/.test(source)) {
247 const code = stripCommentsForRegex(source, 'typescript');
248 const lineAt = makeLineAt(code, 1);
249 const inTemplateText = templateLiteralTextAt(source);
250 EXPORT_DEFAULT_HEAD.lastIndex = 0;
251 for (let m = EXPORT_DEFAULT_HEAD.exec(code); m !== null; m = EXPORT_DEFAULT_HEAD.exec(code)) {
252 if (inTemplateText?.(m.index)) continue;
253 const value = readDefaultExport(code, m.index + m[0].length);
254 if (value === 'anonymous') {
255 stated ??= 'anonymous';
256 continue;
257 }
258 stated ??= 'unstated';
259 if (value === null) continue;
260 const named = nodesInFileNamed(filePath, value.name, context);
261 // A declaration starts on the statement's line, or on its name's when
262 // the statement breaks before it (`export default\nfunction X`).
263 const from = lineAt(m.index);
264 const to = lineAt(value.nameAt);
265 // An object literal's owner is the statement itself (#3393).
266 const node = value.object
267 ? named.find((n) => n.kind === 'constant' && n.startLine === from)
268 : value.declared
269 ? named.find((n) => DEFAULT_DECLARATION_KINDS.has(n.kind) && n.startLine >= from && n.startLine <= to)
270 : moduleBindingsFirst(filePath, named
271 .filter((n) => DEFAULT_BINDING_KINDS.has(n.kind))
272 .sort((a, b) => a.startLine - b.startLine || a.startColumn - b.startColumn))[0];
273 if (node) {
274 stated = node;
275 break;
276 }
277 }
278 }
279 idx.statedDefault = stated ?? 'unstated';
280 return idx.statedDefault;
281}
282const fileExportIndexes = new WeakMap<ResolutionContext, Map<string, FileExportIndex>>();
283
284/**

Callers 3

esmDefaultExportFunction · 0.85
isStatedDefaultExportFunction · 0.85
findExportedSymbolWalkFunction · 0.85

Calls 10

stripCommentsForRegexFunction · 0.90
makeLineAtFunction · 0.90
templateLiteralTextAtFunction · 0.90
readDefaultExportFunction · 0.85
nodesInFileNamedFunction · 0.85
moduleBindingsFirstFunction · 0.85
hasMethod · 0.80
readFileMethod · 0.65
execMethod · 0.65
lineAtFunction · 0.50

Tested by

no test coverage detected