An open source design system that's fully customizable and agent ready
TypeScript
docs(CodeBlock): the record states startLineNumber's file coordinates (#9934)
Cindy Zhang2951a3ac323b
diff --git a/docs/specs/components/CodeBlock/CodeBlock.spec.md b/docs/specs/components/CodeBlock/CodeBlock.spec.mdindex b7c0992e58..2b30c83bd1 100644--- a/docs/specs/components/CodeBlock/CodeBlock.spec.md+++ b/docs/specs/components/CodeBlock/CodeBlock.spec.md@@ -13,6 +13,7 @@ review_triggers: [public-api, behavior, layout, theming, accessibility, testing]verified_by:[packages/core/src/CodeBlock/CodeBlock.test.tsx,+ packages/core/src/CodeBlock/CodeBlock.startLineNumber.test.tsx,packages/core/src/CodeBlock/highlightRanges.test.ts,packages/core/src/CodeBlock/tokenizer.test.ts,packages/core/src/theme/themingTargets.test.ts,@@ -63,6 +64,7 @@ Present read-only, standalone or multi-line code with optional syntax, navigatio- Visible default change (FR11), not breaking: a block with no header shows its controls in their own column beside the code instead of over it, so its code ends that much earlier, a block sized to its content (`fit-content`) grows by the column's width, and a one-line block reads as one row. A block with a title or a visible language label keeps its header at every line count. Markdown fences with no language take it through the default code path (`component:Markdown`). No prop, type, or target migration follows from it.- Visible default change (FR12), not breaking: blocks and Markdown fences in the newly built-in languages are highlighted instead of plain, and the exported `tokenize`, `tokenizeAsync`, and `tokenizeStreaming` functions, with the RichText code blocks and editors that call them, return tokens for those identifiers; text, copy output, labels, and a custom `tokenizer` are unchanged, and the built-in tokenizer grows within the PR4 budget. Markdown fences reach these identifiers by their leading word characters (`component:Markdown` FR29): a `cpp` fence highlights as C++, while a `c++` fence reads as `c`.- Additive API (FR13): `language` also accepts a language module, each module has its own subpath under `@astryxdesign/core/CodeBlock/languages/`, and the exported `tokenize`, `tokenizeAsync`, and `tokenizeStreaming` accept a module wherever they accept a language identifier; nothing changes until a product imports a module and passes it.+- Additive API (FR14), current state, not owner-reviewed: `startLineNumber` sets the first line's number in the source file; its default of 1 keeps the released gutter and `highlightLines` meaning- Deprecation (FR13): the released `tokenizer` prop is deprecated and still works exactly as released; a staged codemod rewrites `tokenizer={fn}` beside a static `language="x"` other than `plaintext` to `language={createCodeLanguage({name: 'x', identifiers: ['x'], tokenize: fn})}`, which keeps `data-language="x"` and the `x` label, and reports every use it cannot prove (a dynamic or missing language, `plaintext`, a spread); the prop is removed only at a minor release.- Migration decision: deprecate `tokenizer` in favour of a custom language value; no change is required until its removal@@ -116,6 +118,7 @@ Consumer migration instructions belong in consumer docs and release notes.| FR11 | With no header, the corner cluster MUST occupy its own inline-end column beside the code viewport, as wide as its content and never over the viewport, so that no line, at rest, scrolled, wrapped, or in either direction, is painted under a control; the cluster MUST be centred on the first line of code. A headerless block whose code is one line — counted after removing one trailing newline — therefore reads as one row: code, then the controls, with even space above and below; the single-line mode a caller gets by leaving out the title and passing `hasLanguageLabel={false}`. When the code gains lines, the block grows downward and the controls keep their place. A visible language label or a title keeps the header at every line count (FR3). The column paints from the `code-block` root, so no theme target is added. | the Code Body and Copy Button design relationships; proposal | verify || FR12 | Without the deprecated `tokenizer`, given a built-in identifier, CodeBlock MUST paint syntax for these built-in identifiers and their aliases: `typescript`/`ts`, `javascript`/`js`, `tsx`, `jsx`; `json`; `html`, `xml`, `svg`; `css`, `scss`, `less`; `python`/`py`; `bash`, `sh`, `zsh`, `shell`; `php`; `hack`; `yaml`/`yml`; `markdown`/`md`; `c`, `h`; `cpp`, `c++`, `cc`, `cxx`, `hpp`, `hh`; `sql`; `rust`/`rs`; `kotlin`, `kt`, `kts`; `swift`; `go`/`golang`. Each built-in paints the comment, string, number, and keyword tokens its language has from the syntax theme. Any other identifier renders as plain code with its label unless the deprecated `tokenizer` paints it; an identifier string never resolves to a language value. | proposal | verify || FR13 | Each less common language MUST be its own module, one per subpath, each exporting one language value: `graphql` (`graphql`, `gql`), `java` (`java`), `thrift` (`thrift`), `objectiveC` (`objective-c`, `objc`, `m`, `mm`), `toml` (`toml`), `powershell` (`powershell`, `ps1`, `pwsh`), `ruby` (`ruby`, `rb`), `lua` (`lua`), and `dockerfile` (`dockerfile`, `docker`), at `@astryxdesign/core/CodeBlock/languages/<path>`, where `<path>` is the export's name in kebab case (`languages/graphql` exports `graphql`, `languages/objective-c` exports `objectiveC`). A language value is a `CodeLanguage` carrying everything needed to render that language: read-only `name` (the identifier its label shows, such as `graphql`: code text shown verbatim like a built-in identifier, never translated prose, so it needs no catalog entry), `identifiers`, `fileExtensions`, and `fileNames` (for example `graphql`, or `Dockerfile`), all required, and its grammar, which stays internal. `language` MUST accept a language value as well as an identifier string: given one, CodeBlock paints the code with that language, labels it with its `name`, and reflects its first identifier wherever it reflects the language, such as `data-language` on its theme targets (FR6), in the same render as a built-in would; a string selects only a built-in grammar (FR12). There is no provider, registry, or grouped export: each module is imported and passed on its own, and only modules a bundle imports reach it. A custom language is the same kind of value: `createCodeLanguage({name, identifiers, fileExtensions, fileNames, tokenize})`, exported from `@astryxdesign/core/CodeBlock`, makes one from a caller's `tokenize(code, language)` function returning `{type, start, end}` tokens, which runs synchronously; `fileExtensions` and `fileNames` default to none. It is passed and listed exactly like an Astryx module, and Astryx modules keep their grammar internal. In a `codeLanguages` list, a listed value takes precedence over a built-in with the same identifier, and a later entry over an earlier one. The released `tokenizer` prop is deprecated and still works exactly as released, taking precedence over the language's grammar; it is removed only at a minor release, and a codemod rewrites a block whose `language` is a static string literal other than `plaintext` to a custom language passed as `language`, whose first identifier is that string, and reports every other use it cannot rewrite. The exported `tokenize`, `tokenizeAsync`, and `tokenizeStreaming` MUST accept a language value wherever they accept a language identifier. A surface that sees only a fence's language word takes modules through its own prop: `Markdown`'s `codeLanguages` (`component:Markdown` FR30) and RichText's (`spec:AST-061`). Lab's `DiffViewer` takes a language value as its `language` the same way; without one it infers a file's language from the file's extension or name among the built-ins and the values in its own `codeLanguages`, and an unmatched file stays plain. It tokenizes each hunk's old and new side as continuous text with whichever grammar applies, and its line-number gutters and change marks are unaffected. Modules are opaque values: CodeBlock exposes no grammar format. The module is the portable form of a language (`spec:AST-082/FR3`): a platform CodeBlock takes the same kind of value for its language. | proposal | verify |+| FR14 | Current state, not owner-reviewed. `startLineNumber` (default 1) MUST set the number of the first line in the source file. The gutter MUST show file coordinates (`startLineNumber + index`), so an excerpt reads with its real line numbers. A value below 1 MUST clamp to 1. `highlightLines` MUST use the same file coordinates, and an out-of-range entry MUST render no highlight. The offset MUST NOT change the displayed or copied source text (FR1). | released public type and JSDoc; shipped API | verify |### Allowed variation@@ -203,6 +206,7 @@ Exact CodeBlock proportions and syntax colors are not contract requirements; the| FR9 | `tokenizer.test.ts` (diff line classes; file-header pair rule; base-language tokens past the glyph); `CodeBlock.test.tsx` (row attributes, marker column, copy string incl. glyphs, `highlightLines`, header kept with the `diff` / `tsx diff` label and viewport name); syntax-theme tests (five slot defaults and overrides; a preset without marker or header slots takes punctuation text and no header paint); exact-head browser evidence incl. every default syntax token colour and the row marker slot (line number and glyph) measured ≥ 4.5:1 on painted rows in light and dark against the code block's own `--color-syntax-background` on the default card surface (the two known shortfalls in FR9 excepted) (on the light page background the surface is translucent and a plain row's default keyword already sits under the floor — a pre-existing shortfall that affects every CodeBlock there, tracked separately), and equal code-column x-offsets for context, added, and removed lines | added/removed/header/context over `tsx` and over plaintext; a removed line starting with `--`; `---i;` / `+++i;` right before an `@@` (removed/added, not header); a `--- `/`+++ ` pair with no `@@` after it (removed/added); a `--- `/`+++ `/`@@` file header after an earlier hunk (header); the layer over plaintext (label and viewport name `diff`) and over `tsx` (`tsx diff`); a context line with no leading space; a line longer than the viewport, scrolled (tint to the full scroll width) and wrapped; with `highlightLines` on an added line and on a header line (highlight paint only, text measured on it); with line numbers (gutter cell tinted, number in the row's marker slot); plain and token-painted text on a changed row keeping their syntax colours; light and dark (contrast measured on each); a custom syntax theme overriding the slots; indented code under all three line kinds; RTL (gutter mirrored, code direction unchanged) | A glyph removed from display or copy, a changed line's code starting at a different offset than its neighbours, a header row painted as a change or hidden, a header's text or paint taken from the page theme under a defined syntax theme, a `--`-prefixed removed line painted as a header, syntax paint lost past the glyph, a tint from anywhere but the three row slots, a gutter cell left untinted, a line number or glyph left in the ordinary colour on a changed row, code text recoloured by the row, a tint that stops at the viewport edge of a scrolled line, the header, collapse control, or viewport name lost on a diff, a `---i;` line painted as a header, or a default token or marker on a painted row under 4.5:1 on the default surface, or an override on which a token that clears 4.5:1 on the theme's own background falls under it or an already-short token drops further | `audit:CodeBlock/a11y` || FR10 | `CodeBlock.test.tsx` (before Copy in the header and in the corner cluster, outside the disclosure control, pointer and Enter/Space inside it do not toggle, no header created, kept in place without Copy); exact-head browser evidence | header and headerless, with and without Copy, collapsible, LTR and RTL | A caller control toggles collapse, sits inside the disclosure or after Copy, creates a header, or is renamed or restyled by CodeBlock. | `audit:CodeBlock/behavior` || FR11 | real-browser geometry evidence over built stories | a long first line at rest, scrolled to its end, and wrapped; a one-line headerless block with and without caller controls; a one-line block with a visible label (keeps its header); one line growing to several; both sizes; LTR and RTL; light and dark | Any control box overlaps the code viewport, the cluster leaves the first line's centre, a one-line headerless block's space above and below differs, a block with a visible label or title loses its header, or the controls move when lines are added. | `audit:CodeBlock/behavior` |+| FR14 | Current state, not owner-reviewed. `CodeBlock.startLineNumber.test.tsx` (gutter from 1 by default; file coordinates from `startLineNumber`; `highlightLines` in file coordinates; a value below 1 clamps to 1; out-of-range highlights render nothing; copy stays exact) | default; a three-line excerpt starting at line 40; a value of 0; a highlight inside the excerpt (42) and entries outside it (1, 2, 39, 43, 100) | The gutter starts at 1 when `startLineNumber` is set, a highlight lands on the excerpt index instead of the file line, a value below 1 shows a number below 1, or copy includes gutter numbers. | `audit:CodeBlock/behavior` || RTL | `rtl:audit -- --filter CodeBlock` D2 target plus exact-head browser evidence | title, caller controls, and copy order, and the corner cluster at the inline end, in LTR/RTL | Logical order fails to mirror or physical placement leaks. | `audit:CodeBlock/rtl` || Consumer metadata | CLI `component CodeBlock --json`; docs typecheck; prop/target validation; package-export checks | released props/defaults/ref and current/deprecated targets | A released prop/default/ref/target disappears from consumer discovery or docs name an unsupported value. | `audit:CodeBlock/docs` |