All files / src/compiler index.js

90.39% Statements 160/177
83.33% Branches 15/18
85.71% Functions 6/7
89.63% Lines 147/164

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 1652x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 4700x 27x 27x 27x 27x 27x 27x 27x 4630x 4630x 4630x 4630x 4630x 4630x 4700x 224x 224x 224x       4700x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 26x 26x 26x 26x 26x 14x 14x 14x       26x 2x 2x 2x 2x 2x 2x 238x 238x 238x 238x 237x 237x 237x 237x 237x 237x 237x 237x 238x 238x 238x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 84x 84x 84x 84x 84x             84x 84x 84x 2x 2x 2x 2x 2x 2x 4560x 4560x 8x 8x 8x 360x 360x 360x 360x 360x 360x 8x 8x 4552x 4552x 4552x 2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x  
import { getLocator } from 'locate-character';
import { 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';
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 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.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 validated = validate_module_options(options, '');
		const analysis = analyze_module(parse_acorn(source, false), validated);
		return transform_module(analysis, source, validated);
	} 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
 * @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 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}
 */
function _walk() {
	throw new Error(
		`'svelte/compiler' no longer exports a \`walk\` utility — please import it directly from 'estree-walker' instead`
	);
}
 
export { _walk as walk };
 
export { CompileError } from './errors.js';
 
export { VERSION } from '../version.js';