Error Handling
Relevant source files
The following files were used as context for generating this wiki page:
Error handling in nth-check primarily occurs during the parsing stage of CSS nth-child formulas. The module distinguishes between syntax errors, which trigger exceptions, and logical edge cases (formulas that match no elements), which are handled by returning specialized optimized functions.
The system ensures that invalid formulas are caught early during the parse phase, while the compile phase handles mathematical edge cases where a formula might be logically valid but results in an empty set of matches.
Parsing Validation
The parse function is the primary gatekeeper for input validation. It performs a manual scan of the formula string and throws a standard Error if the input does not conform to the expected CSS nth-rule syntax.
Syntax Verification Logic
Parsing involves checking for valid signs, integers, and the presence of the n character. The parser throws an error in two specific scenarios:
- Missing Numeric Component: When a number is expected but not found (e.g., after a sign or at the end of the string).
- Trailing Characters: When there is leftover content in the string after the valid formula has been processed.
The diagram shows the decision flow within the parse function where errors are identified.
Sources: src/parse.ts:16-56
Validation Examples
The following table summarizes common invalid inputs that trigger parser errors:
| Input Formula | Reason for Error |
|---|---|
"- " | Sign provided without a following number or n. |
"asdf" | Non-numeric/non-CSS keywords. |
"2n+-0" | Invalid sign combination. |
"2+0" | Missing the n identifier in a complex formula. |
Sources: src/fixtures/rules.ts:39-50, src/parse.spec.ts:7-11
Compiler Logical Handling
Unlike the parser, the compile function does not throw errors for validly parsed tuples. Instead, it handles logical "non-matches" by returning optimized utility functions from the boolbase library.
Empty Match Sets
If a formula is mathematically impossible to satisfy (e.g., both a and b resulting in indices less than 1), the system avoids runtime errors during index checking by returning a falseFunc.
This sequence demonstrates how the compiler gracefully handles formulas that can never match an index.
Sources: src/compile.ts:22-38, src/index.ts:25-27
Implementation of Empty Sequence
For the generate and sequence features, error handling manifests as returning null when a sequence is exhausted or logically empty.
- Non-positive
avalues: Ifa === 0and the offsetbis less than 0, the generator immediately returns a function that always yieldsnull. - Negative
avalues: The generator calculates aminValueand returnsnullonce the sequence exceeds the upper boundb.
Sources: src/compile.ts:89-106, src/compile.spec.ts:79-88
Summary of Error States
| Stage | Action | Result |
|---|---|---|
| Parsing | parse("invalid") | Throws Error("n-th rule couldn't be parsed...") |
| Compilation | compile([0, 0]) | Returns boolbase.falseFunc (no error thrown) |
| Generation | generate([-1, 0]) | Function returns null on first call |
Sources: src/parse.ts:54-56, src/compile.ts:37, src/compile.spec.ts:86-90
Error handling in nth-check ensures that developers receive immediate feedback via exceptions during development when providing malformed CSS selectors, while maintaining high performance at runtime by returning constant boolean functions for logically empty sets.