Home / File/ script.js — svelte Source File

script.js — svelte Source File

Architecture documentation for script.js, a javascript file in the svelte codebase. 9 imports, 1 dependents.

File javascript Compiler Transformer 9 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  90aa5201_1990_23b6_f05a_1ff5d9b22b14["script.js"]
  2627a38d_1d02_8fc1_b764_b2aaf06f372b["acorn.js"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 2627a38d_1d02_8fc1_b764_b2aaf06f372b
  ce051dbd_4cf1_f117_d66e_12cfa122de37["patterns.js"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> ce051dbd_4cf1_f117_d66e_12cfa122de37
  495501a4_a342_6a4d_ac11_e3e2fee8b218["errors.js"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 495501a4_a342_6a4d_ac11_e3e2fee8b218
  a146f6ac_0088_8736_b6ce_318f9f115170["e"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> a146f6ac_0088_8736_b6ce_318f9f115170
  56a689f9_11c0_cc76_bd60_41bb6dc96475["warnings.js"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 56a689f9_11c0_cc76_bd60_41bb6dc96475
  3246e0bc_b9fc_f638_5e35_41e8c39a2408["w"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 3246e0bc_b9fc_f638_5e35_41e8c39a2408
  0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c["ast.js"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c
  653284b2_68fd_eee3_0064_918a4c065d4a["is_text_attribute"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 653284b2_68fd_eee3_0064_918a4c065d4a
  62f818c8_e890_17ed_5ec1_92f953d4a7a6["state.js"]
  90aa5201_1990_23b6_f05a_1ff5d9b22b14 --> 62f818c8_e890_17ed_5ec1_92f953d4a7a6
  206889ff_1f9f_b6c1_d530_059d001e1cf4["element.js"]
  206889ff_1f9f_b6c1_d530_059d001e1cf4 --> 90aa5201_1990_23b6_f05a_1ff5d9b22b14
  style 90aa5201_1990_23b6_f05a_1ff5d9b22b14 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Program } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { Parser } from '../index.js' */
import * as acorn from '../acorn.js';
import { regex_not_newline_characters } from '../../patterns.js';
import * as e from '../../../errors.js';
import * as w from '../../../warnings.js';
import { is_text_attribute } from '../../../utils/ast.js';
import { locator } from '../../../state.js';

const regex_closing_script_tag = /<\/script\s*>/;
const regex_starts_with_closing_script_tag = /^<\/script\s*>/;

const RESERVED_ATTRIBUTES = ['server', 'client', 'worker', 'test', 'default'];
const ALLOWED_ATTRIBUTES = ['context', 'generics', 'lang', 'module'];

/**
 * @param {Parser} parser
 * @param {number} start
 * @param {Array<AST.Attribute | AST.SpreadAttribute | AST.Directive | AST.AttachTag>} attributes
 * @returns {AST.Script}
 */
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

Functions

Frequently Asked Questions

What does script.js do?
script.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Transformer subdomain.
What functions are defined in script.js?
script.js defines 1 function(s): read_script.
What does script.js depend on?
script.js imports 9 module(s): acorn.js, ast.js, e, errors.js, is_text_attribute, patterns.js, state.js, w, and 1 more.
What files import script.js?
script.js is imported by 1 file(s): element.js.
Where is script.js in the architecture?
script.js is located at packages/svelte/src/compiler/phases/1-parse/read/script.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/1-parse/read).

Analyze Your Own Codebase

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

Try Supermodel Free