@se-studio/site-check 2.15.1 → 2.16.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.16.0
4
+
5
+ ### Minor Changes
6
+
7
+ - e39fed8: Add the `check-comments` bin (lifted from HopSkipDrive's `scripts/check-comments.mjs`). It fails on stray comments in `src/**/*.ts(x)` (narration, banners, commented-out code, docs on non-exported symbols), skipping `src/generated/`. Default is a whole-tree scan; `--diff` checks only lines added since the merge base with `origin/develop` (or `$COMMENT_BASE`) and, in CI, fails with a `fetch-depth: 0` hint when the base is missing. Allowed: `biome-ignore`, `@ts-expect-error`, `@ts-ignore`, `se-exception:`, `why-external:`, licence headers, a comment with an http(s) link, `/// <reference`, and a `/**` doc block directly above an `export`. Customer sites opt in with `pnpm exec check-comments`; nothing runs it for them.
8
+
9
+ A `/*` inside a `//` line comment (for example `// rewrite /api/*`) no longer opens a block comment, so the code lines after it are not swallowed.
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [850c4f7]
14
+ - @se-studio/contentful-rest-api@2.8.0
15
+
3
16
  ## 2.15.1
4
17
 
5
18
  ### Patch Changes
package/README.md CHANGED
@@ -405,6 +405,21 @@ color: #111; /* se-exception: print stylesheet, no CMS colour */
405
405
 
406
406
  The reason after `se-exception:` is required. A comment with no reason still fails.
407
407
 
408
+ ## Comment lint (`check-comments`)
409
+
410
+ Fails when `src/**/*.ts(x)` holds a stray comment (narration, banners, commented-out code, docs on non-exported symbols or interface members). `src/generated/` is skipped. Not part of customer CI until a site opts in.
411
+
412
+ Run it from the **app root**:
413
+
414
+ ```bash
415
+ pnpm exec check-comments # whole tree: every line of every file
416
+ pnpm exec check-comments --diff # only lines added since the merge base with origin/develop (or $COMMENT_BASE)
417
+ ```
418
+
419
+ Allowed: `biome-ignore`, `@ts-expect-error`, `@ts-ignore`; `se-exception: <reason>`; `why-external: <system and constraint>`; licence headers (`@license`, `Copyright`, `SPDX-License`, `@generated`); a comment that carries an http(s) link; `/// <reference`; and a `/**` doc block directly above an `export`.
420
+
421
+ `--diff` needs the base ref. Locally a missing base prints a warning and skips; with `CI` set it fails, so check out with `fetch-depth: 0` (or `git fetch origin develop`).
422
+
408
423
  ## Exit codes
409
424
 
410
425
  - `0` — Validation passed (sitemap.xml required; llms.txt and sitemap-unindexed.xml optional). If `--check-rewrites` was set, no unexpected rewrites. Every sitemap page returned 200 for its `.md` URL and files were saved (or in compare mode: all production sitemap pages exist on development and, if `--check-rewrites`, no unexpected rewrites).
@@ -0,0 +1,196 @@
1
+ #!/usr/bin/env node
2
+ import { execFileSync } from 'node:child_process';
3
+ import { readFileSync, realpathSync } from 'node:fs';
4
+ import { fileURLToPath } from 'node:url';
5
+
6
+ /**
7
+ * Guard: no stray comments in src/**\/*.ts(x). By default every line of every tracked
8
+ * (and untracked) file is checked, so the whole tree stays clean. `--diff` checks only
9
+ * lines added since the merge base with origin/develop (or $COMMENT_BASE).
10
+ *
11
+ * Allowed:
12
+ * - biome-ignore, @ts-expect-error, @ts-ignore (their reasons are checked elsewhere)
13
+ * - `se-exception: <reason>` and `why-external: <system and constraint>`
14
+ * - licence headers (@license, Copyright, SPDX)
15
+ * - a comment that carries an http(s) link to an issue or RFC
16
+ * - a /** doc block directly above an exported declaration
17
+ * Everything else (narration, banners, commented-out code, sermons) is removed or replaced
18
+ * by a better name, type or structure.
19
+ *
20
+ * Run from the app root: pnpm exec check-comments [--diff]
21
+ * `--diff` needs the base ref: in CI use `fetch-depth: 0`.
22
+ */
23
+
24
+ const SCOPE = /^src\/.*\.tsx?$/;
25
+ const SKIP = /^src\/generated\//;
26
+ const ALLOWED = [
27
+ /biome-ignore/,
28
+ /@ts-expect-error/,
29
+ /@ts-ignore/,
30
+ /se-exception:\s*\S/,
31
+ /why-external:\s*\S/,
32
+ /@license|Copyright|SPDX-License|@generated/,
33
+ /https?:\/\/\S+/,
34
+ /^\s*\/\/\/\s*<reference/,
35
+ ];
36
+
37
+ const stripStrings = (l) =>
38
+ l.replace(/'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*`/g, '""');
39
+ const stripJsxText = (l) => l.replace(/>[^<>{}]+</g, '><');
40
+
41
+ /** Which lines are comment lines, tracking block comments and template literals across lines. */
42
+ export function commentLines(lines) {
43
+ const flags = [];
44
+ let inBlock = false;
45
+ let inTemplate = false;
46
+ for (const line of lines) {
47
+ if (inBlock) {
48
+ flags.push(true);
49
+ if (line.includes('*/')) inBlock = false;
50
+ continue;
51
+ }
52
+ if (inTemplate) {
53
+ flags.push(false);
54
+ if ((line.match(/(?<!\\)`/g) ?? []).length % 2 === 1) inTemplate = false;
55
+ continue;
56
+ }
57
+ const code = stripJsxText(stripStrings(line));
58
+ const lineStart = code.search(/(?:^|[\s;,{}()\]])\/\/\s/);
59
+ const rawOpen = code.indexOf('/*');
60
+ const open = lineStart !== -1 && lineStart < rawOpen ? -1 : rawOpen;
61
+ const comment =
62
+ /^\s*\/\//.test(code) ||
63
+ open !== -1 ||
64
+ lineStart !== -1 ||
65
+ /(?<=[;,{}()\]])\s*\/\/\s/.test(code);
66
+ if (open !== -1 && !code.includes('*/', open + 2)) inBlock = true;
67
+ if ((stripStrings(line).match(/(?<!\\)`/g) ?? []).length % 2 === 1) inTemplate = true;
68
+ flags.push(comment);
69
+ }
70
+ return flags;
71
+ }
72
+
73
+ /** Group consecutive added comment lines so a doc block is judged as a whole. */
74
+ export function findViolations(file, lines, added) {
75
+ const out = [];
76
+ const flags = commentLines(lines);
77
+ let i = 0;
78
+ while (i < lines.length) {
79
+ if (!added.has(i + 1) || !flags[i]) {
80
+ i++;
81
+ continue;
82
+ }
83
+ let end = i;
84
+ while (end + 1 < lines.length && added.has(end + 2) && flags[end + 1]) end++;
85
+ const block = lines.slice(i, end + 1);
86
+ const allowed = block.some((l) => ALLOWED.some((re) => re.test(l)));
87
+ const next = lines.slice(end + 1).find((l) => l.trim() !== '') ?? '';
88
+ const doc = /^\s*\/\*\*/.test(block[0]) && /^\s*export\s/.test(next);
89
+ if (!allowed && !doc) out.push(`${file}:${i + 1} ${block[0].trim().slice(0, 100)}`);
90
+ i = end + 1;
91
+ }
92
+ return out;
93
+ }
94
+
95
+ const git = (...args) =>
96
+ execFileSync('git', args, { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
97
+
98
+ function addedLines(base, file) {
99
+ const added = new Set();
100
+ const diff = git('diff', '-U0', '--no-color', base, '--', file);
101
+ for (const m of diff.matchAll(/^@@ -\S+ \+(\d+)(?:,(\d+))? @@/gm)) {
102
+ const start = Number(m[1]);
103
+ const count = m[2] === undefined ? 1 : Number(m[2]);
104
+ for (let n = start; n < start + count; n++) added.add(n);
105
+ }
106
+ return added;
107
+ }
108
+
109
+ /** Every line of the file counts as added, so the whole file is checked. */
110
+ export function checkWholeFile(file, text) {
111
+ const lines = text.split('\n');
112
+ return findViolations(file, lines, new Set(lines.map((_, i) => i + 1)));
113
+ }
114
+
115
+ export function inScope(file) {
116
+ return SCOPE.test(file) && !SKIP.test(file);
117
+ }
118
+
119
+ function wholeTreeViolations() {
120
+ const tracked = git('ls-files', 'src').split('\n').filter(Boolean);
121
+ const untracked = git('ls-files', '--others', '--exclude-standard').split('\n').filter(Boolean);
122
+ const violations = [];
123
+ for (const file of new Set([...tracked, ...untracked])) {
124
+ if (!inScope(file)) continue;
125
+ let text;
126
+ try {
127
+ text = readFileSync(file, 'utf8');
128
+ } catch {
129
+ continue;
130
+ }
131
+ violations.push(...checkWholeFile(file, text));
132
+ }
133
+ return violations;
134
+ }
135
+
136
+ function diffViolations() {
137
+ const baseRef = process.env.COMMENT_BASE ?? 'origin/develop';
138
+ let base;
139
+ try {
140
+ base = git('merge-base', 'HEAD', baseRef).trim();
141
+ } catch {
142
+ const msg = `check-comments: cannot find ${baseRef}. Run git fetch origin develop (CI needs fetch-depth: 0).`;
143
+ if (process.env.CI) {
144
+ console.error(msg);
145
+ process.exit(1);
146
+ }
147
+ console.warn(`${msg} Skipping.`);
148
+ return [];
149
+ }
150
+ const changed = git('diff', '--name-only', base).split('\n').filter(Boolean);
151
+ const untracked = git('ls-files', '--others', '--exclude-standard').split('\n').filter(Boolean);
152
+ const violations = [];
153
+ for (const file of new Set([...changed, ...untracked])) {
154
+ if (!inScope(file)) continue;
155
+ let text;
156
+ try {
157
+ text = readFileSync(file, 'utf8');
158
+ } catch {
159
+ continue;
160
+ }
161
+ const lines = text.split('\n');
162
+ const added = untracked.includes(file)
163
+ ? new Set(lines.map((_, i) => i + 1))
164
+ : addedLines(base, file);
165
+ violations.push(...findViolations(file, lines, added));
166
+ }
167
+ return violations;
168
+ }
169
+
170
+ function main() {
171
+ const diffMode = process.argv.includes('--diff');
172
+ const violations = diffMode ? diffViolations() : wholeTreeViolations();
173
+ if (violations.length > 0) {
174
+ console.error('check-comments: comments are not allowed in src.\n');
175
+ for (const v of violations) console.error(` ${v}`);
176
+ console.error(
177
+ `\n${violations.length} comment(s). Delete it, or make the code say it (a name, a type, a smaller function). ` +
178
+ 'Keep only: a link to an issue/RFC, `why-external: <system>` for a constraint we cannot reshape, ' +
179
+ '`se-exception: <reason>`, or a /** doc block above an export.',
180
+ );
181
+ process.exit(1);
182
+ }
183
+ console.log(`check-comments: ok (${diffMode ? 'diff' : 'whole tree'})`);
184
+ }
185
+
186
+ function isDirectRun() {
187
+ const entry = process.argv[1];
188
+ if (!entry) return false;
189
+ try {
190
+ return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
191
+ } catch {
192
+ return false;
193
+ }
194
+ }
195
+
196
+ if (isDirectRun()) main();
package/docs/llms.md CHANGED
@@ -132,6 +132,12 @@ R1 (literal chrome colours) runs only when that file has an `r1` object: `chrome
132
132
 
133
133
  Escape hatch on the same line: `// se-exception: <reason>` or `/* se-exception: <reason> */`. The reason is required.
134
134
 
135
+ ### check-comments
136
+
137
+ Bin: `check-comments`. Run from the app root: `pnpm exec check-comments` (whole tree of `src/**/*.ts(x)`, skipping `src/generated/`) or `check-comments --diff` (lines added since the merge base with `origin/develop`, or `$COMMENT_BASE`). **Customer sites do not run this in CI until they opt in.**
138
+
139
+ Allowed: `biome-ignore`, `@ts-expect-error`, `@ts-ignore`, `se-exception: <reason>`, `why-external: <system and constraint>`, licence headers (`@license`, `Copyright`, `SPDX-License`, `@generated`), a comment with an http(s) link, `/// <reference`, and a `/**` doc block directly above an `export`. Everything else is a violation. A missing `--diff` base fails in CI (`fetch-depth: 0` needed) and is skipped locally.
140
+
135
141
  ### check-no-context-workarounds
136
142
 
137
143
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@se-studio/site-check",
3
- "version": "2.15.1",
3
+ "version": "2.16.0",
4
4
  "description": "Validate SE marketing sites (sitemap, llms.txt) and download markdown files preserving structure",
5
5
  "repository": {
6
6
  "type": "git",
@@ -58,7 +58,8 @@
58
58
  "markdown-accuracy-audit": "./markdown-accuracy-audit.mjs",
59
59
  "check-no-context-workarounds": "./check-no-context-workarounds.mjs",
60
60
  "check-publish-state": "./check-publish-state.mjs",
61
- "check-cms-driven": "./check-cms-driven.mjs"
61
+ "check-cms-driven": "./check-cms-driven.mjs",
62
+ "check-comments": "./check-comments.mjs"
62
63
  },
63
64
  "files": [
64
65
  "dist",
@@ -76,6 +77,7 @@
76
77
  "check-no-context-workarounds.mjs",
77
78
  "check-publish-state.mjs",
78
79
  "check-cms-driven.mjs",
80
+ "check-comments.mjs",
79
81
  "cms-driven-baseline.mjs",
80
82
  "screaming-frog"
81
83
  ],
@@ -95,7 +97,7 @@
95
97
  "url": "https://github.com/Something-Else-Studio/se-core-product/issues"
96
98
  },
97
99
  "dependencies": {
98
- "@se-studio/contentful-rest-api": "2.7.0",
100
+ "@se-studio/contentful-rest-api": "2.8.0",
99
101
  "@se-studio/core-data-types": "2.0.5"
100
102
  },
101
103
  "devDependencies": {
@@ -109,6 +111,6 @@
109
111
  "type-check": "tsc --noEmit",
110
112
  "lint": "biome lint .",
111
113
  "clean": "rm -rf dist .turbo *.tsbuildinfo",
112
- "test": "pnpm build && node --test scripts/screaming-frog-audit.test.mjs scripts/screaming-frog-config.test.mjs scripts/production-audit.test.mjs scripts/discovery.test.mjs scripts/smoke-test.test.mjs scripts/analytics-context-bundle.test.mjs scripts/check-no-context-workarounds.test.mjs scripts/check-cms-driven.test.mjs scripts/revalidation-matrix.test.mjs scripts/develop-sites-timing.test.mjs scripts/markdown-accuracy-audit.test.mjs scripts/publish-state.test.mjs"
114
+ "test": "pnpm build && node --test scripts/screaming-frog-audit.test.mjs scripts/screaming-frog-config.test.mjs scripts/production-audit.test.mjs scripts/discovery.test.mjs scripts/smoke-test.test.mjs scripts/analytics-context-bundle.test.mjs scripts/check-no-context-workarounds.test.mjs scripts/check-cms-driven.test.mjs scripts/check-comments.test.mjs scripts/revalidation-matrix.test.mjs scripts/develop-sites-timing.test.mjs scripts/markdown-accuracy-audit.test.mjs scripts/publish-state.test.mjs"
113
115
  }
114
116
  }