diff --git a/.changeset/compiler-joined-statement-warning.md b/.changeset/compiler-joined-statement-warning.md
new file mode 100644
index 0000000000..cb89b52104
--- /dev/null
+++ b/.changeset/compiler-joined-statement-warning.md
@@ -0,0 +1,5 @@
+---
+'@tanstack/router-utils': patch
+---
+
+A binding only split code uses that stays in the route module because a statement calling a known method on a binding the route module keeps uses it (`table.add(d)` on a `Set`, `list.push(d)`, `map.set(k, d)`) is now reported once, as the same statement with an unknown method already was.
diff --git a/.changeset/compiler-reads-warning-move.md b/.changeset/compiler-reads-warning-move.md
new file mode 100644
index 0000000000..8b3ca25c3b
--- /dev/null
+++ b/.changeset/compiler-reads-warning-move.md
@@ -0,0 +1,5 @@
+---
+'@tanstack/router-utils': patch
+---
+
+The warning for an export that stays in the route module because it reads, when it loads, a binding the route module keeps offers a move that works: it names what the export and that binding read when the module loads (a binding that one reads, a holder, a function it calls) and the code writing them then; when that move would need the route itself, the warning says why it stays instead.
diff --git a/.changeset/compiler-written-warning-move.md b/.changeset/compiler-written-warning-move.md
new file mode 100644
index 0000000000..b74eccb341
--- /dev/null
+++ b/.changeset/compiler-written-warning-move.md
@@ -0,0 +1,5 @@
+---
+'@tanstack/router-utils': patch
+---
+
+The warning for a binding a statement that stays in the route module changes or uses offers a move that works: it names what the moved bindings read when the module loads and the statements writing them then; when that move would need the route itself, the warning says why it stays instead.
diff --git a/packages/router-plugin/tests/integration/code-splitter-kept-cause.test.ts b/packages/router-plugin/tests/integration/code-splitter-kept-cause.test.ts
index e51926d94b..8d23c4dd52 100644
--- a/packages/router-plugin/tests/integration/code-splitter-kept-cause.test.ts
+++ b/packages/router-plugin/tests/integration/code-splitter-kept-cause.test.ts
@@ -9,8 +9,14 @@
* offers names what the moved code reads when the module loads (a holder, a
* function it calls and what that reads) and what reads a moved binding
* before a moved write (round tools class A): what stays would otherwise be
- * imported back from the route module, a cycle. Vite and Rspack client
- * builds, mounted in a DOM.
+ * imported back from the route module, a cycle. So does the move offered for
+ * an export that stays because it reads what the route module keeps (a
+ * binding that one reads, a holder, a function it calls, a write at load),
+ * and the move offered for a binding a statement that stays changes or uses
+ * (what the binding reads at load; the code writing a moved binding then).
+ * A statement a known method makes a writer of a kept binding (`Set.add`)
+ * keeps a binding it uses with one warning, as an unknown call does.
+ * Vite and Rspack client builds, mounted in a DOM.
*/
import { describe, expect, test } from 'vitest'
import {
@@ -329,4 +335,304 @@ o0.v = 5
},
timeout,
)
+
+ // an export only split code uses reads, when it loads, `kept`, which the
+ // loader keeps in the route module: the move takes what the two read
+ // then, and the statements writing them (left behind, the new module
+ // would import it back from the route module)
+ test.each([
+ [
+ 'a binding the read one reads',
+ `const cfg = { k: 2 }
+const kept = { n: cfg.k }
+export const count = kept.n
+`,
+ 'kept',
+ 'count = kept.n',
+ '2',
+ `const cfg = { k: 2 }
+export const kept = { n: cfg.k }
+export const count = kept.n
+`,
+ ],
+ [
+ 'a holder',
+ `const o0 = { v: 1 }
+const h6 = { c: o0 }
+export const count = h6.c.v
+`,
+ 'o0',
+ 'count = h6.c.v',
+ '1',
+ `export const o0 = { v: 1 }
+const h6 = { c: o0 }
+export const count = h6.c.v
+`,
+ ],
+ [
+ 'a function it calls',
+ `const unit = 2
+const kept = { n: 1 }
+function total() {
+ return kept.n * unit
+}
+export const count = total()
+`,
+ 'kept',
+ 'count = total()',
+ '2',
+ `const unit = 2
+export const kept = { n: 1 }
+function total() {
+ return kept.n * unit
+}
+export const count = total()
+`,
+ ],
+ [
+ 'a write when the module loads',
+ `const kept = { n: 1 }
+kept.n = 3
+export const count = kept.n
+`,
+ 'kept',
+ 'count = kept.n',
+ '3',
+ `export const kept = { n: 1 }
+kept.n = 3
+export const count = kept.n
+`,
+ ],
+ ])(
+ 'warns once at an export reading what the route module keeps through %s, and its fix moves what they read when the module loads',
+ async (_, declarations, loaded, at, value, own) => {
+ const options = (
+ imports: string,
+ ) => `${head}${imports}export const Route = createFileRoute('/')({
+ loader: () => ${loaded},
+ component: () =>
{String(count)}
,
+})
+`
+ const route = options(declarations)
+ const r = await mounted(bundler, route)
+ expect(r.text).toBe(value)
+ expectDiagnostic(r.diagnostics, {
+ severity: 'warning',
+ position: diagnosticPosition(route, at),
+ times: 1,
+ })
+ expect(r.diagnostics).toHaveLength(1)
+ // the fix: the export, the binding it reads and what the warning
+ // names with them in a module of their own
+ const fixed = await mounted(
+ bundler,
+ options(`import { count, ${loaded} } from '../own'
+export { count }
+`),
+ { 'own.ts': own },
+ )
+ expect(fixed.text).toBe(value)
+ expect(fixed.diagnostics).toEqual([])
+ },
+ timeout,
+ )
+
+ // an export reading `Route` when it loads: no module of its own can take
+ // it (it would import `Route` back), so the warning offers no move
+ test(
+ 'warns once at an export reading `Route` when the module loads',
+ async () => {
+ const route = `${head}export const Route = createFileRoute('/')({
+ component: () => {label}
,
+})
+export const label = typeof Route.options.component
+`
+ const r = await mounted(bundler, route)
+ expect(r.text).toBe('function')
+ expectDiagnostic(r.diagnostics, {
+ severity: 'warning',
+ position: diagnosticPosition(route, 'label = typeof'),
+ times: 1,
+ })
+ expect(r.diagnostics).toHaveLength(1)
+ },
+ timeout,
+ )
+
+ // `flags` stays with the statement using it, which code that runs later
+ // changes too: the move takes `prefix`, which `flags` reads when the
+ // module loads (the route module imports the new module to re-export
+ // `flags`)
+ test(
+ 'warns once at an export a statement that stays uses, and its fix moves what the export reads when the module loads',
+ async () => {
+ const options = `export const Route = createFileRoute('/')({
+ component: () => {
+ flags.base = 1
+ return {flags.p} {String(context.flags === flags)}
+ },
+})
+`
+ const route = `${head}import { context, setContext } from '../context'
+const prefix = { p: 'x' }
+export const flags: { beta: boolean; p: string; base?: number } = {
+ beta: false,
+ p: prefix.p,
+}
+setContext('flags', flags)
+${options}`
+ const files = {
+ 'context.ts': `export const context: Record = {}
+export function setContext(key: string, value: unknown) {
+ context[key] = value
+}
+`,
+ }
+ const r = await mounted(bundler, route, files)
+ expect(r.text).toBe('x true')
+ expectDiagnostic(r.diagnostics, {
+ severity: 'warning',
+ position: diagnosticPosition(route, 'flags: {'),
+ times: 1,
+ })
+ expect(r.diagnostics).toHaveLength(1)
+ // the fix: `flags`, `prefix` and the statements using them when the
+ // module loads in a module of their own
+ const fixed = await mounted(
+ bundler,
+ `${head}import { context } from '../context'
+import { flags } from '../flags'
+export { flags }
+${options}`,
+ {
+ ...files,
+ 'flags.ts': `import { setContext } from './context'
+const prefix = { p: 'x' }
+export const flags: { beta: boolean; p: string; base?: number } = {
+ beta: false,
+ p: prefix.p,
+}
+setContext('flags', flags)
+`,
+ },
+ )
+ expect(fixed.text).toBe('x true')
+ expect(fixed.diagnostics).toEqual([])
+ },
+ timeout,
+ )
+
+ // round tools class C (fuzz seeds 6104927, 6407720, 6510105, 6518119): the
+ // statement on line 7 uses `Title`, an export, and `count`, which the
+ // chunk takes with its write at load: no module serves both, so both
+ // stay, reported at each. The move takes the statement writing `count`
+ // when the module loads (left behind, it would assign an import)
+ test(
+ 'warns at each binding a statement that stays uses, and their fix moves the statements writing them when the module loads',
+ async () => {
+ const rest = `async function load(x: unknown) {
+ return format(x, count)
+}
+const Header = () => [format('H'), Title()]
+export const Route = createFileRoute('/')({
+ component: () => {format(Header, load)} {count}
,
+})
+`
+ const route = `${head}import { format, log } from '../lib'
+export function Title() { return format('T') }
+let count = format('C')
+count = log('T2', count)
+${rest.replace('const Header', "log('T5', count, Title)\nconst Header")}`
+ const files = {
+ 'lib.ts': `export function format(...a: Array) {
+ return a.length
+}
+export function log(_tag: string, v: unknown, ..._rest: Array) {
+ return typeof v === 'number' ? v + 1 : v
+}
+`,
+ }
+ const r = await mounted(bundler, route, files)
+ expect(r.text).toBe('2 2')
+ for (const at of ['export function Title', 'count = format']) {
+ expectDiagnostic(r.diagnostics, {
+ severity: 'warning',
+ position: diagnosticPosition(route, at),
+ times: 1,
+ })
+ }
+ expect(r.diagnostics).toHaveLength(2)
+ // the fix: `Title`, `count`, that statement and the statements
+ // writing them when the module loads in a module of their own
+ const fixed = await mounted(
+ bundler,
+ `${head}import { format } from '../lib'
+import { Title, count } from '../own'
+export { Title }
+${rest}`,
+ {
+ ...files,
+ 'own.ts': `import { format, log } from './lib'
+export function Title() { return format('T') }
+export let count = format('C')
+count = log('T2', count)
+log('T5', count, Title)
+`,
+ },
+ )
+ expect(fixed.text).toBe('2 2')
+ expect(fixed.diagnostics).toEqual([])
+ },
+ timeout,
+ )
+
+ // phase 6 step 3 (P04b written with the bindings file's `init()`):
+ // `table.add(d)` changes `table`, which `beforeLoad` keeps, so it stays,
+ // and so does `d`, which only the component reads: one warning, as with
+ // `table.link(d)`
+ test(
+ 'warns once at a binding a known method call on a kept binding uses, and its fix splits it',
+ async () => {
+ const files = {
+ 'state.ts': `export const init = (value: T): T => value
+`,
+ }
+ const route = `${head}import { init } from '../state'
+const table = init(new Set())
+const d = init({ name: 'MARK_D' })
+table.add(d)
+export const Route = createFileRoute('/')({
+ beforeLoad: () => table.size,
+ component: () => {d.name}
,
+})
+`
+ const r = await mounted(bundler, route, files)
+ expect(r.text).toBe('MARK_D')
+ expectDiagnostic(r.diagnostics, {
+ severity: 'warning',
+ position: diagnosticPosition(route, 'd = init'),
+ times: 1,
+ })
+ expect(r.diagnostics).toHaveLength(1)
+ // the fix: that statement in the code that needs `d`
+ const fixed = await mounted(
+ bundler,
+ `${head}import { init } from '../state'
+const table = init(new Set())
+const d = init({ name: 'MARK_D' })
+export const Route = createFileRoute('/')({
+ beforeLoad: () => table.size,
+ component: () => {
+ table.add(d)
+ return {d.name}
+ },
+})
+`,
+ files,
+ )
+ expect(fixed.text).toBe('MARK_D')
+ expect(fixed.diagnostics).toEqual([])
+ },
+ timeout,
+ )
})
diff --git a/packages/router-utils/src/compiler/kept-diagnostics.ts b/packages/router-utils/src/compiler/kept-diagnostics.ts
index cfe2832087..7140512c8b 100644
--- a/packages/router-utils/src/compiler/kept-diagnostics.ts
+++ b/packages/router-utils/src/compiler/kept-diagnostics.ts
@@ -43,7 +43,7 @@ export function keptDiagnostic(
items.length === 1
? items[0]!
: `${items.slice(0, -1).join(', ')} and ${items.at(-1)!}`
- /** A `state` move (`k.moves`): `head` (the declaration, `read`) and what moves with them. */
+ /** A `state`, `reads` or `written` move (`k.moves`): `head` (the declaration, `read`) and what moves with them. */
const moving = (head: ReadonlyArray) => [
...head,
...(k.moves?.syms ?? []).map((s) => `\`${symbols.name(s)}\``),
@@ -74,7 +74,7 @@ export function keptDiagnostic(
? `Use \`${symbols.name(k.read!)}\` inside the code that needs it, or move ${what}`
: `Move ${what}`
}
- /** Why no module of its own can take a `state` declaration (`k.stuck`). */
+ /** Why no module of its own can take the declaration a move would take (`k.stuck`). */
const stuck =
k.stuck === undefined
? undefined
@@ -123,7 +123,17 @@ export function keptDiagnostic(
} else {
const read = `\`${symbols.name(k.read)}\``
why = `it reads ${read} when it loads, which the ${module} keeps`
- fix = `Move it and ${read} to their own module and import them from there.`
+ // (what they read when the module loads moves with them, `moving`:
+ // left behind, the new module would import it back; none can:
+ // `stuck`)
+ fix =
+ stuck !== undefined
+ ? `${stuck[0]!.toUpperCase()}${stuck.slice(1)}.`
+ : k.moves === undefined
+ ? `Move it and ${read} to their own module and import them from there.`
+ : k.moves.written
+ ? `Move it, ${moving([read]).join(', ')} and the statements writing them when the module loads to their own module and import them from there.`
+ : `Move it, ${and(moving([read]))} to their own module and import them from there.`
}
break
case 'state': {
@@ -190,20 +200,48 @@ export function keptDiagnostic(
: k.read !== undefined
? `${stays}: it also ${k.readChanged === true ? 'changes' : 'uses'} ${read}, which ${k.in === undefined ? `the ${module} keeps` : `${plan.outputs.get(k.in)!.kind === 'export' ? '' : 'the split '}${output(k.in)} uses`}`
: stays
+ // (a move takes what the moved code reads when the module loads,
+ // `moving`, and the statements writing a moved binding then; none
+ // can: `stuck`)
+ const moves =
+ k.reader === undefined &&
+ (k.later !== undefined ||
+ k.read === undefined ||
+ (k.split !== true && k.in !== undefined))
+ if (moves && stuck !== undefined) {
+ return {
+ severity: 'warning',
+ message: `${tag}${subject} in ${file} stay${names.length === 1 ? 's' : ''} in the ${module} because ${because}; ${stuck}.`,
+ range: k.range,
+ by: 'core',
+ code: `eager-split:${k.reason}`,
+ }
+ }
+ /** The move of `head` and what moves with it, with that statement. */
+ const withStatement = (head: ReadonlyArray) => {
+ const moved = [...moving(head), 'that statement']
+ return k.moves?.written === true
+ ? `${moved.join(', ')} and the statements writing them when the module loads`
+ : and(moved)
+ }
+ // (the later writer may be a line of a component: what moves is
+ // what uses it when the module loads, recheck finding 6)
+ const later = [
+ ...names,
+ ...(k.moves?.syms ?? []).map((s) => `\`${symbols.name(s)}\``),
+ ]
fix =
- // (the later writer may be a line of a component: what moves is
- // what uses it when the module loads, recheck finding 6)
k.later !== undefined
- ? `move ${subject} and the statements using ${them} when the module loads to their own module`
+ ? `move ${later.join(', ')} and the statements using ${later.length === 1 ? 'it' : 'them'} when the module loads to their own module`
: k.reader !== undefined
? `use ${read} inside the code that needs ${reader} instead of when the module loads`
: k.read === undefined
- ? `move ${subject} and that statement to their own module`
+ ? `move ${withStatement(names)} to their own module`
: k.split === true
? `change ${read} in a statement of its own`
: k.in === undefined
? `run that statement in the code that needs ${them}`
- : `move ${subject}, ${read} and that statement to their own module`
+ : `move ${withStatement([...names, read])} to their own module`
break
}
case 'shared':
diff --git a/packages/router-utils/src/compiler/placement/events.ts b/packages/router-utils/src/compiler/placement/events.ts
index 36ccb34e22..c6e001f281 100644
--- a/packages/router-utils/src/compiler/placement/events.ts
+++ b/packages/router-utils/src/compiler/placement/events.ts
@@ -134,14 +134,14 @@ export interface Events {
loadToucher: (sym: SymbolId, c: ClusterId) => ClusterId | undefined
/**
* What moves with cluster `c` and binding `sym` to a module of their own,
- * which evaluates before the env module's body (the move a `state` report
- * offers): what they touch when the module loads (left behind, that
- * module would import it back from the env module: a cycle), the code
- * writing a moved binding when the module loads or assigning it at any
- * time (an imported binding is read-only), and what touches a moved
- * binding when the module loads before one of its writes then (moved, the
- * write would run first: O). `syms`: the moved bindings (of an import,
- * the ones reached).
+ * which evaluates before the env module's body (the move a `state` or
+ * `reads` report offers): what they touch when the module loads (left
+ * behind, that module would import it back from the env module: a cycle),
+ * the code writing a moved binding when the module loads or assigning it
+ * at any time (an imported binding is read-only), and what touches a
+ * moved binding when the module loads before one of its writes then
+ * (moved, the write would run first: O). `syms`: the moved bindings (of
+ * an import, the ones reached).
*/
movedWith: (
c: ClusterId,
diff --git a/packages/router-utils/src/compiler/placement/kept.ts b/packages/router-utils/src/compiler/placement/kept.ts
index 5c2e329243..ed88444025 100644
--- a/packages/router-utils/src/compiler/placement/kept.ts
+++ b/packages/router-utils/src/compiler/placement/kept.ts
@@ -10,7 +10,7 @@
import { statementsIn } from '../units'
import { derivedNeeds } from './settle'
import { NEED_KINDS } from './clusters'
-import type { Cluster, ClusterGraph, ClusterId } from './clusters'
+import type { Cluster, ClusterGraph, ClusterId, Write } from './clusters'
import type { ReadIndex } from './reads'
import type { Seeds } from './roots'
import type { Events, Pin } from './events'
@@ -19,6 +19,7 @@ import type { ClassifiedRef } from '../analysis'
import type { SymbolId } from '../symbols'
import type { OutputId } from '../rewrite'
import type { KeptDeclaration, PlacementInput } from '../reach'
+import type { RegionId, UnitId } from '../units'
export function keptDeclarations(
input: PlacementInput,
@@ -65,39 +66,65 @@ export function keptDeclarations(
: units[declaring]!.range
return { start: range.start, end: range.end }
}
- /** The clusters that stay in the env module whatever moves: the route options' (the declaration holding a claimed region) and generated code. */
+ /**
+ * The clusters that stay in the env module whatever moves: the route
+ * options' declaration (holding the values the router calls later,
+ * `calledLater`) and generated code made from it (a generated unit
+ * claiming a region of it). Other generated code is made from the source
+ * it claims, and moves with it.
+ */
let fixed: Set | undefined
/**
- * The move a `state` report offers (`Events.movedWith`): what moves with
- * cluster `c` and `read`, named, or what keeps it from moving.
+ * The move a `state`, `reads` or `written` report offers
+ * (`Events.movedWith`): what moves with cluster `c` and `read`, named, or
+ * what keeps it from moving. `named`: the statement a `written` report
+ * names already (its position), neither listed nor counted as a write.
*/
const moveOf = (
c: ClusterId,
read: SymbolId,
+ named?: number,
): Pick => {
if (fixed === undefined) {
+ const clusterAt = (range: { start: number; end: number }) => {
+ const u = t1.unitAt(range)
+ return u === null
+ ? undefined
+ : graph.clusterOfRegion.get(units[u]!.region)
+ }
fixed = new Set()
- for (const claim of input.rewrites.claims.values()) {
- const u = t1.unitAt(regions.get(claim.region).range)
- const x =
- u === null ? undefined : graph.clusterOfRegion.get(units[u]!.region)
+ for (const range of input.calledLater) {
+ const x = clusterAt(range)
if (x !== undefined) {
fixed.add(x)
}
}
+ for (const claim of input.rewrites.claims.values()) {
+ const x = clusterAt(regions.get(claim.region).range)
+ const g = graph.clusterOfGen.get(claim.owner)
+ if (x !== undefined && fixed.has(x) && g !== undefined) {
+ fixed.add(g)
+ }
+ }
}
const moved = events.movedWith(c, read)
- const named = (s: SymbolId) =>
+ const source = (s: SymbolId) =>
symbols.isModuleLevel(s) && symbols.kind(s) === 'source'
for (const d of moved.clusters) {
const cl = clusters[d]!
- if (fixed.has(d) || cl.gens.length > 0) {
- const sym = [...cl.declares].find(named)
+ if (fixed.has(d)) {
+ const sym = [...cl.declares].find(source)
return { stuck: { ...(sym !== undefined && { sym }), at: cl.start } }
}
}
const syms: Array = []
const statements: Array = []
+ const statement =
+ named === undefined ? null : t1.unitAt({ start: named, end: named })
+ const inStatement = (at: number) =>
+ statement !== null &&
+ at >= units[statement]!.range.start &&
+ at < units[statement]!.range.end
/** Whether `d` writes a moved binding when the module loads (a statement writing them). */
const writes = (d: ClusterId) =>
graph.writes.some(
@@ -106,26 +133,33 @@ export function keptDeclarations(
w.eager &&
moved.syms.has(w.sym) &&
w.owner !== null &&
+ !inStatement(w.at) &&
graph.clusterOfRegion.get(w.owner) === d,
)
for (const d of moved.clusters) {
- if (d === c) {
+ const cl = clusters[d]!
+ // (generated code only: made again from the source it claims)
+ if (d === c || cl.units.length === 0) {
continue
}
- const cl = clusters[d]!
// (an import is named when the module writes it then: the
// statements writing it move)
const own = [...cl.declares].filter(
(s) =>
s !== read &&
- named(s) &&
+ source(s) &&
(cl.kind !== 'import' ||
(moved.syms.has(s) &&
(graph.writesOfSym.get(s) ?? []).some((w) => w.live && w.eager))),
)
if (own.length > 0) {
syms.push(...own)
- } else if (cl.kind !== 'import' && !cl.declares.has(read) && !writes(d)) {
+ } else if (
+ cl.kind !== 'import' &&
+ !cl.declares.has(read) &&
+ !writes(d) &&
+ !inStatement(cl.start)
+ ) {
statements.push(cl.start)
}
}
@@ -141,10 +175,20 @@ export function keptDeclarations(
/** The pair's other event and binding, as a record words them. */
const pairOf = (c: ClusterId, pin: Pin): Partial => {
const other = pin.other
+ // (`written`: the move of it and the binding the statement also uses,
+ // or, when code that runs later changes it too, of it alone; the
+ // statement is named already)
+ const subject =
+ pin.reason === 'written' &&
+ (pin.later !== undefined || pin.binding === undefined)
+ ? [...clusters[c]!.declares].find((s) => symbols.isModuleLevel(s))
+ : pin.binding
return {
- ...(pin.reason === 'state' &&
- pin.binding !== undefined &&
- moveOf(c, pin.binding)),
+ ...((pin.reason === 'state' ||
+ pin.reason === 'reads' ||
+ pin.reason === 'written') &&
+ subject !== undefined &&
+ moveOf(c, subject, pin.reason === 'written' ? other?.at : undefined)),
// (`state`: only a write when the module loads, O, has a place in
// that order to name; one at an unknown time, V, has none)
...(other !== undefined &&
@@ -541,16 +585,92 @@ export function keptDeclarations(
// a statement writing, when the module loads, a binding the env module
// uses and one only split code uses joins them (a binding's writers at
// load join its cluster, `clusters.ts`): the second stays with the
- // first, reported at its declaration (recheck finding 4)
+ // first, reported at its declaration (recheck finding 4). A statement
+ // joined so (a method the mutation model knows writes only its receiver:
+ // `table.add(d)`) that uses a binding of another cluster only split code
+ // needs keeps it too, as a statement following both does (`seeds`
+ // `stayPins`): reported the same way (phase 6 step 3, P04b)
const writtenTogether: Array = []
+ /** What the env module evaluates for its own needs: not for a cause, a statement reported, or an export statement re-exporting causes. */
+ const ownOfEnv = () =>
+ reads.reach(
+ 'env',
+ [...envRoots].filter(
+ (c) => !causes.has(c) && !statements.has(c) && !gluesCauses(c),
+ ),
+ )
+ let ownCache: Set | undefined
+ /** The write table by owner, once a cluster has a statement. */
+ let writesByOwner: Map> | undefined
let evaluated: Set | undefined
let claimed: Array<{ start: number; end: number }> | undefined
for (const cl of clusters) {
if (
(cl.kind !== 'declaration' && cl.kind !== 'effect') ||
- cl.exported ||
- cl.declares.size < 2 ||
- lazyNeeders(cl.id).length === 0
+ !cl.units.some((u) => units[u]!.kind === 'effect')
+ ) {
+ continue
+ }
+ if (writesByOwner === undefined) {
+ writesByOwner = new Map()
+ for (const w of graph.writes) {
+ if (w.owner !== null) {
+ const list = writesByOwner.get(w.owner)
+ if (list) {
+ list.push(w)
+ } else {
+ writesByOwner.set(w.owner, [w])
+ }
+ }
+ }
+ }
+ const writesOf = writesByOwner
+ // (the statements of `cl` writing its bindings when the module loads,
+ // with them)
+ const joined: Array<{
+ id: UnitId
+ unit: (typeof units)[number]
+ written: Array
+ }> = []
+ for (const u of cl.units) {
+ const unit = units[u]!
+ if (unit.kind !== 'effect') {
+ continue
+ }
+ const written = [
+ ...new Set(
+ (writesOf.get(unit.region) ?? [])
+ .filter(
+ (w) =>
+ w.live &&
+ w.eager &&
+ cl.declares.has(w.sym) &&
+ symbols.isModuleLevel(w.sym) &&
+ !symbols.isImport(w.sym),
+ )
+ .map((w) => w.sym),
+ ),
+ ]
+ if (written.length > 0) {
+ joined.push({ id: u, unit, written })
+ }
+ }
+ // (a statement writing two of its bindings, or using a binding of
+ // another cluster split code needs: the one candidates, before the env
+ // module's reach is walked)
+ if (
+ !joined.some(
+ ({ id, written }) =>
+ written.length > 1 ||
+ events
+ .unitChanges(id)
+ .clusters.some(
+ (x) =>
+ x !== cl.id &&
+ !clusters[x]!.copyable &&
+ lazyNeeders(x).length > 0,
+ ),
+ )
) {
continue
}
@@ -563,12 +683,20 @@ export function keptDeclarations(
(claim) => regions.get(claim.region).range,
)
const inClaimed = claimed
- /** Where the code holding `ref` evaluates, outside `cl`: the env module (`env`), split code only (`split`: an option value a split output claims, or code the env module does not evaluate). */
- const usedAt = (ref: ClassifiedRef): 'env' | 'split' | null => {
+ /** Where the code holding `ref` evaluates, outside `cl` and `outside`: the env module (`env`), split code only (`split`: an option value a split output claims, or code the env module does not evaluate). */
+ const usedAt = (
+ ref: ClassifiedRef,
+ outside = cl.id,
+ ): 'env' | 'split' | null => {
const u = t1.unitAt(ref.range)
const x =
u === null ? undefined : graph.clusterOfRegion.get(units[u]!.region)
- if (x === undefined || x === cl.id || !NEED_KINDS.has(ref.kind)) {
+ if (
+ x === undefined ||
+ x === cl.id ||
+ x === outside ||
+ !NEED_KINDS.has(ref.kind)
+ ) {
return null
}
return liveInEnv(ref) &&
@@ -579,57 +707,130 @@ export function keptDeclarations(
? 'env'
: 'split'
}
- for (const u of cl.units) {
- const unit = units[u]!
- if (unit.kind !== 'effect') {
- continue
- }
- const written = [
- ...new Set(
- graph.writes
- .filter(
- (w) =>
- w.owner === unit.region &&
- w.live &&
- w.eager &&
- cl.declares.has(w.sym) &&
- symbols.isModuleLevel(w.sym) &&
- !symbols.isImport(w.sym),
- )
- .map((w) => w.sym),
- ),
- ]
- if (written.length < 2) {
- continue
- }
+ for (const { id, unit, written } of joined) {
const uses = written.map((sym) => {
- const at = t1.refs.refsOf(sym).map(usedAt)
+ const at = t1.refs.refsOf(sym).map((ref) => usedAt(ref))
return { sym, env: at.includes('env'), split: at.includes('split') }
})
const keptOne = uses.find((x) => x.env)
- const free = uses.filter((x) => !x.env && x.split).map((x) => x.sym)
- if (keptOne === undefined || free.length === 0) {
+ if (keptOne === undefined) {
continue
}
+ const free = uses.filter((x) => !x.env && x.split).map((x) => x.sym)
const node = unit.node
- writtenTogether.push({
- syms: free,
- exported: false,
- reason: 'written',
- range: declaringRange(cl, free[0]),
- needers: lazyNeeders(cl.id),
- at: unit.range.start,
- read: keptOne.sym,
- readChanged: true,
- // (a statement of several writes can change the kept binding in one
- // of its own)
- ...((statementsIn(node) > 1 ||
- (node.type === 'ExpressionStatement' &&
- node.expression.type === 'SequenceExpression')) && {
- split: true,
- }),
- })
- break
+ if (free.length > 0 && lazyNeeders(cl.id).length > 0) {
+ writtenTogether.push({
+ syms: free,
+ exported: false,
+ reason: 'written',
+ range: declaringRange(cl, free[0]),
+ needers: lazyNeeders(cl.id),
+ at: unit.range.start,
+ read: keptOne.sym,
+ readChanged: true,
+ // (a statement of several writes can change the kept binding in
+ // one of its own)
+ ...((statementsIn(node) > 1 ||
+ (node.type === 'ExpressionStatement' &&
+ node.expression.type === 'SequenceExpression')) && {
+ split: true,
+ }),
+ })
+ break
+ }
+ // a binding of another cluster the statement uses: kept by it alone
+ // when only split code uses it otherwise (its uses in `cl`'s joined
+ // statements aside), and the env module keeps `cl` for its own needs
+ // (else `cl` is a cause, or its cascade, reported at the cause)
+ ownCache ??= ownOfEnv()
+ if (!ownCache.has(cl.id)) {
+ continue
+ }
+ const inJoined = (ref: ClassifiedRef) =>
+ joined.some(
+ (j) =>
+ ref.range.start >= j.unit.range.start &&
+ ref.range.end <= j.unit.range.end,
+ )
+ const clusterOfRef = (ref: ClassifiedRef) => {
+ const v = t1.unitAt(ref.range)
+ return v === null
+ ? undefined
+ : graph.clusterOfRegion.get(units[v]!.region)
+ }
+ // what the statement uses (`Events.unitChanges`: what it names, runs,
+ // and the objects those hold), its own cluster aside
+ const changes = events.unitChanges(id)
+ const followed = new Set(changes.clusters)
+ /** Whether the env module uses `ref` outside the joined statements, `x` and what `within` allows (`cl`'s other code does: its declaration). */
+ const usedByEnv = (
+ ref: ClassifiedRef,
+ x: ClusterId,
+ within: (z: ClusterId) => boolean,
+ ) => {
+ if (inJoined(ref)) {
+ return false
+ }
+ const z = clusterOfRef(ref)
+ return z === cl.id
+ ? NEED_KINDS.has(ref.kind)
+ : z !== x &&
+ !(z !== undefined && within(z)) &&
+ usedAt(ref, x) === 'env'
+ }
+ /** Whether nothing the env module evaluates uses `y`'s bindings but the statement and what it follows. */
+ const onlyFollowed = (y: ClusterId) =>
+ [...clusters[y]!.declares].every((s) =>
+ t1.refs
+ .refsOf(s)
+ .every((ref) => !usedByEnv(ref, y, (z) => followed.has(z))),
+ )
+ const reported: Array = []
+ for (const x of changes.clusters) {
+ const xc = clusters[x]!
+ if (
+ x === cl.id ||
+ (xc.kind !== 'declaration' && xc.kind !== 'effect') ||
+ xc.copyable ||
+ xc.gens.length > 0 ||
+ causes.has(x) ||
+ !ev.has(x) ||
+ lazyNeeders(x).length === 0 ||
+ // (another binding the statement follows that the env module
+ // uses keeps it there anyway)
+ ![...xc.declares].every((s) =>
+ t1.refs
+ .refsOf(s)
+ .every(
+ (ref) =>
+ !usedByEnv(ref, x, (z) => followed.has(z) && onlyFollowed(z)),
+ ),
+ )
+ ) {
+ continue
+ }
+ const syms = [...xc.declares].filter(
+ (s) => symbols.isModuleLevel(s) && !symbols.isImport(s),
+ )
+ reported.push({
+ syms,
+ exported: false,
+ reason: 'written',
+ range: declaringRange(xc, syms[0]),
+ needers: lazyNeeders(x),
+ at: unit.range.start,
+ read: keptOne.sym,
+ readChanged: true,
+ // (the statement changes it, or only uses it)
+ ...(!changes.syms.some((s) => xc.declares.has(s)) && {
+ uses: true,
+ }),
+ })
+ }
+ if (reported.length > 0) {
+ writtenTogether.push(...reported)
+ break
+ }
}
}
const kept: Array = [...unusedReports, ...writtenTogether]
@@ -658,12 +859,7 @@ export function keptDeclarations(
}
}
}
- const envOwn = reads.reach(
- 'env',
- [...envRoots].filter(
- (c) => !causes.has(c) && !statements.has(c) && !gluesCauses(c),
- ),
- )
+ const envOwn = ownCache ?? ownOfEnv()
/**
* The cluster declaring what an export cancelled because it reads it
* (`reads`) reads: the export stays only for it, so it keeps nothing by
diff --git a/packages/router-utils/src/compiler/reach.ts b/packages/router-utils/src/compiler/reach.ts
index 095dea14bb..337205e2b8 100644
--- a/packages/router-utils/src/compiler/reach.ts
+++ b/packages/router-utils/src/compiler/reach.ts
@@ -157,13 +157,14 @@ export interface KeptDeclaration {
/** `state`: it uses `read` only in what it hands to this call (`''`: too long to quote), which may or may not run it then. */
handedTo?: string
/**
- * `state`: what the move to a module of their own takes besides it and
- * `read` (`Events.movedWith`): the other bindings, the statements reading
- * one before a write when the module loads (their positions), and whether
- * the module writes a moved binding then (the statements writing them move).
+ * `state`, `reads`, `written`: what the move to a module of their own
+ * takes besides it and `read` (`Events.movedWith`): the other bindings,
+ * the statements reading one before a write when the module loads (their
+ * positions), and whether the module writes a moved binding then (the
+ * statements writing them move; `written`: besides the statement named).
*/
moves?: { syms: Array; statements: Array; written: boolean }
- /** `state`: no module of its own can take it: it would need this code, which stays in the env module (its binding, if it has one, and position). */
+ /** `state`, `reads`, `written`: no module of its own can take it: it would need this code, which stays in the env module (its binding, if it has one, and position). */
stuck?: { sym?: SymbolId; at: number }
}
diff --git a/packages/router-utils/tests/placement.test.ts b/packages/router-utils/tests/placement.test.ts
index 7f58834cbe..7c81c75466 100644
--- a/packages/router-utils/tests/placement.test.ts
+++ b/packages/router-utils/tests/placement.test.ts
@@ -902,6 +902,14 @@ describe("an extracted export's module is a destination", () => {
expect(kept[1]!.message).toContain(
'`l4` in /app/route.tsx stays in the route module because the statement on line 6, which uses it, stays there: it also uses `Ex2`, which the split `component` uses',
)
+ // the move takes the statement writing `l4` when the module loads (left
+ // behind, it would assign an import)
+ expect(kept[0]!.message).toContain(
+ '; move `Ex2`, `l4`, that statement and the statements writing them when the module loads to their own module, so `Ex2` can load',
+ )
+ expect(kept[1]!.message).toContain(
+ '; move `l4`, `Ex2`, that statement and the statements writing them when the module loads to their own module, so `l4` can load',
+ )
})
it('keeps all of it in the route module as written in dev', async () => {
@@ -2747,6 +2755,125 @@ describe('extracted-export outputs', () => {
])
})
+ // The move a `reads` report offers is what the export and the binding it
+ // reads touch when the module loads, with the code writing them then
+ // (`Events.movedWith`): left in the route module, the new module would
+ // import it back, a cycle (Vite reads `undefined`, Rspack throws a TDZ
+ // error). When that needs `Route`, no module of its own can take it.
+ it.each([
+ [
+ 'a binding the read one reads',
+ 'const cfg = { k: 2 }\nconst kept = { n: cfg.k }\nexport const count = kept.n',
+ 'count',
+ 'kept',
+ 'Move it, `kept` and `cfg` to their own module and import them from there.',
+ ],
+ [
+ 'a holder',
+ 'const o0 = { v: 1 }\nconst h6 = { c: o0 }\nexport const count = h6.c.v',
+ 'count',
+ 'h6',
+ 'Move it, `h6` and `o0` to their own module and import them from there.',
+ ],
+ [
+ 'a function it calls',
+ 'const unit = 2\nconst kept = { n: 1 }\nfunction total() { return kept.n * unit }\nexport const count = total()',
+ 'count',
+ 'kept',
+ 'Move it, `kept`, `unit` and `total` to their own module and import them from there.',
+ ],
+ [
+ 'a write when the module loads',
+ 'const kept = { n: 1 }\nkept.n = 3\nexport const count = kept.n',
+ 'count',
+ 'kept',
+ 'Move it, `kept` and the statements writing them when the module loads to their own module and import them from there.',
+ ],
+ [
+ 'nothing else',
+ "import { loadSettings } from './settings'\nconst kept = loadSettings()\nexport const count = kept.pageSize * 2",
+ 'count',
+ 'kept',
+ 'Move it and `kept` to their own module and import them from there.',
+ ],
+ [
+ '`Route` (declared after it)',
+ 'export const count = Route.options.path',
+ 'count',
+ 'Route',
+ "It can't move to a module of its own: that module would also need `Route`, which stays in the route module.",
+ ],
+ ])(
+ 'offers, for an export reading what the route module keeps, the move of what it reads when it loads: %s',
+ async (_, decls, name, read, fix) => {
+ const route = dedent(`
+ export const Route = createFileRoute('/')({
+ loader: () => ${read === 'h6' ? 'o0' : read === 'Route' ? 'null' : read},
+ component: () => ${name},
+ })
+ `)
+ // (`Route` read after its declaration)
+ const code = [
+ "import { createFileRoute } from 'toy-router'",
+ ...(read === 'Route' ? [route, decls] : [decls, route]),
+ ].join('\n')
+ const r = await extract(code)
+ expect(
+ r.diagnostics.map((d) => [d.code, d.range?.start, d.message]),
+ ).toEqual([
+ [
+ 'eager-export:reads',
+ code.indexOf(`${name} = `),
+ `[tanstack-router] The export \`${name}\` of /app/route.tsx is used only by the split \`component\`, but stays in the route module because it reads \`${read}\` when it loads, which the route module keeps, so it loads with the route module instead of with that code. ${fix}`,
+ ],
+ ])
+ },
+ )
+
+ // code another planner claims or generates (a hydration boundary here; a
+ // server function's handler and stubs in Start) is made again from its
+ // source wherever that goes: only the route options' declaration keeps a
+ // move from being offered (corpus: Start server functions reading a
+ // middleware or a schema the route module keeps)
+ it.each([
+ [
+ 'a value holding a claimed boundary',
+ 'const kept = { n: cfg.n, view: () => {cfg.n} }\nexport const count = kept.n',
+ 'Move it, `kept` and `cfg` to their own module and import them from there.',
+ ],
+ [
+ 'a function it calls holding one',
+ 'const kept = { n: cfg.n }\nfunction View() { return {cfg.n} }\nexport const count = [kept.n, View()]',
+ 'Move it, `kept`, `cfg` and `View` to their own module and import them from there.',
+ ],
+ ])(
+ "offers the move when what moves holds another planner's code: %s",
+ async (_, decls, fix) => {
+ const code = dedent(`
+ import { createFileRoute } from 'toy-router'
+ import { Hydrate } from 'toy-start'
+ const cfg = { n: 1 }
+ ${decls.split('\n').join('\n ')}
+ export const Route = createFileRoute('/')({ loader: () => kept, component: () => count })
+ `)
+ const host = new MemoryHost({ files: { '/app/route.tsx': code } })
+ const out = await host.compile(
+ '/app/route.tsx',
+ [
+ toySplitPlanner({ keys: ['component'], extractExports: true }),
+ toyHydratePlanner(),
+ ],
+ build,
+ )
+ expect(out?.diagnostics.map((d) => d.code)).toEqual([
+ 'eager-export:reads',
+ ])
+ expect(out?.diagnostics[0]!.message).toContain(
+ `so it loads with the route module instead of with that code. ${fix}`,
+ )
+ },
+ )
+
// J2.8: an exported declaration a chunk needs that stays in the route
// module is reported once per export with the reason, positioned at the
// declaration; a route file exporting only `Route` reports nothing
@@ -8488,6 +8615,81 @@ describe('eager-split:written: why a statement that changes it stays', () => {
expect(f.chunks.component).toContain('const d = make()')
})
+ // a method the mutation model knows (`Set.add`, `Array.push`, `Map.set`)
+ // writes only the receiver: the statement joins its cluster, which the
+ // route module keeps, and keeps the binding it uses with one warning, as
+ // a call it does not know does (phase 6 step 3, P04b written with `add`)
+ it.each([
+ ['add', 'new Set()', 'k.add(d)'],
+ ['push', '[]', 'k.push(d)'],
+ ['set', 'new Map()', "k.set('d', d)"],
+ ])(
+ 'a binding the route module keeps that a known method (%s) changes',
+ async (_, init, statement) => {
+ const options = 'loader: () => k, component: () => d'
+ const code = route(
+ `const k = ${init}\nconst d = make()\n${statement}`,
+ options,
+ )
+ const r = await report(code)
+ written(
+ r,
+ code,
+ 'd',
+ statement,
+ 'which uses it, stays there: it also changes `k`, which the route module keeps; run that statement in the code that needs it, so `d` can load with the split `component`.',
+ )
+ // the fix: the call in the code that needs `d`
+ const f = await report(
+ route(
+ `const k = ${init}\nconst d = make()`,
+ `loader: () => k, component: () => (${statement}, d)`,
+ ),
+ )
+ expect(f.diagnostics).toEqual([])
+ expect(f.chunks.component).toContain('const d = make()')
+ },
+ )
+
+ // the same through what the statement uses (`unitChanges`): a holder of
+ // it, and an exported binding, as with a call the model does not know
+ it.each([
+ [
+ 'a holder of it',
+ 'const k = new Set()\nconst d = make()\nconst h = { d }',
+ 'k.add(h)',
+ false,
+ ],
+ [
+ 'an exported binding',
+ 'const k = new Set()\nexport const d = make()',
+ 'k.add(d)',
+ true,
+ ],
+ ])(
+ 'a binding the route module keeps that a known method changes, using %s',
+ async (_, decls, statement, extract) => {
+ const options = 'loader: () => k, component: () => d'
+ const code = route(`${decls}\n${statement}`, options)
+ const r = await report(code, undefined, {}, extract)
+ written(
+ r,
+ code,
+ 'd',
+ statement,
+ 'which uses it, stays there: it also changes `k`, which the route module keeps; run that statement in the code that needs it, so `d` can load with the split `component`.',
+ )
+ // the fix: the call in the code that needs `d`
+ const f = await report(
+ route(decls, `loader: () => k, component: () => (${statement}, d)`),
+ undefined,
+ {},
+ extract,
+ )
+ expect(f.diagnostics).toEqual([])
+ },
+ )
+
it('a binding the route module keeps that the statement only uses', async () => {
const statement = 'table.setup(cfg)'
const options = 'loader: () => cfg, component: () => table'
@@ -8556,6 +8758,32 @@ describe('eager-split:written: why a statement that changes it stays', () => {
expect(f.env).not.toContain('own')
})
+ // the move takes what the binding reads when the module loads: left in
+ // the route module, the new module (which the route module imports to
+ // re-export the export) would import it back, a cycle
+ it('code that runs later changing an export reading another binding when the module loads', async () => {
+ const statement = "Sentry.setContext('flags', flags)"
+ const options = 'component: () => { flags.base = 1; return flags }'
+ const code = route(
+ `const prefix = { p: 'x' }\nexport const flags = { beta: false, p: prefix.p }\n${statement}`,
+ options,
+ )
+ const r = await report(code, undefined, {}, true)
+ written(r, code, 'flags', statement, 'code that runs later changes it too')
+ expect(r.diagnostics[0]!.message).toContain(
+ '; move `flags`, `prefix` and the statements using them when the module loads to their own module, so `flags` can load',
+ )
+ const f = await report(
+ route("import { flags } from './flags'\nexport { flags }", options),
+ undefined,
+ {
+ '/app/flags.ts': `import * as Sentry from '@sentry/react'\nconst prefix = { p: 'x' }\nexport const flags = { beta: false, p: prefix.p }\n${statement}\n`,
+ },
+ true,
+ )
+ expect(f.diagnostics).toEqual([])
+ })
+
// an import a statement changes is no declaration of the module: the
// statement stays, the chunk imports the import itself, nothing reported
it('reports nothing for an import a statement changes', async () => {
@@ -8634,6 +8862,36 @@ describe('eager-split:written: why a statement that changes it stays', () => {
},
)
+ // the same for an exported binding (it was not reported: the pass
+ // skipped exported clusters)
+ it('a binding the route module keeps a statement also writes, the other one exported', async () => {
+ const statement = 'if (on) { a.v = 1; k.v = 2 }'
+ const options = 'loader: () => k, component: () => a'
+ const code = route(
+ `export const a = make()\nconst k = make()\n${statement}`,
+ options,
+ )
+ written(
+ await report(code, undefined, {}, true),
+ code,
+ 'a',
+ statement,
+ 'it also changes `k`, which the route module keeps; change `k` in a statement of its own',
+ )
+ // the fix
+ const f = await report(
+ route(
+ 'export const a = make()\nconst k = make()\nif (on) { a.v = 1 }\nif (on) { k.v = 2 }',
+ options,
+ ),
+ undefined,
+ {},
+ true,
+ )
+ expect(f.diagnostics).toEqual([])
+ expect(f.env).not.toContain('const a = make()')
+ })
+
// (one expression writing both: run in the code that needs `a`, as a
// call changing both is)
it('a binding the route module keeps one expression also writes', async () => {