Home / Function/ read_script() — svelte Function Reference

read_script() — svelte Function Reference

Architecture documentation for the read_script() function in script.js from the svelte codebase.

Function javascript Compiler Transformer calls 7 called by 1

Entity Profile

Dependency Diagram

graph TD
  3f7147bd_f765_d166_38e7_10f71b9fd062["read_script()"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14["script.js"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|defined in| 90aa5201_1990_23b6_f05a_1ff5d9b22b14
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb["element()"]
  be9cd3f4_bdc5_dc26_dae4_4a34e45ab7eb -->|calls| 3f7147bd_f765_d166_38e7_10f71b9fd062
  d33ef143_a2e5_8235_a012_3924877b988c["element_unclosed()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| d33ef143_a2e5_8235_a012_3924877b988c
  f47134ed_b840_afa5_bf63_d93768eb2045["parse()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| f47134ed_b840_afa5_bf63_d93768eb2045
  bf811aa5_e004_58ad_ef71_5b27d685f00d["script_reserved_attribute()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| bf811aa5_e004_58ad_ef71_5b27d685f00d
  7bbd9f62_d7a6_d625_e1bd_122f3c52ea2c["script_unknown_attribute()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| 7bbd9f62_d7a6_d625_e1bd_122f3c52ea2c
  3e6eefac_3a65_06e1_e1bb_52d428a65e1c["script_invalid_attribute_value()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| 3e6eefac_3a65_06e1_e1bb_52d428a65e1c
  653284b2_68fd_eee3_0064_918a4c065d4a["is_text_attribute()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| 653284b2_68fd_eee3_0064_918a4c065d4a
  0b2bb018_2ca6_2dbc_ed82_1940ce7d69c1["script_invalid_context()"]
  3f7147bd_f765_d166_38e7_10f71b9fd062 -->|calls| 0b2bb018_2ca6_2dbc_ed82_1940ce7d69c1
  style 3f7147bd_f765_d166_38e7_10f71b9fd062 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/1-parse/read/script.js lines 23–97

export function read_script(parser, start, attributes) {
	const script_start = parser.index;
	const data = parser.read_until(regex_closing_script_tag);
	if (parser.index >= parser.template.length) {
		e.element_unclosed(parser.template.length, 'script');
	}

	const source =
		parser.template.slice(0, script_start).replace(regex_not_newline_characters, ' ') + data;
	parser.read(regex_starts_with_closing_script_tag);

	/** @type {Program} */
	let ast;

	try {
		ast = acorn.parse(source, parser.root.comments, parser.ts, true);
	} catch (err) {
		parser.acorn_error(err);
	}

	ast.start = script_start;

	if (ast.loc) {
		// Acorn always uses `0` as the start of a `Program`, but for sourcemap purposes
		// we need it to be the start of the `<script>` contents
		({ line: ast.loc.start.line, column: ast.loc.start.column } = locator(start));
		({ line: ast.loc.end.line, column: ast.loc.end.column } = locator(parser.index));
	}

	/** @type {'default' | 'module'} */
	let context = 'default';

	for (const attribute of /** @type {AST.Attribute[]} */ (attributes)) {
		if (RESERVED_ATTRIBUTES.includes(attribute.name)) {
			e.script_reserved_attribute(attribute, attribute.name);
		}

		if (!ALLOWED_ATTRIBUTES.includes(attribute.name)) {
			w.script_unknown_attribute(attribute);
		}

		if (attribute.name === 'module') {
			if (attribute.value !== true) {
				// Deliberately a generic code to future-proof for potential other attributes
				e.script_invalid_attribute_value(attribute, attribute.name);
			}

			context = 'module';
		}

		if (attribute.name === 'context') {
			if (attribute.value === true || !is_text_attribute(attribute)) {
				e.script_invalid_context(attribute);
			}

			const value = attribute.value[0].data;

			if (value !== 'module') {
				e.script_invalid_context(attribute);
			}

			context = 'module';
		}
	}

	return {
		type: 'Script',
		start,
		end: parser.index,
		context,
		content: ast,
		// @ts-ignore
		attributes
	};
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does read_script() do?
read_script() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/1-parse/read/script.js.
Where is read_script() defined?
read_script() is defined in packages/svelte/src/compiler/phases/1-parse/read/script.js at line 23.
What does read_script() call?
read_script() calls 7 function(s): element_unclosed, is_text_attribute, parse, script_invalid_attribute_value, script_invalid_context, script_reserved_attribute, script_unknown_attribute.
What calls read_script()?
read_script() is called by 1 function(s): element.

Analyze Your Own Codebase

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

Try Supermodel Free