-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcheck-icon-path-precision.test.ts
More file actions
123 lines (109 loc) · 3.74 KB
/
Copy pathcheck-icon-path-precision.test.ts
File metadata and controls
123 lines (109 loc) · 3.74 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
import { describe, expect, it } from 'vitest'
import {
analyzeIconSource,
effectiveFractionDigits,
findPrecisionCandidates,
} from './check-icon-path-precision'
const FIXTURE_PATH = '/repo/packages/emcn/src/icons/fixture.tsx'
describe('icon path precision audit', () => {
it('accepts the three-decimal boundary and ignores geometry outside literal paths', () => {
const source = `
const dynamicPath = 'M0.1234 1'
const unrelated = "d='M0.1234 1'"
export function SafeIcon() {
return (
<svg viewBox='0 0 10.1234 10' transform='scale(0.16624)'>
<path d='M.5 1.200L1.2e1 2' />
<path d={dynamicPath} />
</svg>
)
}
`
expect(analyzeIconSource(source, FIXTURE_PATH)).toEqual({
candidates: [],
invalidExceptions: [],
})
})
it('finds ordinary decimals and exponents finer than a thousandth', () => {
const source = `
export function PreciseIcon() {
return <svg><path d='M0.1234 1e-4L2.345 5' /></svg>
}
`
const candidates = findPrecisionCandidates(source, FIXTURE_PATH)
expect(candidates).toHaveLength(1)
expect(candidates[0]).toMatchObject({
icon: 'PreciseIcon',
maxFractionDigits: 4,
offendingNumbers: ['0.1234', '1e-4'],
})
expect(effectiveFractionDigits('2.13949e-05')).toBe(10)
})
it('checks literal JSX expressions and static template literals', () => {
const source = `
export function ExpressionIcon() {
return (
<svg>
<path d={'M0.1234 1'} />
<path d={\`M2.3456 3\`} />
</svg>
)
}
`
expect(findPrecisionCandidates(source, FIXTURE_PATH)).toHaveLength(2)
})
it('accepts a reasoned TSDoc exception on the immediately following path', () => {
const source = `
export function PreciseBrandIcon() {
return (
<svg>
{/**
* svg-path-precision-exception: Rounding visibly distorts the provider-authored mark.
*/}
<path d='M0.1234 1' />
</svg>
)
}
`
expect(analyzeIconSource(source, FIXTURE_PATH)).toEqual({
candidates: [],
invalidExceptions: [],
})
})
it('rejects exceptions without a reason and exceptions on already-clean paths', () => {
const missingReason = `
export function PreciseIcon() {
return <svg>{/** svg-path-precision-exception: */}<path d='M0.1234 1' /></svg>
}
`
const unnecessary = `
export function CleanIcon() {
return <svg>{/** svg-path-precision-exception: Keep detail. */}<path d='M0.12 1' /></svg>
}
`
const missingReasonAnalysis = analyzeIconSource(missingReason, FIXTURE_PATH)
expect(missingReasonAnalysis.candidates).toHaveLength(1)
expect(missingReasonAnalysis.invalidExceptions[0]?.message).toContain('specific reason')
const unnecessaryAnalysis = analyzeIconSource(unnecessary, FIXTURE_PATH)
expect(unnecessaryAnalysis.candidates).toEqual([])
expect(unnecessaryAnalysis.invalidExceptions[0]?.message).toContain('unnecessary')
})
it('requires a reasoned exception to remain while its precise path remains', () => {
const excepted = `
export function PreciseBrandIcon() {
return (
<svg>
{/** svg-path-precision-exception: Rounding visibly distorts the brand mark. */}
<path d='M0.1234 1' />
</svg>
)
}
`
const exceptionRemoved = excepted.replace(
'{/** svg-path-precision-exception: Rounding visibly distorts the brand mark. */}',
''
)
expect(findPrecisionCandidates(excepted, FIXTURE_PATH)).toEqual([])
expect(findPrecisionCandidates(exceptionRemoved, FIXTURE_PATH)).toHaveLength(1)
})
})