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

Function unit

src/resolution/frameworks/react.ts:1146–1250  ·  view source on GitHub ↗
(at: number, routeList = false)

Source from the content-addressed store, hash-verified

1144 else if (source[next] === '(') sites.push({ name: { name: id[0], call: true, at: argAt }, at: open });
1145 };
1146 function unit(at: number, routeList = false): number {
1147 const ch = source[at];
1148 if (ch === '"' || ch === "'" || ch === '`') {
1149 let i = at + 1;
1150 while (i < source.length) {
1151 if (source[i] === '\\') { i += 2; continue; }
1152 if (source[i] === ch) return i + 1;
1153 if (ch === '`' && source.startsWith('${', i)) { i = unit(i + 1); continue; }
1154 i++;
1155 }
1156 return i;
1157 }
1158 // A regex can contain braces or JSX-looking text without ending an expression.
1159 if (ch === '/') {
1160 const before = tokenBefore(at);
1161 if (!before || /[=(:,[!&|?{};]$/.test(before) || /\b(?:return|throw|case|yield)\s*$/.test(before)) {
1162 let inClass = false;
1163 for (let i = at + 1; i < source.length && source[i] !== '\n'; i++) {
1164 if (source[i] === '\\') { i++; continue; }
1165 if (source[i] === '[') inClass = true;
1166 else if (source[i] === ']') inClass = false;
1167 else if (source[i] === '/' && !inClass) {
1168 i++;
1169 while (/[a-z]/i.test(source[i] ?? '') && i < source.length) i++;
1170 return i;
1171 }
1172 }
1173 }
1174 }
1175 if (allowJsx && ch === '<' && /^<(?:[A-Za-z][\w.:-]*|>)/.test(source.slice(at))) {
1176 const before = tokenBefore(at);
1177 // `count<limit` and `factory<Type>()` are not JSX opening tags.
1178 if (!/[\w$)\]'"]$/.test(before) || /\breturn$/.test(before)) return jsx(at);
1179 }
1180 const close = ch === '{' ? '}' : ch === '[' ? ']' : ch === '(' ? ')' : undefined;
1181 if (!close) return at + 1;
1182 const call = ch === '(' ? routerCall(at) : null;
1183 const router = ch === '(' && routesFromElements(at);
1184 let i = at + 1;
1185 const fields = new Map<string, { value: string; at: number }>();
1186 while ((i = trivia(i)) < source.length && source[i] !== close) {
1187 // A property must start at an object entry, never inside its value.
1188 const key = ch === '{' ? /^(?:([A-Za-z_$][\w$]*)|["']([^"']+)["'])\s*:/.exec(source.slice(i)) : null;
1189 if (key) {
1190 const name = key[1] ?? key[2]!;
1191 const start = i;
1192 const valueAt = trivia(i + key[0].length);
1193 // A route's children are more routes, written in place or named.
1194 const children = objects && name === 'children';
1195 i = valueAt;
1196 let valueEnd = i;
1197 while ((i = trivia(i)) < source.length && source[i] !== ',' && source[i] !== close) {
1198 i = unit(i, children);
1199 valueEnd = i;
1200 }
1201 const value = source.slice(valueAt, valueEnd).trim();
1202 fields.set(name, { value, at: start });
1203 if (children && /^[A-Za-z_$][\w$]*$/.test(value)) names.push({ name: value, call: false, at: valueAt });

Callers 2

jsxFunction · 0.70
scanRoutesFunction · 0.70

Calls 15

matchBracketFunction · 0.90
jsxFunction · 0.85
routerCallFunction · 0.85
routesFromElementsFunction · 0.85
triviaFunction · 0.85
literalFunction · 0.85
elementTagFunction · 0.85
componentNameFunction · 0.85
lazyRouteReferenceFunction · 0.85
pushMethod · 0.80
hasMethod · 0.80
tokenBeforeFunction · 0.70

Tested by

no test coverage detected