All files / src/compiler index.js

92.34% Statements 193/209
83.33% Branches 15/18
85.71% Functions 6/7
91.75% Lines 178/194

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 1952x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 4785x 31x 31x 31x 31x 31x 31x 31x 4713x 4713x 4713x 4713x 4713x 4713x 4713x 4785x 232x 232x 232x       4785x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 26x 26x 26x 26x 26x 26x 26x 26x 14x 14x 14x       26x 2x 2x 2x 2x 2x 2x 246x 246x 246x 246x 245x 245x 245x 245x 245x 245x 245x 245x 246x 246x 246x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 85x 85x 85x 85x 85x             85x 85x 85x 2x 2x 2x 2x 2x 2x 4638x 4638x 9x 9x 9x 382x 382x 382x 382x 382x 382x 9x 9x 4629x 4629x 4629x 2x 2x 2x 2x 2x 2x         2x 2x 2x 2x  
import { getLocator } from 'locate-character';
import { walk as zimmerframe_walk } from 'zimmerframe';
import { CompileError } from './errors.js';
import { convert } from './legacy.js';
import { parse as parse_acorn } from './phases/1-parse/acorn.js';
import { parse as _parse } from './phases/1-parse/index.js';
import { remove_typescript_nodes } from './phases/1-parse/remove_typescript_nodes.js';
import { analyze_component, analyze_module } from './phases/2-analyze/index.js';
import { transform_component, transform_module } from './phases/3-transform/index.js';
import { validate_component_options, validate_module_options } from './validate-options.js';
import { reset_warnings } from './warnings.js';
export { default as preprocess } from './preprocess/index.js';
 
/**
 * `compile` converts your `.svelte` source code into a JavaScript module that exports a component
 *
 * https://svelte.dev/docs/svelte-compiler#svelte-compile
 * @param {string} source The component source code
 * @param {import('#compiler').CompileOptions} options The compiler options
 * @returns {import('#compiler').CompileResult}
 */
export function compile(source, options) {
	try {
		const warnings = reset_warnings({ source, filename: options.filename });
		const validated = validate_component_options(options, '');
		let parsed = _parse(source);
 
		const { customElement: customElementOptions, ...parsed_options } = parsed.options || {};
 
		/** @type {import('#compiler').ValidatedCompileOptions} */
		const combined_options = {
			...validated,
			...parsed_options,
			customElementOptions
		};
 
		if (parsed.metadata.ts) {
			parsed = {
				...parsed,
				fragment: parsed.fragment && remove_typescript_nodes(parsed.fragment),
				instance: parsed.instance && remove_typescript_nodes(parsed.instance),
				module: parsed.module && remove_typescript_nodes(parsed.module)
			};
		}
 
		const analysis = analyze_component(parsed, source, combined_options);
 
		const result = transform_component(analysis, source, combined_options);
		result.warnings = warnings;
		result.ast = to_public_ast(source, parsed, options.modernAst);
		return result;
	} catch (e) {
		if (e instanceof CompileError) {
			handle_compile_error(e, options.filename, source);
		}

		throw e;
	}
}
 
/**
 * `compileModule` takes your JavaScript source code containing runes, and turns it into a JavaScript module.
 *
 * https://svelte.dev/docs/svelte-compiler#svelte-compile
 * @param {string} source The component source code
 * @param {import('#compiler').ModuleCompileOptions} options
 * @returns {import('#compiler').CompileResult}
 */
export function compileModule(source, options) {
	try {
		const warnings = reset_warnings({ source, filename: options.filename });
		const validated = validate_module_options(options, '');
		const analysis = analyze_module(parse_acorn(source, false), validated);
		const result = transform_module(analysis, source, validated);
		result.warnings = warnings;
		return result;
	} catch (e) {
		if (e instanceof CompileError) {
			handle_compile_error(e, options.filename, source);
		}

		throw e;
	}
}
 
/**
 * @param {import('#compiler').CompileError} error
 * @param {string | undefined} filename
 * @param {string} source
 */
function handle_compile_error(error, filename, source) {
	error.filename = filename;
 
	if (error.position) {
		// TODO this is reused with warnings — DRY out
		const locator = getLocator(source, { offsetLine: 1 });
		const start = locator(error.position[0]);
		const end = locator(error.position[1]);
 
		error.start = start;
		error.end = end;
	}
 
	throw error;
}
 
/**
 * The parse function parses a component, returning only its abstract syntax tree.
 *
 * The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST.
 * `modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
 *
 * https://svelte.dev/docs/svelte-compiler#svelte-parse
 * @overload
 * @param {string} source
 * @param {{ filename?: string; modern: true }} options
 * @returns {import('#compiler').Root}
 */
 
/**
 * The parse function parses a component, returning only its abstract syntax tree.
 *
 * The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST.
 * `modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
 *
 * https://svelte.dev/docs/svelte-compiler#svelte-parse
 * @overload
 * @param {string} source
 * @param {{ filename?: string; modern?: false }} [options]
 * @returns {import('./types/legacy-nodes.js').LegacyRoot}
 */
 
/**
 * The parse function parses a component, returning only its abstract syntax tree.
 *
 * The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST.
 * `modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
 *
 * https://svelte.dev/docs/svelte-compiler#svelte-parse
 * @param {string} source
 * @param {{ filename?: string; modern?: boolean }} [options]
 * @returns {import('#compiler').Root | import('./types/legacy-nodes.js').LegacyRoot}
 */
export function parse(source, options = {}) {
	/** @type {import('#compiler').Root} */
	let ast;
	try {
		ast = _parse(source);
	} catch (e) {
		if (e instanceof CompileError) {
			handle_compile_error(e, options.filename, source);
		}

		throw e;
	}
 
	return to_public_ast(source, ast, options.modern);
}
 
/**
 * @param {string} source
 * @param {import('#compiler').Root} ast
 * @param {boolean | undefined} modern
 */
function to_public_ast(source, ast, modern) {
	if (modern) {
		// remove things that we don't want to treat as public API
		return zimmerframe_walk(ast, null, {
			_(node, { next }) {
				// @ts-ignore
				delete node.parent;
				// @ts-ignore
				delete node.metadata;
				next();
			}
		});
	}
 
	return convert(source, ast);
}
 
/**
 * @deprecated Replace this with `import { walk } from 'estree-walker'`
 * @returns {never}
 */
export function walk() {
	throw new Error(
		`'svelte/compiler' no longer exports a \`walk\` utility — please import it directly from 'estree-walker' instead`
	);
}
 
export { CompileError } from './errors.js';
export { VERSION } from '../version.js';
export { migrate } from './migrate/index.js';