Skip to content

Overhaul Span to track start/end positions instead of offset+length #11

Description

@saiashirwad

The current Span type is stored as a single point plus a byte length:

type Span = {
  offset: number
  length: number
  line: number
  column: number
}

This is fine for simple single-line errors, but it starts to break down once we want:

  • Multi-line spans (e.g. an unclosed string that runs for several lines)
  • Related spans ("unclosed ( opened here")
  • Clean conversion to LSP / JSON / SARIF output formats
  • Span unions and precise end positions

I'd like to move to a start/end position model:

type Position = {
  byteOffset: number
  charOffset: number
  line: number
  column: number
}

type Span = {
  start: Position
  end: Position
}

byteOffset stays for fast source.slice(...), and charOffset/column become user-facing. This is a breaking change to the public Span shape, so it should be done before a 1.0 release if possible.

Open questions:

  • Should we keep the old Span(state, length) factory for backwards compatibility?
  • Should Span carry a cached source slice, or keep slicing on demand in the formatter?
  • How does this affect Parser.spanned() and the internal Span() helper in errors.ts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions