fix(runtime): tokenizeAnsi('') returns a single empty text token, matching Ink (#95)

Ink's tokenizeAnsi has no empty-string early return — '' falls through to the
no-control-chars branch and yields [{type:'text', value:''}]. vue had an extra
`if (text.length === 0) return []` guard that diverged from Ink at the tokenizer
boundary. Removed it (the production caller sanitizeAnsi short-circuits '' before
tokenizing, so no behavior changes downstream) and flipped the test.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yunfei He
2026-05-31 20:59:55 +08:00
committed by GitHub
parent 9ed77fc38c
commit eea9fb6b72
2 changed files with 6 additions and 6 deletions
@@ -44,9 +44,11 @@ describe("ansi-tokenizer", () => {
expect(tokens[0]!.type).toBe("csi");
});
test("returns empty for empty string", () => {
test("empty string returns a single empty text token (Ink parity)", () => {
// Ink's tokenizeAnsi('') falls through to the no-control-chars branch and
// returns [{type:'text', value:''}] — it has no empty-string early return.
const tokens = tokenizeAnsi("");
expect(tokens).toHaveLength(0);
expect(tokens).toEqual([{ type: "text", value: "" }]);
});
// --- Ink parity tests ---
+2 -4
View File
@@ -322,10 +322,8 @@ const malformedFromIndex = (
};
export const tokenizeAnsi = (text: string): AnsiToken[] => {
if (text.length === 0) {
return [];
}
// No empty-string early return: Ink falls through to the no-control-chars
// branch so tokenizeAnsi('') === [{type:'text', value:''}] (ansi-tokenizer.ts).
if (!hasAnsiControlCharacters(text)) {
return [{ type: "text", value: text }];
}