Repository navigation
Expand file tree
/
Copy pathcheck-comment-hygiene.test.ts
More file actions
125 lines (114 loc) · 3.78 KB
/
Copy pathcheck-comment-hygiene.test.ts
File metadata and controls
125 lines (114 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { describe, expect, it } from 'vitest'
import { findViolations } from './check-comment-hygiene'
function rules(source: string, file = 'example.ts') {
return findViolations(file, source).map(({ rule, line }) => `${rule}@${line}`)
}
describe('banner', () => {
it('flags decorated separators in line and single-line block comments', () => {
const source = [
'// ==========',
'// --- Helpers ---',
' // ─── Runs ───',
'/* ------ v2 grammar ------ */',
'const a = 1',
].join('\n')
expect(rules(source)).toEqual(['banner@1', 'banner@2', 'banner@3', 'banner@4'])
})
it('ignores separators inside strings and multi-line TSDoc', () => {
const source = [
'const frontmatter = `',
'// ---',
'`',
'/**',
' * ---',
' * name: skill',
' * ---',
' */',
'export const b = frontmatter',
].join('\n')
expect(rules(source)).toEqual([])
})
})
describe('commented-out-code', () => {
it('flags a commented-out object under a prose heading', () => {
const source = [
'const blocks = [',
' // Disabled until the endpoint is back',
' // {',
" // id: 'query',",
" // condition: { field: 'operation', value: 'search' },",
' // },',
']',
].join('\n')
expect(rules(source)).toEqual(['commented-out-code@3'])
})
it('flags snippets that only parse separately', () => {
const source = [
'// {',
"// id: 'x',",
'// },',
"// export type Mode = 'raw' | 'pretty'",
].join('\n')
expect(rules(source)).toEqual(['commented-out-code@1'])
})
it('flags single commented-out statements', () => {
expect(rules("// export type Mode = 'raw' | 'pretty'")).toEqual(['commented-out-code@1'])
expect(rules('// await flushQueue(queue);')).toEqual(['commented-out-code@1'])
})
it('ignores prose that happens to look like code', () => {
const source = [
'// firstRow: text=bg1, bold, fill=accent',
'// Total = 22',
'// limit=3 → [3,3,1]',
'// Shows (Podcasts)',
'// Title (for issue creation)',
'// Example: [{ fields: { "Field 1": "Value1" } }]',
'// return early when nothing changed.',
'',
'// cos(wd2, 2160000)',
'',
'// settings.get(key).value',
'',
'// Total: { count: 1 }',
'// Shape: { id: 2 }',
'const c = 1',
].join('\n')
expect(rules(source)).toEqual([])
})
it('ignores trailing comments, tool directives and allowed samples', () => {
const source = [
'run() // await retry(run);',
'// biome-ignore lint/suspicious/noExplicitAny: external payload',
'const d: any = 1',
'// comment-hygiene-allow: documents the call shape the worker expects',
'// await worker.run({ id });',
].join('\n')
expect(rules(source)).toEqual([])
})
it('parses JSX in tsx files', () => {
expect(rules('// return <Panel open={open} />;', 'view.tsx')).toEqual(['commented-out-code@1'])
})
})
describe('eslint-directive', () => {
it('flags own-line, trailing, and block ESLint directives', () => {
const source = [
'// eslint-disable-next-line react-hooks/exhaustive-deps',
'useEffect(() => {}, []) // eslint-disable-line react-hooks/exhaustive-deps',
'/* eslint-disable @typescript-eslint/no-explicit-any */',
'/* eslint-enable */',
].join('\n')
expect(rules(source)).toEqual([
'eslint-directive@1',
'eslint-directive@2',
'eslint-directive@3',
'eslint-directive@4',
])
})
it('ignores ESLint mentioned in prose and strings', () => {
const source = [
'// Ported from a file that used an eslint-disable comment',
"const rule = '// eslint-disable-next-line'",
].join('\n')
expect(rules(source)).toEqual([])
})
})