diff --git a/gofaster/app/plugins/route-mapping-plugin.js b/gofaster/app/plugins/route-mapping-plugin.js index 24b1e7c..623d659 100644 --- a/gofaster/app/plugins/route-mapping-plugin.js +++ b/gofaster/app/plugins/route-mapping-plugin.js @@ -306,7 +306,7 @@ function routeMappingPlugin() { const apiCalls = self.extractApiCallsFromFunction(path) apiCalls.forEach(apiCall => { apiMappings.push({ - methodName: functionName, + apiMethodName: functionName, method: apiCall.method, path: apiCall.path, line: apiCall.line @@ -324,7 +324,7 @@ function routeMappingPlugin() { const apiCalls = self.extractApiCallsFromFunction(functionPath) apiCalls.forEach(apiCall => { apiMappings.push({ - methodName: functionName, + apiMethodName: functionName, method: apiCall.method, path: apiCall.path, line: apiCall.line @@ -336,12 +336,12 @@ function routeMappingPlugin() { // 查找对象方法定义 ObjectMethod(path) { - const methodName = path.node.key?.name - if (methodName) { + const apiMethodName = path.node.key?.name + if (apiMethodName) { const apiCalls = self.extractApiCallsFromFunction(path) apiCalls.forEach(apiCall => { apiMappings.push({ - methodName: methodName, + apiMethodName: apiMethodName, method: apiCall.method, path: apiCall.path, line: apiCall.line @@ -353,12 +353,12 @@ function routeMappingPlugin() { // 查找对象属性中的函数 ObjectProperty(path) { if (path.node.value && (path.node.value.type === 'ArrowFunctionExpression' || path.node.value.type === 'FunctionExpression')) { - const methodName = path.node.key?.name - if (methodName) { + const apiMethodName = path.node.key?.name + if (apiMethodName) { const apiCalls = self.extractApiCallsFromFunction(path.get('value')) apiCalls.forEach(apiCall => { apiMappings.push({ - methodName: methodName, + apiMethodName: apiMethodName, method: apiCall.method, path: apiCall.path, line: apiCall.line @@ -484,10 +484,10 @@ function routeMappingPlugin() { routes, moduleMapping.module, moduleMapping.serviceName, - apiMapping.methodName + apiMapping.apiMethodName ) - console.log(`🔍 API方法 ${apiMapping.methodName} 的触发器数量:`, triggerSources.length) + console.log(`🔍 API方法 ${apiMapping.apiMethodName} 的触发器数量:`, triggerSources.length) return { ...apiMapping, @@ -506,10 +506,10 @@ function routeMappingPlugin() { }, // 查找API方法的触发器源 - 使用简化的方法 - findTriggerSourcesForApiMethod(routes, moduleName, serviceName, methodName) { + findTriggerSourcesForApiMethod(routes, moduleName, serviceName, apiMethodName) { const triggerSources = [] - console.log(`🔍 查找API方法 ${serviceName}.${methodName} 的触发器源,模块: ${moduleName}`) + console.log(`🔍 查找API方法 ${serviceName}.${apiMethodName} 的触发器源,模块: ${moduleName}`) // 1. 遍历所有路由,查找调用该API方法的页面组件 routes.forEach(route => { @@ -520,7 +520,7 @@ function routeMappingPlugin() { route.path, moduleName, serviceName, - methodName + apiMethodName ) if (triggerAnalysis && triggerAnalysis.triggerSources.length > 0) { @@ -541,7 +541,7 @@ function routeMappingPlugin() { null, // components目录中的组件没有路由路径 moduleName, serviceName, - methodName + apiMethodName ) if (triggerAnalysis && triggerAnalysis.triggerSources.length > 0) { @@ -562,16 +562,16 @@ function routeMappingPlugin() { } }) - console.log(`🔍 API方法 ${serviceName}.${methodName} 最终触发器数量:`, uniqueTriggerSources.length) + console.log(`🔍 API方法 ${serviceName}.${apiMethodName} 最终触发器数量:`, uniqueTriggerSources.length) return uniqueTriggerSources }, // 简化的组件触发器源分析方法 - analyzeComponentForTriggerSourcesSimple(componentName, componentPath, moduleName, serviceName, methodName) { + analyzeComponentForTriggerSourcesSimple(componentName, componentPath, moduleName, serviceName, apiMethodName) { try { let filePath = componentPath - console.log(`🔍 简化分析组件 ${componentName},查找服务 ${serviceName}.${methodName}`) + console.log(`🔍 简化分析组件 ${componentName},查找服务 ${serviceName}.${apiMethodName}`) // 如果是组件名,需要找到对应的文件 if (!filePath || !existsSync(filePath)) { @@ -586,8 +586,8 @@ function routeMappingPlugin() { console.log(`🔍 找到组件文件: ${filePath}`) const content = readFileSync(filePath, 'utf-8') - // 使用简化的文本搜索方法 - return this.analyzeComponentWithTextSearch(content, componentName, serviceName, methodName) + // 使用AST分析方法 + return this.analyzeComponentWithAST(content, componentName, serviceName, apiMethodName, filePath) } catch (error) { console.warn(`⚠️ 简化分析组件触发器源失败: ${componentName}`, error.message) @@ -595,11 +595,47 @@ function routeMappingPlugin() { } }, - // 使用文本搜索分析组件 - analyzeComponentWithTextSearch(content, componentName, serviceName, methodName) { + // 生成唯一的按钮名称 + generateUniqueButtonName(componentName, clickHandler) { + // 基于组件名生成简洁的名称 + const baseName = componentName.toLowerCase() + // 添加随机尾缀确保唯一性 + const randomSuffix = Math.random().toString(36).substring(2, 8) + return `${baseName}-${randomSuffix}` + }, + + // 为按钮添加name属性 + addNameAttributeToButton(filePath, buttonHtml, generatedName) { + try { + const fs = require('fs') + const content = fs.readFileSync(filePath, 'utf-8') + + // 检查按钮是否已经有name属性 + if (buttonHtml.includes('name=')) { + console.log(`⚠️ 按钮已有name属性,跳过: ${generatedName}`) + return + } + + // 在button标签中添加name属性 + const updatedButtonHtml = buttonHtml.replace(/]*)>/, ``) + + // 替换文件中的按钮HTML + const updatedContent = content.replace(buttonHtml, updatedButtonHtml) + + // 写回文件 + fs.writeFileSync(filePath, updatedContent, 'utf-8') + + console.log(`✅ 已为按钮添加name属性: ${generatedName}`) + } catch (error) { + console.warn(`⚠️ 添加name属性失败: ${error.message}`) + } + }, + + // 使用AST分析组件 + analyzeComponentWithAST(content, componentName, serviceName, apiMethodName, filePath = null) { const triggerSources = [] - console.log(`🔍 文本搜索组件 ${componentName},查找 ${serviceName}.${methodName}`) + console.log(`🔍 AST分析组件 ${componentName},查找 ${serviceName}.${apiMethodName}`) // 1. 检查组件的authType属性,如果是public则跳过 // 支持两种格式: