(
code: string,
typeNames: Iterable<string>
)
| 685 | * identifiers for the types that carry `visibility: "internal"`. |
| 686 | */ |
| 687 | export function renameInternalPythonSymbols( |
| 688 | code: string, |
| 689 | typeNames: Iterable<string> |
| 690 | ): string { |
| 691 | const escapeRegex = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 692 | let result = code; |
| 693 | const sortedTypes = [...typeNames].sort((a, b) => b.length - a.length); |
| 694 | // Phase 1: rename each identifier globally at word boundaries. |
| 695 | for (const t of sortedTypes) { |
| 696 | result = result.replace( |
| 697 | new RegExp(`(?<![A-Za-z0-9_])${escapeRegex(t)}(?![A-Za-z0-9_])`, "g"), |
| 698 | `_${t}` |
| 699 | ); |
| 700 | } |
| 701 | // Phase 2: restore JSON-key strings that match the rename target. Those |
| 702 | // strings carry the wire-protocol definition name and must remain untouched |
| 703 | // regardless of the Python-side rename. Patterns: `obj.get("Foo")` and |
| 704 | // `result["Foo"] = ...` in quicktype's serialization helpers. |
| 705 | for (const t of sortedTypes) { |
| 706 | const escaped = escapeRegex(t); |
| 707 | result = result.replace( |
| 708 | new RegExp(`(obj\\.get\\(")_${escaped}("\\))`, "g"), |
| 709 | `$1${t}$2` |
| 710 | ); |
| 711 | result = result.replace( |
| 712 | new RegExp(`(result\\[")_${escaped}("\\])`, "g"), |
| 713 | `$1${t}$2` |
| 714 | ); |
| 715 | } |
| 716 | return result; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Collects the set of (publicTypeName, internalFieldName[]) pairs from a |
no test coverage detected
searching dependent graphs…