Repository navigation
oj dev: source tags for an editor, on the AST the compile parses - #826
Open
raphamorim wants to merge 4 commits into
Open
raphamorim wants to merge 4 commits into
raphamorim wants to merge 4 commits into
Conversation
In a development serve every JSX element of an app module gets `data-tsd-source="file:line:col"`, the attribute TanStack Devtools' Vite plugin writes, so an editor maps a rendered element back to its source. Asked for by `oj dev --source-tags`, or by that plugin in the config, which the plugin host then leaves out. The tag goes into the AST oj's compile parses anyway, before the JSX transform: no second parse, no splice, no map to compose, no host round trip. A module another transform reads first (StyleX's reprint, a hosted plugin's transform, a Start app's server-function rewrite, the Start client bundle's plugin chain) is tagged in its text ahead of it, as the plugin runs ahead of every other transform, so the server-rendered page and the hydrated client carry the same tags. The plugin's rules are kept, its own test vectors ported: JSX inside a function body only, functions as boundaries, no Fragment, no element already tagged or spreading the enclosing function's props, nothing under node_modules, dist or build, the file from the root, UTF-16 columns. Whether it leaves react-three-fiber files alone is asked of the instance; its other ignore patterns are not read. A production-mode serve tags nothing, and oj itself tags nothing in a build.
Brumor
approved these changes
Dec 3, 2026
…rver, the flag in any mode Review findings on the pull request. `tag_text` returns the insertions and builds its source map from them, so every column after an insertion keeps its own (the line map lost them), and a self-closing element is tagged before its `/` whatever the spacing. The SSR pipeline tags a module a plugin loads under a path of the app, as the client does, and no longer skips a module whose text happens to contain the attribute's name; the Start host says it tagged already. `--source-tags` tags in any mode (the plugin in a config only in development, as its `apply` says). oj's own files under its cache (a Start app's client entry) are never tagged. Config extraction's probe file has a name of its own per run. A test for each.
…'s edge rules, linear positions Two more correctness reviews and two performance reviews of the pull request. The text form's map carries a token per word with its exact original column: the composer takes the nearest token's column as is, so the sparse map collapsed every position after an insertion. The client tags a module the CJS interop rewrites in its text first, at the source's columns (the rewrite moves text on a line; the server saw the source). The plugin's edge rules: a TypeScript `this` parameter is the first parameter, a byte order mark is no column, a comment before a self-closing element's `/` is passed over. Once a text pass ran, the compile's own pass is off (client, SSR, Start bundle), as the plugin runs once. Positions are counted on from the last one on the line and a map is walked once (a line of 2,000 elements: 1.6 ms, from 70 ms to tag and 430 ms to map); files that cannot carry JSX are not parsed; the pre-tag runs off the async workers; the Start loader tags in text only where a rewrite or a hosted transform reads the text; the dev module store's key carries the setting; the SSR hook plan is not evaluated for it with the feature off. The flag's help says any mode; the extraction probe says when it falls back. Tests: compiler 21, dev 11, Start parity on every tag, JS unit for the probe.
…ints it The parity tests compare the reader's output with the engine's, which carries sourceTags since the source tags change; the reader reads no plugin of that kind.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Source tags: every JSX element of an app module a serve compiles gets a
data-tsd-source="file:line:col"attribute, the attribute TanStack Devtools' Vite plugin (@tanstack/devtools-vite,injectSource;addSourceToJsxin@tanstack/devtools-bundler-core) writes, so an editor or a devtools panel can map a rendered element back to the element in its source. Two ways in:oj dev --source-tags, for any app, in any mode.Why in oj's compiler
The plugin parses every app module with oxc-parser in JavaScript, splices the attribute in with magic-string, hands Vite a source map to compose, and the React plugin parses the result again. Under oj it ran in the plugin host, so every TanStack Start module paid a host round trip for it. oj already parses each module with oxc for its JSX transform; the tag goes into that AST before the transform (
oj_compiler::source_tags::apply): no second parse, no splice, no map to compose, no host.Where another transform reads the module before oj's compile, the tag goes into the text first instead (
source_tags::tag_text, a splice with a source map that keeps every column: a token per word, as oj's map composer takes the nearest token's column as is), as the plugin runs ahead of every other transform: StyleX's reprint, a hosted plugin's transform (after which the host strips the JSX in Vite'svite:oxcslot), the CJS interop rewrite (which moves text on a line), a Start app's server-function and env rewrites, and the Start client bundle's plugin chain (a first-in-line rolldown plugin). The compile's own pass is then off for that module, as the plugin runs once, first. A Start server module nothing rewrites and no plugin claims keeps the AST pass: one parse. Server-rendered page and hydrated client carry the same tags; a test asserts it on a Start app with and without React Refresh.Rules
The plugin's, kept exactly, with its own test vectors ported:
FragmentorReact.Fragment, an element already tagged, or one that spreads the enclosing function's props ((props) => <div {...props} />, or the rest of a destructured first parameter), so the caller's tag is the one on the rendered node;filefrom the root with a leading slash (the plugin strips the cwd, the root under an editor), line and column 1-based in UTF-16 units;thisparameter is the first parameter, as the plugin reads it; a byte order mark is no column of the first line (Vite strips it before the plugin runs); a comment between a self-closing element's/and>is passed over;node_modules,distorbuild, nor under oj's own cache (a Start app's generated client entry); a source file a pluginloads is tagged on the server as on the client, a virtual module or a generated component (an svg's, an MDX page's: the plugin sees raw SVG or MDX there and bails) is not; oj itself tags nothing in a build (a development-mode build keeps the plugin hosted, which tags as under Vite).What the plugin ignores beyond that is asked of the instance where it can be: config extraction writes a file that uses react-three-fiber beside its script (a name of its own per extraction) and runs the plugin's transform on it (an editor's matcher reads the file at the id), and oj keeps that answer. Its other
ignorepatterns (component names, path globs) are not read; the docs say so. An instance of another shape (an async transform) stays hosted.The persistent Start bundle store is keyed by the setting, so a bundle built tagged is never restored for a serve without the flag, nor the reverse.
Cost
Off: no extra work on any path (a
Nonecheck per module). On, the AST pass adds about a fifth of a parse to a module's compile (0.2 ms on a 90 KB component file); the text form adds one parse and a map (0.9 ms and 1 ms on that file). Positions are counted linearly (a line of 2,000 elements: 1.6 ms, where a first version took 70 ms to tag and 430 ms to map). Files that cannot carry JSX (.ts, a text with no<) are not parsed for it. The dev module store's key carries the setting. Measured on a Start fixture with the flag: no difference in boot, first render or cold client bundle.Left for a follow-up: a sparse tags map once the composer adds the column delta from the nearest token (the per-word map is 2 to 3 times the source); handing oj's own pre-maps to the composer as values rather than JSON; the Start client bundle's first plugin skipping a module no hosted plugin claims (the refresh transform would tag it on the AST).
Tests
oj_compiler::source_tagsunit tests (21): the plugin's own vectors (nested functions, props spreads in every form, fragments, non-ASCII positions, nested boundaries) plus the first-parameter rule ({...rest} = {},this.props, a default's JSX, a spread of a member or of another name, a TypeScriptthisparameter, a nested function's own props), every kind of function, TypeScript syntax around an element, module-level JSX, JSX in attributes, members and namespaces, already-tagged elements, the tag as the last attribute after a spread, CRLF, a byte order mark, the file filter, the react-three rule, the cache exclusion, a file that does not parse or has no JSX, the text form (a self-closing element with any spacing or comment before its/), the text form's map by lookup (before, inside, between and after insertions, across an emoji, on an untouched line, and as oxc's own value), a file outside the root and a renamed attribute.engine_extraction_tests: the plugin is asked what it ignores (a stand-in that reads the probe file from disk, as the known matcher does) and counts as native in a development serve; a build, a production-mode serve, and an async transform keep it hosted.vite_values_tests: the fact reaches the config.ssr::source_tags_tests: which ids the server tags.e2e/unit/native-plugins-source-tags.test.mjs: how an instance of the plugin is known (name and a synchronous probe), what stays hosted (another name, no or async transform, a transform that leaves the probe, writes another attribute or throws), the probe memoised per instance, a promise answer settled.tests/dev/source_tags(11): the flag tags a served module at the right columns, a TSX module too, in production mode too, where the plugin in the config does not apply; the plugin in the config is done by oj with no host, with oj's locations and the react-three ignore; an edit that moves an element moves its tag; a module a hosted plugin transforms is tagged first, at the original positions; a StyleX module is tagged at its source positions, ahead of the reprint; a module a pluginloads under its path is tagged on the server as on the client; a module the CJS interop rewrites is tagged at the source's columns on both; the served map of a module tagged in its text, through a hosted transform and the compile, points every position at its own column; and, in a browser, every element the page renders resolves to the<of its element in the source file, the caller's element for a component that spreads its props. That last one is the guarantee an editor's element-to-source mapping relies on.crates/oj/tests/start_source_tags: a TanStack Start page's server-rendered HTML and its dev client bundle tag every element the page carries the same, with React Refresh and without it (an editor's HMR gate), and neither page nor bundle is tagged without the flag.Eight independent reviews of the change (six for correctness, two for performance) found the hosted-transform ordering, the StyleX ordering, the default-parameter rule, the synthetic paths, the probe needing a real file, the text form's map and self-closing spacing, plugin-loaded files on the server, the flag's mode gating, oj's own Start client entry getting tagged, the probe file's shared name, the sparse map under the composer's nearest-token lookup, the interop rewrite moving a line's text under the client's AST pass, the TypeScript
thisparameter, the byte order mark, a comment before a self-closing/, quadratic position counting on long lines, parses of files with no JSX, a second pass after a text pass, and the SSR hook plan evaluated with the feature off; all fixed here with tests.