|
| 1 | +import { types as BabelTypes, PluginObj, NodePath } from "@babel/core"; |
| 2 | + |
| 3 | +interface PluginArgs { |
| 4 | + types: typeof BabelTypes; |
| 5 | +} |
| 6 | + |
| 7 | +export interface PluginOptions { |
| 8 | + enabled?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Simple "best effort" to get the base name of a file path. Not fool proof but |
| 13 | + * works in browsers and servers. Good enough for our purposes. |
| 14 | + */ |
| 15 | +function basename(filename: string | undefined): string | undefined { |
| 16 | + return filename?.split(/[\\/]/).pop(); |
| 17 | +} |
| 18 | + |
| 19 | +function isSignalCall(path: NodePath<BabelTypes.CallExpression>): boolean { |
| 20 | + const callee = path.get("callee"); |
| 21 | + |
| 22 | + // Check direct function calls like signal(), computed(), useSignal(), useComputed() |
| 23 | + if (callee.isIdentifier()) { |
| 24 | + const name = callee.node.name; |
| 25 | + return ( |
| 26 | + name === "signal" || |
| 27 | + name === "computed" || |
| 28 | + name === "useSignal" || |
| 29 | + name === "useComputed" |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + return false; |
| 34 | +} |
| 35 | + |
| 36 | +function getVariableNameFromDeclarator( |
| 37 | + path: NodePath<BabelTypes.CallExpression> |
| 38 | +): string | null { |
| 39 | + // Walk up the AST to find a variable declarator |
| 40 | + let currentPath: NodePath | null = path; |
| 41 | + while (currentPath) { |
| 42 | + if ( |
| 43 | + currentPath.isVariableDeclarator() && |
| 44 | + currentPath.node.id.type === "Identifier" |
| 45 | + ) { |
| 46 | + return currentPath.node.id.name; |
| 47 | + } |
| 48 | + currentPath = currentPath.parentPath; |
| 49 | + } |
| 50 | + return null; |
| 51 | +} |
| 52 | + |
| 53 | +function hasNameInOptions( |
| 54 | + t: typeof BabelTypes, |
| 55 | + args: NodePath< |
| 56 | + | BabelTypes.Expression |
| 57 | + | BabelTypes.SpreadElement |
| 58 | + | BabelTypes.JSXNamespacedName |
| 59 | + | BabelTypes.ArgumentPlaceholder |
| 60 | + >[] |
| 61 | +): boolean { |
| 62 | + // Check if there's a second argument with a name property |
| 63 | + if (args.length >= 2) { |
| 64 | + const optionsArg = args[1]; |
| 65 | + if (optionsArg.isObjectExpression()) { |
| 66 | + return optionsArg.node.properties.some(prop => { |
| 67 | + if (t.isObjectProperty(prop) && !prop.computed) { |
| 68 | + if (t.isIdentifier(prop.key, { name: "name" })) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + if (t.isStringLiteral(prop.key) && prop.key.value === "name") { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + return false; |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + return false; |
| 80 | +} |
| 81 | + |
| 82 | +function injectSignalName( |
| 83 | + t: typeof BabelTypes, |
| 84 | + path: NodePath<BabelTypes.CallExpression>, |
| 85 | + variableName: string, |
| 86 | + filename: string | undefined |
| 87 | +): void { |
| 88 | + const args = path.get("arguments"); |
| 89 | + |
| 90 | + // Create enhanced name with filename and line number |
| 91 | + let nameValue = variableName; |
| 92 | + if (filename) { |
| 93 | + const baseName = basename(filename); |
| 94 | + const lineNumber = path.node.loc?.start.line; |
| 95 | + if (baseName && lineNumber) { |
| 96 | + nameValue = `${variableName} (${baseName}:${lineNumber})`; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const name = t.stringLiteral(nameValue); |
| 101 | + |
| 102 | + if (args.length === 0) { |
| 103 | + // No arguments, add both value and options |
| 104 | + const nameOption = t.objectExpression([ |
| 105 | + t.objectProperty(t.identifier("name"), name), |
| 106 | + ]); |
| 107 | + path.node.arguments.push(t.identifier("undefined"), nameOption); |
| 108 | + } else if (args.length === 1) { |
| 109 | + // One argument (value), add options object |
| 110 | + const nameOption = t.objectExpression([ |
| 111 | + t.objectProperty(t.identifier("name"), name), |
| 112 | + ]); |
| 113 | + path.node.arguments.push(nameOption); |
| 114 | + } else if (args.length >= 2) { |
| 115 | + // Two or more arguments, modify existing options object |
| 116 | + const optionsArg = args[1]; |
| 117 | + if (optionsArg.isObjectExpression()) { |
| 118 | + // Add name property to existing options object |
| 119 | + optionsArg.node.properties.push( |
| 120 | + t.objectProperty(t.identifier("name"), name) |
| 121 | + ); |
| 122 | + } else { |
| 123 | + // Replace second argument with options object containing name |
| 124 | + const nameOption = t.objectExpression([ |
| 125 | + t.objectProperty(t.identifier("name"), name), |
| 126 | + ]); |
| 127 | + args[1].replaceWith(nameOption); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export default function signalsTransform( |
| 133 | + { types: t }: PluginArgs, |
| 134 | + options: PluginOptions |
| 135 | +): PluginObj { |
| 136 | + const isEnabled = options.enabled !== false; |
| 137 | + return { |
| 138 | + name: "@preact/signals-transform", |
| 139 | + visitor: { |
| 140 | + CallExpression(path, state) { |
| 141 | + // Handle signal naming |
| 142 | + if (isEnabled && isSignalCall(path)) { |
| 143 | + const args = path.get("arguments"); |
| 144 | + |
| 145 | + // Only inject name if it doesn't already have one |
| 146 | + if (!hasNameInOptions(t, args)) { |
| 147 | + const variableName = getVariableNameFromDeclarator(path); |
| 148 | + if (variableName) { |
| 149 | + injectSignalName(t, path, variableName, this.filename); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + }, |
| 154 | + }, |
| 155 | + }; |
| 156 | +} |
0 commit comments