-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcheck-icon-path-precision.ts
More file actions
402 lines (353 loc) · 13.9 KB
/
Copy pathcheck-icon-path-precision.ts
File metadata and controls
402 lines (353 loc) · 13.9 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env bun
/**
* Prevents overly precise numeric values in literal SVG icon `d` attributes.
*
* Three decimal places are enough for the reusable icons covered here: additional
* digits increase shipped source without a visible benefit. Every path must
* satisfy the limit or carry a reasoned local exception.
*
* Scope is intentionally limited to the shared app/docs icon catalogs and EMCN
* icon components. SVG transforms, view boxes, dynamic path expressions, and
* page-specific artwork are not inspected because their safe precision depends
* on context.
*
* Run with `bun run check:icon-path-precision`.
*/
import { readdir, readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { parse } from '@babel/parser'
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const EMCN_ICONS_DIRECTORY = path.join(ROOT, 'packages/emcn/src/icons')
const STATIC_ICON_FILES = [
path.join(ROOT, 'apps/docs/components/icons.tsx'),
path.join(ROOT, 'apps/sim/components/icons.tsx'),
]
const SVG_NUMBER_PATTERN = /[+-]?(?:(?:\d+\.\d*)|(?:\.\d+)|(?:\d+))(?:[eE][+-]?\d+)?/g
const PRECISION_EXCEPTION_DIRECTIVE = 'svg-path-precision-exception:'
export const MAX_ICON_PATH_FRACTION_DIGITS = 3
interface ParsedPrecisionException {
line: number
reason: string | null
}
interface LiteralPath {
exception: ParsedPrecisionException | null
icon: string
line: number
value: string
}
export interface PrecisionCandidate {
file: string
icon: string
line: number
maxFractionDigits: number
offendingNumbers: string[]
}
export interface InvalidPrecisionException {
file: string
line: number
message: string
}
export interface IconPrecisionAnalysis {
candidates: PrecisionCandidate[]
invalidExceptions: InvalidPrecisionException[]
}
interface ExtractedPaths {
paths: LiteralPath[]
invalidExceptions: Omit<InvalidPrecisionException, 'file'>[]
}
function asRecord(value: unknown): Record<string, unknown> | null {
return value !== null && typeof value === 'object' ? (value as Record<string, unknown>) : null
}
function jsxStringValue(attribute: Record<string, unknown>): string | null {
const value = asRecord(attribute.value)
if (!value) return null
if (value.type === 'StringLiteral' && typeof value.value === 'string') return value.value
if (value.type !== 'JSXExpressionContainer') return null
const expression = asRecord(value.expression)
if (!expression) return null
if (expression.type === 'StringLiteral' && typeof expression.value === 'string') {
return expression.value
}
if (expression.type !== 'TemplateLiteral') return null
const expressions = expression.expressions
const quasis = expression.quasis
if (!Array.isArray(expressions) || expressions.length > 0 || !Array.isArray(quasis)) return null
const quasi = asRecord(quasis[0])
const quasiValue = asRecord(quasi?.value)
if (!quasiValue) return null
if (typeof quasiValue.cooked === 'string') return quasiValue.cooked
return typeof quasiValue.raw === 'string' ? quasiValue.raw : null
}
function iconNameAt(source: string, offset: number): string {
const before = source.slice(0, offset)
const matches = [...before.matchAll(/export (?:function|const) (\w+)\s*[=(]/g)]
return matches.length > 0 ? matches[matches.length - 1][1] : '<unknown>'
}
function nodeLine(node: Record<string, unknown>): number {
const location = asRecord(node.loc)
const start = asRecord(location?.start)
return typeof start?.line === 'number' ? start.line : 1
}
function jsxElementName(node: Record<string, unknown>): string | null {
const openingElement = asRecord(node.openingElement)
const name = asRecord(openingElement?.name)
return name?.type === 'JSXIdentifier' && typeof name.name === 'string' ? name.name : null
}
function normalizedComment(comment: Record<string, unknown>): string {
if (typeof comment.value !== 'string') return ''
return comment.value
.split('\n')
.map((line) => line.replace(/^\s*\*?\s?/, '').trim())
.filter(Boolean)
.join(' ')
}
function precisionExceptionFromChild(
child: Record<string, unknown>,
source: string
): ParsedPrecisionException | null {
if (child.type !== 'JSXExpressionContainer') return null
const expression = asRecord(child.expression)
if (expression?.type !== 'JSXEmptyExpression') return null
const comments = expression.innerComments
if (!Array.isArray(comments)) return null
for (const value of comments) {
const comment = asRecord(value)
if (!comment) continue
const start = typeof comment.start === 'number' ? comment.start : -1
const end = typeof comment.end === 'number' ? comment.end : -1
if (start < 0 || end < 0 || !source.slice(start, end).startsWith('/**')) continue
const text = normalizedComment(comment)
if (!text.startsWith(PRECISION_EXCEPTION_DIRECTIVE)) continue
const reason = text.slice(PRECISION_EXCEPTION_DIRECTIVE.length).trim()
return { line: nodeLine(comment), reason: reason || null }
}
return null
}
function extractLiteralPaths(source: string, file: string): ExtractedPaths {
const syntaxTree = parse(source, {
sourceFilename: file,
sourceType: 'module',
plugins: ['typescript', 'jsx'],
})
const paths: LiteralPath[] = []
const invalidExceptions: Omit<InvalidPrecisionException, 'file'>[] = []
function invalidate(exception: ParsedPrecisionException, message: string): void {
invalidExceptions.push({ line: exception.line, message })
}
function visitChildren(children: unknown): void {
if (!Array.isArray(children)) return
let pendingException: ParsedPrecisionException | null = null
for (const value of children) {
const child = asRecord(value)
if (!child) continue
if (child.type === 'JSXText' && typeof child.value === 'string' && !child.value.trim())
continue
const exception = precisionExceptionFromChild(child, source)
if (exception) {
if (pendingException) {
invalidate(pendingException, 'Exception must immediately precede one literal <path>.')
}
pendingException = exception
continue
}
if (pendingException && (child.type !== 'JSXElement' || jsxElementName(child) !== 'path')) {
invalidate(pendingException, 'Exception must immediately precede one literal <path>.')
pendingException = null
}
visit(child, pendingException)
pendingException = null
}
if (pendingException) {
invalidate(pendingException, 'Exception must immediately precede one literal <path>.')
}
}
function visit(value: unknown, exception: ParsedPrecisionException | null = null): void {
if (Array.isArray(value)) {
for (const entry of value) visit(entry)
return
}
const node = asRecord(value)
if (!node) return
if (node.type === 'JSXElement') {
const openingElement = asRecord(node.openingElement)
if (jsxElementName(node) === 'path' && openingElement) {
const attributes = openingElement.attributes
const dAttribute = Array.isArray(attributes)
? attributes.map(asRecord).find((attribute) => {
const name = asRecord(attribute?.name)
return name?.type === 'JSXIdentifier' && name.name === 'd'
})
: null
const pathValue = dAttribute ? jsxStringValue(dAttribute) : null
if (pathValue !== null) {
const start = typeof openingElement.start === 'number' ? openingElement.start : 0
paths.push({
exception,
icon: iconNameAt(source, start),
line: nodeLine(dAttribute ?? openingElement),
value: pathValue,
})
} else if (exception) {
invalidate(exception, 'Exception applies only to a literal <path d> value.')
}
} else if (exception) {
invalidate(exception, 'Exception must immediately precede one literal <path>.')
}
visitChildren(node.children)
return
}
if (node.type === 'JSXFragment') {
if (exception) {
invalidate(exception, 'Exception must immediately precede one literal <path>.')
}
visitChildren(node.children)
return
}
for (const [key, child] of Object.entries(node)) {
if (key === 'loc' || key === 'start' || key === 'end' || key === 'extra') continue
visit(child)
}
}
visit(syntaxTree)
return { paths, invalidExceptions }
}
/**
* Counts both digits written after the decimal point and precision introduced
* by a negative exponent. This catches values such as `1.234` and `1e-3`.
*/
export function effectiveFractionDigits(numberLiteral: string): number {
const [mantissa, exponentText] = numberLiteral.toLowerCase().split('e')
const decimalIndex = mantissa.indexOf('.')
const writtenFractionDigits = decimalIndex < 0 ? 0 : mantissa.length - decimalIndex - 1
const exponent = exponentText === undefined ? 0 : Number.parseInt(exponentText, 10)
const exponentFractionDigits = Math.max(0, writtenFractionDigits - exponent)
return Math.max(writtenFractionDigits, exponentFractionDigits)
}
function normalizedRelativePath(file: string): string {
return path.relative(ROOT, file).split(path.sep).join('/')
}
export function analyzeIconSource(source: string, file: string): IconPrecisionAnalysis {
return analyzeExtractedPaths(extractLiteralPaths(source, file), file)
}
function analyzeExtractedPaths(extracted: ExtractedPaths, file: string): IconPrecisionAnalysis {
const candidates: PrecisionCandidate[] = []
const normalizedFile = normalizedRelativePath(file)
const invalidExceptions = extracted.invalidExceptions.map((exception) => ({
...exception,
file: normalizedFile,
}))
for (const literalPath of extracted.paths) {
const preciseNumbers = [...literalPath.value.matchAll(SVG_NUMBER_PATTERN)]
.map((match) => match[0])
.filter(
(numberLiteral) => effectiveFractionDigits(numberLiteral) > MAX_ICON_PATH_FRACTION_DIGITS
)
if (literalPath.exception) {
if (!literalPath.exception.reason) {
invalidExceptions.push({
file: normalizedFile,
line: literalPath.exception.line,
message: 'Exception must include a specific reason after the colon.',
})
} else if (preciseNumbers.length === 0) {
invalidExceptions.push({
file: normalizedFile,
line: literalPath.exception.line,
message: 'Exception is unnecessary because this path uses at most three decimal places.',
})
} else {
continue
}
}
if (preciseNumbers.length === 0) continue
candidates.push({
file: normalizedFile,
icon: literalPath.icon,
line: literalPath.line,
maxFractionDigits: Math.max(...preciseNumbers.map(effectiveFractionDigits)),
offendingNumbers: [...new Set(preciseNumbers)].slice(0, 4),
})
}
return { candidates, invalidExceptions }
}
export function findPrecisionCandidates(source: string, file: string): PrecisionCandidate[] {
return analyzeIconSource(source, file).candidates
}
async function currentIconFiles(): Promise<string[]> {
const emcnIcons = (await readdir(EMCN_ICONS_DIRECTORY))
.filter((file) => file.endsWith('.tsx'))
.sort()
.map((file) => path.join(EMCN_ICONS_DIRECTORY, file))
return [...STATIC_ICON_FILES, ...emcnIcons]
}
async function scanCurrentFiles(files: string[]): Promise<IconPrecisionAnalysis> {
const candidates: PrecisionCandidate[] = []
const invalidExceptions: InvalidPrecisionException[] = []
const sources = await Promise.all(files.map((file) => readFile(file, 'utf8')))
const parsedBySource = new Map<string, ExtractedPaths>()
for (const [index, file] of files.entries()) {
const source = sources[index]
let extracted = parsedBySource.get(source)
if (!extracted) {
extracted = extractLiteralPaths(source, file)
parsedBySource.set(source, extracted)
}
const analysis = analyzeExtractedPaths(extracted, file)
candidates.push(...analysis.candidates)
invalidExceptions.push(...analysis.invalidExceptions)
}
return { candidates, invalidExceptions }
}
function printCandidate(candidate: PrecisionCandidate): void {
console.error(
` ${candidate.file}:${candidate.line} (${candidate.icon}) — ${candidate.maxFractionDigits} fractional digits`
)
console.error(` values: ${candidate.offendingNumbers.join(', ')}`)
}
async function main(): Promise<void> {
if (process.argv.length > 2) {
console.error('Usage: bun run check:icon-path-precision')
process.exit(1)
}
const files = await currentIconFiles()
const current = await scanCurrentFiles(files)
if (current.invalidExceptions.length > 0) {
console.error(
`\nFound ${current.invalidExceptions.length} invalid SVG precision exception(s):\n`
)
for (const exception of current.invalidExceptions) {
console.error(` ${exception.file}:${exception.line} — ${exception.message}`)
}
}
if (current.candidates.length > 0) {
console.error(
`\nFound ${current.candidates.length} icon path(s) with more than ${MAX_ICON_PATH_FRACTION_DIGITS} fractional digits:\n`
)
for (const candidate of current.candidates) printCandidate(candidate)
console.error(
'\nRound only numeric values inside the literal d attribute to at most three decimal places.'
)
console.error(
'If extra precision is visibly necessary, place this reasoned exception immediately before that path:'
)
console.error(`{/**
* ${PRECISION_EXCEPTION_DIRECTIVE} Explain why rounding changes this geometry.
*/}`)
console.error(
'Do not round transform or viewBox values automatically; verify those geometry changes separately.'
)
}
if (current.invalidExceptions.length > 0 || current.candidates.length > 0) {
process.exit(1)
}
console.log(
`✓ All literal icon paths use at most three decimal places (${files.length} files checked).`
)
}
if (import.meta.main) {
main().catch((error: unknown) => {
console.error(error)
process.exit(1)
})
}