MCPcopy
hub / github.com/google-labs-code/design.md / fixSectionOrder

Function fixSectionOrder

packages/cli/src/linter/fixer/handler.ts:18–62  ·  view source on GitHub ↗
(input: FixerInput)

Source from the content-addressed store, hash-verified

16import { CANONICAL_ORDER, resolveAlias } from '../linter/rules/section-order.js';
17
18export function fixSectionOrder(input: FixerInput): FixerResult {
19 const { sections } = input;
20
21 const prelude = sections.find(s => s.heading === '');
22
23 const known = sections.filter(s => {
24 if (s.heading === '') return false;
25 return CANONICAL_ORDER.includes(resolveAlias(s.heading));
26 });
27
28 const unknown = sections.filter(s => {
29 if (s.heading === '') return false;
30 return !CANONICAL_ORDER.includes(resolveAlias(s.heading));
31 });
32
33 // Sort known sections by canonical order
34 known.sort((a, b) => {
35 return CANONICAL_ORDER.indexOf(resolveAlias(a.heading)) - CANONICAL_ORDER.indexOf(resolveAlias(b.heading));
36 });
37
38 const resultSections = [];
39 if (prelude) resultSections.push(prelude);
40 resultSections.push(...known);
41 resultSections.push(...unknown);
42
43 // Join content with newlines.
44 // We might need to ensure there are enough newlines between sections.
45 // The parser keeps the trailing newlines if they are part of the section content.
46 // Let's see if we need to add a newline between them.
47 // If we join with '\n', and content already ends with '\n', we might get double newlines.
48 // Let's just join them for now and see what happens in tests!
49 const fixedContent = resultSections.map(s => s.content).join('\n');
50
51 const beforeOrder = sections.map(s => s.heading).filter(h => h !== '');
52 const afterOrder = resultSections.map(s => s.heading).filter(h => h !== '');
53
54 return {
55 success: true,
56 fixedContent,
57 details: {
58 beforeOrder,
59 afterOrder
60 }
61 };
62}

Callers 1

handler.test.tsFile · 0.85

Calls 1

resolveAliasFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…