Repository navigation
Add Polytypo::analyze(), the reported-edits entry point - #4
Merged
Merged
Conversation
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"?', 'en-US') // [ Change { ruleId: "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), 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. Two places this could quietly go wrong in PHP specifically, both tested: - Offsets are code points, not bytes. `strpos` on `😀 and "this"` says 9 and analyze() says 6. Every ASCII test passes either way, which is what makes it worth an explicit case rather than a comment. - In html mode 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 (analyze.md §6). Spans::originOfSpans maps them back to document offsets, asserted in a three-span document as well as a single-span one. markdown is rejected by analyze() exactly as transform() rejects it, through the same transformMarkdown() — A1 requires the two entry points to reject the same arguments with the same code, and this runtime implements no markdown dialect. Seventeen tests, including A3 against the entire vendored fixture corpus — empty list exactly when in == out — and analyze.md §5's French overlap at the spec's own offsets, spaces at 3–4 and nbsp at 4–4 on `Oui : non`. Full suite 1119 tests, 2907 assertions, 21 skipped (the markdown cases this runtime declines); PHPStan level 8 and phpcs clean. transform()'s behaviour is untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Sep 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ports spec 1.3.0's
spec/rules/analyze.md(canonical issue polytypo/polytypo#15), after polytypo/polytypo-js#13, polytypo/polytypo-python#4, polytypo/polytypo-go#4 and polytypo/polytypo-ruby#4. This is the fifth and last runtime.Polytypo::analyzeruns the identical pipeline astransform()and reports what it would do instead of doing it. Contract A1–A5: same arguments and same error codes, pure, empty exactly whentransform()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).Two PHP-specific traps, both covered by tests rather than comments:
😀 and "this",strpossays 9 andanalyze()says 6. Every ASCII case passes either way.htmlmode the rules run over the marker-joined span concatenation; reporting those offsets passes every text-mode test and is useless to a caller (analyze.md §6).Spans::originOfSpansmaps them back, asserted in a three-span document too.markdownis rejected byanalyze()through the sametransformMarkdown()thattransform()uses — A1 requires both entry points to reject the same arguments with the same code, and this runtime implements no markdown dialect.Checks: 1119 tests, 2907 assertions, 21 skipped (the markdown cases this runtime declines); PHPStan level 8 clean; phpcs clean — all run locally on PHP 8.3.32.
transform()'s behaviour is untouched.🤖 Generated with Claude Code