All files / src/compiler/phases/1-parse index.js

98.2% Statements 328/334
94.02% Branches 63/67
100% Functions 16/16
98.15% Lines 319/325

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 3262x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x     4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 106796x 106796x 4718x 4781x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 4716x 4781x     4716x 4781x 4574x 4574x 4574x 4574x 4574x 4574x 4574x 4574x 4574x 4574x 4781x 142x 142x 142x 4716x 4716x 4716x 4716x 4716x 4781x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 1177x 1177x 1177x 1177x 1177x 4781x 4781x 78907x 78907x 4781x 4781x 4781x 4781x 4781x 4781x 5x 5x 4781x 4781x 4781x 4781x 4781x 4781x 219821x 81023x 81023x 81023x 138798x 219821x 1x 1x 1x     1x 138797x 138797x 138797x 4781x 4781x 4781x 517987x 517987x 465907x 465907x 465907x 52080x 52080x 52080x 4781x 4781x 4781x 4781x 4781x 4781x 108970x 108970x 8755x 8755x 8755x 4781x 4781x 96022x 31994x 31994x 96022x 4781x 4781x 4781x 4781x 4781x 4781x 97904x 97904x 97904x 97904x 4781x 4781x 4781x 1895x 1895x 1895x 1895x 1895x 1895x 1894x 1895x 1895x 1895x 7948x 7948x 7948x 7948x 7948x 1894x 1894x 1894x 1895x 2x 2x 1892x 1892x 1892x 4781x 4781x 4781x 50688x 3x 3x 50685x 50685x 50685x 50685x 50688x 50682x 50682x 50682x 3x 3x 3x 3x 4781x 4781x 3209x 1x 1x 3208x 3208x 3208x 4781x 4781x 10263x 10263x 10263x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 38474x 4781x 2x 2x 2x 2x 2x 2x 4781x 4781x 4781x 2x 2x 2x 2x 2x 2x 2x 2x  
// @ts-expect-error acorn type definitions are borked in the release we use
import { isIdentifierStart, isIdentifierChar } from 'acorn';
import fragment from './state/fragment.js';
import { regex_whitespace } from '../patterns.js';
import { reserved } from './utils/names.js';
import full_char_code_at from './utils/full_char_code_at.js';
import { error } from '../../errors.js';
import { create_fragment } from './utils/create.js';
import read_options from './read/options.js';
import { getLocator } from 'locate-character';
 
const regex_position_indicator = / \(\d+:\d+\)$/;
 
