import { describe, expect, it } from "bun:test"; import grammar from "../syntaxes/gram.tmLanguage.json"; // Since 1.6.0 an alias is glued to the colon (`@salt:fine{2 g}`): a blank // after it closes the name, and the compiler reads `@salt` then text // (ALIAS_AFTER_BLANK). The highlighting has to read it the same way, so the // braced rules of ingredients and cookware must not take ` fine` as an alias. type Rule = { name: string; begin?: string; match?: string }; const repository = grammar.repository as Record; const rule = (group: string, name: string) => { const found = repository[group]?.patterns.find((p) => p.name === name); return new RegExp((found?.begin ?? found?.match) as string, "u"); }; const ingredient = rule("ingredients", "meta.ingredient.gram"); const bareIngredient = rule("ingredients", "meta.ingredient.bare.gram"); const cookware = rule("cookware", "meta.cookware.gram"); /** The name and alias the braced rule reads at the start of `text`, or null. */ function braced(regex: RegExp, text: string) { const m = regex.exec(text); if (!m || m.index !== 0) return null; return { name: m[3], alias: m[5] }; } describe("TextMate alias", () => { it("reads an alias glued to the colon, in one or several words", () => { expect(braced(ingredient, "@salt:fine{2 g}")).toEqual({ name: "salt", alias: "fine", }); expect(braced(ingredient, "@sel:fleur de sel{1 g}")).toEqual({ name: "sel", alias: "fleur de sel", }); expect(braced(cookware, "#pan:big pan{}")).toEqual({ name: "pan", alias: "big pan", }); }); it("reads no alias after a blank: the braced rule does not start there", () => { expect(braced(ingredient, "@salt: fine{2 g}")).toBeNull(); expect(braced(ingredient, "@dry white wine: wine{10 ml}")).toBeNull(); expect(braced(cookware, "#big pan: hot{}")).toBeNull(); }); it("leaves the bare name, as the compiler reads it", () => { const m = bareIngredient.exec("@salt: fine{2 g}"); expect(m?.[0]).toBe("@salt"); expect(m?.[5]).toBeUndefined(); }); });