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

Function conditionalBranches

src/resolution/frameworks/react.ts:800–817  ·  view source on GitHub ↗

The two branches of a conditional expression, `test ? yes : no`; null for any other expression.

(text: string)

Source from the content-addressed store, hash-verified

798
799/** The two branches of a conditional expression, `test ? yes : no`; null for any other expression. */
800function conditionalBranches(text: string): [string, string] | null {
801 let question = -1;
802 let open = 0;
803 for (let i = 0; i < text.length; i++) {
804 const ch = text[i]!;
805 if (ch === '"' || ch === "'" || ch === '`' || ch === '{' || ch === '(' || ch === '[') {
806 i = ch === '{' || ch === '(' || ch === '[' ? matchBracket(text, i) : skipString(text, i);
807 if (i < 0) return null;
808 } else if (ch === '?') {
809 // `??` and `?.` are no conditional.
810 if (text[i + 1] === '?' || (text[i + 1] === '.' && !/\d/.test(text[i + 2] ?? ''))) i++;
811 else if (open++ === 0) question = i;
812 } else if (ch === ':' && open > 0 && --open === 0) {
813 return [text.slice(question + 1, i), text.slice(i + 1)];
814 }
815 }
816 return null;
817}
818
819/**
820 * The page a lazy wrapper's loader resolves to: a module's default export, an

Callers 1

conditionalLoaderFunction · 0.85

Calls 2

matchBracketFunction · 0.90
skipStringFunction · 0.90

Tested by

no test coverage detected