* Parses a binary expression and returns an @ExpressionParserResult.
(
expr: string,
options: { loose?: boolean } = {
loose: false,
}
)
| 166 | * Parses a binary expression and returns an @ExpressionParserResult. |
| 167 | */ |
| 168 | public parse( |
| 169 | expr: string, |
| 170 | options: { loose?: boolean } = { |
| 171 | loose: false, |
| 172 | } |
| 173 | ): ExpressionParserResult { |
| 174 | try { |
| 175 | const ast = options.loose |
| 176 | ? parseLoose(expr, { ...this.#parserOptions }) |
| 177 | : parse(expr, { ...this.#parserOptions }); |
| 178 | |
| 179 | if (!ast.body || ast.body.length === 0) { |
| 180 | throw new ExpressionError('Empty or invalid expression'); |
| 181 | } |
| 182 | |
| 183 | // Extract the first expression statement that we find |
| 184 | const firstExprIndex = ast.body.findIndex((node) => isParsedExpressionStatement(node)); |
| 185 | const [statement] = ast.body.splice(firstExprIndex, 1); |
| 186 | |
| 187 | if (!statement || !isParsedExpressionStatement(statement)) { |
| 188 | throw new ExpressionError('Empty or invalid expression'); |
| 189 | } |
| 190 | |
| 191 | // Return information on the other nodes as invalid nodes |
| 192 | const invalidNodes = ast.body.filter(filterOutModuleDeclarationStatement); |
| 193 | |
| 194 | return { |
| 195 | result: statement.expression, |
| 196 | invalidNodes, |
| 197 | }; |
| 198 | } catch (error) { |
| 199 | if (error instanceof SyntaxError) { |
| 200 | throw createExpressionErrorFromSyntaxError(expr, error); |
| 201 | } |
| 202 | if (error instanceof ExpressionError) { |
| 203 | throw error; |
| 204 | } |
| 205 | throw new ExpressionError('Unexpected error'); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Provides autocomplete suggestions for the given expression at the provided cursor offset. |
no test coverage detected