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:
func Lint(src string) []stringAcceptance criteria
- A valid sequence diagram containing
autonumber, participant declarations, an actor declaration, a participant alias, aNoteline, and a supported message produces no diagnostics. - The first non-blank line must be exactly
sequenceDiagram. For input beginning withflowchart LR, returnline 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 MySQLallowsDB->>DB: commitwithout diagnostics. - Messages referencing undeclared participants report their names and source lines. With only
Appdeclared,App->>Cache: GETon line 3 reportsline 3: undeclared participant Cache, andWorker->>App: doneon line 4 reportsline 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 mermaidreportsline 3: unrecognised line. - Blank lines and lines beginning with
%%after trimming surrounding whitespace are ignored. - Block openings and closings must balance. A balanced
loopproduces no diagnostics. An unclosedaltopened on line 3 reportsline 3: block opened here is never closed with end. Anendon line 3 without an open block reportsline 3: end without an open block. activateanddeactivatecommands must reference declared participants. WithAdeclared,activate Aproduces no diagnostic, whiledeactivate Bon line 4 reportsline 4: undeclared participant B.
Constraints
- Keep the implementation in package
mermaidlintand provide the exportedLintsignature 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:
cd backend
make test W=8O2Read 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement
Lintin packagemermaidlint. - Recognise participant declarations, aliases, and all eight message arrows.
- Report undeclared participants and missing message text with source line numbers.
- Validate block boundaries and activation commands.
- Run
make test W=8O2without modifying the provided tests.
Commands
cd backend && make test W=8O2Tests
TestW8O2_AValidSequenceDiagramHasNoProblemsinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_TheFirstLineMustBeSequenceDiagraminbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_AnEmptyDiagramIsAProbleminbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_UndeclaredParticipantsAreReportedWithTheirLineinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_ParticipantAliasesAreDeclarationsTooinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_EveryArrowStyleIsRecognisedinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_AMessageNeedsTextinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_UnrecognisedLinesAreReportedinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_CommentsAndBlankLinesAreIgnoredinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_BlocksMustBeClosedinbackend/internal/mermaidlint/mermaidlint_test.goTestW8O2_ActivateNeedsADeclaredParticipantinbackend/internal/mermaidlint/mermaidlint_test.go