|
| 1 | +import { checkIfComponent } from '@/modules/component-detector' |
| 2 | +import generateImport from '@babel/generator' |
| 3 | +import { parse } from '@babel/parser' |
| 4 | +import traverseImport, { NodePath, Visitor } from '@babel/traverse' |
| 5 | +import * as t from '@babel/types' |
| 6 | + |
| 7 | +// Handle ESM/CJS interop |
| 8 | +const traverse = |
| 9 | + (traverseImport as unknown as { default?: typeof traverseImport }).default ?? traverseImport |
| 10 | +const generate = |
| 11 | + (generateImport as unknown as { default?: typeof generateImport }).default ?? generateImport |
| 12 | + |
| 13 | +export type TransformResult = { |
| 14 | + code: string |
| 15 | + /** Maps original local name → prop key (e.g. "localName" → "propKey") */ |
| 16 | + propMappings: Map<string, string> |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Transforms destructured component props into `props.X` member expressions |
| 21 | + * for linting purposes. This is a minimal transformation — no mergeProps/splitProps/imports |
| 22 | + * are added, only the patterns the eslint-plugin-solid reactivity rule needs to see. |
| 23 | + */ |
| 24 | +export function transformForLinting(code: string): TransformResult | null { |
| 25 | + // Quick check for destructuring pattern |
| 26 | + if (!/\(\s*\{/.test(code)) { |
| 27 | + return null |
| 28 | + } |
| 29 | + |
| 30 | + const propMappings = new Map<string, string>() |
| 31 | + |
| 32 | + let transformed = false |
| 33 | + |
| 34 | + try { |
| 35 | + const ast = parse(code, { |
| 36 | + sourceType: 'module', |
| 37 | + plugins: ['typescript', 'jsx'] |
| 38 | + }) |
| 39 | + |
| 40 | + const astNode = ast as unknown as t.Node |
| 41 | + traverse(astNode, { |
| 42 | + Function(path: NodePath<t.Function>) { |
| 43 | + const params = path.node.params |
| 44 | + if (params.length !== 1) return |
| 45 | + |
| 46 | + const firstParam = params[0] |
| 47 | + if (!t.isObjectPattern(firstParam)) return |
| 48 | + |
| 49 | + if (!checkIfComponent(path)) return |
| 50 | + |
| 51 | + transformDestructuredProps(path, firstParam, propMappings) |
| 52 | + transformed = true |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 57 | + if (!transformed) { |
| 58 | + return null |
| 59 | + } |
| 60 | + |
| 61 | + const output = generate(astNode, { |
| 62 | + retainLines: true, |
| 63 | + compact: false |
| 64 | + }) |
| 65 | + |
| 66 | + return { code: output.code, propMappings } |
| 67 | + } catch (error) { |
| 68 | + console.warn('Failed to transform:', error) |
| 69 | + return null |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function transformDestructuredProps( |
| 74 | + path: NodePath<t.Function>, |
| 75 | + objectPattern: t.ObjectPattern, |
| 76 | + propMappings: Map<string, string> |
| 77 | +) { |
| 78 | + const propsIdentifier = t.identifier('props') |
| 79 | + |
| 80 | + // Preserve TypeAnnotation from the destructured param |
| 81 | + if (objectPattern.typeAnnotation) { |
| 82 | + propsIdentifier.typeAnnotation = objectPattern.typeAnnotation |
| 83 | + } |
| 84 | + |
| 85 | + // Extract prop names and build mappings |
| 86 | + const localNames: string[] = [] |
| 87 | + const localToKey = new Map<string, string>() |
| 88 | + const nestedPropPaths = new Map<string, string[]>() |
| 89 | + |
| 90 | + function processObjectPattern(pattern: t.ObjectPattern, parentPath: string[] = []) { |
| 91 | + for (const prop of pattern.properties) { |
| 92 | + if (t.isRestElement(prop)) { |
| 93 | + // Rest elements are not reactive — leave as-is |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if (!t.isObjectProperty(prop)) continue |
| 98 | + |
| 99 | + let key: string | null = null |
| 100 | + if (t.isIdentifier(prop.key)) { |
| 101 | + key = prop.key.name |
| 102 | + } else if (t.isStringLiteral(prop.key)) { |
| 103 | + key = prop.key.value |
| 104 | + } |
| 105 | + if (!key) continue |
| 106 | + |
| 107 | + const currentPath = [...parentPath, key] |
| 108 | + |
| 109 | + // Handle nested object patterns |
| 110 | + if (t.isObjectPattern(prop.value)) { |
| 111 | + processObjectPattern(prop.value, currentPath) |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + // Extract local name |
| 116 | + let localName: string | null = null |
| 117 | + if (t.isIdentifier(prop.value)) { |
| 118 | + localName = prop.value.name |
| 119 | + } else if (t.isAssignmentPattern(prop.value) && t.isIdentifier(prop.value.left)) { |
| 120 | + localName = prop.value.left.name |
| 121 | + } |
| 122 | + |
| 123 | + if (!localName) continue |
| 124 | + |
| 125 | + if (parentPath.length === 0) { |
| 126 | + localNames.push(localName) |
| 127 | + localToKey.set(localName, key) |
| 128 | + propMappings.set(localName, key) |
| 129 | + } else { |
| 130 | + nestedPropPaths.set(localName, currentPath) |
| 131 | + propMappings.set(localName, currentPath.join('.')) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + processObjectPattern(objectPattern) |
| 137 | + |
| 138 | + // Replace parameter with `props` identifier |
| 139 | + path.node.params[0] = propsIdentifier |
| 140 | + |
| 141 | + // Replace all references to destructured prop names with props.X |
| 142 | + const bodyPath = path.get('body') |
| 143 | + if (Array.isArray(bodyPath)) return |
| 144 | + |
| 145 | + const visitor: Visitor = { |
| 146 | + Identifier(identPath) { |
| 147 | + const parent = identPath.parent |
| 148 | + // Skip property keys and computed member expression properties |
| 149 | + if ( |
| 150 | + (t.isMemberExpression(parent) && parent.property === identPath.node && !parent.computed) || |
| 151 | + (t.isObjectProperty(parent) && parent.key === identPath.node && !parent.computed) |
| 152 | + ) { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + // Skip binding positions (declarations) |
| 157 | + if (identPath.isBindingIdentifier()) return |
| 158 | + |
| 159 | + const idPath = identPath as NodePath<t.Identifier> |
| 160 | + const name = idPath.node.name |
| 161 | + |
| 162 | + // Handle nested property paths |
| 163 | + const propPath = nestedPropPaths.get(name) |
| 164 | + if (propPath) { |
| 165 | + let memberExpr: t.MemberExpression = t.memberExpression( |
| 166 | + t.identifier('props'), |
| 167 | + t.identifier(propPath[0]) |
| 168 | + ) |
| 169 | + for (let i = 1; i < propPath.length; i++) { |
| 170 | + memberExpr = t.memberExpression(memberExpr, t.identifier(propPath[i])) |
| 171 | + } |
| 172 | + idPath.replaceWith(memberExpr) |
| 173 | + } else if (localNames.includes(name)) { |
| 174 | + const propKey = localToKey.get(name) ?? name |
| 175 | + idPath.replaceWith(t.memberExpression(t.identifier('props'), t.identifier(propKey))) |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + bodyPath.traverse(visitor) |
| 180 | +} |
0 commit comments