Home / Function/ read_type_annotation() — svelte Function Reference

read_type_annotation() — svelte Function Reference

Architecture documentation for the read_type_annotation() function in context.js from the svelte codebase.

Function javascript Compiler Transformer calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  b0e2fc2e_b944_6248_d26e_696646af398f["read_type_annotation()"]
  caefc1b2_dc4c_2cff_4013_e8ded13e7974["context.js"]
  b0e2fc2e_b944_6248_d26e_696646af398f -->|defined in| caefc1b2_dc4c_2cff_4013_e8ded13e7974
  6a786ac5_79fd_078b_6df2_8e1e4a370161["read_pattern()"]
  6a786ac5_79fd_078b_6df2_8e1e4a370161 -->|calls| b0e2fc2e_b944_6248_d26e_696646af398f
  bb0eb4c2_ab75_feac_3814_01d1e4346bb8["parse_expression_at()"]
  b0e2fc2e_b944_6248_d26e_696646af398f -->|calls| bb0eb4c2_ab75_feac_3814_01d1e4346bb8
  style b0e2fc2e_b944_6248_d26e_696646af398f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/1-parse/read/context.js lines 76–116

function read_type_annotation(parser) {
	const start = parser.index;
	parser.allow_whitespace();

	if (!parser.eat(':')) {
		parser.index = start;
		return undefined;
	}

	// we need to trick Acorn into parsing the type annotation
	const insert = '_ as ';
	let a = parser.index - insert.length;
	const template =
		parser.template.slice(0, a).replace(/[^\n]/g, ' ') +
		insert +
		// If this is a type annotation for a function parameter, Acorn-TS will treat subsequent
		// parameters as part of a sequence expression instead, and will then error on optional
		// parameters (`?:`). Therefore replace that sequence with something that will not error.
		parser.template.slice(parser.index).replace(/\?\s*:/g, ':');
	let expression = parse_expression_at(template, parser.root.comments, parser.ts, a);

	// `foo: bar = baz` gets mangled — fix it
	if (expression.type === 'AssignmentExpression') {
		let b = expression.right.start;
		while (template[b] !== '=') b -= 1;
		expression = parse_expression_at(template.slice(0, b), parser.root.comments, parser.ts, a);
	}

	// `array as item: string, index` becomes `string, index`, which is mistaken as a sequence expression - fix that
	if (expression.type === 'SequenceExpression') {
		expression = expression.expressions[0];
	}

	parser.index = /** @type {number} */ (expression.end);
	return {
		type: 'TSTypeAnnotation',
		start,
		end: parser.index,
		typeAnnotation: /** @type {any} */ (expression).typeAnnotation
	};
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does read_type_annotation() do?
read_type_annotation() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/1-parse/read/context.js.
Where is read_type_annotation() defined?
read_type_annotation() is defined in packages/svelte/src/compiler/phases/1-parse/read/context.js at line 76.
What does read_type_annotation() call?
read_type_annotation() calls 1 function(s): parse_expression_at.
What calls read_type_annotation()?
read_type_annotation() is called by 1 function(s): read_pattern.

Analyze Your Own Codebase

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

Try Supermodel Free