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

Function bindPattern

src/resolution/frameworks/react.ts:1889–1915  ·  view source on GitHub ↗

* Bind the names `pattern` declares to the parts of `value` they take: `{ * Page, default: Home }` of a module to its exports, `[{ Page }]` to the first * item of a `Promise.all`. A name whose value this does not read is bound to * null, so it never counts as a name of the route's file.

(scope: Map<string, Loaded | null>, pattern: string, value: Loaded | null)

Source from the content-addressed store, hash-verified

1887 * null, so it never counts as a name of the route's file.
1888 */
1889function bindPattern(scope: Map<string, Loaded | null>, pattern: string, value: Loaded | null): void {
1890 const p = pattern.trim();
1891 if (!p) return;
1892 if (/^[A-Za-z_$][\w$]*$/.test(p)) {
1893 scope.set(p, value);
1894 return;
1895 }
1896 const close = p.startsWith('{') || p.startsWith('[') ? matchBracket(p, 0) : -1;
1897 if (close !== p.length - 1) {
1898 for (const name of p.matchAll(/[A-Za-z_$][\w$]*/g)) scope.set(name[0], null);
1899 return;
1900 }
1901 const items = topLevelItems(p.slice(1, -1));
1902 items.forEach((item, index) => {
1903 if (!item) return;
1904 const rest = /^\.\.\.\s*([\s\S]*)$/.exec(item);
1905 if (rest) return bindPattern(scope, rest[1]!, null);
1906 if (p[0] === '[') {
1907 return bindPattern(scope, item.replace(/\s*=(?![=>])[\s\S]*$/, ''), value?.kind === 'array' ? value.items[index] ?? null : null);
1908 }
1909 // `Page`, `Page = Fallback`, `default: Home`, `'default': Home`.
1910 const prop = /^(?:([A-Za-z_$][\w$]*)|(["'])([^"']*)\2)\s*(?::\s*([\s\S]+?))?\s*(?:=(?![=>])[\s\S]*)?$/.exec(item);
1911 if (!prop) return bindPattern(scope, item.replace(/^[^:]*:/, ''), null);
1912 const key = prop[1] ?? prop[3]!;
1913 bindPattern(scope, prop[4] ?? key, memberOf(value, key));
1914 });
1915}
1916
1917/** An object literal's own properties, each name to its value's text — shorthand and methods included — and its spreads. */
1918function objectEntries(text: string): { props: Map<string, string>; spreads: string[] } {

Callers 2

loaderValueFunction · 0.70
declareFunction · 0.70

Calls 5

matchBracketFunction · 0.90
topLevelItemsFunction · 0.70
memberOfFunction · 0.70
execMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected