(input: FixerInput)
| 16 | import { CANONICAL_ORDER, resolveAlias } from '../linter/rules/section-order.js'; |
| 17 | |
| 18 | export 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 | } |
no test coverage detected
searching dependent graphs…