Home / Function/ get_comment_handlers() — svelte Function Reference

get_comment_handlers() — svelte Function Reference

Architecture documentation for the get_comment_handlers() function in acorn.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  9bfeaa6b_e84a_de23_08c7_df130f4cacf9["get_comment_handlers()"]
  2627a38d_1d02_8fc1_b764_b2aaf06f372b["acorn.js"]
  9bfeaa6b_e84a_de23_08c7_df130f4cacf9 -->|defined in| 2627a38d_1d02_8fc1_b764_b2aaf06f372b
  f47134ed_b840_afa5_bf63_d93768eb2045["parse()"]
  f47134ed_b840_afa5_bf63_d93768eb2045 -->|calls| 9bfeaa6b_e84a_de23_08c7_df130f4cacf9
  bb0eb4c2_ab75_feac_3814_01d1e4346bb8["parse_expression_at()"]
  bb0eb4c2_ab75_feac_3814_01d1e4346bb8 -->|calls| 9bfeaa6b_e84a_de23_08c7_df130f4cacf9
  style 9bfeaa6b_e84a_de23_08c7_df130f4cacf9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/1-parse/acorn.js lines 104–198

function get_comment_handlers(source, comments, index = 0) {
	return {
		/**
		 * @param {boolean} block
		 * @param {string} value
		 * @param {number} start
		 * @param {number} end
		 * @param {import('acorn').Position} [start_loc]
		 * @param {import('acorn').Position} [end_loc]
		 */
		onComment: (block, value, start, end, start_loc, end_loc) => {
			if (block && /\n/.test(value)) {
				let a = start;
				while (a > 0 && source[a - 1] !== '\n') a -= 1;

				let b = a;
				while (/[ \t]/.test(source[b])) b += 1;

				const indentation = source.slice(a, b);
				value = value.replace(new RegExp(`^${indentation}`, 'gm'), '');
			}

			comments.push({
				type: block ? 'Block' : 'Line',
				value,
				start,
				end,
				loc: {
					start: /** @type {import('acorn').Position} */ (start_loc),
					end: /** @type {import('acorn').Position} */ (end_loc)
				}
			});
		},

		/** @param {acorn.Node & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; }} ast */
		add_comments(ast) {
			if (comments.length === 0) return;

			comments = comments
				.filter((comment) => comment.start >= index)
				.map(({ type, value, start, end }) => ({ type, value, start, end }));

			walk(ast, null, {
				_(node, { next, path }) {
					let comment;

					while (comments[0] && comments[0].start < node.start) {
						comment = /** @type {CommentWithLocation} */ (comments.shift());
						(node.leadingComments ||= []).push(comment);
					}

					next();

					if (comments[0]) {
						const parent = /** @type {any} */ (path.at(-1));

						if (parent === undefined || node.end !== parent.end) {
							const slice = source.slice(node.end, comments[0].start);
							const is_last_in_body =
								((parent?.type === 'BlockStatement' || parent?.type === 'Program') &&
									parent.body.indexOf(node) === parent.body.length - 1) ||
								(parent?.type === 'ArrayExpression' &&
									parent.elements.indexOf(node) === parent.elements.length - 1) ||
								(parent?.type === 'ObjectExpression' &&
									parent.properties.indexOf(node) === parent.properties.length - 1);

							if (is_last_in_body) {
								// Special case: There can be multiple trailing comments after the last node in a block,
								// and they can be separated by newlines
								let end = node.end;

								while (comments.length) {
									const comment = comments[0];
									if (parent && comment.start >= parent.end) break;

									(node.trailingComments ||= []).push(comment);
									comments.shift();
									end = comment.end;
								}
							} else if (node.end <= comments[0].start && /^[,) \t]*$/.test(slice)) {
								node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];

Domain

Subdomains

Frequently Asked Questions

What does get_comment_handlers() do?
get_comment_handlers() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/1-parse/acorn.js.
Where is get_comment_handlers() defined?
get_comment_handlers() is defined in packages/svelte/src/compiler/phases/1-parse/acorn.js at line 104.
What calls get_comment_handlers()?
get_comment_handlers() is called by 2 function(s): parse, parse_expression_at.

Analyze Your Own Codebase

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

Try Supermodel Free