
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
auto-import-fix
Advanced tools
一个智能的依赖自动导入工具,让你告别手动添加 import 语句的烦恼!
npm install auto-import-fix
const autoImport = require('auto-import-fix');
// 准备需要处理的文件
const files = [
{
filename: '/project/src/components/MyButton.jsx',
content: `
function MyButton() {
return <Button type="primary">Click Me!</Button>;
}
export default MyButton;
`
}
];
// 定义可用的包信息
const availablePackages = {
"antd": {
"Button": {},
"DatePicker": {
"RangePicker": {}
}
}
};
// 运行自动导入
const result = autoImport(files, availablePackages);
console.log(result[0].content);
// 输出:
// import { Button } from 'antd';
//
// function MyButton() {
// return <Button type="primary">Click Me!</Button>;
// }
// export default MyButton;
files Array<FileObject>
必需
FileObject 结构:
{
filename: string; // 文件路径(绝对路径)
content: string; // 文件内容
}
externalAvailablePackages Object
可选
{}
options Object
可选
{}
Options 结构:
{
fixHallucinatedImports?: boolean; // 是否启用幻觉导入修复,默认 false
plugins?: Array<Plugin>; // 插件列表,默认 []
}
返回值 Array<ResultObject>
ResultObject 结构:
{
filename: string; // 原文件路径
content: string; // 处理后的文件内容
updated: boolean; // 是否有更新
}
const files = [{
filename: '/project/src/MyForm.jsx',
content: `
function MyForm() {
const [form] = Form.useForm();
return (
<Form form={form}>
<Input placeholder="请输入" />
<Button type="primary">提交</Button>
</Form>
);
}
`
}];
const packages = {
"antd": {
"Form": {},
"Input": {},
"Button": {}
}
};
const result = autoImport(files, packages);
// 自动添加: import { Form, Input, Button } from 'antd';
const files = [
{
filename: '/project/src/utils/helpers.js',
content: `
export const formatDate = (date) => {
return date.toLocaleDateString();
};
export const validateEmail = (email) => {
return email.includes('@');
};
`
},
{
filename: '/project/src/components/UserForm.jsx',
content: `
function UserForm() {
const isValid = validateEmail('test@example.com');
const today = formatDate(new Date());
return <div>{today}</div>;
}
`
}
];
const result = autoImport(files, {});
// 自动添加: import { formatDate, validateEmail } from '../utils/helpers';
const files = [{
filename: '/project/src/components/UserCard.jsx',
content: `
// 错误的导入路径
import { formatDate } from '@utils/helpers'; // @ 开头的幻觉路径
import { Button } from '@antd/button'; // 错误的包名
import UserService from './wrong-path/user'; // 错误的相对路径
function UserCard() {
const date = formatDate(new Date());
return <Button>{date}</Button>;
}
`
}];
// 启用幻觉导入修复功能
const result = autoImport(files, packages, {
fixHallucinatedImports: true
});
// 修复后的导入语句:
// import { formatDate } from '../utils/helpers';
// import { Button } from 'antd';
// import UserService from '../services/user';
专门用于处理 @ant-design/icons
图标的智能导入和修复插件。
功能特性:
使用方法:
// 引入插件
const AntdIconsPlugin = require('auto-import-fix/dist/plugins/antd-icons-plugin');
// 或者 ES6 模块
// import AntdIconsPlugin from 'auto-import-fix/dist/plugins/antd-icons-plugin.esm.js';
// 创建插件实例
const plugin = new AntdIconsPlugin({
autoFix: true, // 是否自动修复错误的图标名称
threshold: 0.6, // 相似度阈值 (0-1)
fallbackIcon: 'StarOutlined' // 兜底图标
});
// 定义可用的图标
const availableIcons = {
"@ant-design/icons": {
"UserOutlined": {},
"HomeOutlined": {},
"SettingOutlined": {},
"StarOutlined": {},
"UpOutlined": {}
}
};
// 使用插件
const result = autoImport(files, availableIcons, {
plugins: [plugin]
});
配置选项:
选项 | 类型 | 默认值 | 描述 |
---|---|---|---|
autoFix | boolean | true | 是否自动修复错误的图标名称 |
threshold | number | 0.6 | 相似度匹配阈值,范围 0-1 |
fallbackIcon | string | 'StarOutlined' | 找不到相似图标时的兜底图标 |
debug | boolean | false | 是否输出调试信息 |
支持的修复模式:
// 输入
function MyComponent() {
return <UserOutlined />;
}
// 输出
import { UserOutlined } from '@ant-design/icons';
function MyComponent() {
return <UserOutlined />;
}
// 输入(错误的图标名)
function MyComponent() {
return <UserIcon />; // 应该是 UserOutlined
}
// 输出(自动修复)
import { UserOutlined } from '@ant-design/icons';
function MyComponent() {
return <UserOutlined />;
}
// 输入(完全错误的图标名)
function MyComponent() {
return <SomeRandomIcon />;
}
// 输出(使用兜底图标)
import { StarOutlined } from '@ant-design/icons';
function MyComponent() {
return <StarOutlined />;
}
性能特性:
你可以开发自己的插件来扩展功能:
class MyCustomPlugin {
constructor(options = {}) {
this.options = options;
}
process(context) {
// context 包含:
// - filename: 当前文件路径
// - content: 文件内容
// - ast: 抽象语法树
// - newImports: 新的导入语句
// - availablePackages: 可用包信息
// 在这里实现你的逻辑
// 返回更新后的上下文
return {
newImports: updatedImports,
ast: updatedAst
};
}
}
// 使用自定义插件
const myPlugin = new MyCustomPlugin({ option1: 'value1' });
const result = autoImport(files, packages, {
plugins: [myPlugin]
});
const fs = require('fs');
const path = require('path');
// 批量处理整个目录
function processDirectory(dirPath) {
const files = [];
function readDir(dir) {
const items = fs.readdirSync(dir);
items.forEach(item => {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
readDir(fullPath);
} else if (/\.(js|jsx|ts|tsx)$/.test(item)) {
files.push({
filename: fullPath,
content: fs.readFileSync(fullPath, 'utf8')
});
}
});
}
readDir(dirPath);
return autoImport(files, availablePackages);
}
创建 auto-import.config.js
:
module.exports = {
availablePackages: {
"antd": {
"Button": {},
"Input": {},
"Form": {}
},
"@ant-design/icons": {
"UserOutlined": {},
"HomeOutlined": {}
}
},
options: {
fixHallucinatedImports: true,
plugins: [
new (require('auto-import-fix/dist/plugins/antd-icons-plugin'))({
autoFix: true,
threshold: 0.7
})
]
}
};
使用配置文件:
const config = require('./auto-import.config.js');
const result = autoImport(files, config.availablePackages, config.options);
try {
const result = autoImport(files, packages, options);
result.forEach((fileResult, index) => {
if (fileResult.updated) {
console.log(`✅ ${fileResult.filename} 已更新`);
} else {
console.log(`ℹ️ ${fileResult.filename} 无需更新`);
}
});
} catch (error) {
console.error('❌ 处理失败:', error.message);
}
.js
、.jsx
、.ts
、.tsx
文件# 克隆项目
git clone <repository-url>
cd auto-import-fix
# 安装依赖
npm install
# 运行测试
npm test
# 构建项目
npm run build
# 运行特定测试
npm run test:antd-icons
// 启用调试信息
const plugin = new AntdIconsPlugin({ debug: true });
const result = autoImport(files, packages, { plugins: [plugin] });
我们欢迎所有形式的贡献!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)如果你发现了 bug 或有功能建议,请:
MIT License - 详见 LICENSE 文件
感谢所有贡献者和使用者的支持!
让编码更智能,让开发更高效! 🚀
如果这个工具对你有帮助,请给我们一个 ⭐️!
有任何问题或建议,欢迎提交 Issue 或 Pull Request!
FAQs
A utility function npm package
The npm package auto-import-fix receives a total of 46 weekly downloads. As such, auto-import-fix popularity was classified as not popular.
We found that auto-import-fix demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.