Mermaid sequence linter

You build a Go linter that accepts Mermaid sequence diagram source and returns line-numbered diagnostics for malformed declarations, messages, and blocks.

Ticket

Build a Go linter in the mermaidlint package. It accepts Mermaid sequenceDiagram source and returns one diagnostic for each detected structural problem.

The project README and ADRs contain Mermaid diagrams. A local linter gives you a way to catch malformed sequence diagrams before committing documentation changes. This optional tool is independent of the required Week 8 work.

The tests call this exported function:

go
func Lint(src string) []string

Acceptance criteria

  • A valid sequence diagram containing autonumber, participant declarations, an actor declaration, a participant alias, a Note line, and a supported message produces no diagnostics.
  • The first non-blank line must be exactly sequenceDiagram. For input beginning with flowchart LR, return line 1: a sequence diagram must start with sequenceDiagram.
  • Whitespace-only input returns exactly line 1: the diagram is empty.
  • Participant aliases declare the identifier before as. For example, participant DB as MySQL allows DB->>DB: commit without diagnostics.
  • Messages referencing undeclared participants report their names and source lines. With only App declared, App->>Cache: GET on line 3 reports line 3: undeclared participant Cache, and Worker->>App: done on line 4 reports line 4: undeclared participant Worker.
  • All eight message arrow forms are accepted when both endpoints are declared: ->>, -->>, ->, -->, -x, --x, -), and --).
  • A message without a colon or with an empty message after the colon reports line N: missing message text. The tested inputs on lines 4 and 5 produce those exact diagnostics.
  • An unsupported non-blank line such as this is not mermaid reports line 3: unrecognised line.
  • Blank lines and lines beginning with %% after trimming surrounding whitespace are ignored.
  • Block openings and closings must balance. A balanced loop produces no diagnostics. An unclosed alt opened on line 3 reports line 3: block opened here is never closed with end. An end on line 3 without an open block reports line 3: end without an open block.
  • activate and deactivate commands must reference declared participants. With A declared, activate A produces no diagnostic, while deactivate B on line 4 reports line 4: undeclared participant B.

Constraints

  • Keep the implementation in package mermaidlint and provide the exported Lint signature shown in the ticket.
  • Do not modify the provided tests.

Design direction

Keep the parser in internal/mermaidlint. A small set of private helpers can separate declaration recognition, message parsing, and block handling. You can use a declaration pass to collect participant identifiers before validating message endpoints.

Think about these trade-offs:

  • A two-pass parser can recognise participants declared later in the source. A single pass is smaller but needs a rule for forward references.
  • A stack preserves the opening line number of every nested block. A counter can detect imbalance but cannot identify each unclosed opening.
  • Regular expressions can make supported declaration and message forms explicit. Manual parsing gives you more control over malformed input.

Keep the implementation independent of external services. Any layout that meets the contract passes.

How to check your work

Run the provided tests from the backend directory:

bash
cd backend
make test W=8O2

Read the first failing assertion as a contract mismatch. Compare the returned diagnostic text and source line number with the expected value before changing the parser.

Finished Exercise 8.14?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Implement Lint in package mermaidlint.
  2. Recognise participant declarations, aliases, and all eight message arrows.
  3. Report undeclared participants and missing message text with source line numbers.
  4. Validate block boundaries and activation commands.
  5. Run make test W=8O2 without modifying the provided tests.

Commands

terminal
cd backend && make test W=8O2

Tests

  • TestW8O2_AValidSequenceDiagramHasNoProblems in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_TheFirstLineMustBeSequenceDiagram in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_AnEmptyDiagramIsAProblem in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_UndeclaredParticipantsAreReportedWithTheirLine in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_ParticipantAliasesAreDeclarationsToo in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_EveryArrowStyleIsRecognised in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_AMessageNeedsText in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_UnrecognisedLinesAreReported in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_CommentsAndBlankLinesAreIgnored in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_BlocksMustBeClosed in backend/internal/mermaidlint/mermaidlint_test.go
  • TestW8O2_ActivateNeedsADeclaredParticipant in backend/internal/mermaidlint/mermaidlint_test.go