Skip to content

refactor: convert the legend plugin to TypeScript - #12537

Merged
kurkle merged 2 commits into
masterfrom
rework-legend-p19-legend-ts
Nov 27, 2026
Merged

kurkle merged 2 commits into
masterfrom
rework-legend-p19-legend-ts

Conversation

@kurkle

@kurkle kurkle commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

The legend plugin was one of the last JavaScript plugins. Every later change to the legend gets type-checked if the plugin is written in TypeScript first. This PR converts it without changing behaviour.

What changed

  • src/plugins/plugin.legend.js is now src/plugins/plugin.legend.ts. The rename is in the same commit as the annotations, so git records it as a rename. Imports keep the .js extension.
  • The class fields are declared, and the methods and the helper functions have typed signatures. The internal shapes have types in the file: a hit box (LegendHitBox), a column size, and the options as the plugin reads them once they are resolved (LegendOpts: fonts are objects, a title can have several lines). The public types (LegendItem, LegendOptions, LegendElement) stay in src/types/index.d.ts. The plugin object's hooks are left untyped.
  • src/core/core.layouts.js (JSDoc only): Legend implements LayoutItem from src/types/layout.d.ts, so its position is a LayoutPosition. The file's own JSDoc typedef of LayoutItem said position: string, so layouts.configure and layouts.addBox rejected the legend. The local typedef is replaced by @typedef {import('../types/layout.js').LayoutItem} LayoutItem, which leaves the public interface as the only description of a layout item.
  • buildLabels() no longer falls back to {} when options.labels is missing. The defaults always supply it, and the other methods already read it without a fallback.
  • The default onClick's unused e parameter is renamed _e (noUnusedParameters).
  • restoreTextDirection(ctx, opts.textDirection) gets a @ts-expect-error. The helper expects the [value, priority] pair that overrideTextDirection saved, and the legend passes the direction string. That mismatch is older than this PR and the call is left as it is. The tooltip makes the same call.
  • etc/chart.js.api.md is regenerated. Only the declaration form changes:
    • From a .js file, tsc emitted the plugin object (plugins.Legend) as namespaces. It is now a const of an object type, with the same members and the same any types.
    • The @private Legend class, exposed as _element, now appears in the report as Legend_2 with its typed members. The internal types it refers to (LegendOpts, LegendHitBox, ...) appear with it.

Evidence

On master, the plain rename alone gives:

  • tsc --noEmit: 127 errors in src/plugins/plugin.legend.ts: 124 × TS2339 (fields not declared) and 3 × TS2345 (clipArea(ctx, this), and twice Legend not assignable to LayoutItem).
  • pnpm lint-ts-flags: 128 diagnostic(s) in src/**/*.ts with the transition flags. (the same 127 plus TS6133 for the unused e).

With this PR:

  • tsc --noEmit: 0 errors.
  • pnpm lint-ts-flags: exits 0.
  • ESLint on src/**/*.{js,ts}: no errors. The file has one warning, adjustHitBoxes max-statements (33), which it also has on master.
  • test/specs/plugin.legend.spec.js and the plugin.legend image fixtures pass unchanged in Chromium and Firefox (136 tests).
  • dist/chart.js built before and after: with comments ignored, the legend code differs only by the bare class field declarations (_added; legendHitBoxes; _hoveredItem; ..., in constructor order), onClick(_e, ...) and the dropped || {} in buildLabels(). The constructor still assigns every field. Rollup's cleanup step strips comments only from .js sources, so the legend's comments now stay in the unminified bundle, as they already do for the other .ts modules.
  • pnpm lint-types: the chart.js report matches the regenerated etc/chart.js.api.md. The command still fails on master's stale etc/chart.js-helpers.api.md (_isClickEvent), which test: record the events parameter of _isClickEvent in the helpers API #12532 fixes. This PR does not touch that report.

Try it

pnpm install
pnpm exec tsc --noEmit
pnpm lint-ts-flags
pnpm exec vitest run --browser.headless test/specs/plugin.legend.spec.js test/specs/fixtures.spec.js -t "plugin.legend|Legend"

Open PRs that touch the legend

#11825 and #12242 edit plugin.legend.js. Please rebase them onto this PR once it lands. The code is the same, only now in plugin.legend.ts.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Size Change: +3.85 kB (+1.05%)

Total Size: 370 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 84.1 kB +1.86 kB (+2.26%)
dist/chart.js 84.4 kB +1.84 kB (+2.23%)
dist/chart.umd.js 73 kB +74 B (+0.1%)
dist/chart.umd.min.js 73 kB +74 B (+0.1%)
ℹ️ View Unchanged
Filename Size
dist/chunks/helpers.dataset.cjs 26.4 kB
dist/chunks/helpers.dataset.js 26.2 kB
dist/helpers.cjs 1.52 kB
dist/helpers.js 1.33 kB

compressed-size-action

@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit a751db9
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b08a08adc4586e00deb5a5a
😎 Deploy Preview https://deploy-preview-12537--chartjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kurkle
kurkle force-pushed the rework-legend-p19-legend-ts branch from 8e288f7 to 4a58924 Compare November 26, 2026 21:08
Rename src/plugins/plugin.legend.js to plugin.legend.ts and type it:
the class fields are declared, the methods and helper functions have
typed signatures, and the internal shapes (a hit box, a column size and
the options as the plugin reads them once resolved) have types in the
file. The public types stay in src/types/index.d.ts. The plugin object's
hooks are left untyped.

No behaviour changes. The emitted JavaScript differs only by the bare
class field declarations and the default onClick's unused `e`, now `_e`.

The layout item typedef in core.layouts.js typed `position` as a
string; it is a LayoutPosition, which also allows the object form.

etc/chart.js.api.md is regenerated: the legend plugin object is now
declared as a const of an object type rather than as namespaces, with
the same members, and the private Legend class it exposes as
`_element` (Legend_2) is now recorded with its internal types.
core.layouts.js kept its own JSDoc description of LayoutItem next to the
public interface in src/types/layout.d.ts. It now imports that interface,
so there is one source for the shape.

buildLabels() read `this.options.labels || {}`. The rest of the class
reads `options.labels` unguarded, and the defaults always supply it, so
the fallback (and the cast it needed) is gone.

The P19 row of the rework-legend design lists core.layouts.js.
@kurkle
kurkle force-pushed the rework-legend-p19-legend-ts branch from 4a58924 to a751db9 Compare November 26, 2026 21:11
@kurkle
kurkle marked this pull request as ready for review November 26, 2026 21:16
@kurkle
kurkle merged commit 8a8af09 into master Nov 27, 2026
10 checks passed
@kurkle
kurkle deleted the rework-legend-p19-legend-ts branch November 27, 2026 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants