当前位置: 首页 > 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.rkmt.cn/news/10752.html

相关文章:

  • SAP BAPI_PR_CREATE 创建采购申请(含自定义字段)
  • NCCL论文阅读
  • 皇牌空战7豪华版DLC补丁
  • BeanUtils中的copyProperties方法使用和分析
  • WoTerm、WindTerm及putty的性能测试对比
  • Python - csv.writer()
  • BM25 关键词检索算法
  • 55.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--实现手机邮箱登录 - 实践
  • 详细介绍:Xilinx系列FPGA实现12G-SDI音视频编解码,支持4K60帧分辨率,提供2套工程源码和技术支持
  • 使用 VMware Workstation 安装 CentOS-7 虚拟机
  • uv Python安装镜像加速
  • AT_arc167_c [ARC167C] MST on Line++
  • CentOS操作系统
  • window系统下使用二进制包安装MySQL数据库
  • 在Vona ORM中实现多数据库/多数据源
  • sql over()函数使用
  • 小柏实战学习Liunx(图文教程三十二)
  • VPX处理板设计原理图:9-基于DSP TMS320C6678+FPGA XC7V690T的6U VPX信号处理卡 C6678板卡, XC7VX690T板卡, VPX处理板
  • VitePress 添加友链界面
  • 洛谷题单指南-进阶数论-P1495 【模板】中国剩余定理(CRT)/ 曹冲养猪
  • 发现5个宝藏文件摆渡系统 2025年企业首选的摆渡方案是这个!
  • BilldDesk:基于Vue3+WebRTC+Nodejs+Electron的开源远程桌面控制 - 详解
  • 查看linux部署网站的TLS版本号
  • 按照DDD的方式写的一个.net有关Web项目框架
  • 【习题答案】《深入理解计算机系统(原书第三版)》
  • 软件体系结构——负载均衡 - 指南
  • Qwen3-Max 2025年完整发布解析:阿里巴巴最强AI模型深度评测
  • css-伪元素清除浮动
  • 在K8S中,Deployment⽀持扩容吗?它与HPA有什么区别?
  • ABC424 游记(VP)