MCPcopy
hub / github.com/sveltejs/svelte / generate

Method generate

packages/svelte/src/compiler/phases/scope.js:710–742  ·  view source on GitHub ↗

* @param {string} preferred_name * @returns {string}

(preferred_name)

Source from the content-addressed store, hash-verified

708 * @returns {string}
709 */
710 generate(preferred_name) {
711 if (this.#porous) {
712 return /** @type {Scope} */ (this.parent).generate(preferred_name);
713 }
714
715 preferred_name = preferred_name.replace(/[^a-zA-Z0-9_$]/g, '_').replace(/^[0-9]/, '_');
716
717 // Use cached counter to skip names already known to be taken (avoids O(n²) scanning)
718 let n = this.root.next_counter(preferred_name);
719 let name;
720
721 if (n === 0) {
722 name = preferred_name;
723 n = 1;
724 } else {
725 name = `${preferred_name}_${n}`;
726 n++;
727 }
728
729 while (
730 this.references.has(name) ||
731 this.declarations.has(name) ||
732 this.root.conflicts.has(name) ||
733 is_reserved(name)
734 ) {
735 name = `${preferred_name}_${n++}`;
736 }
737
738 this.root.set_counter(preferred_name, n);
739 this.references.set(name, []);
740 this.root.conflicts.add(name);
741 return name;
742 }
743
744 /**
745 * @param {string} name

Callers 15

SvelteComponentFunction · 0.80
SlotElementFunction · 0.80
handle_identifierFunction · 0.80
IfBlockFunction · 0.80
FragmentFunction · 0.80
RegularElementFunction · 0.80
EachBlockFunction · 0.80
SnippetBlockFunction · 0.80
LetDirectiveFunction · 0.80
ConstTagFunction · 0.80
SvelteElementFunction · 0.80

Calls 6

is_reservedFunction · 0.90
next_counterMethod · 0.80
set_counterMethod · 0.80
setMethod · 0.65
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected