Skip to content

Add analyze(), the reported-edits entry point - #4

Merged
rogulia merged 1 commit into
mainfrom
feat/analyze
Sep 18, 2026
Merged

rogulia merged 1 commit into
mainfrom
feat/analyze

Conversation

@rogulia

@rogulia rogulia commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Ports spec 1.3.0's spec/rules/analyze.md (canonical issue polytypo/polytypo#15), after polytypo/polytypo-js#13.

analyze() runs the identical pipeline as transform and reports what it would do instead of doing it. Contract A1–A5: same arguments and same error codes, pure, empty exactly when transform returns the input unchanged, only rules enabled for the call, code-point offsets inside the input. The decomposition into records is explicitly not contract (analyze.md §4), so no fixture asserts it and the five runtimes are not required to agree on it.

Nothing in the engine changed. The whole feature is an origin map travelling beside the code-point array (_engine/origin.py); a same-length replacement keeps its origins position by position, which is what makes a converted space still point at the space it converted.

The place this could go quietly wrong is html/markdown mode, where rules run over the marker-joined span concatenation: reporting those offsets passes every text-mode test and is useless to a caller. origin_of_spans() maps them back to document offsets — a range() here, since a Python Span carries code-point offsets already, unlike the UTF-16 index table polytypo-js needs.

Checks: 1176 tests pass (--ignore=tests/packaging), tests/packaging 4 pass separately, ruff check/ruff format --check clean, mypy --strict src clean. transform's behaviour is untouched.

🤖 Generated with Claude Code

Implements spec 1.3.0's spec/rules/analyze.md (polytypo/polytypo@691c180,
canonical issue #15), porting polytypo-js@1a2ef57. Same pipeline as transform,
reporting what it would do instead of doing it:

  polytypo.analyze('Wait... "really"?', locale="en-US")
  # [Change(rule_id='ellipsis', start=4, end=7, before='...', after='…'), …]

Nothing in the engine changed to support it. Rules already produce edits and the
pipeline already applies them, so the whole feature is one structure: an origin
map travelling alongside the code-point array (_engine/origin.py), holding the
input offset each code point came from. A same-length replacement keeps its
origins position by position, which is what makes a converted space still point
at the space it converted.

The mode adapters are where this could quietly go wrong, and analyze.md §6 says
so: in html and markdown the rules run over the marker-joined concatenation of
spans, so reporting those offsets would pass every text-mode test and be useless
to a caller. origin_of_spans() maps them back to document offsets. A Python Span
carries code-point offsets already, so that conversion is a range() here where
polytypo-js needs a UTF-16 index table — the cheap half of this port, and the
reason the offset tests assert document positions in a three-span document too,
not only in a single-span one.

Eighteen tests, one per contract clause plus the three that matter most: A3
against the entire vendored fixture corpus (empty list exactly when in == out),
A5 across the mode adapters, and analyze.md §5's French overlap asserted at the
spec's own offsets — spaces at 3–4 and nbsp at 4–4 on `Oui : non`.

Full suite 1176 pass, packaging 4 pass, ruff and mypy --strict clean.
transform's behaviour is untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant