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

Function VariableDeclaration

packages/svelte/src/compiler/migrate/index.js:567–917  ·  view source on GitHub ↗
(node, { state, path, visit, next })

Source from the content-addressed store, hash-verified

565 }
566 },
567 VariableDeclaration(node, { state, path, visit, next }) {
568 if (state.scope !== state.analysis.instance.scope) {
569 return;
570 }
571
572 let nr_of_props = 0;
573
574 for (let i = 0; i < node.declarations.length; i++) {
575 const declarator = node.declarations[i];
576 if (state.analysis.runes) {
577 if (get_rune(declarator.init, state.scope) === '$props') {
578 state.props_insertion_point = /** @type {number} */ (declarator.id.start) + 1;
579 state.has_props_rune = true;
580 }
581 continue;
582 }
583
584 let bindings;
585 try {
586 bindings = state.scope.get_bindings(declarator);
587 } catch (e) {
588 // no bindings, so we can skip this
589 next();
590 continue;
591 }
592 const has_state = bindings.some((binding) => binding.kind === 'state');
593 const has_props = bindings.some((binding) => binding.kind === 'bindable_prop');
594
595 if (!has_state && !has_props) {
596 next();
597 continue;
598 }
599
600 if (has_props) {
601 nr_of_props++;
602
603 if (declarator.id.type !== 'Identifier') {
604 // TODO invest time in this?
605 throw new MigrationError(
606 'Encountered an export declaration pattern that is not supported for automigration.'
607 );
608 // Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
609 // means that foo and bar are the props (i.e. the leaves are the prop names), not x and z.
610 // const tmp = b.id(state.scope.generate('tmp'));
611 // const paths = extract_paths(declarator.id, tmp);
612 // state.props_pre.push(
613 // b.declaration('const', tmp, visit(declarator.init!) as Expression)
614 // );
615 // for (const path of paths) {
616 // const name = (path.node as Identifier).name;
617 // const binding = state.scope.get(name)!;
618 // const value = path.expression;
619 // if (binding.kind === 'bindable_prop' || binding.kind === 'rest_prop') {
620 // state.props.push({
621 // local: name,
622 // exported: binding.prop_alias ? binding.prop_alias : name,
623 // init: value
624 // });

Callers

nothing calls this directly

Calls 12

get_runeFunction · 0.90
extract_type_and_commentFunction · 0.85
check_rune_bindingFunction · 0.85
get_bindingsMethod · 0.80
pushMethod · 0.80
updateMethod · 0.65
nextFunction · 0.50
visitFunction · 0.50
getMethod · 0.45
toStringMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected