@adea-ai/ui 0.148.0 → 0.149.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.
@@ -0,0 +1,17 @@
1
+ export {
2
+ collectLinkedImports,
3
+ collectUiSourceFiles,
4
+ formatUiSourceDirectives,
5
+ frameworkSourceRoots,
6
+ missingRenderedUiSources,
7
+ tailwindSources,
8
+ uiSourcePaths,
9
+ type GraphCheckContext,
10
+ type LinkedImport,
11
+ type LinkedImportOptions,
12
+ type Selection,
13
+ type SourceDirectiveOptions,
14
+ type SourceDiscoveryOptions,
15
+ type TailwindSourcesOptions,
16
+ type UiSourcePaths,
17
+ } from './tailwind-sources'
@@ -0,0 +1,358 @@
1
+ import { parse } from '@babel/parser'
2
+ import { extname } from 'node:path'
3
+
4
+ // Babel's node unions are too wide to narrow usefully across this many shapes;
5
+ // every reader below checks `type` before it touches a field.
6
+ // oxlint-disable-next-line typescript/no-explicit-any
7
+ export type SyntaxNode = any
8
+
9
+ export type Selection = { kind: 'all' } | { kind: 'named'; names: string[] }
10
+
11
+ export interface StaticRuntimeImport {
12
+ specifier: string
13
+ kind: 'side-effect' | 'bindings'
14
+ bindings: Array<{ imported: string; local: string }>
15
+ }
16
+
17
+ export type RuntimeReexport =
18
+ | { kind: 'star'; source: string }
19
+ | { kind: 'empty'; source: string }
20
+ | { kind: 'namespace'; source?: string; exported: string }
21
+ | { kind: 'named'; source?: string; exported: string; imported: string }
22
+
23
+ /** Parse source without depending on the TypeScript compiler's JavaScript API. */
24
+ export function parseSource(file: string, source: string): SyntaxNode {
25
+ const extension = extname(file).toLowerCase()
26
+ const typed = ['.ts', '.tsx', '.mts', '.cts'].includes(extension)
27
+ const jsx = ['.jsx', '.tsx'].includes(extension)
28
+ const ast = parse(source, {
29
+ sourceFilename: file,
30
+ sourceType: 'unambiguous',
31
+ allowUndeclaredExports: true,
32
+ allowReturnOutsideFunction: extension === '.cjs',
33
+ allowNewTargetOutsideFunction: extension === '.cjs',
34
+ plugins: [...(typed ? (['typescript'] as const) : []), ...(jsx ? (['jsx'] as const) : [])],
35
+ })
36
+ Object.defineProperty(ast, 'sourceText', { value: source })
37
+ return ast
38
+ }
39
+
40
+ /** Visit syntax nodes with their actual parent, without traversing location metadata. */
41
+ export function visitSource(
42
+ node: SyntaxNode,
43
+ visit: (node: SyntaxNode, parent: SyntaxNode) => void,
44
+ parent: SyntaxNode = null
45
+ ): void {
46
+ if (!node || typeof node !== 'object' || typeof node.type !== 'string') return
47
+ visit(node, parent)
48
+ for (const [key, value] of Object.entries(node)) {
49
+ if (key === 'loc' || key === 'tokens' || key === 'comments' || key === 'errors') continue
50
+ if (Array.isArray(value)) {
51
+ for (const child of value) visitSource(child, visit, node)
52
+ } else visitSource(value, visit, node)
53
+ }
54
+ }
55
+
56
+ /** Describe statically imported runtime bindings without treating type imports as runtime edges. */
57
+ export function staticRuntimeImports(ast: SyntaxNode): StaticRuntimeImport[] {
58
+ const imports: StaticRuntimeImport[] = []
59
+ for (const declaration of ast.program.body) {
60
+ if (declaration.type !== 'ImportDeclaration' || declaration.importKind === 'type') continue
61
+ if (declaration.specifiers.length === 0) {
62
+ const prefix = ast.sourceText
63
+ .slice(declaration.start, declaration.source.start)
64
+ .replace(/\/\*[\s\S]*?\*\/|\/\/[^\n\r]*/g, '')
65
+ .trim()
66
+ // Preserve the existing collector's distinction between bare imports and `import {}`.
67
+ if (prefix === 'import')
68
+ imports.push({
69
+ specifier: declaration.source.value,
70
+ kind: 'side-effect',
71
+ bindings: [],
72
+ })
73
+ continue
74
+ }
75
+ const bindings: StaticRuntimeImport['bindings'] = []
76
+ for (const binding of declaration.specifiers) {
77
+ if (binding.importKind === 'type') continue
78
+ const imported =
79
+ binding.type === 'ImportDefaultSpecifier'
80
+ ? 'default'
81
+ : binding.type === 'ImportNamespaceSpecifier'
82
+ ? '*'
83
+ : (binding.imported.name ?? binding.imported.value)
84
+ bindings.push({ imported, local: binding.local.name })
85
+ }
86
+ if (bindings.length)
87
+ imports.push({
88
+ specifier: declaration.source.value,
89
+ kind: 'bindings',
90
+ bindings,
91
+ })
92
+ }
93
+ return imports
94
+ }
95
+
96
+ /** Conservatively select members used through a namespace import. */
97
+ export function selectNamespace(
98
+ ast: SyntaxNode,
99
+ local: string,
100
+ ignoredBinding: SyntaxNode = null
101
+ ): Selection | undefined {
102
+ const names = new Set<string>()
103
+ let used = false
104
+ let opaque = false
105
+ let shadowed = false
106
+ visitSource(ast, (node, parent) => {
107
+ if (
108
+ !parent ||
109
+ node === ignoredBinding ||
110
+ !['Identifier', 'JSXIdentifier'].includes(node.type) ||
111
+ node.name !== local
112
+ )
113
+ return
114
+ if (parent.type.startsWith('Import')) return
115
+ const binding =
116
+ (parent.type === 'VariableDeclarator' && parent.id === node) ||
117
+ (parent.type === 'CatchClause' && parent.param === node) ||
118
+ (/^(?:Function|Class|TSInterface|TSTypeAlias|TSEnum|TSModule)/.test(parent.type) &&
119
+ (parent.id === node || parent.params?.includes(node)))
120
+ if (binding) {
121
+ shadowed = true
122
+ return
123
+ }
124
+ if (['MemberExpression', 'OptionalMemberExpression'].includes(parent.type)) {
125
+ if (parent.property === node && !parent.computed) return
126
+ if (parent.object === node) {
127
+ used = true
128
+ if (!parent.computed && parent.property.type === 'Identifier')
129
+ names.add(parent.property.name)
130
+ else if (parent.computed && parent.property.type === 'StringLiteral')
131
+ names.add(String(parent.property.value))
132
+ else if (
133
+ parent.computed &&
134
+ parent.property.type === 'TemplateLiteral' &&
135
+ parent.property.expressions.length === 0 &&
136
+ typeof parent.property.quasis[0].value.cooked === 'string'
137
+ )
138
+ names.add(parent.property.quasis[0].value.cooked)
139
+ else opaque = true
140
+ return
141
+ }
142
+ }
143
+ if (parent.type === 'JSXMemberExpression') {
144
+ if (parent.property === node) return
145
+ if (parent.object === node) {
146
+ used = true
147
+ names.add(parent.property.name)
148
+ return
149
+ }
150
+ }
151
+ if (parent.type === 'JSXAttribute' && parent.name === node) return
152
+ used = true
153
+ opaque = true
154
+ })
155
+ if (!used) return undefined
156
+ return shadowed || opaque ? { kind: 'all' } : { kind: 'named', names: [...names].toSorted() }
157
+ }
158
+
159
+ /** Collect literal dynamic imports and require edges, rejecting opaque dynamic paths. */
160
+ export function dynamicRuntimeImports(
161
+ ast: SyntaxNode,
162
+ file: string,
163
+ label = 'ui-tailwind-sources'
164
+ ): Array<{ specifier: string; selection: Selection }> {
165
+ const parents = new WeakMap<SyntaxNode, SyntaxNode>()
166
+ const nodes: SyntaxNode[] = []
167
+ visitSource(ast, (node, parent) => {
168
+ if (parent) parents.set(node, parent)
169
+ if (['ImportExpression', 'CallExpression', 'OptionalCallExpression'].includes(node.type))
170
+ nodes.push(node)
171
+ })
172
+ const result: Array<{ specifier: string; selection: Selection }> = []
173
+ for (const node of nodes) {
174
+ if (node.type === 'ImportExpression') {
175
+ const argument = node.source
176
+ const specifier: string | undefined =
177
+ argument.type === 'StringLiteral'
178
+ ? argument.value
179
+ : argument.type === 'TemplateLiteral' && argument.expressions.length === 0
180
+ ? argument.quasis[0].value.cooked
181
+ : undefined
182
+ if (specifier == null)
183
+ throw new Error(
184
+ `[${label}] non-literal runtime import in ${file}; use a literal import path so shared UI sources remain discoverable`
185
+ )
186
+ let selection: Selection = { kind: 'all' }
187
+ let current = node
188
+ while (parents.has(current)) {
189
+ current = parents.get(current)
190
+ if (
191
+ ['CallExpression', 'OptionalCallExpression'].includes(current.type) &&
192
+ ['MemberExpression', 'OptionalMemberExpression'].includes(current.callee.type) &&
193
+ !current.callee.computed &&
194
+ current.callee.property.name === 'then'
195
+ ) {
196
+ const callback = current.arguments[0]
197
+ const parameter = ['ArrowFunctionExpression', 'FunctionExpression'].includes(
198
+ callback?.type
199
+ )
200
+ ? callback.params[0]
201
+ : undefined
202
+ if (parameter?.type === 'Identifier')
203
+ selection = selectNamespace(callback, parameter.name, parameter) ?? selection
204
+ else if (parameter?.type === 'ObjectPattern') {
205
+ const names: string[] = []
206
+ let valid = true
207
+ for (const property of parameter.properties) {
208
+ if (
209
+ property.type !== 'ObjectProperty' ||
210
+ property.computed ||
211
+ (property.value.type !== 'Identifier' &&
212
+ !(
213
+ property.value.type === 'AssignmentPattern' &&
214
+ property.value.left.type === 'Identifier'
215
+ )) ||
216
+ !['Identifier', 'StringLiteral', 'NumericLiteral'].includes(property.key.type)
217
+ ) {
218
+ valid = false
219
+ break
220
+ }
221
+ names.push(String(property.key.name ?? property.key.value))
222
+ }
223
+ if (valid && names.length) selection = { kind: 'named', names: names.toSorted() }
224
+ }
225
+ break
226
+ }
227
+ if (current.type.endsWith('Statement') || current.type === 'Program') break
228
+ }
229
+ result.push({ specifier, selection })
230
+ } else if (
231
+ node.callee?.type === 'Identifier' &&
232
+ node.callee.name === 'require' &&
233
+ node.arguments[0]?.type === 'StringLiteral'
234
+ ) {
235
+ result.push({
236
+ specifier: node.arguments[0].value,
237
+ selection: { kind: 'all' },
238
+ })
239
+ }
240
+ }
241
+ return result
242
+ }
243
+
244
+ function bindingNames(node: SyntaxNode): string[] {
245
+ if (!node) return []
246
+ if (node.type === 'Identifier') return [node.name]
247
+ if (node.type === 'RestElement') return bindingNames(node.argument)
248
+ if (node.type === 'AssignmentPattern') return bindingNames(node.left)
249
+ if (node.type === 'ArrayPattern') return node.elements.flatMap(bindingNames)
250
+ if (node.type === 'ObjectPattern')
251
+ return node.properties.flatMap((property: SyntaxNode) =>
252
+ bindingNames(property.type === 'RestElement' ? property.argument : property.value)
253
+ )
254
+ return []
255
+ }
256
+
257
+ function declarationNames(declaration: SyntaxNode, includeModule = true): string[] {
258
+ if (!declaration || declaration.declare) return []
259
+ if (declaration.type === 'VariableDeclaration')
260
+ return declaration.declarations.flatMap((item: SyntaxNode) => bindingNames(item.id))
261
+ if (
262
+ [
263
+ 'FunctionDeclaration',
264
+ 'ClassDeclaration',
265
+ 'TSEnumDeclaration',
266
+ ...(includeModule ? ['TSModuleDeclaration'] : []),
267
+ ].includes(declaration.type)
268
+ )
269
+ return declaration.id?.type === 'Identifier' ? [declaration.id.name] : []
270
+ return []
271
+ }
272
+
273
+ /** Runtime declarations exported directly with a modifier or default export. */
274
+ export function directRuntimeExports(ast: SyntaxNode): string[] {
275
+ const names: string[] = []
276
+ for (const statement of ast.program.body) {
277
+ if (statement.type === 'ExportDefaultDeclaration') {
278
+ if (
279
+ ['TSInterfaceDeclaration', 'TSTypeAliasDeclaration'].includes(statement.declaration.type) ||
280
+ statement.declaration.declare
281
+ )
282
+ continue
283
+ names.push(...declarationNames(statement.declaration), 'default')
284
+ } else if (
285
+ statement.type === 'ExportNamedDeclaration' &&
286
+ statement.exportKind !== 'type' &&
287
+ statement.declaration
288
+ ) {
289
+ names.push(...declarationNames(statement.declaration))
290
+ } else if (statement.type === 'TSExportAssignment') names.push('default')
291
+ }
292
+ return names
293
+ }
294
+
295
+ /** Runtime declarations local to this module, excluding type-only declarations. */
296
+ export function hasRuntimeDeclaration(ast: SyntaxNode, name: string): boolean {
297
+ return ast.program.body.some((statement: SyntaxNode) => {
298
+ const declaration = ['ExportDefaultDeclaration', 'ExportNamedDeclaration'].includes(
299
+ statement.type
300
+ )
301
+ ? statement.declaration
302
+ : statement
303
+ return declarationNames(declaration, false).includes(name)
304
+ })
305
+ }
306
+
307
+ /** Runtime re-exports, preserving aliases and type-only exclusions. */
308
+ export function runtimeReexports(ast: SyntaxNode): RuntimeReexport[] {
309
+ const entries: RuntimeReexport[] = []
310
+ for (const statement of ast.program.body) {
311
+ if (statement.exportKind === 'type') continue
312
+ if (statement.type === 'ExportAllDeclaration') {
313
+ entries.push({ kind: 'star', source: statement.source.value })
314
+ } else if (statement.type === 'ExportNamedDeclaration' && !statement.declaration) {
315
+ const first = entries.length
316
+ for (const specifier of statement.specifiers) {
317
+ if (specifier.exportKind === 'type') continue
318
+ const exported = specifier.exported.name ?? specifier.exported.value
319
+ if (specifier.type === 'ExportNamespaceSpecifier') {
320
+ entries.push({
321
+ kind: 'namespace',
322
+ source: statement.source?.value,
323
+ exported,
324
+ })
325
+ } else if (specifier.type === 'ExportSpecifier') {
326
+ entries.push({
327
+ kind: 'named',
328
+ source: statement.source?.value,
329
+ exported,
330
+ imported: specifier.local.name ?? specifier.local.value,
331
+ })
332
+ }
333
+ }
334
+ if (statement.source && entries.length === first)
335
+ entries.push({ kind: 'empty', source: statement.source.value })
336
+ }
337
+ }
338
+ return entries
339
+ }
340
+
341
+ export function defaultExport(
342
+ ast: SyntaxNode
343
+ ): { kind: 'expression' } | { kind: 'identifier'; name: string } | undefined {
344
+ const statement = ast.program.body.find(
345
+ (item: SyntaxNode) =>
346
+ item.type === 'ExportDefaultDeclaration' || item.type === 'TSExportAssignment'
347
+ )
348
+ if (!statement) return undefined
349
+ if (statement.type === 'TSExportAssignment') return { kind: 'expression' }
350
+ if (
351
+ ['TSInterfaceDeclaration', 'TSTypeAliasDeclaration'].includes(statement.declaration.type) ||
352
+ statement.declaration.declare
353
+ )
354
+ return undefined
355
+ return statement.declaration.type === 'Identifier'
356
+ ? { kind: 'identifier', name: statement.declaration.name }
357
+ : { kind: 'expression' }
358
+ }