Skip to content

Commit d942a40

Browse files
carderneTrigger.dev RepoOps
authored andcommitted
fix(ci): concise route handlers in the mutation corpus
Mono-RevId: 932bb2d9b5b4515232070b168813f384c8e29427
1 parent 2d2aaff commit d942a40

2 files changed

Lines changed: 31 additions & 21 deletions

File tree

‎internal-packages/observability-map/src/mutationCorpus.test.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,6 @@ describeCorpus("mutation corpus over the real route tree", { timeout: ENTRY_TIME
261261
baseline = measure(files);
262262
}, ENTRY_TIMEOUT_MS);
263263

264-
/**
265-
* The one documented exclusion from the population assertion below. `admin.tsx`'s handler is a
266-
* concise arrow with no block for a block wrapper to wrap, which is a limit of the rewrite rather
267-
* than a gap in the enumeration. Named rather than counted so a second one cannot appear silently.
268-
*/
269-
const CONCISE_ARROW_BODIES = new Set(["admin.tsx"]);
270-
271264
it("wraps a body in every non-delegating entry point the scanner finds", () => {
272265
const wrap = MUTATIONS.find((m) => m.id === "wrap-body-in-rethrow")!;
273266
const root = materialize(files);
@@ -280,7 +273,7 @@ describeCorpus("mutation corpus over the real route tree", { timeout: ENTRY_TIME
280273

281274
const byName = new Map(files.map((f) => [f.relativeName, f.source]));
282275
const untouched = entryPoints
283-
.filter((ep) => !ep.delegating && !CONCISE_ARROW_BODIES.has(ep.fileName))
276+
.filter((ep) => !ep.delegating)
284277
.filter((ep) => wrap.apply(ep.fileName, byName.get(ep.fileName)!) === null)
285278
.map((ep) => ep.fileName);
286279

‎internal-packages/observability-map/src/mutations.ts‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,14 @@ function localDeclarations(sf: ts.SourceFile): LocalDeclarations {
225225
}
226226

227227
/**
228-
* Block bodies of the exported `loader`/`action` handlers, the region a whole-body wrapper wraps. Reads
228+
* Bodies of the exported `loader`/`action` handlers, the region a whole-body wrapper wraps. Reads
229229
* the same four export forms `scan.ts` reads, and `wraps a body in every non-delegating entry point the
230230
* scanner finds` pins that so the harness cannot lag the scanner again.
231231
*
232232
* Not a retreat from the independence `collectNamedHandlers` keeps: that is about where a HANDLER sits
233233
* inside a builder's argument, which is a judgement. Which exports exist is not.
234234
*/
235-
function entryBodies(sf: ts.SourceFile): ts.Block[] {
235+
function entryBodies(sf: ts.SourceFile): ts.ConciseBody[] {
236236
const functions: EntryFunction[] = [];
237237
const locals = localDeclarations(sf);
238238
const fromLocal = (name: string) => {
@@ -284,11 +284,11 @@ function entryBodies(sf: ts.SourceFile): ts.Block[] {
284284
// action of `const { loader, action } = createActionApiRoute({ handler })` resolve to the same
285285
// node. Wrapping it twice would splice the same text in twice at the same offset, because
286286
// `applyEdits` treats two zero-width inserts at one position as non-overlapping.
287-
const bodies: ts.Block[] = [];
288-
for (const fn of new Set(functions)) {
289-
if (fn.body && ts.isBlock(fn.body)) bodies.push(fn.body);
290-
}
291-
return bodies;
287+
return [...new Set(functions)].flatMap((fn) => (fn.body ? [fn.body] : []));
288+
}
289+
290+
function entryBlocks(sf: ts.SourceFile): ts.Block[] {
291+
return entryBodies(sf).filter(ts.isBlock);
292292
}
293293

294294
// -- generic mutation shapes ----------------------------------------------------------------
@@ -416,9 +416,26 @@ function wrapEveryBody(
416416
apply(fileName, source) {
417417
const sf = parse(fileName, source);
418418
const edits: Edit[] = [];
419-
for (const body of entryBodies(sf)) {
420-
edits.push(insert(body.getStart() + 1, `\n${open}\n`));
421-
edits.push(insert(body.end - 1, `\n${close}\n`));
419+
const bodies = entryBodies(sf);
420+
const hasBlock = bodies.some(ts.isBlock);
421+
for (const body of bodies) {
422+
if (ts.isBlock(body)) {
423+
edits.push(insert(body.getStart() + 1, `\n${open}\n`));
424+
edits.push(insert(body.end - 1, `\n${close}\n`));
425+
} else {
426+
const expression = unwrap(body);
427+
// A redirect-only loader is not the substantive handler of a mixed route.
428+
if (
429+
hasBlock &&
430+
ts.isCallExpression(expression) &&
431+
ts.isIdentifier(expression.expression) &&
432+
expression.expression.text === "redirect"
433+
) {
434+
continue;
435+
}
436+
edits.push(insert(body.getStart(), `{\n${open}\nreturn (`));
437+
edits.push(insert(body.end, `);\n${close}\n}`));
438+
}
422439
}
423440
return applyEdits(source, edits);
424441
},
@@ -811,7 +828,7 @@ export const MUTATIONS: Mutation[] = [
811828
apply(fileName, source) {
812829
const sf = parse(fileName, source);
813830
const edits: Edit[] = [];
814-
for (const body of entryBodies(sf)) {
831+
for (const body of entryBlocks(sf)) {
815832
edits.push(
816833
insert(
817834
body.getStart() + 1,
@@ -831,7 +848,7 @@ export const MUTATIONS: Mutation[] = [
831848
apply(fileName, source) {
832849
const sf = parse(fileName, source);
833850
const edits: Edit[] = [];
834-
for (const body of entryBodies(sf)) {
851+
for (const body of entryBlocks(sf)) {
835852
edits.push(
836853
insert(
837854
body.getStart() + 1,
@@ -999,7 +1016,7 @@ export const MUTATIONS: Mutation[] = [
9991016
const sf = parse(fileName, source);
10001017
const edits: Edit[] = [];
10011018
let n = 0;
1002-
for (const body of entryBodies(sf)) {
1019+
for (const body of entryBlocks(sf)) {
10031020
forEachNode(body, (node) => {
10041021
if (!ts.isTryStatement(node)) return;
10051022
if (!node.parent || !ts.isBlock(node.parent)) return;

0 commit comments

Comments
 (0)