`create.path` as the chain its root was destructured from in `fromFile`, or null when the root is no local alias.
(expr: string, fromFile: string, context: ResolutionContext)
| 566 | |
| 567 | /** `create.path` as the chain its root was destructured from in `fromFile`, or null when the root is no local alias. */ |
| 568 | function localAlias(expr: string, fromFile: string, context: ResolutionContext): string | null { |
| 569 | const content = context.readFile(fromFile); |
| 570 | if (!content) return null; |
| 571 | const [root, ...rest] = expr.split('.'); |
| 572 | if (!root) return null; |
| 573 | const destructure = /\b(?:const|let)\s*\{([^}]*)\}\s*=\s*([A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*)\s*;?/g; |
| 574 | let m: RegExpExecArray | null; |
| 575 | while ((m = destructure.exec(content)) !== null) { |
| 576 | for (const binding of m[1]!.split(',')) { |
| 577 | const [prop, local] = binding.split(':').map((part) => part.trim()); |
| 578 | if ((local ?? prop) === root && prop) return [`${m[2]!.replace(/\s+/g, '')}.${prop}`, ...rest].join('.'); |
| 579 | } |
| 580 | } |
| 581 | const simple = new RegExp(String.raw`\b(?:const|let)\s+${escapeRegExp(root)}\s*=\s*([A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)+)\s*;`).exec(content); |
| 582 | return simple ? [simple[1]!.replace(/\s+/g, ''), ...rest].join('.') : null; |
| 583 | } |
| 584 | |
| 585 | /** A constant path's value: `internalRoutes.account.path` → `account`. */ |
| 586 | function constantValue(expr: string, fromFile: string, context: ResolutionContext): string | null { |
no test coverage detected