Skip to content

Commit 6754592

Browse files
authored
css-calc: add onParseError option (#1781)
1 parent 98fdb95 commit 6754592

24 files changed

Lines changed: 1124 additions & 25 deletions

packages/css-calc/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to CSS Calc
22

3+
### Unreleased (minor)
4+
5+
- Add `onParseError` option to `calc()`.
6+
37
### 3.0.1
48

59
_February 10, 2026_

packages/css-calc/dist/index.d.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ComponentValue } from '@csstools/css-parser-algorithms';
1+
import { ComponentValue } from '@csstools/css-parser-algorithms';
22
import type { TokenDimension } from '@csstools/css-tokenizer';
33
import type { TokenNumber } from '@csstools/css-tokenizer';
44
import type { TokenPercentage } from '@csstools/css-tokenizer';
@@ -8,6 +8,14 @@ export declare function calc(css: string, options?: conversionOptions): string;
88
export declare function calcFromComponentValues(componentValuesList: Array<Array<ComponentValue>>, options?: conversionOptions): Array<Array<ComponentValue>>;
99

1010
export declare type conversionOptions = {
11+
/**
12+
* If a calc expression can not be solved the parse error might be reported through this callback.
13+
* Not all cases are covered. Open an issue if you need specific errors reported.
14+
*
15+
* Values are recursively visited and at each nesting level an attempt is made to solve the expression.
16+
* Errors can be reported multiple times as a result of this.
17+
*/
18+
onParseError?: (error: ParseError) => void;
1119
/**
1220
* Pass global values as a map of key value pairs.
1321
*/
@@ -68,4 +76,26 @@ export declare type GlobalsWithStrings = Map<string, TokenDimension | TokenNumbe
6876

6977
export declare const mathFunctionNames: Set<string>;
7078

79+
/**
80+
* Any errors are reported through the `onParseError` callback.
81+
*/
82+
export declare class ParseError extends Error {
83+
/** The index of the start character of the current token. */
84+
sourceStart: number;
85+
/** The index of the end character of the current token. */
86+
sourceEnd: number;
87+
constructor(message: string, sourceStart: number, sourceEnd: number);
88+
}
89+
90+
export declare const ParseErrorMessage: {
91+
UnexpectedAdditionOfDimensionOrPercentageWithNumber: string;
92+
UnexpectedSubtractionOfDimensionOrPercentageWithNumber: string;
93+
};
94+
95+
export declare class ParseErrorWithComponentValues extends ParseError {
96+
/** The associated component values. */
97+
componentValues: Array<ComponentValue>;
98+
constructor(message: string, componentValues: Array<ComponentValue>);
99+
}
100+
71101
export { }

packages/css-calc/dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)