All files / src/compiler/phases/1-parse/read expression.js

95.74% Statements 45/47
90.9% Branches 10/11
100% Functions 1/1
95.45% Lines 42/44

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 452x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12353x 12353x 12353x 12353x 12353x 12353x 80x 80x 12351x 12351x 12353x 1x 1x 12351x 12353x 14x 14x 14x 14x 14x     14x 14x 14x 12351x 12351x 12351x 12351x 12353x 2x 2x 12353x  
/** @import { Expression } from 'estree' */
/** @import { Parser } from '../index.js' */
import { parse_expression_at } from '../acorn.js';
import { regex_whitespace } from '../../patterns.js';
import * as e from '../../../errors.js';
 
/**
 * @param {Parser} parser
 * @returns {Expression}
 */
export default function read_expression(parser) {
	try {
		const node = parse_expression_at(parser.template, parser.ts, parser.index);
 
		let num_parens = 0;
 
		for (let i = parser.index; i < /** @type {number} */ (node.start); i += 1) {
			if (parser.template[i] === '(') num_parens += 1;
		}
 
		let index = /** @type {number} */ (node.end);
		if (node.trailingComments !== undefined && node.trailingComments.length > 0) {
			index = node.trailingComments.at(-1).end;
		}
 
		while (num_parens > 0) {
			const char = parser.template[index];
 
			if (char === ')') {
				num_parens -= 1;
			} else if (!regex_whitespace.test(char)) {
				e.expected_token(index, ')');
			}
 
			index += 1;
		}
 
		parser.index = index;
 
		return /** @type {Expression} */ (node);
	} catch (err) {
		parser.acorn_error(err);
	}
}