|
| 1 | +const path = require("path"); |
| 2 | +const crypto = require("crypto"); |
| 3 | +const { isInsideReactComponent } = require("./react-helper.cjs"); |
| 4 | + |
| 5 | +/** |
| 6 | + * Babel plugin that automatically injects the file path and other metadata |
| 7 | + * to registerPreview() calls imported from "rozenite-preview" |
| 8 | + */ |
| 9 | +const ROZENITE_PREVIEW_MODULE = "rozenite-preview"; |
| 10 | +const TARGET_FUNCTION = "registerPreview"; |
| 11 | +const EXPECTED_ARGS_COUNT = 2; |
| 12 | + |
| 13 | +const cache = {}; |
| 14 | + |
| 15 | +function getOrCreateId(file, line) { |
| 16 | + const key = `${file}:${line}`; |
| 17 | + if (cache[key]) return cache[key]; |
| 18 | + |
| 19 | + const hash = crypto.createHash("md5").update(key).digest("hex").slice(0, 6); |
| 20 | + const id = `${path |
| 21 | + .basename(file, path.extname(file)) |
| 22 | + .toLowerCase()}_${line}_${hash}`; |
| 23 | + cache[key] = id; |
| 24 | + return id; |
| 25 | +} |
| 26 | + |
| 27 | +module.exports = function ({ types: t }) { |
| 28 | + return { |
| 29 | + visitor: { |
| 30 | + Program(path, state) { |
| 31 | + const rozenitePreviewImports = collectRozenitePreviewImports(path, t); |
| 32 | + |
| 33 | + if (rozenitePreviewImports.size === 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + injectFilePathIntoRegisterPreviewCalls( |
| 38 | + path, |
| 39 | + state, |
| 40 | + rozenitePreviewImports, |
| 41 | + t |
| 42 | + ); |
| 43 | + }, |
| 44 | + }, |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * Collects all identifiers imported from "rozenite-preview" |
| 50 | + * @param {Object} programPath - The program AST path |
| 51 | + * @param {Object} t - Babel types helper |
| 52 | + * @returns {Set} Set of imported identifier names |
| 53 | + */ |
| 54 | +function collectRozenitePreviewImports(programPath, t) { |
| 55 | + const imports = new Set(); |
| 56 | + |
| 57 | + programPath.get("body").forEach((statement) => { |
| 58 | + if (!isRozenitePreviewImportDeclaration(statement)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + statement.node.specifiers.forEach((specifier) => { |
| 63 | + if (isValidImportSpecifier(specifier, t)) { |
| 64 | + imports.add(specifier.local.name); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + return imports; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Checks if a statement is an import from "rozenite-preview" |
| 74 | + * @param {Object} statement - AST statement node |
| 75 | + * @returns {boolean} |
| 76 | + */ |
| 77 | +function isRozenitePreviewImportDeclaration(statement) { |
| 78 | + return ( |
| 79 | + statement.isImportDeclaration() && |
| 80 | + statement.node.source.value === ROZENITE_PREVIEW_MODULE |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Checks if a specifier is a valid import specifier (named or default) |
| 86 | + * @param {Object} specifier - Import specifier node |
| 87 | + * @param {Object} t - Babel types helper |
| 88 | + * @returns {boolean} |
| 89 | + */ |
| 90 | +function isValidImportSpecifier(specifier, t) { |
| 91 | + return ( |
| 92 | + t.isImportSpecifier(specifier) || t.isImportDefaultSpecifier(specifier) |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Traverses the AST and injects file paths into registerPreview calls |
| 98 | + * @param {Object} programPath - The program AST path |
| 99 | + * @param {Object} state - Babel plugin state |
| 100 | + * @param {Set} rozenitePreviewImports - Set of imported identifiers from rozenite-preview |
| 101 | + * @param {Object} t - Babel types helper |
| 102 | + */ |
| 103 | +function injectFilePathIntoRegisterPreviewCalls( |
| 104 | + programPath, |
| 105 | + state, |
| 106 | + rozenitePreviewImports, |
| 107 | + t |
| 108 | +) { |
| 109 | + const filename = state.file.opts.filename || ""; |
| 110 | + const relativeFilename = path.relative(process.cwd(), filename); |
| 111 | + |
| 112 | + programPath.traverse({ |
| 113 | + CallExpression(callPath) { |
| 114 | + if (isTargetRegisterPreviewCall(callPath, rozenitePreviewImports)) { |
| 115 | + const metadata = getMetadata(callPath); |
| 116 | + const filePath = t.stringLiteral(filename); |
| 117 | + const id = getOrCreateId(relativeFilename, metadata.line); |
| 118 | + const argument = t.objectExpression([ |
| 119 | + t.objectProperty(t.identifier("id"), t.stringLiteral(id)), |
| 120 | + t.objectProperty(t.identifier("filePath"), filePath), |
| 121 | + t.objectProperty( |
| 122 | + t.identifier("relativeFilename"), |
| 123 | + t.stringLiteral(relativeFilename) |
| 124 | + ), |
| 125 | + t.objectProperty( |
| 126 | + t.identifier("isInsideReactComponent"), |
| 127 | + t.booleanLiteral(isInsideReactComponent(callPath)) |
| 128 | + ), |
| 129 | + t.objectProperty( |
| 130 | + t.identifier("name"), |
| 131 | + t.stringLiteral(metadata.name) |
| 132 | + ), |
| 133 | + t.objectProperty( |
| 134 | + t.identifier("nameType"), |
| 135 | + t.stringLiteral(metadata.nameType) |
| 136 | + ), |
| 137 | + t.objectProperty( |
| 138 | + t.identifier("callId"), |
| 139 | + t.stringLiteral(metadata.callId) |
| 140 | + ), |
| 141 | + t.objectProperty( |
| 142 | + t.identifier("componentType"), |
| 143 | + t.stringLiteral(metadata.componentType) |
| 144 | + ), |
| 145 | + t.objectProperty( |
| 146 | + t.identifier("line"), |
| 147 | + t.numericLiteral(metadata.line) |
| 148 | + ), |
| 149 | + t.objectProperty( |
| 150 | + t.identifier("column"), |
| 151 | + t.numericLiteral(metadata.column) |
| 152 | + ), |
| 153 | + ]); |
| 154 | + callPath.node.arguments.push(argument); |
| 155 | + } |
| 156 | + }, |
| 157 | + }); |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Determines the component type from the second argument |
| 162 | + * @param {Object} componentArg - Component argument AST node |
| 163 | + * @returns {string} Component type description |
| 164 | + */ |
| 165 | +function getComponentType(componentArg) { |
| 166 | + if (!componentArg) return "unknown"; |
| 167 | + |
| 168 | + switch (componentArg.type) { |
| 169 | + case "Identifier": |
| 170 | + return `component:${componentArg.name}`; |
| 171 | + case "ArrowFunctionExpression": |
| 172 | + case "FunctionExpression": |
| 173 | + return "inline-function"; |
| 174 | + case "CallExpression": |
| 175 | + return "call-expression"; |
| 176 | + default: |
| 177 | + return componentArg.type.toLowerCase(); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * Extracts detailed information from a registerPreview call |
| 183 | + * @param {Object} callPath - The call expression AST path |
| 184 | + * @returns {Object} Preview details including name, location, and arguments |
| 185 | + */ |
| 186 | +function getMetadata(callPath) { |
| 187 | + const firstArg = callPath.node.arguments[0]; |
| 188 | + const secondArg = callPath.node.arguments[1]; |
| 189 | + const loc = callPath.node.loc; |
| 190 | + |
| 191 | + let name = null; |
| 192 | + let nameType = "unknown"; |
| 193 | + |
| 194 | + if (firstArg && firstArg.type === "StringLiteral") { |
| 195 | + name = firstArg.value; |
| 196 | + nameType = "literal"; |
| 197 | + } else if (firstArg && firstArg.type === "Identifier") { |
| 198 | + name = `<dynamic:${firstArg.name}>`; |
| 199 | + nameType = "identifier"; |
| 200 | + } |
| 201 | + |
| 202 | + // Create a unique identifier for this specific call |
| 203 | + const callId = `${name}:${loc?.start?.line || 0}:${loc?.start?.column || 0}`; |
| 204 | + |
| 205 | + return { |
| 206 | + name, |
| 207 | + nameType, |
| 208 | + callId, |
| 209 | + line: loc?.start?.line || 0, |
| 210 | + column: loc?.start?.column || 0, |
| 211 | + componentType: getComponentType(secondArg), |
| 212 | + }; |
| 213 | +} |
| 214 | + |
| 215 | +/** |
| 216 | + * Determines if a call expression is a registerPreview call that needs modification |
| 217 | + * @param {Object} callPath - The call expression AST path |
| 218 | + * @param {Set} rozenitePreviewImports - Set of imported identifiers from rozenite-preview |
| 219 | + * @returns {boolean} |
| 220 | + */ |
| 221 | +function isTargetRegisterPreviewCall(callPath, rozenitePreviewImports) { |
| 222 | + const callee = callPath.get("callee"); |
| 223 | + |
| 224 | + return ( |
| 225 | + callee.isIdentifier() && |
| 226 | + callee.node.name === TARGET_FUNCTION && |
| 227 | + rozenitePreviewImports.has(callee.node.name) && |
| 228 | + callPath.node.arguments.length === EXPECTED_ARGS_COUNT |
| 229 | + ); |
| 230 | +} |
0 commit comments