Home / Function/ extract_type_and_comment() — svelte Function Reference

extract_type_and_comment() — svelte Function Reference

Architecture documentation for the extract_type_and_comment() function in index.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  01f56d6c_9f35_6b0e_ad0a_fdb401fca3b4["extract_type_and_comment()"]
  cab41022_1b55_3b7a_06c6_b90763bbea47["index.js"]
  01f56d6c_9f35_6b0e_ad0a_fdb401fca3b4 -->|defined in| cab41022_1b55_3b7a_06c6_b90763bbea47
  8f93b8d1_a873_5c72_eae3_de296245116a["instance_script.VariableDeclaration()"]
  8f93b8d1_a873_5c72_eae3_de296245116a -->|calls| 01f56d6c_9f35_6b0e_ad0a_fdb401fca3b4
  style 01f56d6c_9f35_6b0e_ad0a_fdb401fca3b4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/migrate/index.js lines 1589–1709

function extract_type_and_comment(declarator, state, path) {
	const str = state.str;
	const parent = path.at(-1);

	// Try to find jsdoc above the declaration
	let comment_node = /** @type {Node} */ (parent)?.leadingComments?.at(-1);

	const comment_start = /** @type {any} */ (comment_node)?.start;
	const comment_end = /** @type {any} */ (comment_node)?.end;
	let comment = comment_node && str.original.substring(comment_start, comment_end);
	if (comment_node) {
		str.update(comment_start, comment_end, '');
	}

	// Find trailing comments
	const trailing_comment_node = /** @type {Node} */ (parent)?.trailingComments?.at(0);
	const trailing_comment_start = /** @type {any} */ (trailing_comment_node)?.start;
	const trailing_comment_end = /** @type {any} */ (trailing_comment_node)?.end;
	let trailing_comment =
		trailing_comment_node && str.original.substring(trailing_comment_start, trailing_comment_end);

	if (trailing_comment_node) {
		str.update(trailing_comment_start, trailing_comment_end, '');
	}

	if (declarator.id.typeAnnotation) {
		state.has_type_or_fallback = true;
		let start = declarator.id.typeAnnotation.start + 1; // skip the colon
		while (str.original[start] === ' ') {
			start++;
		}
		return {
			type: str.original.substring(start, declarator.id.typeAnnotation.end),
			comment,
			trailing_comment
		};
	}

	let cleaned_comment_arr = comment
		?.split('\n')
		.map((line) =>
			line
				.trim()
				// replace `// ` for one liners
				.replace(/^\/\/\s*/g, '')
				// replace `\**` for the initial JSDoc
				.replace(/^\/\*\*?\s*/g, '')
				// migrate `*/` for the end of JSDoc
				.replace(/\s*\*\/$/g, '')
				// remove any initial `* ` to clean the comment
				.replace(/^\*\s*/g, '')
		)
		.filter(Boolean);
	const first_at_comment = cleaned_comment_arr?.findIndex((line) => line.startsWith('@'));
	let cleaned_comment = cleaned_comment_arr
		?.slice(0, first_at_comment !== -1 ? first_at_comment : cleaned_comment_arr.length)
		.join('\n');

	let cleaned_comment_arr_trailing = trailing_comment
		?.split('\n')
		.map((line) =>
			line
				.trim()
				// replace `// ` for one liners
				.replace(/^\/\/\s*/g, '')
				// replace `\**` for the initial JSDoc
				.replace(/^\/\*\*?\s*/g, '')
				// migrate `*/` for the end of JSDoc
				.replace(/\s*\*\/$/g, '')
				// remove any initial `* ` to clean the comment
				.replace(/^\*\s*/g, '')
		)
		.filter(Boolean);
	const first_at_comment_trailing = cleaned_comment_arr_trailing?.findIndex((line) =>
		line.startsWith('@')
	);
	let cleaned_comment_trailing = cleaned_comment_arr_trailing
		?.slice(
			0,
			first_at_comment_trailing !== -1
				? first_at_comment_trailing

Domain

Subdomains

Frequently Asked Questions

What does extract_type_and_comment() do?
extract_type_and_comment() is a function in the svelte codebase, defined in packages/svelte/src/compiler/migrate/index.js.
Where is extract_type_and_comment() defined?
extract_type_and_comment() is defined in packages/svelte/src/compiler/migrate/index.js at line 1589.
What calls extract_type_and_comment()?
extract_type_and_comment() is called by 1 function(s): instance_script.VariableDeclaration.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free