当前位置: 首页 > news >正文

深入解析:find_code 插件 react_vite

深入解析:find_code 插件 react_vite

find_code 插件 react_vite

const fs = require("fs");
const path = require("path");
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generate = require("@babel/generator").default;
// 读取文件内容
const filePath = path.join(__dirname, "index.tsx");
const code = fs.readFileSync(filePath, "utf8");
// 解析代码生成 AST
const ast = parser.parse(code, {
sourceType: "module",
plugins: ["jsx"],
});
// 遍历 AST
// 遍历 AST
traverse(ast, {
JSXOpeningElement(path) {
const line = path.node.loc.start.line;
const pathAttribute = {
type: "JSXAttribute",
name: { type: "JSXIdentifier", name: "path"
},
value: {
type: "StringLiteral",
value: `${filePath
}:${line
}`,
},
};
// 检查是否已经存在 path 属性,如果不存在则添加
const existingPathAttribute = path.node.attributes.find((attr) =>
{
return (
attr.name &&
attr.name.type === "JSXIdentifier" &&
attr.name.name === "path"
);
});
if (!existingPathAttribute) {
path.node.attributes.push(pathAttribute);
}
},
});
// 生成新代码,设置 retainLines 为 true 避免生成不必要的转义序列
const { code: newCode
} = generate(ast, {
retainLines: true,
jsescOption: {
minimal: true,
},
});
// 写入文件
fs.writeFileSync(filePath, newCode, "utf8");
console.log("代码修改完成");
// module
import fs from "fs/promises";
import path from "path";
import parser from "@babel/parser";
import traverse from "@babel/traverse";
import generate from "@babel/generator";
// 定义处理文件的异步函数
async function processFile(filePath) {
try {
// 读取文件内容
const code = await fs.readFile(filePath, "utf8");
// 解析代码生成 AST
const ast = parser.parse(code, {
sourceType: "module",
plugins: ["jsx"],
});
// 遍历 AST
traverse.default(ast, {
JSXOpeningElement(path) {
const line = path?.node?.loc?.start?.line;
const pathAttribute = {
type: "JSXAttribute",
name: { type: "JSXIdentifier", name: "path"
},
value: {
type: "StringLiteral",
value: `${filePath
}:${line
}`,
},
};
// 检查是否已经存在 path 属性,如果不存在则添加
const existingPathAttribute = path.node.attributes.find((attr) =>
{
return (
attr?.name &&
attr?.name.type === "JSXIdentifier" &&
attr?.name.name === "path"
);
});
if (!existingPathAttribute) {
path.node.attributes.push(pathAttribute);
}
},
});
// 生成新代码,设置 retainLines 为 true 避免生成不必要的转义序列
const { code: newCode
} = generate.default(ast, {
retainLines: true,
jsescOption: {
minimal: true,
},
});
// 写入文件
await fs.writeFile(filePath, newCode, "utf8");
console.log("代码修改完成");
} catch (error) {
console.error("处理文件时出错:", error);
}
}
// 获取要处理的文件路径
const filePath = path.join(process.cwd(), "node/index.tsx");
// 调用处理函数
processFile(filePath);

代码修改完成效果

import React from "react";
const Test = () =>
{
return (
<div path="/Users/guojie/跳槽学习文档——速成/vite/node/index.tsx:4">我是根目录<div path="/Users/guojie/跳槽学习文档——速成/vite/node/index.tsx:6">我是子目录</div><span path="/Users/guojie/跳槽学习文档——速成/vite/node/index.tsx:7">我是孙目录</span></div>);};export default Test;
http://www.hskmm.com/?act=detail&tid=15599

相关文章:

  • 二十一、DevOps:从零建设基于K8s的DevOps平台(二)
  • 中电金信:从“通用”到“专用”:加速实现金融行业生成式AI应用的必由之路
  • 自动构建高质量测试集
  • SAP BAPI_PR_CREATE 创建采购申请(含自定义字段)
  • NCCL论文阅读
  • 新手项目经理如何选工具?2025年这5款上手快、不复杂的项目管理软件适合你
  • 用DiskGenius重新分区,检测出U盘虚标容量。
  • 2025低空经济时空信息平台
  • linux gcc attribute
  • 那个…以后拍证件照,可能真不用花钱了
  • CF2147G
  • 全栈开发者效率工具图谱:从IDE到云服务的最优组合 - 指南
  • 皇牌空战7豪华版DLC补丁
  • 基础语法
  • 遥感影像处理利器:PCL Geomatica 2018 功能与安装指南
  • EaseUS Partition Master 13.8 技术员版功能介绍与安装教程
  • 使用 Ansible 批量完成 CentOS 7 操作系统基础配置
  • BeanUtils中的copyProperties方法使用和分析
  • VUE + Nginx + Traefik 项目的发布与反向代理
  • CF *3500
  • CF *3400
  • 深度优先检索:单词搜索
  • WoTerm、WindTerm及putty的性能测试对比
  • CF333E Summer Earnings
  • 一文看懂Playwright MCP如何引爆AI智能体爆发
  • 从nano banana模型到更加真实的3D打印技术
  • 职业卡点怎么破?3个月私教服务助你升级技能与面试技巧
  • OI?原来这么简单-语法算法入门篇
  • 跨境tk避雷proxy-cheap代理服务商!!!
  • Rouyan:使用WPF/C#构建的基于LLM的快捷翻译小工具