@mintlify/cli 4.0.1740 → 4.0.1742
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/__test__/validateCommand.test.ts +126 -27
- package/__test__/validateEngine.test.ts +186 -2
- package/__test__/validateRules.test.ts +64 -49
- package/bin/cli.js +13 -1
- package/bin/tsconfig.build.tsbuildinfo +1 -1
- package/bin/validate/command.js +48 -10
- package/bin/validate/engine.js +75 -14
- package/bin/validate/files.js +1 -1
- package/bin/validate/output.js +13 -2
- package/bin/validate/report.js +3 -8
- package/package.json +4 -4
- package/src/cli.tsx +20 -1
- package/src/validate/command.tsx +48 -9
- package/src/validate/engine.ts +101 -13
- package/src/validate/files.ts +1 -1
- package/src/validate/output.tsx +18 -0
- package/src/validate/report.ts +9 -2
- package/src/validate/types.ts +10 -0
|
@@ -4,7 +4,7 @@ import path from 'node:path';
|
|
|
4
4
|
import { mockProcessExit } from 'vitest-mock-process';
|
|
5
5
|
|
|
6
6
|
import { sendPublishPrompt } from '../src/signup.js';
|
|
7
|
-
import {
|
|
7
|
+
import type { Report } from '../src/validate/report.js';
|
|
8
8
|
import { runCommand } from './utils.js';
|
|
9
9
|
|
|
10
10
|
const { dir } = vi.hoisted(() => ({
|
|
@@ -26,11 +26,6 @@ vi.mock('@mintlify/previewing', async (importOriginal) => {
|
|
|
26
26
|
return { ...mod, addLog: vi.fn(), checkBuild: vi.fn() };
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
vi.mock('../src/validate/report.js', async (importOriginal) => {
|
|
30
|
-
const mod = await importOriginal<typeof import('../src/validate/report.js')>();
|
|
31
|
-
return { ...mod, toReport: vi.fn(mod.toReport) };
|
|
32
|
-
});
|
|
33
|
-
|
|
34
29
|
const processExitMock = mockProcessExit();
|
|
35
30
|
|
|
36
31
|
const HOME = '---\ntitle: Home\n---\n\nBody\n';
|
|
@@ -49,14 +44,35 @@ const writeSite = (files: Record<string, string>, config: Record<string, unknown
|
|
|
49
44
|
}
|
|
50
45
|
};
|
|
51
46
|
|
|
47
|
+
const runJson = async (...args: string[]) => {
|
|
48
|
+
const write = vi.spyOn(process.stdout, 'write').mockImplementation((...params: unknown[]) => {
|
|
49
|
+
params.find((param): param is () => void => typeof param === 'function')?.();
|
|
50
|
+
return true;
|
|
51
|
+
});
|
|
52
|
+
try {
|
|
53
|
+
await runCommand(...args, '--format', 'json');
|
|
54
|
+
return write.mock.calls.map(([chunk]) => String(chunk));
|
|
55
|
+
} finally {
|
|
56
|
+
write.mockRestore();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
52
60
|
const report = async (...args: string[]) => {
|
|
53
|
-
await
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return result?.value as ReturnType<typeof toReport>;
|
|
61
|
+
const chunks = await runJson(...args);
|
|
62
|
+
expect(chunks).toHaveLength(1);
|
|
63
|
+
return JSON.parse(chunks[0] ?? '') as Report;
|
|
57
64
|
};
|
|
58
65
|
|
|
59
|
-
|
|
66
|
+
/** Runs in json mode expecting a failure: nothing on stdout, the error on stderr. */
|
|
67
|
+
const jsonError = async (...args: string[]) => {
|
|
68
|
+
const stderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
69
|
+
try {
|
|
70
|
+
expect(await runJson(...args)).toEqual([]);
|
|
71
|
+
return stderr.mock.calls.map(([chunk]) => String(chunk)).join('');
|
|
72
|
+
} finally {
|
|
73
|
+
stderr.mockRestore();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
60
76
|
|
|
61
77
|
const rules = (result: { diagnostics: { rule: string }[] }) =>
|
|
62
78
|
result.diagnostics.map(({ rule }) => rule);
|
|
@@ -71,7 +87,28 @@ describe('mint validate', () => {
|
|
|
71
87
|
fs.removeSync(dir);
|
|
72
88
|
});
|
|
73
89
|
|
|
74
|
-
it('
|
|
90
|
+
it('exits only after stdout has flushed the JSON report', async () => {
|
|
91
|
+
writeSite({ 'index.mdx': HOME });
|
|
92
|
+
let flush: (() => void) | undefined;
|
|
93
|
+
const write = vi.spyOn(process.stdout, 'write').mockImplementation((...params: unknown[]) => {
|
|
94
|
+
flush = params.find((param): param is () => void => typeof param === 'function');
|
|
95
|
+
return true;
|
|
96
|
+
});
|
|
97
|
+
try {
|
|
98
|
+
const run = runCommand('validate', '--no-build', '--format', 'json');
|
|
99
|
+
await vi.waitFor(() => expect(write).toHaveBeenCalled());
|
|
100
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
101
|
+
expect(processExitMock).not.toHaveBeenCalled();
|
|
102
|
+
|
|
103
|
+
flush?.();
|
|
104
|
+
await run;
|
|
105
|
+
expect(processExitMock).toHaveBeenCalledWith(0);
|
|
106
|
+
} finally {
|
|
107
|
+
write.mockRestore();
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('prints one JSON report with a summary and every diagnostic', async () => {
|
|
75
112
|
writeSite({
|
|
76
113
|
'index.mdx': '---\ntitle: Home\n---\n\nline<br>\n',
|
|
77
114
|
'other.mdx': '---\ntitle: Other\n---\n\nText {broken\n',
|
|
@@ -100,6 +137,7 @@ describe('mint validate', () => {
|
|
|
100
137
|
],
|
|
101
138
|
build: { ran: false, reason: 'disabled' },
|
|
102
139
|
});
|
|
140
|
+
expect(addLog).not.toHaveBeenCalled();
|
|
103
141
|
expect(processExitMock).toHaveBeenCalledWith(1);
|
|
104
142
|
});
|
|
105
143
|
|
|
@@ -111,7 +149,7 @@ describe('mint validate', () => {
|
|
|
111
149
|
expect(result).toMatchObject({ passed: true, build: { ran: true, passed: true } });
|
|
112
150
|
expect(rules(result)).toEqual([]);
|
|
113
151
|
expect(checkBuild).toHaveBeenCalled();
|
|
114
|
-
expect(sendPublishPrompt).toHaveBeenCalled();
|
|
152
|
+
expect(sendPublishPrompt).not.toHaveBeenCalled();
|
|
115
153
|
expect(processExitMock).toHaveBeenCalledWith(0);
|
|
116
154
|
});
|
|
117
155
|
|
|
@@ -208,7 +246,6 @@ describe('mint validate', () => {
|
|
|
208
246
|
|
|
209
247
|
expect(result).toMatchObject({ passed: true, build: { ran: true, passed: false } });
|
|
210
248
|
expect(rules(result)).toEqual([]);
|
|
211
|
-
expect(sendPublishPrompt).not.toHaveBeenCalled();
|
|
212
249
|
expect(processExitMock).toHaveBeenCalledWith(0);
|
|
213
250
|
});
|
|
214
251
|
|
|
@@ -307,7 +344,7 @@ describe('mint validate', () => {
|
|
|
307
344
|
|
|
308
345
|
const result = await report('validate');
|
|
309
346
|
|
|
310
|
-
expect(result.diagnostics.map(({ file }) => file)).toEqual([
|
|
347
|
+
expect(result.diagnostics.map(({ file }: { file: string }) => file)).toEqual([
|
|
311
348
|
'index.mdx',
|
|
312
349
|
'index.mdx',
|
|
313
350
|
'index.mdx',
|
|
@@ -360,12 +397,46 @@ describe('mint validate', () => {
|
|
|
360
397
|
writeSite({ 'index.mdx': HOME });
|
|
361
398
|
vi.mocked(checkBuild).mockRejectedValue(new Error('requires an internet connection'));
|
|
362
399
|
|
|
363
|
-
await
|
|
364
|
-
|
|
365
|
-
expect(logged()).toContain('requires an internet connection');
|
|
400
|
+
expect(await jsonError('validate')).toBe('Error: requires an internet connection\n');
|
|
366
401
|
expect(processExitMock).toHaveBeenCalledWith(2);
|
|
367
402
|
});
|
|
368
403
|
|
|
404
|
+
it('keeps console output during the build off stdout in json mode', async () => {
|
|
405
|
+
writeSite({ 'index.mdx': HOME });
|
|
406
|
+
vi.mocked(checkBuild).mockImplementation(async () => {
|
|
407
|
+
console.log('progress line');
|
|
408
|
+
return { passed: true, warnings: [] };
|
|
409
|
+
});
|
|
410
|
+
const stderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
411
|
+
|
|
412
|
+
const result = await report('validate');
|
|
413
|
+
|
|
414
|
+
const errors = stderr.mock.calls.map(([chunk]) => String(chunk));
|
|
415
|
+
stderr.mockRestore();
|
|
416
|
+
expect(result.passed).toBe(true);
|
|
417
|
+
expect(errors).toContain('progress line\n');
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it('applies fixes with --fix and lists them', async () => {
|
|
421
|
+
writeSite({ 'index.mdx': '---\ntitle: Home\n---\n\nline<br>\n' });
|
|
422
|
+
|
|
423
|
+
const result = await report('validate', '--no-build', '--fix');
|
|
424
|
+
|
|
425
|
+
expect(fs.readFileSync(path.join(dir, 'index.mdx'), 'utf-8')).toContain('line<br />');
|
|
426
|
+
expect(result.fixed).toEqual([
|
|
427
|
+
{
|
|
428
|
+
rule: 'void-tags',
|
|
429
|
+
file: 'index.mdx',
|
|
430
|
+
line: 5,
|
|
431
|
+
column: 5,
|
|
432
|
+
before: '<br>',
|
|
433
|
+
after: '<br />',
|
|
434
|
+
},
|
|
435
|
+
]);
|
|
436
|
+
expect(rules(result)).toEqual([]);
|
|
437
|
+
expect(processExitMock).toHaveBeenCalledWith(0);
|
|
438
|
+
});
|
|
439
|
+
|
|
369
440
|
it('turns rules and groups on and off', async () => {
|
|
370
441
|
writeSite({
|
|
371
442
|
'index.mdx': '---\ntitle: Home\n---\n\nline<br>\n',
|
|
@@ -389,13 +460,14 @@ describe('mint validate', () => {
|
|
|
389
460
|
it('rejects unknown rules and disabling parse-error', async () => {
|
|
390
461
|
writeSite({ 'index.mdx': HOME });
|
|
391
462
|
|
|
392
|
-
await
|
|
393
|
-
|
|
463
|
+
expect(await jsonError('validate', '--disable', 'nope')).toContain(
|
|
464
|
+
'Error: unknown rule or group "nope"'
|
|
465
|
+
);
|
|
394
466
|
expect(processExitMock).toHaveBeenLastCalledWith(2);
|
|
395
467
|
|
|
396
|
-
await
|
|
397
|
-
|
|
398
|
-
|
|
468
|
+
expect(await jsonError('validate', '--disable', 'parse-error')).toBe(
|
|
469
|
+
"Error: parse-error always runs and can't be disabled\n"
|
|
470
|
+
);
|
|
399
471
|
});
|
|
400
472
|
|
|
401
473
|
it('limits page findings to --files', async () => {
|
|
@@ -473,9 +545,9 @@ describe('mint validate', () => {
|
|
|
473
545
|
it('rejects --files that match nothing', async () => {
|
|
474
546
|
writeSite({ 'index.mdx': HOME });
|
|
475
547
|
|
|
476
|
-
await
|
|
477
|
-
|
|
478
|
-
|
|
548
|
+
expect(await jsonError('validate', '--files', 'missing.mdx')).toContain(
|
|
549
|
+
'Error: No matching files'
|
|
550
|
+
);
|
|
479
551
|
expect(processExitMock).toHaveBeenCalledWith(2);
|
|
480
552
|
expect(checkBuild).not.toHaveBeenCalled();
|
|
481
553
|
});
|
|
@@ -495,10 +567,37 @@ describe('mint validate', () => {
|
|
|
495
567
|
|
|
496
568
|
await runCommand('validate', '--no-build');
|
|
497
569
|
|
|
498
|
-
const text =
|
|
570
|
+
const text = JSON.stringify(vi.mocked(addLog).mock.calls);
|
|
499
571
|
expect(text).toContain('index.mdx');
|
|
500
572
|
expect(text).toContain('must be self-closed');
|
|
501
573
|
expect(text).toContain('1 error');
|
|
502
574
|
expect(processExitMock).toHaveBeenCalledWith(1);
|
|
503
575
|
});
|
|
576
|
+
|
|
577
|
+
it('prompts to publish in text mode only when the build passed', async () => {
|
|
578
|
+
writeSite({ 'index.mdx': HOME });
|
|
579
|
+
|
|
580
|
+
await runCommand('validate');
|
|
581
|
+
expect(sendPublishPrompt).toHaveBeenCalledTimes(1);
|
|
582
|
+
|
|
583
|
+
vi.mocked(checkBuild).mockResolvedValue({
|
|
584
|
+
passed: false,
|
|
585
|
+
error: 'Build validation failed with 1 warning(s). See above for details.',
|
|
586
|
+
strict: true,
|
|
587
|
+
warnings: [{ type: 'missing-nav-page', message: '"gone" is missing.' }],
|
|
588
|
+
});
|
|
589
|
+
await runCommand('validate', '--disable', 'missing-nav-page');
|
|
590
|
+
expect(sendPublishPrompt).toHaveBeenCalledTimes(1);
|
|
591
|
+
expect(processExitMock).toHaveBeenLastCalledWith(0);
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it('rejects --format values other than text and json', async () => {
|
|
595
|
+
writeSite({ 'index.mdx': HOME });
|
|
596
|
+
const error = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
597
|
+
|
|
598
|
+
await expect(runCommand('validate', '--format', 'table')).rejects.toThrow();
|
|
599
|
+
|
|
600
|
+
expect(error).toHaveBeenCalledWith(expect.stringContaining('Invalid values'));
|
|
601
|
+
error.mockRestore();
|
|
602
|
+
});
|
|
504
603
|
});
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import fsp from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { applyFixes, checkSite, selectRules, UsageError } from '../src/validate/engine.js';
|
|
2
7
|
import { RULES } from '../src/validate/rules/index.js';
|
|
3
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
GROUPS,
|
|
10
|
+
type Diagnostic,
|
|
11
|
+
type FileRule,
|
|
12
|
+
type Site,
|
|
13
|
+
type SiteRule,
|
|
14
|
+
} from '../src/validate/types.js';
|
|
4
15
|
|
|
5
16
|
const ids = (rules: { id: string }[]) => rules.map(({ id }) => id);
|
|
6
17
|
|
|
@@ -44,3 +55,176 @@ describe('selectRules', () => {
|
|
|
44
55
|
expect(new Set(ids(RULES)).size).toBe(RULES.length);
|
|
45
56
|
});
|
|
46
57
|
});
|
|
58
|
+
|
|
59
|
+
describe('applyFixes', () => {
|
|
60
|
+
let dir: string;
|
|
61
|
+
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'validate-engine-'));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
afterEach(() => {
|
|
67
|
+
fs.removeSync(dir);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const site = () => ({ baseDir: dir, files: [] }) as unknown as Site;
|
|
71
|
+
|
|
72
|
+
const diagnostic = (fix: Diagnostic['fix'], rule = 'r'): Diagnostic => ({
|
|
73
|
+
rule,
|
|
74
|
+
group: 'content',
|
|
75
|
+
severity: 'error',
|
|
76
|
+
file: 'a.mdx',
|
|
77
|
+
message: 'm',
|
|
78
|
+
fix,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('applies fixes across files, skipping ones that overlap an applied fix', async () => {
|
|
82
|
+
fs.writeFileSync(path.join(dir, 'a.mdx'), 'one two three');
|
|
83
|
+
fs.writeFileSync(path.join(dir, 'b.json'), '{}');
|
|
84
|
+
|
|
85
|
+
const changes = await applyFixes(site(), [
|
|
86
|
+
diagnostic({ file: 'a.mdx', start: 0, end: 3, text: '1' }),
|
|
87
|
+
diagnostic({ file: 'a.mdx', start: 8, end: 13, text: '3' }),
|
|
88
|
+
diagnostic({ file: 'b.json', start: 1, end: 1, text: '"k":1' }),
|
|
89
|
+
// Overlaps the first fix, so it waits for the next pass
|
|
90
|
+
diagnostic({ file: 'a.mdx', start: 2, end: 5, text: 'x' }),
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
expect(fs.readFileSync(path.join(dir, 'a.mdx'), 'utf-8')).toBe('1 two 3');
|
|
94
|
+
expect(fs.readFileSync(path.join(dir, 'b.json'), 'utf-8')).toBe('{"k":1}');
|
|
95
|
+
expect(changes.map(({ file, before, after }) => [file, before, after])).toEqual([
|
|
96
|
+
['a.mdx', 'one', '1'],
|
|
97
|
+
['a.mdx', 'three', '3'],
|
|
98
|
+
['b.json', '', '"k":1'],
|
|
99
|
+
]);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('applies two insertions at the same point', async () => {
|
|
103
|
+
fs.writeFileSync(path.join(dir, 'b.json'), '{}');
|
|
104
|
+
|
|
105
|
+
const changes = await applyFixes(site(), [
|
|
106
|
+
diagnostic({ file: 'b.json', start: 1, end: 1, text: ',"j":2' }),
|
|
107
|
+
diagnostic({ file: 'b.json', start: 1, end: 1, text: '"k":1' }),
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
expect(fs.readFileSync(path.join(dir, 'b.json'), 'utf-8')).toBe('{"k":1,"j":2}');
|
|
111
|
+
expect(changes).toHaveLength(2);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('applies a replacement and an insertion at its start', async () => {
|
|
115
|
+
fs.writeFileSync(path.join(dir, 'a.mdx'), 'a<br>b');
|
|
116
|
+
|
|
117
|
+
const changes = await applyFixes(site(), [
|
|
118
|
+
diagnostic({ file: 'a.mdx', start: 1, end: 5, text: '<br />' }),
|
|
119
|
+
diagnostic({ file: 'a.mdx', start: 1, end: 1, text: 'x' }),
|
|
120
|
+
]);
|
|
121
|
+
|
|
122
|
+
expect(fs.readFileSync(path.join(dir, 'a.mdx'), 'utf-8')).toBe('ax<br />b');
|
|
123
|
+
expect(changes.map(({ after }) => after)).toEqual(['<br />', 'x']);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('skips writing a Markdown file whose fixed content no longer parses', async () => {
|
|
127
|
+
fs.writeFileSync(path.join(dir, 'a.mdx'), 'hello world\n');
|
|
128
|
+
fs.writeFileSync(path.join(dir, 'b.mdx'), 'line<br>\n');
|
|
129
|
+
|
|
130
|
+
const changes = await applyFixes(site(), [
|
|
131
|
+
diagnostic({ file: 'a.mdx', start: 0, end: 5, text: '<Note>' }),
|
|
132
|
+
diagnostic({ file: 'b.mdx', start: 4, end: 8, text: '<br />' }),
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
expect(fs.readFileSync(path.join(dir, 'a.mdx'), 'utf-8')).toBe('hello world\n');
|
|
136
|
+
expect(fs.readFileSync(path.join(dir, 'b.mdx'), 'utf-8')).toBe('line<br />\n');
|
|
137
|
+
expect(changes.map(({ file, after }) => [file, after])).toEqual([['b.mdx', '<br />']]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('fixes a page that only parses after the build preprocesses its <MDX> fragments', async () => {
|
|
141
|
+
const fragment = '{true ? <MDX>\n```bash\n<root>/{arch}/app.vpkg\n```\n</MDX> : null}\n';
|
|
142
|
+
fs.writeFileSync(path.join(dir, 'a.mdx'), `${fragment}\nline<br>\n`);
|
|
143
|
+
const start = fragment.length + 5;
|
|
144
|
+
|
|
145
|
+
const changes = await applyFixes(site(), [
|
|
146
|
+
diagnostic({ file: 'a.mdx', start, end: start + 4, text: '<br />' }),
|
|
147
|
+
]);
|
|
148
|
+
|
|
149
|
+
expect(fs.readFileSync(path.join(dir, 'a.mdx'), 'utf-8')).toBe(`${fragment}\nline<br />\n`);
|
|
150
|
+
expect(changes).toHaveLength(1);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('fixes an unparseable page even when another error keeps it unparseable', async () => {
|
|
154
|
+
fs.writeFileSync(path.join(dir, 'a.mdx'), 'line<br>\n');
|
|
155
|
+
fs.writeFileSync(path.join(dir, 'b.mdx'), 'line<br>\n\nText {broken\n');
|
|
156
|
+
|
|
157
|
+
const changes = await applyFixes(site(), [
|
|
158
|
+
diagnostic({ file: 'a.mdx', start: 4, end: 8, text: '<br />' }),
|
|
159
|
+
diagnostic({ file: 'b.mdx', start: 4, end: 8, text: '<br />' }),
|
|
160
|
+
]);
|
|
161
|
+
|
|
162
|
+
expect(fs.readFileSync(path.join(dir, 'a.mdx'), 'utf-8')).toBe('line<br />\n');
|
|
163
|
+
expect(fs.readFileSync(path.join(dir, 'b.mdx'), 'utf-8')).toBe('line<br />\n\nText {broken\n');
|
|
164
|
+
expect(changes).toHaveLength(2);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe('checkSite with fix', () => {
|
|
169
|
+
let dir: string;
|
|
170
|
+
|
|
171
|
+
beforeEach(() => {
|
|
172
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'validate-fix-passes-'));
|
|
173
|
+
fs.writeJSONSync(path.join(dir, 'docs.json'), {
|
|
174
|
+
name: 'Test',
|
|
175
|
+
navigation: { pages: ['index', 'other'] },
|
|
176
|
+
});
|
|
177
|
+
fs.writeFileSync(path.join(dir, 'index.mdx'), '---\ntitle: Home\n---\n\nText\n');
|
|
178
|
+
fs.writeFileSync(path.join(dir, 'other.mdx'), '---\ntitle: Other\n---\n\nText!!!\n');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
afterEach(() => {
|
|
182
|
+
fs.removeSync(dir);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
/** Offers one "!" at a time until a page ends with `limit` of them, so each pass unlocks the next. */
|
|
186
|
+
const bang = (limit: number): FileRule => ({
|
|
187
|
+
id: 'bang',
|
|
188
|
+
group: 'content',
|
|
189
|
+
severity: 'warning',
|
|
190
|
+
scope: 'file',
|
|
191
|
+
raw: true,
|
|
192
|
+
description: 'test rule',
|
|
193
|
+
check: ({ content }) => {
|
|
194
|
+
const end = content.trimEnd().length;
|
|
195
|
+
return content.endsWith(`${'!'.repeat(limit)}\n`)
|
|
196
|
+
? []
|
|
197
|
+
: [{ message: 'needs a bang', edit: { start: end, end, text: '!' } }];
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const siteRule = (): SiteRule => ({
|
|
202
|
+
id: 'site',
|
|
203
|
+
group: 'config',
|
|
204
|
+
severity: 'warning',
|
|
205
|
+
scope: 'site',
|
|
206
|
+
description: 'test rule',
|
|
207
|
+
check: vi.fn(() => []),
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('rechecks only changed files between passes and runs site rules once more at the end', async () => {
|
|
211
|
+
const site = siteRule();
|
|
212
|
+
const reads = vi.spyOn(fsp, 'readFile');
|
|
213
|
+
|
|
214
|
+
const result = await checkSite(dir, [bang(3), site], { localSchema: false, fix: true });
|
|
215
|
+
|
|
216
|
+
const otherReads = reads.mock.calls.filter(([file]) => String(file).endsWith('other.mdx'));
|
|
217
|
+
reads.mockRestore();
|
|
218
|
+
expect(result.fixed.map(({ file }) => file)).toEqual(['index.mdx', 'index.mdx', 'index.mdx']);
|
|
219
|
+
expect(fs.readFileSync(path.join(dir, 'index.mdx'), 'utf-8')).toContain('Text!!!\n');
|
|
220
|
+
expect(result.diagnostics).toEqual([]);
|
|
221
|
+
expect(site.check).toHaveBeenCalledTimes(2);
|
|
222
|
+
expect(otherReads).toHaveLength(2);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('applies up to five passes of fixes', async () => {
|
|
226
|
+
const result = await checkSite(dir, [bang(10)], { localSchema: false, fix: true });
|
|
227
|
+
|
|
228
|
+
expect(result.fixed.filter(({ file }) => file === 'index.mdx')).toHaveLength(5);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -20,11 +20,15 @@ const hasErrors = (diagnostics: Diagnostic[]) =>
|
|
|
20
20
|
diagnostics.some(({ severity }) => severity === 'error');
|
|
21
21
|
|
|
22
22
|
/** Runs the file rules (all defaults, or exactly `rules`) the way mint validate --no-build does. */
|
|
23
|
-
const check = async (
|
|
23
|
+
const check = async (
|
|
24
|
+
dir: string,
|
|
25
|
+
{ rules, fix = false, files }: { rules?: string[]; fix?: boolean; files?: string[] }
|
|
26
|
+
) => {
|
|
24
27
|
const selected = selectRules({ disable: ['all'], enable: rules ?? ['content'] });
|
|
25
28
|
const result = await checkSite(dir, selected, {
|
|
26
29
|
selected: files && new Set(files),
|
|
27
30
|
localSchema: false,
|
|
31
|
+
fix,
|
|
28
32
|
});
|
|
29
33
|
const issues = result.diagnostics.map((diagnostic) => ({
|
|
30
34
|
...diagnostic,
|
|
@@ -32,6 +36,8 @@ const check = async (dir: string, { rules, files }: { rules?: string[]; files?:
|
|
|
32
36
|
}));
|
|
33
37
|
return {
|
|
34
38
|
issues,
|
|
39
|
+
fixed: result.fixed,
|
|
40
|
+
filesFixed: [...new Set(result.fixed.map(({ file }) => file))].sort(),
|
|
35
41
|
filesChecked: result.site.files.filter(({ file }) => !files || files.includes(file)).length,
|
|
36
42
|
};
|
|
37
43
|
};
|
|
@@ -59,26 +65,23 @@ describe('validate rules', () => {
|
|
|
59
65
|
|
|
60
66
|
const read = (file: string) => fs.readFileSync(path.join(dir, file), 'utf-8');
|
|
61
67
|
|
|
62
|
-
|
|
63
|
-
const previews = (issues: Diagnostic[]) =>
|
|
64
|
-
issues.map(
|
|
65
|
-
({ file, fix }) => fix && { before: read(file).slice(fix.start, fix.end), after: fix.text }
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
it('flags lowercase void tags with edits, skipping code and components', async () => {
|
|
68
|
+
it('self-closes lowercase void tags, skipping code and components', async () => {
|
|
69
69
|
write(dir, {
|
|
70
70
|
'index.mdx':
|
|
71
71
|
'---\ntitle: Home\n---\n\nline<br>\n\n<hr>\n\n<img src="/a.png" alt="a">\n\n<Link>x</Link>\n\n`<br>`\n\n```html\n<br>\n```\n',
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
const
|
|
74
|
+
const plain = await check(dir, {});
|
|
75
|
+
expect(plain.issues.map(({ rule }) => rule)).toEqual(['void-tags', 'void-tags', 'void-tags']);
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
expect(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
const fixed = await check(dir, { fix: true });
|
|
78
|
+
expect(fixed.issues).toEqual([]);
|
|
79
|
+
const content = read('index.mdx');
|
|
80
|
+
expect(content).toContain('line<br />');
|
|
81
|
+
expect(content).toContain('<hr />');
|
|
82
|
+
expect(content).toContain('<img src="/a.png" alt="a" />');
|
|
83
|
+
expect(content).toContain('`<br>`');
|
|
84
|
+
expect(content).toContain('```html\n<br>\n```');
|
|
82
85
|
});
|
|
83
86
|
|
|
84
87
|
it('skips void tags inside block-quoted code fences', async () => {
|
|
@@ -87,16 +90,13 @@ describe('validate rules', () => {
|
|
|
87
90
|
'other.mdx': '---\ntitle: Other\n---\n\n> ```\n> <br>\n\nafter<br>\n',
|
|
88
91
|
});
|
|
89
92
|
|
|
90
|
-
const result = await check(dir, {});
|
|
93
|
+
const result = await check(dir, { fix: true });
|
|
91
94
|
|
|
92
|
-
expect(result.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
expect(
|
|
97
|
-
{ before: '<br>', after: '<br />' },
|
|
98
|
-
{ before: '<br>', after: '<br />' },
|
|
99
|
-
]);
|
|
95
|
+
expect(result.fixed.map(({ before }) => before).sort()).toEqual(['<br>', '<br>']);
|
|
96
|
+
expect(read('index.mdx')).toContain('> <br>\n');
|
|
97
|
+
expect(read('index.mdx')).toContain('line<br />');
|
|
98
|
+
expect(read('other.mdx')).toContain('> <br>\n');
|
|
99
|
+
expect(read('other.mdx')).toContain('after<br />');
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
it('keeps an info-string fence line from closing an open fence', async () => {
|
|
@@ -105,10 +105,12 @@ describe('validate rules', () => {
|
|
|
105
105
|
'---\ntitle: Home\n---\n\n```bash\n$ one\n\nStep 2\n\n```bash\n$ two <br>\n```\n\nafter<br>\n',
|
|
106
106
|
});
|
|
107
107
|
|
|
108
|
-
const result = await check(dir, {});
|
|
108
|
+
const result = await check(dir, { fix: true });
|
|
109
109
|
|
|
110
|
-
expect(result.
|
|
111
|
-
|
|
110
|
+
expect(result.fixed.map(({ before }) => before)).toEqual(['<br>']);
|
|
111
|
+
const content = read('index.mdx');
|
|
112
|
+
expect(content).toContain('$ two <br>\n');
|
|
113
|
+
expect(content).toContain('after<br />');
|
|
112
114
|
});
|
|
113
115
|
|
|
114
116
|
it('does not close a block-quoted fence with an info-string fence line', async () => {
|
|
@@ -117,10 +119,12 @@ describe('validate rules', () => {
|
|
|
117
119
|
'---\ntitle: Home\n---\n\n> ```\n> <br>\n> ```bash\n> <hr>\n> ```\n\nafter<br>\n',
|
|
118
120
|
});
|
|
119
121
|
|
|
120
|
-
const result = await check(dir, {});
|
|
122
|
+
const result = await check(dir, { fix: true });
|
|
121
123
|
|
|
122
|
-
expect(result.
|
|
123
|
-
|
|
124
|
+
expect(result.fixed.map(({ before }) => before)).toEqual(['<br>']);
|
|
125
|
+
const content = read('index.mdx');
|
|
126
|
+
expect(content).toContain('> <hr>\n');
|
|
127
|
+
expect(content).toContain('after<br />');
|
|
124
128
|
});
|
|
125
129
|
|
|
126
130
|
it('skips custom elements, frontmatter, ESM and attribute strings for void tags', async () => {
|
|
@@ -130,11 +134,15 @@ describe('validate rules', () => {
|
|
|
130
134
|
});
|
|
131
135
|
|
|
132
136
|
const { issues } = await check(dir, { rules: ['void-tags'] });
|
|
133
|
-
|
|
134
137
|
expect(issues.map(({ line }) => line)).toEqual([12]);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
|
|
139
|
+
await check(dir, { rules: ['void-tags'], fix: true });
|
|
140
|
+
const content = read('index.mdx');
|
|
141
|
+
expect(content).toContain('<img src="/a.png" alt="<hr>" />');
|
|
142
|
+
expect(content).toContain('title: "a<br>b"');
|
|
143
|
+
expect(content).toContain('export const html = "<br>";');
|
|
144
|
+
expect(content).toContain('<Card title="one<br>two">');
|
|
145
|
+
expect(content).toContain('`` a`<br>`b ``');
|
|
138
146
|
});
|
|
139
147
|
|
|
140
148
|
it('leaves escaped void tags alone', async () => {
|
|
@@ -159,7 +167,9 @@ describe('validate rules', () => {
|
|
|
159
167
|
expect(issues.map(({ rule, line, column }) => [rule, line, column])).toEqual([
|
|
160
168
|
['void-tags', 1, 23],
|
|
161
169
|
]);
|
|
162
|
-
|
|
170
|
+
|
|
171
|
+
await check(dir, { fix: true });
|
|
172
|
+
expect(read('snippets/note.mdx')).toBe('Hello {{foo.bar}} line<br />\n');
|
|
163
173
|
});
|
|
164
174
|
|
|
165
175
|
it('leaves void tags that already have a closing tag', async () => {
|
|
@@ -167,6 +177,8 @@ describe('validate rules', () => {
|
|
|
167
177
|
write(dir, { 'index.mdx': page });
|
|
168
178
|
|
|
169
179
|
expect((await check(dir, { rules: ['void-tags'] })).issues).toEqual([]);
|
|
180
|
+
await check(dir, { rules: ['void-tags'], fix: true });
|
|
181
|
+
expect(read('index.mdx')).toBe(page);
|
|
170
182
|
});
|
|
171
183
|
|
|
172
184
|
it('accepts <MDX> fragments the build preprocesses', async () => {
|
|
@@ -222,15 +234,17 @@ describe('validate rules', () => {
|
|
|
222
234
|
});
|
|
223
235
|
|
|
224
236
|
const { issues } = await check(dir, { rules: ['void-tags'] });
|
|
225
|
-
|
|
226
237
|
expect(issues.map(({ rule, line }) => [rule, line])).toEqual([
|
|
227
238
|
['void-tags', 7],
|
|
228
239
|
['void-tags', 11],
|
|
229
240
|
]);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
241
|
+
|
|
242
|
+
await check(dir, { rules: ['void-tags'], fix: true });
|
|
243
|
+
const content = read('index.mdx');
|
|
244
|
+
expect(content).toContain("s.replace(/<br>/g, '')");
|
|
245
|
+
expect(content).toContain("replace(/\\/<hr>/, '')");
|
|
246
|
+
expect(content).toContain('and<br />');
|
|
247
|
+
expect(content).toContain('line<br />');
|
|
234
248
|
});
|
|
235
249
|
|
|
236
250
|
it('masks line comments inside expressions', async () => {
|
|
@@ -262,11 +276,11 @@ describe('validate rules', () => {
|
|
|
262
276
|
);
|
|
263
277
|
write(dir, Object.fromEntries(names.map((name) => [name, '---\ntitle: T\n---\n\nline<br>\n'])));
|
|
264
278
|
|
|
265
|
-
const
|
|
279
|
+
const plain = await check(dir, { rules: ['void-tags'] });
|
|
280
|
+
expect(plain.issues.map(({ file }) => file)).toEqual(names);
|
|
266
281
|
|
|
267
|
-
const
|
|
268
|
-
expect(
|
|
269
|
-
expect(flagged).toHaveLength(40);
|
|
282
|
+
const fixed = await check(dir, { rules: ['void-tags'], fix: true });
|
|
283
|
+
expect(fixed.fixed.map(({ file }) => file)).toEqual(names);
|
|
270
284
|
});
|
|
271
285
|
|
|
272
286
|
it('rejects unknown rule ids', async () => {
|
|
@@ -368,8 +382,9 @@ describe('validate rules', () => {
|
|
|
368
382
|
|
|
369
383
|
const { issues } = await check(dir, { rules: ['void-tags'] });
|
|
370
384
|
|
|
371
|
-
expect(issues.map(({ line }) => line)).toEqual([
|
|
372
|
-
|
|
385
|
+
expect(issues.map(({ line, message }) => [line, message])).toEqual([
|
|
386
|
+
[7, '<br> must be self-closed'],
|
|
387
|
+
]);
|
|
373
388
|
});
|
|
374
389
|
|
|
375
390
|
it('does not pair backticks across table cells', async () => {
|
|
@@ -403,9 +418,9 @@ describe('validate rules', () => {
|
|
|
403
418
|
|
|
404
419
|
const { issues } = await check(dir, { rules: ['void-tags'] });
|
|
405
420
|
|
|
406
|
-
expect(
|
|
407
|
-
|
|
408
|
-
|
|
421
|
+
expect(issues.map(({ message }) => message)).toEqual([
|
|
422
|
+
'<br> must be self-closed',
|
|
423
|
+
'<hr> must be self-closed',
|
|
409
424
|
]);
|
|
410
425
|
});
|
|
411
426
|
});
|