|
| 1 | +import { readFile } from 'node:fs/promises' |
| 2 | +import { createRequire } from 'node:module' |
| 3 | +import { glob } from 'tinyglobby' |
| 4 | +import { parse } from 'vue-eslint-parser' |
| 5 | + |
| 6 | +const require = createRequire(import.meta.url) |
| 7 | + |
| 8 | +function walk (node: any, callback: (node: any, parent: any) => void, parent?: any) { |
| 9 | + if (!node || typeof node !== 'object') { |
| 10 | + return |
| 11 | + } |
| 12 | + |
| 13 | + callback(node, parent) |
| 14 | + |
| 15 | + for (const key in node) { |
| 16 | + if (key === 'parent' || key === 'loc' || key === 'range' || key === 'tokens' || key === 'comments') { |
| 17 | + continue |
| 18 | + } |
| 19 | + const child = node[key] |
| 20 | + if (Array.isArray(child)) { |
| 21 | + for (const item of child) { |
| 22 | + walk(item, callback, node) |
| 23 | + } |
| 24 | + } else { |
| 25 | + walk(child, callback, node) |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export function analyzeCode (code: string, targetPackage = '@vuetify/v0') { |
| 31 | + const ast = parse(code, { |
| 32 | + sourceType: 'module', |
| 33 | + ecmaVersion: 2022, |
| 34 | + parser: require.resolve('@typescript-eslint/parser'), |
| 35 | + }) |
| 36 | + |
| 37 | + const found = new Set<string>() |
| 38 | + const importedFromVuetify = new Set<string>() |
| 39 | + |
| 40 | + if (ast.body) { |
| 41 | + walk(ast, (node, parent) => { |
| 42 | + // Static imports: import { X } from 'pkg' |
| 43 | + if (node.type === 'ImportDeclaration' && typeof node.source.value === 'string' && (node.source.value === targetPackage || node.source.value.startsWith(`${targetPackage}/`))) { |
| 44 | + for (const spec of node.specifiers) { |
| 45 | + if (spec.type === 'ImportSpecifier' && 'name' in spec.imported) { |
| 46 | + found.add(spec.imported.name) |
| 47 | + importedFromVuetify.add(spec.local.name) |
| 48 | + } else if (spec.type === 'ImportDefaultSpecifier') { |
| 49 | + found.add('default') |
| 50 | + importedFromVuetify.add(spec.local.name) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Dynamic imports: import('pkg').then(...) or import('pkg')['prop'] |
| 56 | + // Node structure for import('pkg'): { type: 'ImportExpression', source: { value: 'pkg' } } |
| 57 | + if (node.type === 'ImportExpression' && node.source.value === targetPackage // Case 1: import('pkg')['Prop'] or import('pkg').Prop |
| 58 | + && parent?.type === 'MemberExpression' && parent.object === node) { |
| 59 | + if (parent.property.type === 'Identifier' && !parent.computed) { |
| 60 | + // .Prop |
| 61 | + found.add(parent.property.name) |
| 62 | + } else if (parent.property.type === 'Literal') { |
| 63 | + // ['Prop'] |
| 64 | + found.add(parent.property.value) |
| 65 | + } |
| 66 | + } |
| 67 | + // Case 2: (await import('pkg')).Prop |
| 68 | + // AwaitExpression -> MemberExpression |
| 69 | + // parent is AwaitExpression |
| 70 | + // parent.parent is MemberExpression |
| 71 | + // We can't easily access parent.parent with simple walk unless we pass it down or track path. |
| 72 | + // But our walk function passes `parent`. |
| 73 | + // So we need to check if we are inside an AwaitExpression, and that AwaitExpression is part of MemberExpression. |
| 74 | + // This direction (upwards) is hard if we only have one level of parent. |
| 75 | + // Instead, let's catch MemberExpression and check if object is AwaitExpression -> ImportExpression |
| 76 | + |
| 77 | + // Handle (await import('pkg')).Prop |
| 78 | + if (node.type === 'MemberExpression' // Check if object is AwaitExpression |
| 79 | + && node.object.type === 'AwaitExpression' && node.object.argument.type === 'ImportExpression') { |
| 80 | + const source = node.object.argument.source.value |
| 81 | + if (source === targetPackage) { |
| 82 | + if (node.property.type === 'Identifier' && !node.computed) { |
| 83 | + found.add(node.property.name) |
| 84 | + } else if (node.property.type === 'Literal') { |
| 85 | + found.add(node.property.value) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + return Array.from(found) |
| 93 | +} |
| 94 | + |
| 95 | +export async function analyzeProject (cwd: string = process.cwd(), targetPackage = '@vuetify/v0') { |
| 96 | + const files = await glob(['**/*.{vue,ts,js,tsx,jsx}'], { |
| 97 | + cwd, |
| 98 | + ignore: ['**/node_modules/**', '**/dist/**', '**/.git/**'], |
| 99 | + absolute: true, |
| 100 | + }) |
| 101 | + |
| 102 | + const features = new Set<string>() |
| 103 | + |
| 104 | + for (const file of files) { |
| 105 | + try { |
| 106 | + const code = await readFile(file, 'utf8') |
| 107 | + const fileFeatures = analyzeCode(code, targetPackage) |
| 108 | + for (const feature of fileFeatures) { |
| 109 | + features.add(feature) |
| 110 | + } |
| 111 | + } catch { |
| 112 | + // console.warn(`Failed to analyze ${file}:`, error) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return Array.from(features).toSorted() |
| 117 | +} |
0 commit comments