Skip to content

Commit 0593acf

Browse files
camdecosteremilykl
andauthored
Apply suggestions from code review
Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com>
1 parent 5fa57a9 commit 0593acf

9 files changed

Lines changed: 28 additions & 77 deletions

File tree

src/traces/image/attributes.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ for (const name of cm) {
1515
zmaxDesc.push(`For the \`${name}\` colormodel, it is [${(cr.zmaxDflt || cr.max).join(', ')}].`);
1616
}
1717

18-
/**
19-
* Image trace attributes.
20-
* Consumer-facing types are generated from the schema by generate_schema_types.mjs.
21-
*/
2218
const attributes = {
2319
source: {
2420
valType: 'string',

src/types/ARCHITECTURE.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ re-exports.
3838

3939
The split:
4040

41-
- **Generated types** are the authoritative TypeScript representation of
41+
- **Generated types** (`src/types/generated`) are the authoritative TypeScript representation of
4242
the runtime schema. The schema itself is produced from Plotly's JS
4343
attribute files (`src/.../attributes.js`), which remain the source of
4444
truth: chain is **attribute files → `plot-schema.json` → generated
@@ -49,7 +49,7 @@ The split:
4949
- **Shared sub-interfaces** extracted from repeated subtrees (Font,
5050
FontArray, ColorBar, HoverLabel, Domain, Pattern, TickFormatStops,
5151
LegendGroupTitle).
52-
- **Per-trace data interfaces** all 49 (BarData, ScatterData,
52+
- **Per-trace data interfaces** for all trace types (BarData, ScatterData,
5353
IndicatorData, etc.).
5454
- **Layout component interfaces** (LayoutAxis, Legend, Scene,
5555
Annotation, Shape, Slider, UpdateMenu, etc.) and the Layout
@@ -65,7 +65,8 @@ The split:
6565

6666
Generated from `plot-schema.json` by `tasks/generate_schema_types.mjs`.
6767
Run `npm run schema` to regenerate.
68-
- **Hand-written types** cover everything the schema doesn't describe:
68+
- **Hand-written types** (everything in `src/types/` besides the `generated/` directory)
69+
cover everything the schema doesn't describe:
6970
events, internal runtime state, public API function signatures,
7071
utility types (Color, Datum, MarkerSymbol, ErrorBar), behavioral types
7172
(ModeBarButton, Icon, etc.).

src/types/CONVERTING_ATTRIBUTES.md

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,11 @@ for the canonical example. The full conversion changed:
135135
- `.default` added to `require('./attributes')` in `index.js` and `defaults.js`
136136

137137
Note: Consumer-facing types for modebar (and all other layout components)
138-
are now generated from `plot-schema.json` by `tasks/generate_schema_types.mjs`,
138+
are generated from `plot-schema.json` by `tasks/generate_schema_types.mjs`,
139139
not from the individual `attributes.ts` files. The `attributes.ts` conversion
140140
still adds value by type-checking the source attribute definitions against
141141
`AttributeMap`.
142142

143-
Schema output verified byte-identical (2547 bytes) before and after the
144-
conversion.
145-
146-
## What stays hand-written
147-
148-
The schema does not describe these — they remain in `src/types/`:
149-
150-
- **Events** (`PlotMouseEvent`, `LegendClickEvent`, etc.)
151-
- **Public API function signatures** (`Plotly.newPlot`, `relayout`, ...)
152-
- **Internal types** (`FullLayout._modules`, `GraphDiv._fullData`, ...)
153-
- **Utility types** (`Color`, `Datum`, `TypedArray`, `MarkerSymbol`, etc.) —
154-
these are the primitives the generator's emitted types reference.
155-
156-
If you find yourself converting one of these, stop and ask.
157143

158144
## What if I need a type the schema doesn't describe well?
159145

@@ -177,17 +163,13 @@ The schema-generated types are authoritative for everything in
177163

178164
## Schema-generated types
179165

180-
All 49 trace data interfaces, layout component interfaces, and the Layout
166+
All trace data interfaces, layout component interfaces, and the Layout
181167
interface itself are generated from `plot-schema.json` by
182168
`tasks/generate_schema_types.mjs` (run via `npm run schema`). Individual
183169
trace and layout `attributes.js` files do **not** need to be converted
184170
for their types to appear in the public API — the schema generator
185171
covers them automatically.
186172

187-
Converting an `attributes.js` file to TypeScript is still valuable because
188-
it type-checks the source definitions against `AttributeMap`, catching
189-
typos and structural errors at compile time.
190-
191173
## Order of conversion (for parallel work)
192174

193175
Pick from this priority list. Lower-numbered items are smaller / simpler.

src/types/GENERATOR.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ types into `src/types/generated/schema.d.ts`:
77
- Common enum aliases (Calendar, Dash, AxisType, PatternShape, XRef, YRef,
88
TransitionEasing, PlotType)
99
- Shared sub-interfaces (Font, ColorBar, HoverLabel, etc.)
10-
- 49 per-trace data interfaces (BarData, ScatterData, IndicatorData, ...)
10+
- Data interfaces for each trace type (BarData, ScatterData, IndicatorData, etc.)
1111
- Layout component interfaces (LayoutAxis, Legend, Scene, Annotation, etc.)
1212
and the Layout interface itself
1313
- Animation / frame / edits interfaces (AnimationOpts, Frame, Edits)
@@ -59,8 +59,8 @@ and leaf `valType`s produce the same fingerprint string.
5959

6060
### Phase 2: Shared interface extraction
6161

62-
Containers that appear at least `MIN_OCCURRENCES` (= 5) times AND have at
63-
least `MIN_PROPERTIES` (= 4) properties become shared interfaces (Font,
62+
Containers that appear at least `MIN_OCCURRENCES` times AND have at
63+
least `MIN_PROPERTIES` properties become shared interfaces (Font,
6464
ColorBar, HoverLabel, etc.). PascalCase naming is controlled by
6565
`SHARED_NAME_OVERRIDES` so e.g. `colorbar` becomes `ColorBar` rather than
6666
`Colorbar`:
@@ -196,8 +196,6 @@ src/types/generated/schema.d.ts
196196
└── Animation / frames / config (AnimationOpts, Frame, Edits, ConfigBase)
197197
```
198198

199-
Regenerate with `npm run schema`.
200-
201199
## valType → TypeScript mapping
202200

203201
Summary:
@@ -322,20 +320,13 @@ re-exported to consumers. Types inside the `_internal` namespace are still
322320
reachable via `_internal.X` (the namespace itself is exported by the
323321
wildcard) but their bare names are not.
324322

325-
The wildcard is load-bearing: it removes the maintenance burden of keeping
326-
`lib/index.d.ts` in sync with new schema additions. If anyone ever swaps
327-
it for an explicit allowlist, restore the per-name re-export verifier
328-
that previously lived in `tasks/schema.mjs` (see git history) — otherwise
329-
new generated types will silently fail to surface in the public API.
330323

331324
## CI integration
332325

333326
`npm run schema-typegen-diff-check` runs the generator and then verifies that
334327
both `test/plot-schema.json` and `src/types/generated/` are unchanged via
335-
`git diff --exit-code`. If either differs, exit code 1 — meaning either a
336-
developer changed the source schema but didn't commit the regenerated
337-
artifacts, or an attribute-file conversion silently altered the runtime
338-
schema and the change wasn't intentionally committed.
328+
`git diff --exit-code`. If either differs, the command fails with exit code 1
329+
and outputs the diff to the console.
339330

340331
This is what makes the JS-to-TS conversion workflow safe: a correct
341332
conversion produces a byte-identical schema, so the check passes; an

src/types/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This directory documents the TypeScript conversion in progress.
1414
- TypeScript build infrastructure: ✅ done
1515
- Public type surface in `src/types/`: ✅ done
1616
- `AttributeMap` validation machinery: ✅ done
17-
- **Schema-based type generator**: ✅ done — all 49 trace types + layout + shared interfaces
17+
- **Schema-based type generator**: ✅ done — all trace types + layout + shared interfaces
1818
- Consumer entry point (`lib/index.d.ts`, wired via `package.json#types`): ✅ done
1919
- CI gates (`typecheck` + `schema-typegen-diff-check`): ✅ done
2020
- First attribute file converted (modebar): ✅ done
@@ -31,7 +31,7 @@ The following are **auto-generated from `plot-schema.json`** by
3131

3232
- Common enum aliases (Calendar, Dash, AxisType, PatternShape, XRef, YRef,
3333
TransitionEasing, PlotType)
34-
- All 49 per-trace data interfaces (BarData, ScatterData, IndicatorData, ...)
34+
- Data interfaces for each trace type (BarData, ScatterData, IndicatorData, etc.)
3535
- Layout component interfaces (LayoutAxis, Legend, Scene, Annotation,
3636
Shape, Slider, UpdateMenu, etc.) and the Layout interface itself
3737
- Shared sub-interfaces (Font, ColorBar, HoverLabel, LegendGroupTitle, etc.)

src/types/SETUP.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ Quick reference for the TypeScript toolchain in plotly.js.
44

55
## What's installed
66

7-
- **TypeScript** (`typescript ^5.9.3`) — type checker only, never emits JS
8-
- **ts-node** (`^10.9.2`) — runs TS scripts directly (build helpers)
9-
- **@types/node**, **@types/d3** — third-party type definitions
10-
- esbuild handles `.ts` natively for bundling — no plugins needed
7+
The following dev dependencies are used for maintaining plotly.js types:
118

12-
## Two tools, two jobs
9+
- `typescript` — used for type-checking only
10+
- `ts-node` — used for running TS scripts (build helpers)
11+
- `@types/node`, `@types/d3` — provide third-party type definitions
1312

14-
| Tool | Job |
15-
|---|---|
16-
| **esbuild** | Bundle for production. Strips types, transpiles to ES2016, emits `dist/plotly.js`. Fast (~450ms full build). Does **not** check types. |
17-
| **tsc** | Type-check only. Reads `.ts` and `.js` (with `allowJs: true`), reports errors, no output. Slower (~2-5s) but catches bugs. |
13+
Note: esbuild handles `.ts` files natively for bundling, so no extra plugins are needed for the bundling process.
14+
15+
`tsconfig.json` sets `noEmit: true` so that tsc never writes files. esbuild is the build system; tsc is the verifier.
1816

19-
`tsconfig.json` sets `noEmit: true` so tsc never writes files. esbuild is the build system; tsc is the verifier.
2017

2118
## Configuration
2219

@@ -31,10 +28,9 @@ Both target ES2016. `strict: true` is on in `tsconfig.json` — the type system
3128
npm run typecheck # tsc --noEmit, errors reported, no output
3229
npm run typecheck-watch # incremental rechecking on change
3330

34-
npm run schema # rebuild test/plot-schema.json + regenerate types
35-
npm run schema-typegen-diff-check # regenerate + verify no uncommitted drift in test/plot-schema.json or schema.d.ts
36-
npm run bundle # esbuild → dist/plotly.js
37-
npm run build # full production build
31+
npm run schema # rebuild test/plot-schema.json + regenerate types under src/types/generated/
32+
npm run schema-typegen-diff-check # regenerate + verify no changes to test/plot-schema.json or src/types/generated/schema.d.ts
33+
npm run build # full production build (regenerate all files under `dist/`)
3834
```
3935

4036
## Workflows
@@ -77,12 +73,3 @@ var attributes = require('./attributes').default;
7773

7874
This shows up when converting `attributes.js``attributes.ts`. See [CONVERTING_ATTRIBUTES.md](CONVERTING_ATTRIBUTES.md) step 4.
7975

80-
## Performance
81-
82-
For a codebase of ~750 source files:
83-
84-
| Operation | Time |
85-
|---|---|
86-
| `tsc --noEmit` cold | ~2-5s |
87-
| `tsc --noEmit --watch` incremental | ~100ms |
88-
| esbuild full bundle | ~450ms |

tasks/generate_schema_types.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ let META_KEYS = new Set();
2828
// ---------------------------------------------------------------------------
2929
// Common enum types — discovered from the schema at generation time and
3030
// emitted as named type aliases in the output. Each anchor specifies a
31-
// PascalCase type name and a `match(key, path, values)` predicate. The first
32-
// enumerated attribute whose match returns true defines the alias's values.
31+
// PascalCase type name and a `match(key, path, values)` predicate. If multiple
32+
// enumerated attributes match the predicate, the one with the largest set of values
33+
// is chosen.
3334
// ---------------------------------------------------------------------------
3435

3536
const COMMON_TYPE_ANCHORS = [

tasks/schema.mjs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,8 @@ await build(localDevConfig);
7272
// output plot-schema JSON
7373
makeSchema(pathToPlotly, pathToSchema);
7474

75-
// generate TypeScript types from the schema — traces, layout, common enum
76-
// aliases, animation/frame/config interfaces, and an `_internal` namespace.
77-
//
78-
// New schema-derived types automatically reach `plotly.js` consumers because
79-
// `lib/index.d.ts` uses `export type * from '../src/types/generated/schema'`.
80-
// If you ever swap that wildcard for an explicit allowlist, restore the
81-
// per-name re-export verifier that lived here (see git history) — otherwise
82-
// new types will silently fail to surface in the public API.
75+
// generate TypeScript types from the schema
76+
// and write to `src/types/generated/schema.d.ts`
8377
const schema = JSON.parse(fs.readFileSync(pathToSchema, 'utf-8'));
8478
const pathToGeneratedTypes = path.join(constants.pathToSrc, 'types/generated/schema.d.ts');
8579
generateSchemaTypes(schema, pathToGeneratedTypes);

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"outDir": "./dist",
2727
"noEmit": true,
2828

29-
// Type checking - start strict, loosen if necessary
3029
"strict": true,
3130

3231
// Linting

0 commit comments

Comments
 (0)