@warlock.js/grimoire 5.63.1 → 5.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -0
- package/esm/book/build.d.mts.map +1 -1
- package/esm/book/build.mjs +24 -2
- package/esm/book/build.mjs.map +1 -1
- package/esm/check/check-grimoire.d.mts.map +1 -1
- package/esm/check/check-grimoire.mjs +11 -1
- package/esm/check/check-grimoire.mjs.map +1 -1
- package/esm/check/rules/api-snapshot.mjs +39 -0
- package/esm/check/rules/api-snapshot.mjs.map +1 -0
- package/esm/check/rules/code-rules.mjs +45 -0
- package/esm/check/rules/code-rules.mjs.map +1 -0
- package/esm/check/rules/drift.mjs +54 -0
- package/esm/check/rules/drift.mjs.map +1 -0
- package/esm/check/rules/focused-tests.mjs +39 -0
- package/esm/check/rules/focused-tests.mjs.map +1 -0
- package/esm/check/rules/shows.mjs +52 -0
- package/esm/check/rules/shows.mjs.map +1 -0
- package/esm/codes/codes.d.mts +48 -0
- package/esm/codes/codes.d.mts.map +1 -1
- package/esm/codes/codes.mjs +26 -1
- package/esm/codes/codes.mjs.map +1 -1
- package/esm/contracts/adapter.d.mts +6 -0
- package/esm/contracts/adapter.d.mts.map +1 -1
- package/esm/contracts/guard.d.mts +104 -0
- package/esm/contracts/guard.d.mts.map +1 -0
- package/esm/contracts/index.mjs +5 -0
- package/esm/contracts/matrix.d.mts +75 -0
- package/esm/contracts/matrix.d.mts.map +1 -0
- package/esm/contracts/scry.d.mts +42 -0
- package/esm/contracts/scry.d.mts.map +1 -1
- package/esm/contracts/settings.d.mts +71 -0
- package/esm/contracts/settings.d.mts.map +1 -0
- package/esm/contracts/settings.mjs +67 -0
- package/esm/contracts/settings.mjs.map +1 -0
- package/esm/contracts/shots.d.mts +146 -0
- package/esm/contracts/shots.d.mts.map +1 -0
- package/esm/contracts/snapshot.d.mts +10 -0
- package/esm/contracts/snapshot.d.mts.map +1 -1
- package/esm/contracts/written.d.mts +13 -0
- package/esm/contracts/written.d.mts.map +1 -1
- package/esm/contracts/written.mjs +7 -1
- package/esm/contracts/written.mjs.map +1 -1
- package/esm/devtools/approvals.mjs +59 -0
- package/esm/devtools/approvals.mjs.map +1 -0
- package/esm/devtools/data.mjs +2 -0
- package/esm/devtools/data.mjs.map +1 -1
- package/esm/devtools/graphs.mjs +13 -3
- package/esm/devtools/graphs.mjs.map +1 -1
- package/esm/devtools/index.d.mts +7 -0
- package/esm/devtools/index.d.mts.map +1 -1
- package/esm/devtools/index.mjs +33 -3
- package/esm/devtools/index.mjs.map +1 -1
- package/esm/devtools/matrix.mjs +207 -0
- package/esm/devtools/matrix.mjs.map +1 -0
- package/esm/devtools/product-map.d.mts.map +1 -1
- package/esm/devtools/product-map.mjs +87 -1
- package/esm/devtools/product-map.mjs.map +1 -1
- package/esm/devtools/site.d.mts.map +1 -1
- package/esm/devtools/site.mjs +12 -3
- package/esm/devtools/site.mjs.map +1 -1
- package/esm/devtools/snapshot-source.mjs +13 -3
- package/esm/devtools/snapshot-source.mjs.map +1 -1
- package/esm/git/index.d.mts +20 -0
- package/esm/git/index.d.mts.map +1 -0
- package/esm/git/index.mjs +280 -0
- package/esm/git/index.mjs.map +1 -0
- package/esm/guard/api.mjs +70 -0
- package/esm/guard/api.mjs.map +1 -0
- package/esm/guard/assertions.mjs +18 -0
- package/esm/guard/assertions.mjs.map +1 -0
- package/esm/guard/collect.mjs +139 -0
- package/esm/guard/collect.mjs.map +1 -0
- package/esm/guard/format.mjs +27 -0
- package/esm/guard/format.mjs.map +1 -0
- package/esm/guard/glob.mjs +79 -0
- package/esm/guard/glob.mjs.map +1 -0
- package/esm/guard/index.mjs +9 -0
- package/esm/guard/rules.mjs +310 -0
- package/esm/guard/rules.mjs.map +1 -0
- package/esm/guard/scope.mjs +95 -0
- package/esm/guard/scope.mjs.map +1 -0
- package/esm/index.d.mts +10 -3
- package/esm/index.mjs +6 -2
- package/esm/paths/index.d.mts +15 -1
- package/esm/paths/index.d.mts.map +1 -1
- package/esm/paths/index.mjs +27 -3
- package/esm/paths/index.mjs.map +1 -1
- package/esm/proves/index.mjs +2 -1
- package/esm/proves/scan-proofs.mjs +1 -1
- package/esm/proves/scan-proofs.mjs.map +1 -1
- package/esm/proves/scan-shows.d.mts +21 -0
- package/esm/proves/scan-shows.d.mts.map +1 -0
- package/esm/proves/scan-shows.mjs +168 -0
- package/esm/proves/scan-shows.mjs.map +1 -0
- package/esm/releases/index.d.mts.map +1 -1
- package/esm/releases/index.mjs +5 -3
- package/esm/releases/index.mjs.map +1 -1
- package/esm/releases/stamp.mjs +121 -0
- package/esm/releases/stamp.mjs.map +1 -0
- package/esm/rules/focused.mjs +26 -0
- package/esm/rules/focused.mjs.map +1 -0
- package/esm/rules/index.mjs +88 -0
- package/esm/rules/index.mjs.map +1 -0
- package/esm/rules/logical-css.mjs +112 -0
- package/esm/rules/logical-css.mjs.map +1 -0
- package/esm/rules/no-js-imports.mjs +22 -0
- package/esm/rules/no-js-imports.mjs.map +1 -0
- package/esm/rules/scan.mjs +196 -0
- package/esm/rules/scan.mjs.map +1 -0
- package/esm/rules/specific-errors.mjs +51 -0
- package/esm/rules/specific-errors.mjs.map +1 -0
- package/esm/rules/type-discriminator.mjs +27 -0
- package/esm/rules/type-discriminator.mjs.map +1 -0
- package/esm/scry/also-changes.mjs +107 -0
- package/esm/scry/also-changes.mjs.map +1 -0
- package/esm/scry/format.d.mts.map +1 -1
- package/esm/scry/format.mjs +35 -0
- package/esm/scry/format.mjs.map +1 -1
- package/esm/scry/query.d.mts.map +1 -1
- package/esm/scry/query.mjs +40 -2
- package/esm/scry/query.mjs.map +1 -1
- package/esm/scry/search.d.mts.map +1 -1
- package/esm/scry/search.mjs +43 -11
- package/esm/scry/search.mjs.map +1 -1
- package/esm/shots/approve.mjs +195 -0
- package/esm/shots/approve.mjs.map +1 -0
- package/esm/shots/drift.mjs +174 -0
- package/esm/shots/drift.mjs.map +1 -0
- package/esm/shots/png.mjs +166 -0
- package/esm/shots/png.mjs.map +1 -0
- package/esm/shots/state.mjs +43 -0
- package/esm/shots/state.mjs.map +1 -0
- package/esm/shots/store.mjs +66 -0
- package/esm/shots/store.mjs.map +1 -0
- package/esm/shots/touched.mjs +38 -0
- package/esm/shots/touched.mjs.map +1 -0
- package/esm/warlock/adapter.d.mts.map +1 -1
- package/esm/warlock/adapter.mjs +22 -2
- package/esm/warlock/adapter.mjs.map +1 -1
- package/esm/warlock/api-command.mjs +41 -0
- package/esm/warlock/api-command.mjs.map +1 -0
- package/esm/warlock/approve-command.mjs +80 -0
- package/esm/warlock/approve-command.mjs.map +1 -0
- package/esm/warlock/collect-off-thread.mjs +66 -0
- package/esm/warlock/collect-off-thread.mjs.map +1 -0
- package/esm/warlock/commands.d.mts +1 -1
- package/esm/warlock/commands.d.mts.map +1 -1
- package/esm/warlock/commands.mjs +61 -8
- package/esm/warlock/commands.mjs.map +1 -1
- package/esm/warlock/guard-command.mjs +116 -0
- package/esm/warlock/guard-command.mjs.map +1 -0
- package/esm/warlock/index.d.mts +2 -1
- package/esm/warlock/index.mjs +2 -1
- package/esm/warlock/languages.mjs +75 -0
- package/esm/warlock/languages.mjs.map +1 -0
- package/esm/warlock/mcp-tools.mjs +2 -2
- package/esm/warlock/mcp-tools.mjs.map +1 -1
- package/esm/warlock/release-command.mjs +65 -0
- package/esm/warlock/release-command.mjs.map +1 -0
- package/esm/warlock/shows.d.mts +35 -0
- package/esm/warlock/shows.d.mts.map +1 -0
- package/esm/warlock/shows.mjs +195 -0
- package/esm/warlock/shows.mjs.map +1 -0
- package/esm/workspace/cache.mjs +4 -1
- package/esm/workspace/cache.mjs.map +1 -1
- package/esm/workspace/derived.mjs +11 -6
- package/esm/workspace/derived.mjs.map +1 -1
- package/esm/workspace/index.d.mts.map +1 -1
- package/esm/workspace/index.mjs +8 -1
- package/esm/workspace/index.mjs.map +1 -1
- package/esm/workspace/stored.mjs +4 -1
- package/esm/workspace/stored.mjs.map +1 -1
- package/esm/written/features.mjs +21 -5
- package/esm/written/features.mjs.map +1 -1
- package/esm/written/index.mjs +2 -1
- package/esm/written/parse-grimoire.d.mts.map +1 -1
- package/esm/written/parse-grimoire.mjs +16 -3
- package/esm/written/parse-grimoire.mjs.map +1 -1
- package/esm/written/settings.d.mts +11 -0
- package/esm/written/settings.d.mts.map +1 -0
- package/esm/written/settings.mjs +88 -0
- package/esm/written/settings.mjs.map +1 -0
- package/llms-full.txt +282 -39
- package/llms.txt +2 -0
- package/package.json +3 -3
- package/skills/backfill-a-grimoire/SKILL.md +4 -0
- package/skills/describe-an-app/SKILL.md +4 -1
- package/skills/guard-a-change/SKILL.md +85 -0
- package/skills/index.md +7 -3
- package/skills/record-a-change/SKILL.md +52 -16
- package/skills/review-a-change/SKILL.md +53 -0
- package/skills/use-the-grimoire/SKILL.md +27 -18
- package/skills/write-a-feature/SKILL.md +42 -1
- package/ui/grimoire-view.js +756 -40
- package/ui/grimoire.css +303 -28
- package/ui/product-map.css +43 -7
- package/ui/product-map.js +212 -37
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { GrimoireError } from "../codes/index.mjs";
|
|
2
|
+
import { releaseFilePath } from "../paths/index.mjs";
|
|
3
|
+
import { isDate } from "../written/fields.mjs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
|
|
7
|
+
//#region ../../packages/grimoire/src/releases/stamp.ts
|
|
8
|
+
/**
|
|
9
|
+
* Release stamping (`warlock grimoire release --stamp`): moves the entries under
|
|
10
|
+
* `## Unreleased` of each feature changelog under `## <version> (<date>)` and
|
|
11
|
+
* writes the release file when it is missing. It is the one edit Warlock makes to
|
|
12
|
+
* written files, and only on request.
|
|
13
|
+
*
|
|
14
|
+
* `planStamp()` is pure (text in, text out). `applyStamp()` is the thin writer.
|
|
15
|
+
*/
|
|
16
|
+
const UNRELEASED = /^##[ \t]+unreleased[ \t]*$/i;
|
|
17
|
+
/** A section heading of a feature changelog: level 2 only (`### Added` stays inside its section). */
|
|
18
|
+
const HEADING = /^##[ \t]/;
|
|
19
|
+
/** A release section heading, the way the changelog parser reads it. */
|
|
20
|
+
const RELEASE_HEADING = /^##[ \t]+(\S+)[ \t]+\((.+)\)$/;
|
|
21
|
+
function refuse(message) {
|
|
22
|
+
throw new GrimoireError("GR9006", message);
|
|
23
|
+
}
|
|
24
|
+
/** "a", "a and b", "a, b and c". */
|
|
25
|
+
function listOf(items) {
|
|
26
|
+
if (items.length <= 1) return items.join("");
|
|
27
|
+
return `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The new text of one changelog: the `## Unreleased` heading line stays as the
|
|
31
|
+
* (now empty) unreleased section, and `## <version> (<date>)` and a blank line
|
|
32
|
+
* follow it. When the changelog already has a section for the version (same date),
|
|
33
|
+
* the unreleased lines move to the top of that section instead.
|
|
34
|
+
*/
|
|
35
|
+
function stampText(text, file, version, date, hasSection) {
|
|
36
|
+
const lines = text.split("\n");
|
|
37
|
+
const cr = (line) => line.endsWith("\r") ? "\r" : "";
|
|
38
|
+
const start = lines.findIndex((line) => UNRELEASED.test(line.trimEnd()));
|
|
39
|
+
if (start === -1) refuse(`${file} has no "## Unreleased" heading to stamp.`);
|
|
40
|
+
const heading = `## ${version} (${date})`;
|
|
41
|
+
if (!hasSection) {
|
|
42
|
+
const fresh = lines[start] ?? "";
|
|
43
|
+
lines.splice(start, 1, fresh, cr(fresh), `${heading}${cr(fresh)}`);
|
|
44
|
+
return lines.join("\n");
|
|
45
|
+
}
|
|
46
|
+
let end = start + 1;
|
|
47
|
+
while (end < lines.length && !HEADING.test(lines[end] ?? "")) end++;
|
|
48
|
+
const body = lines.slice(start + 1, end);
|
|
49
|
+
while (body.length > 0 && (body[body.length - 1] ?? "").trim() === "") body.pop();
|
|
50
|
+
while (body.length > 0 && (body[0] ?? "").trim() === "") body.shift();
|
|
51
|
+
const out = [
|
|
52
|
+
...lines.slice(0, start + 1),
|
|
53
|
+
cr(lines[start] ?? ""),
|
|
54
|
+
...lines.slice(end)
|
|
55
|
+
];
|
|
56
|
+
const at = out.findIndex((line) => {
|
|
57
|
+
const match = RELEASE_HEADING.exec(line.trimEnd());
|
|
58
|
+
return match !== null && match[1] === version && match[2]?.trim() === date;
|
|
59
|
+
});
|
|
60
|
+
if (at === -1) refuse(`${file} has no "${heading}" heading to stamp.`);
|
|
61
|
+
out.splice(at + 1, 0, ...body);
|
|
62
|
+
return out.join("\n");
|
|
63
|
+
}
|
|
64
|
+
/** Plan a stamp: the new changelog texts and the release file to create. Pure. */
|
|
65
|
+
function planStamp(input) {
|
|
66
|
+
const { version, date, written, texts } = input;
|
|
67
|
+
if (!isDate(date)) refuse(`The release date must be yyyy-mm-dd (got ${JSON.stringify(date)}).`);
|
|
68
|
+
const changelogs = [];
|
|
69
|
+
const titles = [];
|
|
70
|
+
for (const feature of written.features) {
|
|
71
|
+
const changelog = feature.changelog;
|
|
72
|
+
if (changelog === null || !changelog.entries.some((entry) => entry.release === null)) continue;
|
|
73
|
+
const same = changelog.sections.find((section) => section.release === version);
|
|
74
|
+
if (same !== void 0 && same.date !== date) refuse(`${changelog.file} already has a section for ${version} dated ${same.date ?? ""}, not ${date}. Use that date, or correct the heading.`);
|
|
75
|
+
const text = texts[changelog.file];
|
|
76
|
+
if (text === void 0) refuse(`The text of ${changelog.file} was not given.`);
|
|
77
|
+
changelogs.push({
|
|
78
|
+
feature: feature.id,
|
|
79
|
+
file: changelog.file,
|
|
80
|
+
text: stampText(text, changelog.file, version, date, same !== void 0)
|
|
81
|
+
});
|
|
82
|
+
titles.push(feature.readme?.title.value ?? feature.id);
|
|
83
|
+
}
|
|
84
|
+
const file = releaseFilePath(version);
|
|
85
|
+
const exists = written.releases.some((item) => item.release.value === version);
|
|
86
|
+
const brief = titles.length === 0 ? "No feature changes in this release." : `Changes to ${listOf(titles)}.`;
|
|
87
|
+
return {
|
|
88
|
+
version,
|
|
89
|
+
date,
|
|
90
|
+
changelogs,
|
|
91
|
+
release: exists ? null : {
|
|
92
|
+
file,
|
|
93
|
+
text: `---\nrelease: ${version}\ndate: ${date}\n---\n\n# Release ${version}\n\n${brief}\n`
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** Read the changelogs, plan the stamp and write it. Returns the plan. */
|
|
98
|
+
async function applyStamp(root, written, version, date) {
|
|
99
|
+
const texts = {};
|
|
100
|
+
for (const feature of written.features) {
|
|
101
|
+
const file = feature.changelog?.file;
|
|
102
|
+
if (file !== void 0) texts[file] = await readFile(path.join(root, ...file.split("/")), "utf8");
|
|
103
|
+
}
|
|
104
|
+
const plan = planStamp({
|
|
105
|
+
version,
|
|
106
|
+
date,
|
|
107
|
+
written,
|
|
108
|
+
texts
|
|
109
|
+
});
|
|
110
|
+
for (const item of plan.changelogs) await writeFile(path.join(root, ...item.file.split("/")), item.text);
|
|
111
|
+
if (plan.release !== null) {
|
|
112
|
+
const absolute = path.join(root, ...plan.release.file.split("/"));
|
|
113
|
+
await mkdir(path.dirname(absolute), { recursive: true });
|
|
114
|
+
await writeFile(absolute, plan.release.text);
|
|
115
|
+
}
|
|
116
|
+
return plan;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
export { applyStamp, planStamp };
|
|
121
|
+
//# sourceMappingURL=stamp.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamp.mjs","names":[],"sources":["../../../../../../../../packages/grimoire/src/releases/stamp.ts"],"sourcesContent":["/**\n * Release stamping (`warlock grimoire release --stamp`): moves the entries under\n * `## Unreleased` of each feature changelog under `## <version> (<date>)` and\n * writes the release file when it is missing. It is the one edit Warlock makes to\n * written files, and only on request.\n *\n * `planStamp()` is pure (text in, text out). `applyStamp()` is the thin writer.\n */\nimport { mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { GrimoireError } from \"../codes\";\nimport type { WrittenGrimoire } from \"../contracts/written\";\nimport { releaseFilePath } from \"../paths\";\nimport { isDate } from \"../written/fields\";\n\nconst UNRELEASED = /^##[ \\t]+unreleased[ \\t]*$/i;\n/** A section heading of a feature changelog: level 2 only (`### Added` stays inside its section). */\nconst HEADING = /^##[ \\t]/;\n/** A release section heading, the way the changelog parser reads it. */\nconst RELEASE_HEADING = /^##[ \\t]+(\\S+)[ \\t]+\\((.+)\\)$/;\n\nexport type StampInput = {\n /** The release name, such as `1.4.0`. */\n version: string;\n /** `yyyy-mm-dd`. */\n date: string;\n written: WrittenGrimoire;\n /** The text of each feature changelog by project-relative path (at least the ones with unreleased entries). */\n texts: Readonly<Record<string, string>>;\n};\n\nexport type StampPlan = {\n version: string;\n date: string;\n /** Changelogs that change, with their new text. */\n changelogs: { feature: string; file: string; text: string }[];\n /** The release file to create, or `null` when it exists. */\n release: { file: string; text: string } | null;\n};\n\nfunction refuse(message: string): never {\n throw new GrimoireError(\"GR9006\", message);\n}\n\n/** \"a\", \"a and b\", \"a, b and c\". */\nfunction listOf(items: string[]): string {\n if (items.length <= 1) return items.join(\"\");\n\n return `${items.slice(0, -1).join(\", \")} and ${items[items.length - 1]}`;\n}\n\n/**\n * The new text of one changelog: the `## Unreleased` heading line stays as the\n * (now empty) unreleased section, and `## <version> (<date>)` and a blank line\n * follow it. When the changelog already has a section for the version (same date),\n * the unreleased lines move to the top of that section instead.\n */\nfunction stampText(\n text: string,\n file: string,\n version: string,\n date: string,\n hasSection: boolean,\n): string {\n const lines = text.split(\"\\n\");\n const cr = (line: string): string => (line.endsWith(\"\\r\") ? \"\\r\" : \"\");\n const start = lines.findIndex((line) => UNRELEASED.test(line.trimEnd()));\n\n if (start === -1) refuse(`${file} has no \"## Unreleased\" heading to stamp.`);\n\n const heading = `## ${version} (${date})`;\n\n if (!hasSection) {\n const fresh = lines[start] ?? \"\";\n\n lines.splice(start, 1, fresh, cr(fresh), `${heading}${cr(fresh)}`);\n\n return lines.join(\"\\n\");\n }\n\n // The version already has a section: move the unreleased lines to the top of it.\n let end = start + 1;\n\n while (end < lines.length && !HEADING.test(lines[end] ?? \"\")) end++;\n\n const body = lines.slice(start + 1, end);\n\n while (body.length > 0 && (body[body.length - 1] ?? \"\").trim() === \"\") body.pop();\n while (body.length > 0 && (body[0] ?? \"\").trim() === \"\") body.shift();\n\n const out = [...lines.slice(0, start + 1), cr(lines[start] ?? \"\"), ...lines.slice(end)];\n const at = out.findIndex((line) => {\n const match = RELEASE_HEADING.exec(line.trimEnd());\n\n return match !== null && match[1] === version && match[2]?.trim() === date;\n });\n\n if (at === -1) refuse(`${file} has no \"${heading}\" heading to stamp.`);\n\n out.splice(at + 1, 0, ...body);\n\n return out.join(\"\\n\");\n}\n\n/** Plan a stamp: the new changelog texts and the release file to create. Pure. */\nexport function planStamp(input: StampInput): StampPlan {\n const { version, date, written, texts } = input;\n\n if (!isDate(date)) refuse(`The release date must be yyyy-mm-dd (got ${JSON.stringify(date)}).`);\n\n const changelogs: StampPlan[\"changelogs\"] = [];\n const titles: string[] = [];\n\n for (const feature of written.features) {\n const changelog = feature.changelog;\n\n if (changelog === null || !changelog.entries.some((entry) => entry.release === null)) continue;\n\n const same = changelog.sections.find((section) => section.release === version);\n\n if (same !== undefined && same.date !== date) {\n refuse(\n `${changelog.file} already has a section for ${version} dated ${same.date ?? \"\"}, not ${date}. Use that date, or correct the heading.`,\n );\n }\n\n const text = texts[changelog.file];\n\n if (text === undefined) refuse(`The text of ${changelog.file} was not given.`);\n\n changelogs.push({\n feature: feature.id,\n file: changelog.file,\n text: stampText(text, changelog.file, version, date, same !== undefined),\n });\n titles.push(feature.readme?.title.value ?? feature.id);\n }\n\n const file = releaseFilePath(version);\n const exists = written.releases.some((item) => item.release.value === version);\n const brief =\n titles.length === 0 ? \"No feature changes in this release.\" : `Changes to ${listOf(titles)}.`;\n\n return {\n version,\n date,\n changelogs,\n release: exists\n ? null\n : {\n file,\n text: `---\\nrelease: ${version}\\ndate: ${date}\\n---\\n\\n# Release ${version}\\n\\n${brief}\\n`,\n },\n };\n}\n\n/** Read the changelogs, plan the stamp and write it. Returns the plan. */\nexport async function applyStamp(\n root: string,\n written: WrittenGrimoire,\n version: string,\n date: string,\n): Promise<StampPlan> {\n const texts: Record<string, string> = {};\n\n for (const feature of written.features) {\n const file = feature.changelog?.file;\n\n if (file !== undefined) {\n texts[file] = await readFile(path.join(root, ...file.split(\"/\")), \"utf8\");\n }\n }\n\n const plan = planStamp({ version, date, written, texts });\n\n for (const item of plan.changelogs) {\n await writeFile(path.join(root, ...item.file.split(\"/\")), item.text);\n }\n\n if (plan.release !== null) {\n const absolute = path.join(root, ...plan.release.file.split(\"/\"));\n\n await mkdir(path.dirname(absolute), { recursive: true });\n await writeFile(absolute, plan.release.text);\n }\n\n return plan;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAM,aAAa;;AAEnB,MAAM,UAAU;;AAEhB,MAAM,kBAAkB;AAqBxB,SAAS,OAAO,SAAwB;CACtC,MAAM,IAAI,cAAc,UAAU,OAAO;AAC3C;;AAGA,SAAS,OAAO,OAAyB;CACvC,IAAI,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,EAAE;CAE3C,OAAO,GAAG,MAAM,MAAM,GAAG,EAAE,CAAC,CAAC,KAAK,IAAI,EAAE,OAAO,MAAM,MAAM,SAAS;AACtE;;;;;;;AAQA,SAAS,UACP,MACA,MACA,SACA,MACA,YACQ;CACR,MAAM,QAAQ,KAAK,MAAM,IAAI;CAC7B,MAAM,MAAM,SAA0B,KAAK,SAAS,IAAI,IAAI,OAAO;CACnE,MAAM,QAAQ,MAAM,WAAW,SAAS,WAAW,KAAK,KAAK,QAAQ,CAAC,CAAC;CAEvE,IAAI,UAAU,IAAI,OAAO,GAAG,KAAK,0CAA0C;CAE3E,MAAM,UAAU,MAAM,QAAQ,IAAI,KAAK;CAEvC,IAAI,CAAC,YAAY;EACf,MAAM,QAAQ,MAAM,UAAU;EAE9B,MAAM,OAAO,OAAO,GAAG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AM;EACzB,MAAM,WAAW,KAAK,KAAK,MAAM,GAAG,KAAK,QAAQ,KAAK,MAAM,GAAG,CAAC;EAEhE,MAAM,MAAM,KAAK,QAAQ,QAAQ,GAAG,EAAE,WAAW,KAAK,CAAC;EACvD,MAAM,UAAU,UAAU,KAAK,QAAQ,IAAI;CAC7C;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { scanSource } from "./scan.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../packages/grimoire/src/rules/focused.ts
|
|
4
|
+
/**
|
|
5
|
+
* GR4011 / GR4012: tests focused with `.only` or skipped with `.skip`.
|
|
6
|
+
* `.skipIf(...)` and `.todo` are not flagged; comments and strings are ignored.
|
|
7
|
+
*/
|
|
8
|
+
const FOCUSED = /\b(describe|it|test|suite)((?:\.(?:concurrent|sequential))?)\.(only|skip)\b/g;
|
|
9
|
+
/** Find `.only` and `.skip` calls in a test file's text. */
|
|
10
|
+
function findFocusedTests(text) {
|
|
11
|
+
const { bare } = scanSource(text, "code");
|
|
12
|
+
const hits = [];
|
|
13
|
+
for (const match of bare.matchAll(FOCUSED)) {
|
|
14
|
+
const offset = match.index ?? 0;
|
|
15
|
+
hits.push({
|
|
16
|
+
line: bare.slice(0, offset).split("\n").length,
|
|
17
|
+
type: match[3] === "only" ? "only" : "skip",
|
|
18
|
+
call: match[0]
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return hits;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { findFocusedTests };
|
|
26
|
+
//# sourceMappingURL=focused.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focused.mjs","names":[],"sources":["../../../../../../../../packages/grimoire/src/rules/focused.ts"],"sourcesContent":["/**\n * GR4011 / GR4012: tests focused with `.only` or skipped with `.skip`.\n * `.skipIf(...)` and `.todo` are not flagged; comments and strings are ignored.\n */\nimport { scanSource } from \"./scan\";\n\n/** One focused or skipped test call. */\nexport type FocusedHit = { line: number; type: \"only\" | \"skip\"; call: string };\n\nconst FOCUSED = /\\b(describe|it|test|suite)((?:\\.(?:concurrent|sequential))?)\\.(only|skip)\\b/g;\n\n/** Find `.only` and `.skip` calls in a test file's text. */\nexport function findFocusedTests(text: string): FocusedHit[] {\n const { bare } = scanSource(text, \"code\");\n const hits: FocusedHit[] = [];\n\n for (const match of bare.matchAll(FOCUSED)) {\n const offset = match.index ?? 0;\n\n hits.push({\n line: bare.slice(0, offset).split(\"\\n\").length,\n type: match[3] === \"only\" ? \"only\" : \"skip\",\n call: match[0],\n });\n }\n\n return hits;\n}\n"],"mappings":";;;;;;;AASA,MAAM,UAAU;;AAGhB,SAAgB,iBAAiB,MAA4B;CAC3D,MAAM,EAAE,SAAS,WAAW,MAAM,MAAM;CACxC,MAAM,OAAqB,CAAC;CAE5B,KAAK,MAAM,SAAS,KAAK,SAAS,OAAO,GAAG;EAC1C,MAAM,SAAS,MAAM,SAAS;EAE9B,KAAK,KAAK;GACR,MAAM,KAAK,MAAM,GAAG,MAAM,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC;GACxC,MAAM,MAAM,OAAO,SAAS,SAAS;GACrC,MAAM,MAAM;EACd,CAAC;CACH;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { CODE_RULES } from "../contracts/settings.mjs";
|
|
2
|
+
import { logicalCss } from "./logical-css.mjs";
|
|
3
|
+
import { noJsImports } from "./no-js-imports.mjs";
|
|
4
|
+
import { scanSource } from "./scan.mjs";
|
|
5
|
+
import { isGenericError, phraseOf, specificErrors } from "./specific-errors.mjs";
|
|
6
|
+
import { typeDiscriminator } from "./type-discriminator.mjs";
|
|
7
|
+
|
|
8
|
+
//#region ../../packages/grimoire/src/rules/index.ts
|
|
9
|
+
/**
|
|
10
|
+
* The code rules: four text rules over a file's content, shared by
|
|
11
|
+
* `grimoire check` (whole files), the guard (added lines) and the specs.
|
|
12
|
+
*
|
|
13
|
+
* Escape hatch: a comment on the line or the line above reading
|
|
14
|
+
* `grimoire-allow <rule>: <why>` (several rules: `grimoire-allow a, b: why`).
|
|
15
|
+
* Limits: the scanner is not a parser (see `scan.ts`), and editors show no
|
|
16
|
+
* squiggles for these rules.
|
|
17
|
+
*/
|
|
18
|
+
/** The finding code of each rule. */
|
|
19
|
+
const RULE_CODES = {
|
|
20
|
+
"logical-css": "GR3007",
|
|
21
|
+
"specific-errors": "GR3008",
|
|
22
|
+
"type-discriminator": "GR3009",
|
|
23
|
+
"no-js-imports": "GR3010"
|
|
24
|
+
};
|
|
25
|
+
const CODE_FILE = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
26
|
+
const STYLE_FILE = /\.(?:css|scss|less)$/;
|
|
27
|
+
const LOCALE_PATH = /(?:^|[/._-])(?:locales?|lang|langs|i18n|translations?)(?:[/._-]|$)/i;
|
|
28
|
+
const SPEC_FILE = /\.(?:spec|test)\.[cm]?[jt]sx?$/;
|
|
29
|
+
const SKIPPED_FOLDER = /(?:^|\/)(?:node_modules|dist|grimoire)\//;
|
|
30
|
+
/** Whether the rules read a file at all: code, style and locale JSON files, not specs, builds or `grimoire/`. */
|
|
31
|
+
function isRuleFile(file) {
|
|
32
|
+
if (SKIPPED_FOLDER.test(file) || SPEC_FILE.test(file) || file.endsWith(".d.ts")) return false;
|
|
33
|
+
if (file.endsWith(".json")) return LOCALE_PATH.test(file);
|
|
34
|
+
return CODE_FILE.test(file) || STYLE_FILE.test(file);
|
|
35
|
+
}
|
|
36
|
+
function modeOf(file) {
|
|
37
|
+
if (file.endsWith(".json")) return "json";
|
|
38
|
+
if (file.endsWith(".scss")) return "scss";
|
|
39
|
+
if (STYLE_FILE.test(file)) return "css";
|
|
40
|
+
return "code";
|
|
41
|
+
}
|
|
42
|
+
/** Lines (1-based) a `grimoire-allow` comment covers, per rule. */
|
|
43
|
+
function allowedLines(scanned) {
|
|
44
|
+
const allowed = /* @__PURE__ */ new Map();
|
|
45
|
+
for (const comment of scanned.comments) {
|
|
46
|
+
const match = /grimoire-allow\s+([a-z-]+(?:\s*,\s*[a-z-]+)*)\s*:\s*\S/.exec(comment.text);
|
|
47
|
+
if (!match) continue;
|
|
48
|
+
for (const name of (match[1] ?? "").split(",").map((part) => part.trim())) {
|
|
49
|
+
const lines = allowed.get(name) ?? /* @__PURE__ */ new Set();
|
|
50
|
+
for (let line = comment.startLine; line <= comment.endLine + 1; line++) lines.add(line);
|
|
51
|
+
allowed.set(name, lines);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return allowed;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Run the rules over one file's text. `file` decides the kind of scan (CSS,
|
|
58
|
+
* JSON locale, code) and whether it is skipped. Findings come back in line
|
|
59
|
+
* order; a rule's finding on a line a `grimoire-allow` comment covers is left out.
|
|
60
|
+
*/
|
|
61
|
+
function runCodeRules(file, text, rules = CODE_RULES) {
|
|
62
|
+
if (!isRuleFile(file)) return [];
|
|
63
|
+
const mode = modeOf(file);
|
|
64
|
+
const scanned = scanSource(text, mode);
|
|
65
|
+
const allowed = allowedLines(scanned);
|
|
66
|
+
const style = mode === "css" || mode === "scss" ? "css" : "code";
|
|
67
|
+
const locale = mode === "json" || LOCALE_PATH.test(file);
|
|
68
|
+
const findings = [];
|
|
69
|
+
const add = (rule, hits) => {
|
|
70
|
+
for (const hit of hits) {
|
|
71
|
+
if (allowed.get(rule)?.has(hit.line)) continue;
|
|
72
|
+
findings.push({
|
|
73
|
+
...hit,
|
|
74
|
+
rule,
|
|
75
|
+
code: RULE_CODES[rule]
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
for (const rule of rules) if (rule === "logical-css" && mode !== "json") add(rule, logicalCss(scanned, style));
|
|
80
|
+
else if (rule === "specific-errors" && style === "code") add(rule, specificErrors(scanned, locale));
|
|
81
|
+
else if (rule === "type-discriminator" && mode === "code") add(rule, typeDiscriminator(scanned));
|
|
82
|
+
else if (rule === "no-js-imports" && mode === "code") add(rule, noJsImports(scanned));
|
|
83
|
+
return findings.sort((left, right) => left.line - right.line || left.column - right.column);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
export { RULE_CODES, isRuleFile, runCodeRules };
|
|
88
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../../../../packages/grimoire/src/rules/index.ts"],"sourcesContent":["/**\n * The code rules: four text rules over a file's content, shared by\n * `grimoire check` (whole files), the guard (added lines) and the specs.\n *\n * Escape hatch: a comment on the line or the line above reading\n * `grimoire-allow <rule>: <why>` (several rules: `grimoire-allow a, b: why`).\n * Limits: the scanner is not a parser (see `scan.ts`), and editors show no\n * squiggles for these rules.\n */\nimport { CODE_RULES, type CodeRuleName } from \"../contracts/settings\";\nimport { logicalCss } from \"./logical-css\";\nimport { noJsImports } from \"./no-js-imports\";\nimport { scanSource, type ScannedSource, type ScanMode } from \"./scan\";\nimport { specificErrors } from \"./specific-errors\";\nimport { typeDiscriminator } from \"./type-discriminator\";\nimport type { RuleFinding, RuleHit } from \"./types\";\n\nexport { phraseOf, isGenericError } from \"./specific-errors\";\nexport { scanSource, type ScannedSource, type ScanMode } from \"./scan\";\nexport type { RuleFinding, RuleHit } from \"./types\";\n\n/** The finding code of each rule. */\nexport const RULE_CODES: Record<CodeRuleName, string> = {\n \"logical-css\": \"GR3007\",\n \"specific-errors\": \"GR3008\",\n \"type-discriminator\": \"GR3009\",\n \"no-js-imports\": \"GR3010\",\n};\n\nconst CODE_FILE = /\\.(?:[cm]?[jt]s|[jt]sx)$/;\nconst STYLE_FILE = /\\.(?:css|scss|less)$/;\nconst LOCALE_PATH = /(?:^|[/._-])(?:locales?|lang|langs|i18n|translations?)(?:[/._-]|$)/i;\nconst SPEC_FILE = /\\.(?:spec|test)\\.[cm]?[jt]sx?$/;\nconst SKIPPED_FOLDER = /(?:^|\\/)(?:node_modules|dist|grimoire)\\//;\n\n/** Whether the rules read a file at all: code, style and locale JSON files, not specs, builds or `grimoire/`. */\nexport function isRuleFile(file: string): boolean {\n if (SKIPPED_FOLDER.test(file) || SPEC_FILE.test(file) || file.endsWith(\".d.ts\")) return false;\n\n if (file.endsWith(\".json\")) return LOCALE_PATH.test(file);\n\n return CODE_FILE.test(file) || STYLE_FILE.test(file);\n}\n\nfunction modeOf(file: string): ScanMode {\n if (file.endsWith(\".json\")) return \"json\";\n if (file.endsWith(\".scss\")) return \"scss\";\n if (STYLE_FILE.test(file)) return \"css\";\n\n return \"code\";\n}\n\n/** Lines (1-based) a `grimoire-allow` comment covers, per rule. */\nfunction allowedLines(scanned: ScannedSource): Map<string, Set<number>> {\n const allowed = new Map<string, Set<number>>();\n\n for (const comment of scanned.comments) {\n const match = /grimoire-allow\\s+([a-z-]+(?:\\s*,\\s*[a-z-]+)*)\\s*:\\s*\\S/.exec(comment.text);\n\n if (!match) continue;\n\n for (const name of (match[1] ?? \"\").split(\",\").map((part) => part.trim())) {\n const lines = allowed.get(name) ?? new Set<number>();\n\n for (let line = comment.startLine; line <= comment.endLine + 1; line++) lines.add(line);\n\n allowed.set(name, lines);\n }\n }\n\n return allowed;\n}\n\n/**\n * Run the rules over one file's text. `file` decides the kind of scan (CSS,\n * JSON locale, code) and whether it is skipped. Findings come back in line\n * order; a rule's finding on a line a `grimoire-allow` comment covers is left out.\n */\nexport function runCodeRules(\n file: string,\n text: string,\n rules: readonly CodeRuleName[] = CODE_RULES,\n): RuleFinding[] {\n if (!isRuleFile(file)) return [];\n\n const mode = modeOf(file);\n const scanned = scanSource(text, mode);\n const allowed = allowedLines(scanned);\n const style = mode === \"css\" || mode === \"scss\" ? \"css\" : \"code\";\n const locale = mode === \"json\" || LOCALE_PATH.test(file);\n const findings: RuleFinding[] = [];\n\n const add = (rule: CodeRuleName, hits: RuleHit[]): void => {\n for (const hit of hits) {\n if (allowed.get(rule)?.has(hit.line)) continue;\n\n findings.push({ ...hit, rule, code: RULE_CODES[rule] });\n }\n };\n\n for (const rule of rules) {\n if (rule === \"logical-css\" && mode !== \"json\") add(rule, logicalCss(scanned, style));\n else if (rule === \"specific-errors\" && style === \"code\")\n add(rule, specificErrors(scanned, locale));\n else if (rule === \"type-discriminator\" && mode === \"code\")\n add(rule, typeDiscriminator(scanned));\n else if (rule === \"no-js-imports\" && mode === \"code\") add(rule, noJsImports(scanned));\n }\n\n return findings.sort((left, right) => left.line - right.line || left.column - right.column);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsBA,MAAa,aAA2C;CACtD,eAAe;CACf,mBAAmB;CACnB,sBAAsB;CACtB,iBAAiB;AACnB;AAEA,MAAM,YAAY;AAClB,MAAM,aAAa;AACnB,MAAM,cAAc;AACpB,MAAM,YAAY;AAClB,MAAM,iBAAiB;;AAGvB,SAAgB,WAAW,MAAuB;CAChD,IAAI,eAAe,KAAK,IAAI,KAAK,UAAU,KAAK,IAAI,KAAK,KAAK,SAAS,OAAO,GAAG,OAAO;CAExF,IAAI,KAAK,SAAS,OAAO,GAAG,OAAO,YAAY,KAAK,IAAI;CAExD,OAAO,UAAU,KAAK,IAAI,KAAK,WAAW,KAAK,IAAI;AACrD;AAEA,SAAS,OAAO,MAAwB;CACtC,IAAI,KAAK,SAAS,OAAO,GAAG,OAAO;CACnC,IAAI,KAAK,SAAS,OAAO,GAAG,OAAO;CACnC,IAAI,WAAW,KAAK,IAAI,GAAG,OAAO;CAElC,OAAO;AACT;;AAGA,SAAS,aAAa,SAAkD;CACtE,MAAM,0BAAU,IAAI,IAAyB;CAE7C,KAAK,MAAM,WAAW,QAAQ,UAAU;EACtC,MAAM,QAAQ,yDAAyD,KAAK,QAAQ,IAAI;EAExF,IAAI,CAAC,OAAO;EAEZ,KAAK,MAAM,SAAS,MAAM,MAAM,GAAE,CAAE,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,KAAK,KAAK,CAAC,GAAG;GACzE,MAA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are physical, matched in CSS files and in strings (`margin-left:`). */
|
|
3
|
+
const PHYSICAL_PROPERTY = new RegExp([
|
|
4
|
+
String.raw`(?:margin|padding|scroll-margin|scroll-padding)-(?:left|right)`,
|
|
5
|
+
String.raw`border-(?:left|right)(?:-(?:width|style|color))?`,
|
|
6
|
+
String.raw`border-(?:top|bottom)-(?:left|right)-radius`
|
|
7
|
+
].join("|"));
|
|
8
|
+
const PHYSICAL_PROPERTY_DECLARATION = new RegExp(`(?<![\\w-])(${PHYSICAL_PROPERTY.source})\\s*:`, "g");
|
|
9
|
+
/** `left:` and `right:` as properties (CSS files only). */
|
|
10
|
+
const POSITION_DECLARATION = /(?<![\w-])(left|right)\s*:(?!:)/g;
|
|
11
|
+
const DIRECTION_VALUE = /(?<![\w-])(text-align|float|clear)\s*:\s*(left|right)\b/g;
|
|
12
|
+
/** Camel-case style keys (`style={{ marginLeft: 4 }}`). */
|
|
13
|
+
const CAMEL_KEY = /(?<![\w$.])(marginLeft|marginRight|paddingLeft|paddingRight|borderLeft(?:Width|Style|Color)?|borderRight(?:Width|Style|Color)?|borderTopLeftRadius|borderTopRightRadius|borderBottomLeftRadius|borderBottomRightRadius|scrollMarginLeft|scrollMarginRight|scrollPaddingLeft|scrollPaddingRight)\s*:/g;
|
|
14
|
+
const CAMEL_DIRECTION_VALUE = /(?<![\w$.])(textAlign|float|clear)\s*:\s*["'`](left|right)["'`]/g;
|
|
15
|
+
/** `left: 0`, `right: "8px"`: a bare key counts only with a length-like value, so data objects with `left`/`right` fields pass. */
|
|
16
|
+
const POSITION_KEY = /(?<![\w$.-])(left|right)\s*:\s*(?:-?\d[\d.]*|["'`]-?[\d.]+(?:px|rem|em|%|vw|vh)?["'`]|["'`]auto["'`])/g;
|
|
17
|
+
const VARIANTS = String.raw`(?:(?:[a-z0-9_-]+|\[[^\]\s]+\]):)*`;
|
|
18
|
+
const LENGTH = String.raw`(?:\d[\d./]*|px|auto|full|\[[^\]]+\]|\([^)]+\))`;
|
|
19
|
+
/** A Tailwind-style class token with a physical direction. */
|
|
20
|
+
const CLASS_TOKEN = new RegExp(`^${VARIANTS}!?(?:-?m[lr]-${LENGTH}|-?p[lr]-${LENGTH}|-?(?:left|right)-${LENGTH}|text-(?:left|right)|float-(?:left|right)|border-[lr](?:-\\S+)?|rounded-(?:[lr]|t[lr]|b[lr])(?:-\\S+)?|scroll-m[lr]-${LENGTH}|scroll-p[lr]-${LENGTH})!?$`);
|
|
21
|
+
const LOGICAL = {
|
|
22
|
+
"margin-left": "margin-inline-start",
|
|
23
|
+
"margin-right": "margin-inline-end",
|
|
24
|
+
"padding-left": "padding-inline-start",
|
|
25
|
+
"padding-right": "padding-inline-end",
|
|
26
|
+
left: "inset-inline-start",
|
|
27
|
+
right: "inset-inline-end"
|
|
28
|
+
};
|
|
29
|
+
function suggestion(text) {
|
|
30
|
+
const property = text.toLowerCase().replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
31
|
+
const logical = LOGICAL[property];
|
|
32
|
+
return logical ? ` (use ${logical})` : "";
|
|
33
|
+
}
|
|
34
|
+
function classSuggestion(token) {
|
|
35
|
+
const base = token.slice(token.lastIndexOf(":") + 1).replace(/^!/, "");
|
|
36
|
+
if (/^-?m[lr]-/.test(base)) return base.replace(/m([lr])-/, (_all, side) => `m${side === "l" ? "s" : "e"}-`);
|
|
37
|
+
if (/^-?p[lr]-/.test(base)) return base.replace(/p([lr])-/, (_all, side) => `p${side === "l" ? "s" : "e"}-`);
|
|
38
|
+
if (/^-?(?:left|right)-/.test(base)) return base.replace(/(left|right)-/, (_all, side) => `${side === "left" ? "start" : "end"}-`);
|
|
39
|
+
if (/^text-/.test(base)) return base.replace(/left|right/, (side) => side === "left" ? "start" : "end");
|
|
40
|
+
if (/^float-/.test(base)) return base.replace(/left|right/, (side) => side === "left" ? "start" : "end");
|
|
41
|
+
if (/^border-[lr]/.test(base)) return base.replace(/^border-([lr])/, (_all, side) => `border-${side === "l" ? "s" : "e"}`);
|
|
42
|
+
if (/^rounded-[lr]/.test(base)) return base.replace(/^rounded-([lr])/, (_all, side) => `rounded-${side === "l" ? "s" : "e"}`);
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
function hit(line, column, found, hint) {
|
|
46
|
+
return {
|
|
47
|
+
line,
|
|
48
|
+
column,
|
|
49
|
+
message: `${JSON.stringify(found)} is a physical direction${hint}; right-to-left layouts need the logical form`
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function lineColumn(text, offset) {
|
|
53
|
+
const before = text.slice(0, offset);
|
|
54
|
+
return {
|
|
55
|
+
line: before.split("\n").length,
|
|
56
|
+
column: offset - (before.lastIndexOf("\n") + 1) + 1
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function eachMatch(text, pattern, visit) {
|
|
60
|
+
for (const match of text.matchAll(pattern)) visit(match);
|
|
61
|
+
}
|
|
62
|
+
/** The hits of the rule in one scanned file. `style` says whether the file is CSS (`css`) or code. */
|
|
63
|
+
function logicalCss(scanned, style) {
|
|
64
|
+
const hits = [];
|
|
65
|
+
const declarations = style === "css" ? scanned.stripped : null;
|
|
66
|
+
if (declarations !== null) {
|
|
67
|
+
eachMatch(declarations, PHYSICAL_PROPERTY_DECLARATION, (match) => {
|
|
68
|
+
const place = lineColumn(declarations, match.index ?? 0);
|
|
69
|
+
hits.push(hit(place.line, place.column, match[1] ?? "", suggestion(match[1] ?? "")));
|
|
70
|
+
});
|
|
71
|
+
eachMatch(declarations, POSITION_DECLARATION, (match) => {
|
|
72
|
+
const place = lineColumn(declarations, match.index ?? 0);
|
|
73
|
+
hits.push(hit(place.line, place.column, match[1] ?? "", suggestion(match[1] ?? "")));
|
|
74
|
+
});
|
|
75
|
+
eachMatch(declarations, DIRECTION_VALUE, (match) => {
|
|
76
|
+
const place = lineColumn(declarations, match.index ?? 0);
|
|
77
|
+
const side = match[2] === "left" ? "start" : "end";
|
|
78
|
+
hits.push(hit(place.line, place.column, `${match[1]}: ${match[2]}`, ` (use ${match[1]}: ${side})`));
|
|
79
|
+
});
|
|
80
|
+
return hits;
|
|
81
|
+
}
|
|
82
|
+
for (const part of scanned.strings) {
|
|
83
|
+
for (const match of part.text.matchAll(PHYSICAL_PROPERTY_DECLARATION)) hits.push(hit(part.line, part.column, match[1] ?? "", suggestion(match[1] ?? "")));
|
|
84
|
+
for (const match of part.text.matchAll(DIRECTION_VALUE)) {
|
|
85
|
+
const side = match[2] === "left" ? "start" : "end";
|
|
86
|
+
hits.push(hit(part.line, part.column, `${match[1]}: ${match[2]}`, ` (use ${match[1]}: ${side})`));
|
|
87
|
+
}
|
|
88
|
+
for (const token of part.text.split(/\s+/)) if (token !== "" && CLASS_TOKEN.test(token)) {
|
|
89
|
+
const fix = classSuggestion(token);
|
|
90
|
+
hits.push(hit(part.line, part.column, token, fix === "" ? "" : ` (use ${fix})`));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const code = scanned.bare;
|
|
94
|
+
eachMatch(code, CAMEL_KEY, (match) => {
|
|
95
|
+
const place = lineColumn(code, match.index ?? 0);
|
|
96
|
+
hits.push(hit(place.line, place.column, match[1] ?? "", ""));
|
|
97
|
+
});
|
|
98
|
+
eachMatch(scanned.stripped, CAMEL_DIRECTION_VALUE, (match) => {
|
|
99
|
+
const place = lineColumn(scanned.stripped, match.index ?? 0);
|
|
100
|
+
const side = match[2] === "left" ? "start" : "end";
|
|
101
|
+
hits.push(hit(place.line, place.column, `${match[1]}: "${match[2]}"`, ` (use "${side}")`));
|
|
102
|
+
});
|
|
103
|
+
eachMatch(scanned.stripped, POSITION_KEY, (match) => {
|
|
104
|
+
const place = lineColumn(scanned.stripped, match.index ?? 0);
|
|
105
|
+
hits.push(hit(place.line, place.column, match[1] ?? "", suggestion(match[1] ?? "")));
|
|
106
|
+
});
|
|
107
|
+
return hits;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
export { logicalCss };
|
|
112
|
+
//# sourceMappingURL=logical-css.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logical-css.mjs","names":[],"sources":["../../../../../../../../packages/grimoire/src/rules/logical-css.ts"],"sourcesContent":["/**\n * GR3007 `logical-css`: no physical directions in styles, so right-to-left\n * languages work. Reads CSS declarations, Tailwind class tokens in string\n * literals, and inline style keys.\n */\nimport type { ScannedSource } from \"./scan\";\nimport type { RuleHit } from \"./types\";\n\n/** CSS property names that are physical, matched in CSS files and in strings (`margin-left:`). */\nconst PHYSICAL_PROPERTY = new RegExp(\n [\n String.raw`(?:margin|padding|scroll-margin|scroll-padding)-(?:left|right)`,\n String.raw`border-(?:left|right)(?:-(?:width|style|color))?`,\n String.raw`border-(?:top|bottom)-(?:left|right)-radius`,\n ].join(\"|\"),\n);\nconst PHYSICAL_PROPERTY_DECLARATION = new RegExp(\n `(?<![\\\\w-])(${PHYSICAL_PROPERTY.source})\\\\s*:`,\n \"g\",\n);\n/** `left:` and `right:` as properties (CSS files only). */\nconst POSITION_DECLARATION = /(?<![\\w-])(left|right)\\s*:(?!:)/g;\nconst DIRECTION_VALUE = /(?<![\\w-])(text-align|float|clear)\\s*:\\s*(left|right)\\b/g;\n\n/** Camel-case style keys (`style={{ marginLeft: 4 }}`). */\nconst CAMEL_KEY =\n /(?<![\\w$.])(marginLeft|marginRight|paddingLeft|paddingRight|borderLeft(?:Width|Style|Color)?|borderRight(?:Width|Style|Color)?|borderTopLeftRadius|borderTopRightRadius|borderBottomLeftRadius|borderBottomRightRadius|scrollMarginLeft|scrollMarginRight|scrollPaddingLeft|scrollPaddingRight)\\s*:/g;\nconst CAMEL_DIRECTION_VALUE = /(?<![\\w$.])(textAlign|float|clear)\\s*:\\s*[\"'`](left|right)[\"'`]/g;\n/** `left: 0`, `right: \"8px\"`: a bare key counts only with a length-like value, so data objects with `left`/`right` fields pass. */\nconst POSITION_KEY =\n /(?<![\\w$.-])(left|right)\\s*:\\s*(?:-?\\d[\\d.]*|[\"'`]-?[\\d.]+(?:px|rem|em|%|vw|vh)?[\"'`]|[\"'`]auto[\"'`])/g;\n\nconst VARIANTS = String.raw`(?:(?:[a-z0-9_-]+|\\[[^\\]\\s]+\\]):)*`;\nconst LENGTH = String.raw`(?:\\d[\\d./]*|px|auto|full|\\[[^\\]]+\\]|\\([^)]+\\))`;\n/** A Tailwind-style class token with a physical direction. */\nconst CLASS_TOKEN = new RegExp(\n `^${VARIANTS}!?(?:-?m[lr]-${LENGTH}|-?p[lr]-${LENGTH}|-?(?:left|right)-${LENGTH}|text-(?:left|right)|float-(?:left|right)|border-[lr](?:-\\\\S+)?|rounded-(?:[lr]|t[lr]|b[lr])(?:-\\\\S+)?|scroll-m[lr]-${LENGTH}|scroll-p[lr]-${LENGTH})!?$`,\n);\n\nconst LOGICAL: Record<string, string> = {\n \"margin-left\": \"margin-inline-start\",\n \"margin-right\": \"margin-inline-end\",\n \"padding-left\": \"padding-inline-start\",\n \"padding-right\": \"padding-inline-end\",\n left: \"inset-inline-start\",\n right: \"inset-inline-end\",\n};\n\nfunction suggestion(text: string): string {\n const property = text.toLowerCase().replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n const logical = LOGICAL[property];\n\n return logical ? ` (use ${logical})` : \"\";\n}\n\nfunction classSuggestion(token: string): string {\n const base = token.slice(token.lastIndexOf(\":\") + 1).replace(/^!/, \"\");\n\n if (/^-?m[lr]-/.test(base))\n return base.replace(/m([lr])-/, (_all, side: string) => `m${side === \"l\" ? \"s\" : \"e\"}-`);\n if (/^-?p[lr]-/.test(base))\n return base.replace(/p([lr])-/, (_all, side: string) => `p${side === \"l\" ? \"s\" : \"e\"}-`);\n if (/^-?(?:left|right)-/.test(base))\n return base.replace(\n /(left|right)-/,\n (_all, side: string) => `${side === \"left\" ? \"start\" : \"end\"}-`,\n );\n if (/^text-/.test(base))\n return base.replace(/left|right/, (side) => (side === \"left\" ? \"start\" : \"end\"));\n if (/^float-/.test(base))\n return base.replace(/left|right/, (side) => (side === \"left\" ? \"start\" : \"end\"));\n if (/^border-[lr]/.test(base))\n return base.replace(\n /^border-([lr])/,\n (_all, side: string) => `border-${side === \"l\" ? \"s\" : \"e\"}`,\n );\n if (/^rounded-[lr]/.test(base))\n return base.replace(\n /^rounded-([lr])/,\n (_all, side: string) => `rounded-${side === \"l\" ? \"s\" : \"e\"}`,\n );\n\n return \"\";\n}\n\nfunction hit(line: number, column: number, found: string, hint: string): RuleHit {\n return {\n line,\n column,\n message: `${JSON.stringify(found)} is a physical direction${hint}; right-to-left layouts need the logical form`,\n };\n}\n\nfunction lineColumn(text: string, offset: number): { line: number; column: number } {\n const before = text.slice(0, offset);\n const line = before.split(\"\\n\").length;\n\n return { line, column: offset - (before.lastIndexOf(\"\\n\") + 1) + 1 };\n}\n\nfunction eachMatch(text: string, pattern: RegExp, visit: (match: RegExpExecArray) => void): void {\n for (const match of text.matchAll(pattern)) visit(match);\n}\n\n/** The hits of the rule in one scanned file. `style` says whether the file is CSS (`css`) or code. */\nexport function logicalCss(scanned: ScannedSource, style: \"css\" | \"code\"): RuleHit[] {\n const hits: RuleHit[] = [];\n const declarations = style === \"css\" ? scanned.stripped : null;\n\n if (declarations !== null) {\n eachMatch(declarations, PHYSICAL_PROPERTY_DECLARATION, (match) => {\n const place = lineColumn(declarations, match.index ?? 0);\n\n hits.push(hit(place.line, place.column, match[1] ?? \"\", suggestion(match[1] ?? \"\")));\n });\n eachMatch(declarations, POSITION_DECLARATION, (match) => {\n const place = lineColumn(declarations, match.index ?? 0);\n\n hits.push(hit(place.line, place.column, match[1] ?? \"\", suggestion(match[1] ?? \"\")));\n });\n eachMatch(declarations, DIRECTION_VALUE, (match) => {\n const place = lineColumn(declarations, match.index ?? 0);\n const side = match[2] === \"left\" ? \"start\" : \"end\";\n\n hits.push(\n hit(place.line, place.column, `${match[1]}: ${match[2]}`, ` (use ${match[1]}: ${side})`),\n );\n });\n\n return hits;\n }\n\n for (const part of scanned.strings) {\n // CSS written inside a string (`style=\"margin-left: 4px\"`, a css`…` template).\n for (const match of part.text.matchAll(PHYSICAL_PROPERTY_DECLARATION)) {\n hits.push(hit(part.line, part.column, match[1] ?? \"\", suggestion(match[1] ?? \"\")));\n }\n\n for (const match of part.text.matchAll(DIRECTION_VALUE)) {\n const side = match[2] === \"left\" ? \"start\" : \"end\";\n\n hits.push(\n hit(part.line, part.column, `${match[1]}: ${match[2]}`, ` (use ${match[1]}: ${side})`),\n );\n }\n\n for (const token of part.text.split(/\\s+/)) {\n if (token !== \"\" && CLASS_TOKEN.test(token)) {\n const fix = classSuggestion(token);\n\n hits.push(hit(part.line, part.column, token, fix === \"\" ? \"\" : ` (use ${fix})`));\n }\n }\n }\n\n // Inline style keys live in code, not in strings.\n const code = scanned.bare;\n\n eachMatch(code, CAMEL_KEY, (match) => {\n const place = lineColumn(code, match.index ?? 0);\n\n hits.push(hit(place.line, place.column, match[1] ?? \"\", \"\"));\n });\n eachMatch(scanned.stripped, CAMEL_DIRECTION_VALUE, (match) => {\n const place = lineColumn(scanned.stripped, match.index ?? 0);\n const side = match[2] === \"left\" ? \"start\" : \"end\";\n\n hits.push(hit(place.line, place.column, `${match[1]}: \"${match[2]}\"`, ` (use \"${side}\")`));\n });\n eachMatch(scanned.stripped, POSITION_KEY, (match) => {\n const place = lineColumn(scanned.stripped, match.index ?? 0);\n\n hits.push(hit(place.line, place.column, match[1] ?? \"\", suggestion(match[1] ?? \"\")));\n });\n\n return hits;\n}\n"],"mappings":";;AASA,MAAM,oBAAoB,IAAI,OAC5B;CACE,OAAO,GAAG;CACV,OAAO,GAAG;CACV,OAAO,GAAG;AACZ,CAAC,CAAC,KAAK,GAAG,CACZ;AACA,MAAM,gCAAgC,IAAI,OACxC,eAAe,kBAAkB,OAAO,SACxC,GACF;;AAEA,MAAM,uBAAuB;AAC7B,MAAM,kBAAkB;;AAGxB,MAAM,YACJ;AACF,MAAM,wBAAwB;;AAE9B,MAAM,eACJ;AAEF,MAAM,WAAW,OAAO,GAAG;AAC3B,MAAM,SAAS,OAAO,GAAG;;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,UAAU,MAAM,SAAS,CAAC;EAE3D,KAAK,KAAK,IAAI,MAAM,MAAM,MAAM,QAAQ,MAAM,MAAM,IAAI,WAAW,MAAM,MAAM,EAAE,CAAC,CAAC;CACrF,CAAC;CAED,OAAO;AACT"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region ../../packages/grimoire/src/rules/no-js-imports.ts
|
|
2
|
+
const RELATIVE_JS = /^\.\.?\/.*\.(?:js|jsx|mjs|cjs)$/;
|
|
3
|
+
const IMPORT_CONTEXT = /(?:\bfrom|\bimport\s*\(?|\brequire\s*\()\s*$/;
|
|
4
|
+
/** The hits of the rule. */
|
|
5
|
+
function noJsImports(scanned) {
|
|
6
|
+
const hits = [];
|
|
7
|
+
for (const part of scanned.strings) {
|
|
8
|
+
if (!RELATIVE_JS.test(part.text)) continue;
|
|
9
|
+
const before = scanned.bare.slice(Math.max(0, part.quoteStart - 40), part.quoteStart);
|
|
10
|
+
if (!IMPORT_CONTEXT.test(before)) continue;
|
|
11
|
+
hits.push({
|
|
12
|
+
line: part.line,
|
|
13
|
+
column: part.column,
|
|
14
|
+
message: `the import ${JSON.stringify(part.text)} ends in a JavaScript extension; drop it`
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return hits;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { noJsImports };
|
|
22
|
+
//# sourceMappingURL=no-js-imports.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-js-imports.mjs","names":[],"sources":["../../../../../../../../packages/grimoire/src/rules/no-js-imports.ts"],"sourcesContent":["/**\n * GR3010 `no-js-imports`: relative import paths carry no `.js` extension.\n * Flags the specifier of `import … from`, `export … from`, `import()`,\n * `import \"x\"` and `require()` when it starts with `./` or `../` and ends in\n * `.js`, `.jsx`, `.mjs` or `.cjs`. Package imports are never flagged.\n */\nimport type { ScannedSource } from \"./scan\";\nimport type { RuleHit } from \"./types\";\n\nconst RELATIVE_JS = /^\\.\\.?\\/.*\\.(?:js|jsx|mjs|cjs)$/;\nconst IMPORT_CONTEXT = /(?:\\bfrom|\\bimport\\s*\\(?|\\brequire\\s*\\()\\s*$/;\n\n/** The hits of the rule. */\nexport function noJsImports(scanned: ScannedSource): RuleHit[] {\n const hits: RuleHit[] = [];\n\n for (const part of scanned.strings) {\n if (!RELATIVE_JS.test(part.text)) continue;\n\n const before = scanned.bare.slice(Math.max(0, part.quoteStart - 40), part.quoteStart);\n\n if (!IMPORT_CONTEXT.test(before)) continue;\n\n hits.push({\n line: part.line,\n column: part.column,\n message: `the import ${JSON.stringify(part.text)} ends in a JavaScript extension; drop it`,\n });\n }\n\n return hits;\n}\n"],"mappings":";AASA,MAAM,cAAc;AACpB,MAAM,iBAAiB;;AAGvB,SAAgB,YAAY,SAAmC;CAC7D,MAAM,OAAkB,CAAC;CAEzB,KAAK,MAAM,QAAQ,QAAQ,SAAS;EAClC,IAAI,CAAC,YAAY,KAAK,KAAK,IAAI,GAAG;EAElC,MAAM,SAAS,QAAQ,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK,aAAa,EAAE,GAAG,KAAK,UAAU;EAEpF,IAAI,CAAC,eAAe,KAAK,MAAM,GAAG;EAElC,KAAK,KAAK;GACR,MAAM,KAAK;GACX,QAAQ,KAAK;GACb,SAAS,cAAc,KAAK,UAAU,KAAK,IAAI,EAAE;EACnD,CAAC;CACH;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
//#region ../../packages/grimoire/src/rules/scan.ts
|
|
2
|
+
const blank = (text) => text.replace(/[^\r\n]/g, " ");
|
|
3
|
+
/** Scan `text`. */
|
|
4
|
+
function scanSource(text, mode = "code") {
|
|
5
|
+
const stripped = [];
|
|
6
|
+
const bare = [];
|
|
7
|
+
const strings = [];
|
|
8
|
+
const comments = [];
|
|
9
|
+
const length = text.length;
|
|
10
|
+
let line = 1;
|
|
11
|
+
let lineStart = 0;
|
|
12
|
+
let index = 0;
|
|
13
|
+
const templates = [];
|
|
14
|
+
let braces = 0;
|
|
15
|
+
const emit = (strippedPart, barePart) => {
|
|
16
|
+
stripped.push(strippedPart);
|
|
17
|
+
bare.push(barePart);
|
|
18
|
+
};
|
|
19
|
+
const advanceLines = (chunk, from) => {
|
|
20
|
+
for (let at = 0; at < chunk.length; at++) if (chunk[at] === "\n") {
|
|
21
|
+
line++;
|
|
22
|
+
lineStart = from + at + 1;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const readQuoted = (quote) => {
|
|
26
|
+
const start = index;
|
|
27
|
+
const startLine = line;
|
|
28
|
+
const startColumn = index + 2 - lineStart;
|
|
29
|
+
let cursor = index + 1;
|
|
30
|
+
while (cursor < length) {
|
|
31
|
+
const char = text[cursor];
|
|
32
|
+
if (char === "\\") {
|
|
33
|
+
cursor += 2;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (char === quote || char === "\n") break;
|
|
37
|
+
cursor++;
|
|
38
|
+
}
|
|
39
|
+
const closed = text[cursor] === quote;
|
|
40
|
+
const content = text.slice(start + 1, Math.min(cursor, length));
|
|
41
|
+
const end = closed ? cursor + 1 : cursor;
|
|
42
|
+
let isKey = false;
|
|
43
|
+
if (mode === "json") isKey = /^\s*:/.exec(text.slice(end, end + 200)) !== null;
|
|
44
|
+
strings.push({
|
|
45
|
+
text: content,
|
|
46
|
+
line: startLine,
|
|
47
|
+
column: startColumn,
|
|
48
|
+
quoteStart: start,
|
|
49
|
+
isKey
|
|
50
|
+
});
|
|
51
|
+
emit(text.slice(start, end), `${quote}${blank(content)}${closed ? quote : ""}`);
|
|
52
|
+
advanceLines(text.slice(start, end), start);
|
|
53
|
+
index = end;
|
|
54
|
+
};
|
|
55
|
+
const readTemplatePart = () => {
|
|
56
|
+
const start = index;
|
|
57
|
+
const startLine = line;
|
|
58
|
+
const startColumn = index + 2 - lineStart;
|
|
59
|
+
let cursor = index + 1;
|
|
60
|
+
let ends = "eof";
|
|
61
|
+
while (cursor < length) {
|
|
62
|
+
const char = text[cursor];
|
|
63
|
+
if (char === "\\") {
|
|
64
|
+
cursor += 2;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (char === "`") {
|
|
68
|
+
ends = "quote";
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
if (char === "$" && text[cursor + 1] === "{") {
|
|
72
|
+
ends = "expression";
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
cursor++;
|
|
76
|
+
}
|
|
77
|
+
cursor = Math.min(cursor, length);
|
|
78
|
+
const content = text.slice(start + 1, cursor);
|
|
79
|
+
const opener = text[start] ?? "`";
|
|
80
|
+
const closer = ends === "quote" ? "`" : ends === "expression" ? "${" : "";
|
|
81
|
+
strings.push({
|
|
82
|
+
text: content,
|
|
83
|
+
line: startLine,
|
|
84
|
+
column: startColumn,
|
|
85
|
+
quoteStart: start,
|
|
86
|
+
isKey: false
|
|
87
|
+
});
|
|
88
|
+
emit(`${opener}${content}${closer}`, `${opener}${blank(content)}${closer}`);
|
|
89
|
+
advanceLines(text.slice(start, cursor + closer.length), start);
|
|
90
|
+
if (ends === "expression") templates.push(braces);
|
|
91
|
+
index = cursor + closer.length;
|
|
92
|
+
};
|
|
93
|
+
while (index < length) {
|
|
94
|
+
const char = text[index] ?? "";
|
|
95
|
+
const next = text[index + 1];
|
|
96
|
+
if (mode !== "json" && char === "/" && next === "*") {
|
|
97
|
+
const close = text.indexOf("*/", index + 2);
|
|
98
|
+
const end = close === -1 ? length : close + 2;
|
|
99
|
+
const chunk = text.slice(index, end);
|
|
100
|
+
const startLine = line;
|
|
101
|
+
advanceLines(chunk, index);
|
|
102
|
+
comments.push({
|
|
103
|
+
text: chunk,
|
|
104
|
+
startLine,
|
|
105
|
+
endLine: line
|
|
106
|
+
});
|
|
107
|
+
emit(blank(chunk), blank(chunk));
|
|
108
|
+
index = end;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if ((mode === "code" || mode === "scss") && char === "/" && next === "/") {
|
|
112
|
+
let end = text.indexOf("\n", index);
|
|
113
|
+
if (end === -1) end = length;
|
|
114
|
+
const chunk = text.slice(index, end);
|
|
115
|
+
comments.push({
|
|
116
|
+
text: chunk,
|
|
117
|
+
startLine: line,
|
|
118
|
+
endLine: line
|
|
119
|
+
});
|
|
120
|
+
emit(blank(chunk), blank(chunk));
|
|
121
|
+
index = end;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (char === "\"" || char === "'") {
|
|
125
|
+
readQuoted(char);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (mode === "code" && char === "`") {
|
|
129
|
+
readTemplatePart();
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (mode === "code") {
|
|
133
|
+
if (char === "{") braces++;
|
|
134
|
+
if (char === "}") {
|
|
135
|
+
if (templates.length > 0 && templates[templates.length - 1] === braces) {
|
|
136
|
+
templates.pop();
|
|
137
|
+
emit("}", "}");
|
|
138
|
+
index++;
|
|
139
|
+
const resume = index - 1;
|
|
140
|
+
const startLine = line;
|
|
141
|
+
const startColumn = index + 1 - lineStart;
|
|
142
|
+
let cursor = index;
|
|
143
|
+
let ends = "eof";
|
|
144
|
+
while (cursor < length) {
|
|
145
|
+
const inner = text[cursor];
|
|
146
|
+
if (inner === "\\") {
|
|
147
|
+
cursor += 2;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (inner === "`") {
|
|
151
|
+
ends = "quote";
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
if (inner === "$" && text[cursor + 1] === "{") {
|
|
155
|
+
ends = "expression";
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
cursor++;
|
|
159
|
+
}
|
|
160
|
+
cursor = Math.min(cursor, length);
|
|
161
|
+
const content = text.slice(index, cursor);
|
|
162
|
+
const closer = ends === "quote" ? "`" : ends === "expression" ? "${" : "";
|
|
163
|
+
strings.push({
|
|
164
|
+
text: content,
|
|
165
|
+
line: startLine,
|
|
166
|
+
column: startColumn,
|
|
167
|
+
quoteStart: resume,
|
|
168
|
+
isKey: false
|
|
169
|
+
});
|
|
170
|
+
emit(`${content}${closer}`, `${blank(content)}${closer}`);
|
|
171
|
+
advanceLines(text.slice(index, cursor + closer.length), index);
|
|
172
|
+
if (ends === "expression") templates.push(braces);
|
|
173
|
+
index = cursor + closer.length;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
braces--;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (char === "\n") {
|
|
180
|
+
line++;
|
|
181
|
+
lineStart = index + 1;
|
|
182
|
+
}
|
|
183
|
+
emit(char, char);
|
|
184
|
+
index++;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
stripped: stripped.join(""),
|
|
188
|
+
bare: bare.join(""),
|
|
189
|
+
strings,
|
|
190
|
+
comments
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//#endregion
|
|
195
|
+
export { scanSource };
|
|
196
|
+
//# sourceMappingURL=scan.mjs.map
|