const regex_lang_attribute =
	/<!--[^]*?-->|<script\s+(?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=(["'])?([^"' >]+)\1[^>]*>/g;
 
export class Parser {
	/**
	 * @readonly
	 * @type {string}
	 */
	template;
 
	/** */
	index = 0;
 
	/** Whether we're parsing in TypeScript mode */
	ts = false;
 
	/** @type {import('#compiler').TemplateNode[]} */
	stack = [];
 
	/** @type {import('#compiler').Fragment[]} */
	fragments = [];
 
	/** @type {import('#compiler').Root} */
	root;
 
	/** @type {Record<string, boolean>} */
	meta_tags = {};
 
	/** @type {LastAutoClosedTag | undefined} */
	last_auto_closed_tag;
 
	locate;
 
	/** @param {string} template */
	constructor(template) {
		if (typeof template !== 'string') {
			throw new TypeError('Template must be a string');
		}
 
		this.template = template.trimEnd();
		this.locate = getLocator(this.template, { offsetLine: 1 });
 
		let match_lang;
 
		do match_lang = regex_lang_attribute.exec(template);
		while (match_lang && match_lang[0][1] !== 's'); // ensure it starts with '<s' to match script tags
 
		regex_lang_attribute.lastIndex = 0; // reset matched index to pass tests - otherwise declare the regex inside the constructor
 
		this.ts = match_lang?.[2] === 'ts';
 
		this.root = {
			css: null,
			js: [],
			// @ts-ignore
			start: null,
			// @ts-ignore
			end: null,
			type: 'Root',
			fragment: create_fragment(),
			options: null,
			metadata: {
				ts: this.ts
			}
		};
 
		this.stack.push(this.root);
		this.fragments.push(this.root.fragment);
 
		/** @type {ParserState} */
		let state = fragment;
 
		while (this.index < this.template.length) {
			state = state(this) || fragment;
		}
 
		if (this.stack.length > 1) {
			const current = this.current();
 
			if (current.type === 'RegularElement') {
				current.end = current.start + 1;
				error(current, 'unclosed-element', current.name);
			} else {
				current.end = current.start + 1;
				error(current, 'unclosed-block');
			}
		}
 
		if (state !== fragment) {
			error(this.index, 'unexpected-eof');
		}
 
		if (this.root.fragment.nodes.length) {
			let start = /** @type {number} */ (this.root.fragment.nodes[0].start);
			while (regex_whitespace.test(template[start])) start += 1;
 
			let end = /** @type {number} */ (
				this.root.fragment.nodes[this.root.fragment.nodes.length - 1].end
			);
			while (regex_whitespace.test(template[end - 1])) end -= 1;
 
			this.root.start = start;
			this.root.end = end;
		} else {
			// @ts-ignore
			this.root.start = this.root.end = null;
		}
 
		const options_index = this.root.fragment.nodes.findIndex(
			/** @param {any} thing */
			(thing) => thing.type === 'SvelteOptions'
		);
		if (options_index !== -1) {
			const options = /** @type {import('#compiler').SvelteOptionsRaw} */ (
				this.root.fragment.nodes[options_index]
			);
			this.root.fragment.nodes.splice(options_index, 1);
			this.root.options = read_options(options);
			// We need this for the old AST format
			Object.defineProperty(this.root.options, '__raw__', {
				value: options,
				enumerable: false
			});
		}
	}
 
	/**
	 * offset -> line/column
	 * @param {number} start
	 * @param {number} end
	 */
	get_location(start, end) {
		return {
			start: /** @type {import('locate-character').Location_1} */ (this.locate(start)),
			end: /** @type {import('locate-character').Location_1} */ (this.locate(end))
		};
	}
 
	current() {
		return this.stack[this.stack.length - 1];
	}
 
	/**
	 * @param {any} err
	 * @returns {never}
	 */
	acorn_error(err) {
		error(err.pos, 'js-parse-error', err.message.replace(regex_position_indicator, ''));
	}
 
	/**
	 * @param {string} str
	 * @param {boolean} required
	 */
	eat(str, required = false) {
		if (this.match(str)) {
			this.index += str.length;
			return true;
		}
 
		if (required) {
			if (this.index === this.template.length) {
				error(this.index, 'unexpected-eof', str);
			} else {
				error(this.index, 'expected-token', str);
			}
		}
 
		return false;
	}
 
	/** @param {string} str */
	match(str) {
		const length = str.length;
		if (length === 1) {
			// more performant than slicing
			return this.template[this.index] === str;
		}
 
		return this.template.slice(this.index, this.index + length) === str;
	}
 
	/**
	 * Match a regex at the current index
	 * @param {RegExp} pattern  Should have a ^ anchor at the start so the regex doesn't search past the beginning, resulting in worse performance
	 */
	match_regex(pattern) {
		const match = pattern.exec(this.template.slice(this.index));
		if (!match || match.index !== 0) return null;
 
		return match[0];
	}
 
	allow_whitespace() {
		while (this.index < this.template.length && regex_whitespace.test(this.template[this.index])) {
			this.index++;
		}
	}
 
	/**
	 * Search for a regex starting at the current index and return the result if it matches
	 * @param {RegExp} pattern  Should have a ^ anchor at the start so the regex doesn't search past the beginning, resulting in worse performance
	 */
	read(pattern) {
		const result = this.match_regex(pattern);
		if (result) this.index += result.length;
		return result;
	}
 
	/** @param {any} allow_reserved */
	read_identifier(allow_reserved = false) {
		const start = this.index;
 
		let i = this.index;
 
		const code = full_char_code_at(this.template, i);
		if (!isIdentifierStart(code, true)) return null;
 
		i += code <= 0xffff ? 1 : 2;
 
		while (i < this.template.length) {
			const code = full_char_code_at(this.template, i);
 
			if (!isIdentifierChar(code, true)) break;
			i += code <= 0xffff ? 1 : 2;
		}
 
		const identifier = this.template.slice(this.index, (this.index = i));
 
		if (!allow_reserved && reserved.includes(identifier)) {
			error(start, 'unexpected-reserved-word', identifier);
		}
 
		return identifier;
	}
 
	/** @param {RegExp} pattern */
	read_until(pattern) {
		if (this.index >= this.template.length) {
			error(this.template.length, 'unexpected-eof');
		}
 
		const start = this.index;
		const match = pattern.exec(this.template.slice(start));
 
		if (match) {
			this.index = start + match.index;
			return this.template.slice(start, this.index);
		}
 
		this.index = this.template.length;
		return this.template.slice(start);
	}
 
	require_whitespace() {
		if (!regex_whitespace.test(this.template[this.index])) {
			error(this.index, 'missing-whitespace');
		}
 
		this.allow_whitespace();
	}
 
	pop() {
		this.fragments.pop();
		return this.stack.pop();
	}
 
	/**
	 * @template T
	 * @param {Omit<T, 'prev' | 'parent'>} node
	 * @returns {T}
	 */
	append(node) {
		const current = this.current();
		const fragment = this.fragments.at(-1);
 
		Object.defineProperties(node, {
			prev: {
				enumerable: false,
				value: fragment?.nodes.at(-1) ?? null
			},
			parent: {
				enumerable: false,
				configurable: true,
				value: current
			}
		});
 
		// @ts-expect-error
		fragment.nodes.push(node);
 
		// @ts-expect-error
		return node;
	}
}
 
/**
 * @param {string} template
 * @returns {import('#compiler').Root}
 */
export function parse(template) {
	const parser = new Parser(template);
	return parser.root;
}
 
/** @typedef {(parser: Parser) => ParserState | void} ParserState */
 
/** @typedef {Object} LastAutoClosedTag
 * @property {string} tag
 * @property {string} reason
 * @property {number} depth
 */