Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions scripts/linters/lint-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
* Lists
* - Items start with "* " (not "- ")
* - No trailing whitespace
* - No blank lines between consecutive list items
*
* Spacing
* - No leading blank lines before the title
* - Exactly 1 blank line before every ## heading (not before the first one)
* - Exactly 1 blank line after every # and ## headings
* - No blank lines after ### headings
Expand Down Expand Up @@ -58,6 +60,33 @@ const collapseBlankLines = (lines: string[]): string[] => {
return out
}

/** Remove blank lines before the first content line. */
const stripLeadingBlanks = (lines: string[]): string[] => {
let i = 0
while (i < lines.length && '' === lines[i].trim()) {i += 1}
return lines.slice(i)
}

/**
* Drop blank lines that sit between two list items. Bullets accumulate stray
* blank lines over successive edits; consecutive items should be contiguous.
*/
const removeBlanksBetweenListItems = (lines: string[]): string[] => {
const isItem = (l: string): boolean => /^\s*[*-] /.test(l)
const out: string[] = []
for (let i = 0; i < lines.length; i += 1) {
if ('' === lines[i].trim()) {
let j = i + 1
while (j < lines.length && '' === lines[j].trim()) {j += 1}
const prev = out[out.length - 1]
const next = j < lines.length ? lines[j] : ''
if (0 < out.length && isItem(prev) && isItem(next)) {continue}
}
out.push(lines[i])
}
return out
}

/**
* Ensure exactly `n` blank lines appear immediately before every line matching
* `headingRe`. Lines matching `skipAfterRe` suppress spacing for the immediately
Expand Down Expand Up @@ -257,6 +286,7 @@ export const lintChangelog = (src: string): { fixed: string; errors: string[] }
const errors: string[] = []
let lines = src.split('\n')

lines = stripLeadingBlanks(lines)
lines = ensureTitle(lines, errors)
lines = trimTrailing(lines)
lines = promoteBoldChangeTypeLabels(lines)
Expand All @@ -265,6 +295,7 @@ export const lintChangelog = (src: string): { fixed: string; errors: string[] }
lines = normaliseIndentedSubListItems(lines)
lines = normaliseTopLevelBulletMarkers(lines)
lines = applySpacingRules(lines)
lines = removeBlanksBetweenListItems(lines)
lines = finaliseLines(lines)

return { fixed: lines.join('\n'), errors }
Expand Down
37 changes: 37 additions & 0 deletions scripts/linters/lint-readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
* Lists
* - Items start with "* " (not "- ")
* - No trailing whitespace
* - No blank lines between consecutive list items
*
* Spacing
* - No leading blank lines before the plugin header
* - Exactly 1 blank line before every == section (not the very first)
* - Exactly 1 blank line after every == section heading
* - Exactly 1 blank line before every = sub-section (not right after == heading)
Expand Down Expand Up @@ -98,6 +100,39 @@ const collapseBlankLines = (lines: string[]): string[] => {
return out
}

/** Remove blank lines before the first content line. */
const stripLeadingBlanks = (lines: string[]): string[] => {
let i = 0
while (i < lines.length && '' === lines[i].trim()) {
i += 1
}
return lines.slice(i)
}

/**
* Drop blank lines that sit between two list items. Bullets accumulate stray
* blank lines over successive edits; consecutive items should be contiguous.
*/
const removeBlanksBetweenListItems = (lines: string[]): string[] => {
const isItem = (l: string): boolean => /^\s*[*-] /.test(l)
const out: string[] = []
for (let i = 0; i < lines.length; i += 1) {
if ('' === lines[i].trim()) {
let j = i + 1
while (j < lines.length && '' === lines[j].trim()) {
j += 1
}
const prev = out[out.length - 1]
const next = j < lines.length ? lines[j] : ''
if (0 < out.length && isItem(prev) && isItem(next)) {
continue
}
}
out.push(lines[i])
}
return out
}

/**
* Ensure exactly `n` blank lines appear immediately before every line matching
* `headingRe`. Lines matching `skipAfterRe` reset the "just-saw-section-start"
Expand Down Expand Up @@ -345,6 +380,7 @@ export const lintReadme = (src: string): { fixed: string; errors: string[] } =>
const errors: string[] = []
let lines = src.split('\n')

lines = stripLeadingBlanks(lines)
lines = normalisePluginHeader(lines, errors)
lines = trimTrailing(lines)
lines = normaliseHeaderFieldSpacing(lines)
Expand All @@ -354,6 +390,7 @@ export const lintReadme = (src: string): { fixed: string; errors: string[] } =>
lines = normaliseChangelogChangeTypes(lines)
lines = normaliseListItems(lines)
lines = applySpacingRules(lines)
lines = removeBlanksBetweenListItems(lines)
lines = finaliseLines(lines)

return { fixed: lines.join('\n'), errors }
Expand Down
Loading