todo

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 39 Imported by: 0

README

TODO

A library for parsing structured TODO comments out of code.

Implementation

  • Tree-Sitter is used to parse comments out of source files.
  • TODOs are extracted from the comments using a recursive descent parser.

Syntax

Each valid TODO line must begin with TODO. It may optionally include a comma‐separated attribute list in parentheses immediately following TODO, but must always include a colon (:). Everything after the colon is treated as the description.

Basic Structure
TODO(key1, key2=value, key3="quoted value", ...): description

Without attributes, the line still requires the colon:

TODO: description
Attributes
  • Attributes appear inside parentheses right after TODO.
  • They are separated by commas.
  • Each attribute can be:
  1. Bare Key - (ex: 2025-03-06)
  2. Unquoted Key–Value - (ex: key=value)
  3. Quoted Key–Value - (ex: author="John Doe")

Within quoted values, \" is interpreted as " and \\ as a literal backslash \.

Examples
// TODO: no attributes
// TODO(foo,bar): 2 key-only attributes
// TODO(created=2025-03-09, assigned=john): multiple key/value attributes
// TODO(deadline="June 2025"): quoted value 

CLI Tool

This repository comes with a very minimal tool for testing this package. It parses TODO comments from source files outputs them in JSON format.

Install
go install github.com/icholy/todo/cmd/todo@latest
Usage
$ todo ./**/*.go
{
  "Location": "./todo.go:88",
  "Line": "// TODO: investigate compilation error",
  "Description": "investigate compilation error",
  "Attributes": {}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LanguageFor

func LanguageFor(file string) (*sitter.Language, bool)

LanguageFor returns the language for the given file name

Types

type Attribute

type Attribute struct {
	Key   string
	Value string
	Quote bool
}

Attribute represents a key=value pair

type Location

type Location struct {
	File string
	Line int
}

Location represents a file location

type Todo

type Todo struct {
	Line        string
	Location    Location
	Description string
	Attributes  []Attribute
}

Todo represents a TODO line

func Parse

func Parse(ctx context.Context, file string, source []byte) ([]Todo, error)

Parse parses the source and returns all TODO comments.

func ParseCode

func ParseCode(ctx context.Context, file string, source []byte, lang *sitter.Language) ([]Todo, error)

ParseCode parses the source code and returns all TODO comments

func ParseLine

func ParseLine(line string) (Todo, bool)

ParseLine parses a single TODO line. Does not set the Location or Line fields.

func ParseText

func ParseText(file, text string) []Todo

ParseText parses a text string and returns all TODO comments

Directories

Path Synopsis
cmd
todo command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL