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

95.55% Statements 43/45
90.9% Branches 10/11
100% Functions 1/1
95.23% Lines 40/42

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 432x 2x 2x 2x 2x 2x 2x 2x 2x 11887x 11887x 11887x 11887x 11887x 11887x 78x 78x 11885x 11885x 11887x 1x 1x 11885x 11887x 12x 12x 12x 12x 12x     12x 12x 12x 11885x 11885x 11885x 11885x 11887x 2x 2x 11887x  
import { parse_expression_at } from '../acorn.js';
import { regex_whitespace } from '../../patterns.js';
import * as e from '../../../errors.js';
 
/**
 * @param {import('../index.js').Parser} parser
 * @returns {import('estree').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 {import('estree').Expression} */ (node);
	} catch (err) {
		parser.acorn_error(err);
	}
}