fix: Handle regex values when generating types · plotly/plotly.js@f4fe890 · GitHub
Skip to content

Commit f4fe890

Browse files
committed
fix: Handle regex values when generating types
1 parent 389d256 commit f4fe890

4 files changed

Lines changed: 150 additions & 35 deletions

File tree

‎lib/index.d.ts‎

Lines changed: 6 additions & 5 deletions

‎src/types/core/layout.d.ts‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,10 @@ import type { Data, Layout, TraceType } from '../generated/schema';
1212
import type { PlotlyHTMLElement } from './events';
1313

1414
// ---------------------------------------------------------------------------
15-
// Axis name types (template literal utilities — not in schema)
15+
// Axis name types
1616
// ---------------------------------------------------------------------------
1717

18-
/**
19-
* Numeric axis suffix plus the optional ` domain` qualifier. The suffix is
20-
* empty for the first axis (`x` / `y`) and `2` through `99` otherwise.
21-
*/
22-
type xYAxisNames = `${
23-
| ''
24-
| `${2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`
25-
| `${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`}${'' | ' domain'}`;
26-
27-
/** Any valid x-axis reference: `'x'`, `'x2'`, …, `'x99'`, optionally `' domain'`. */
28-
export type XAxisName = `x${xYAxisNames}`;
29-
/** Any valid y-axis reference: `'y'`, `'y2'`, …, `'y99'`, optionally `' domain'`. */
30-
export type YAxisName = `y${xYAxisNames}`;
31-
/** Any valid axis reference (x or y, numbered or not, domain-qualified or not). */
32-
export type AxisName = XAxisName | YAxisName;
18+
export type { AxisName, CartesianSubplotId, XAxisName, YAxisName } from '../lib/common';
3319

3420
// ---------------------------------------------------------------------------
3521
// ModeBar / Icon (behavioral types — not in schema)
@@ -169,4 +155,3 @@ export interface Template {
169155
/** Template layout defaults. */
170156
layout?: Partial<Layout> | undefined;
171157
}
172-

‎src/types/lib/common.d.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,52 @@ export type XAnchor = 'auto' | 'left' | 'center' | 'right';
6464
/** Vertical anchor position for components (legend, annotation, etc.). */
6565
export type YAnchor = 'auto' | 'top' | 'middle' | 'bottom';
6666

67+
// ---------------------------------------------------------------------------
68+
// Axis and subplot identifiers
69+
//
70+
// The schema states these as regexes, which no TypeScript type can express
71+
// exactly. The template literal types below enumerate the accepted strings
72+
// instead, so they are bounded where the schema is not. See the digit-tier
73+
// note on `AxisNumber`.
74+
//
75+
// tasks/generate_schema_types.mjs maps each schema regex onto one of these
76+
// types through its REGEX_VALUE_TYPES table.
77+
// ---------------------------------------------------------------------------
78+
79+
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
80+
type NonZeroDigit = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
81+
82+
/**
83+
* Numeric axis suffix. Empty for the first axis (`x` / `y`), then `2` through
84+
* `999`. There is no `1` suffix — the first axis is unnumbered.
85+
*
86+
* The schema regex accepts any number of digits. This type stops at three
87+
* because a template literal union has to be finite, so charts with 1000 or
88+
* more axes of one letter cannot be typed.
89+
*/
90+
type AxisNumber = '' | `${Exclude<NonZeroDigit, 1>}` | `${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}${Digit}`;
91+
92+
/**
93+
* Two-digit variant of `AxisNumber`, capped at `99`.
94+
*
95+
* Used only where the suffix appears twice in one identifier. Three digits
96+
* squared exceeds the TypeScript union size limit.
97+
*/
98+
type ShortAxisNumber = '' | `${Exclude<NonZeroDigit, 1>}` | `${NonZeroDigit}${Digit}`;
99+
100+
/** Any valid x-axis reference: `'x'`, `'x2'`, …, optionally `' domain'`. */
101+
export type XAxisName = `x${AxisNumber}${'' | ' domain'}`;
102+
/** Any valid y-axis reference: `'y'`, `'y2'`, …, optionally `' domain'`. */
103+
export type YAxisName = `y${AxisNumber}${'' | ' domain'}`;
104+
/** Any valid axis reference (x or y, numbered or not, domain-qualified or not). */
105+
export type AxisName = XAxisName | YAxisName;
106+
107+
/**
108+
* A cartesian subplot id pairing an x and a y axis, such as `'xy'` or
109+
* `'x3y2'`. Unlike `XAxisName`, no `' domain'` qualifier is permitted.
110+
*/
111+
export type CartesianSubplotId = `x${ShortAxisNumber}y${ShortAxisNumber}`;
112+
67113
// ---------------------------------------------------------------------------
68114
// Error bars
69115
// ---------------------------------------------------------------------------

‎tasks/generate_schema_types.mjs‎

Lines changed: 96 additions & 13 deletions

0 commit comments

Comments
 (0)