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

Function bindPattern

src/resolution/frameworks/react.ts:1911–1937  ·  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

1909 * null, so it never counts as a name of the route's file.
1910 */
1911function bindPattern(scope: Map<string, Loaded | null>, pattern: string, value: Loaded | null): void {
1912 const p = pattern.trim();
1913 if (!p) return;
1914 if (/^[A-Za-z_$][\w$]*$/.test(p)) {
1915 scope.set(p, value);
1916 return;
1917 }
1918 const close = p.startsWith('{') || p.startsWith('[') ? matchBracket(p, 0) : -1;
1919 if (close !== p.length - 1) {
1920 for (const name of p.matchAll(/[A-Za-z_$][\w$]*/g)) scope.set(name[0], null);
1921 return;
1922 }
1923 const items = topLevelItems(p.slice(1, -1));
1924 items.forEach((item, index) => {
1925 if (!item) return;
1926 const rest = /^\.\.\.\s*([\s\S]*)$/.exec(item);
1927 if (rest) return bindPattern(scope, rest[1]!, null);
1928 if (p[0] === '[') {
1929 return bindPattern(scope, item.replace(/\s*=(?![=>])[\s\S]*$/, ''), value?.kind === 'array' ? value.items[index] ?? null : null);
1930 }
1931 // `Page`, `Page = Fallback`, `default: Home`, `'default': Home`.
1932 const prop = /^(?:([A-Za-z_$][\w$]*)|(["'])([^"']*)\2)\s*(?::\s*([\s\S]+?))?\s*(?:=(?![=>])[\s\S]*)?$/.exec(item);
1933 if (!prop) return bindPattern(scope, item.replace(/^[^:]*:/, ''), null);
1934 const key = prop[1] ?? prop[3]!;
1935 bindPattern(scope, prop[4] ?? key, memberOf(value, key));
1936 });
1937}
1938
1939/** An object literal's own properties, each name to its value's text — shorthand and methods included — and its spreads. */
1940function 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