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

95.12% Statements 39/41
87.5% Branches 7/8
100% Functions 1/1
94.73% Lines 36/38

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 392x 2x 2x 2x 2x 2x 2x 2x 2x 11171x 11171x 11171x 11171x 11171x 11171x 25x 25x 11169x 11169x 11171x 12x 12x 12x 12x 12x     12x 12x 12x 11169x 11169x 11169x 11169x 11171x 2x 2x 11171x  
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);
		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);
	}
}