Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongable committed May 7, 2024
1 parent e9dd0a0 commit f2e694b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main
pull_request:
branches:
- main
- '**'
jobs:
lint:

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr_tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ on:
- main
pull_request:
branches:
- main
- '**'
jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: [3.8, 3.12.3]

steps:
- uses: actions/checkout@v4
Expand Down
64 changes: 37 additions & 27 deletions tools/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"bufio"
_ "embed"
"errors"
"fmt"
"os"
"slices"
"strings"
)

Expand Down Expand Up @@ -37,16 +37,24 @@ func main() {

anyErr := false

err = lintHeadings(lines)
if err != nil {
fmt.Fprintln(os.Stderr, err)
errs := make(chan error)
go func() {
lintHeadings(lines, errs)
close(errs)
}()
for err = range errs {
anyErr = true
fmt.Fprintln(os.Stderr, err)
}

err = lintEmptySections(lines)
if err != nil {
fmt.Fprintln(os.Stderr, err)
errs = make(chan error)
go func() {
lintEmptySections(lines, errs)
close(errs)
}()
for err = range errs {
anyErr = true
fmt.Fprintln(os.Stderr, err)
}

if anyErr {
Expand All @@ -56,41 +64,45 @@ func main() {
fmt.Println("lint checks complete; no findings")
}

func lintHeadings(lines []string) error {
// lintHeadings tries to locate every heading that we expect to exist, and
// checks that they appear in the correct order.
func lintHeadings(lines []string, errs chan<- error) {
outline = strings.TrimSpace(outline)
headings := strings.Split(outline, "\n")
headingLines := make([]int, len(headings))

nextHeadIdx := 0
for _, line := range lines {
if line == headings[nextHeadIdx] {
// If we found the heading we're looking for, start searching for the
// next one.
nextHeadIdx++
}
for i, heading := range headings {
headingLines[i] = slices.Index(lines, heading)
}

if nextHeadIdx == len(headings) {
// If we've found all the headings we expect, we're done.
break
for i, lineNo := range headingLines {
if lineNo == -1 {
errs <- fmt.Errorf("heading %q not found", headings[i])
continue
}
}

if nextHeadIdx != len(headings) {
return fmt.Errorf("expected heading %q not found", headings[nextHeadIdx])
if i > 0 && lineNo <= headingLines[i-1] {
errs <- fmt.Errorf("heading %q found out-of-order on line %d", headings[i], lineNo)
continue
}
}
return nil
}

func lintEmptySections(lines []string) error {
// lintEmptySections looks for places where two section headings occur with
// nothing other than empty lines between them, and the second section is of
// equal or lesser depth (being of greater depth is fine, that's a subsection).
func lintEmptySections(lines []string, errs chan<- error) {
lastHeadingDepth := 0
sectionBodySeen := false
for _, line := range lines {
for i, line := range lines {
if line == "" {
continue
}

if strings.HasPrefix(line, "#") {
currHeadingDepth := len(line) - len(strings.TrimLeft(line, "#"))
if currHeadingDepth <= lastHeadingDepth && !sectionBodySeen {
return errors.New("empty section found")
errs <- fmt.Errorf("empty section found at line %d", i)
}

lastHeadingDepth = currHeadingDepth
Expand All @@ -100,6 +112,4 @@ func lintEmptySections(lines []string) error {

sectionBodySeen = true
}

return nil
}

0 comments on commit f2e694b

Please sign in to comment.