脚本如何规范调整代码导入顺序

wen 实用脚本 31

本文目录导读:

脚本如何规范调整代码导入顺序

  1. 通用导入顺序规范
  2. 常见语言的导入规范
  3. 自动化工具
  4. 编辑器和IDE设置
  5. 最佳实践
  6. 特殊情况处理
  7. 检查清单

通用导入顺序规范

标准分组顺序

# 1. Python标准库
import os
import sys
from datetime import datetime
# 2. 第三方库
import numpy as np
import pandas as pd
from flask import Flask
# 3. 本地应用/库
from myapp.models import User
from myapp.utils import helper
from .config import settings

分组内排序规则

  • 每个分组内按字母顺序排序
  • import X 排在 from X import Y 之前
  • 相对导入排在绝对导入之后

常见语言的导入规范

Python (PEP 8)

# 标准库
import json
import os
import sys
# 第三方库
import requests
from django.shortcuts import render
# 本地模块
from .models import Product
from .views import product_list

JavaScript/TypeScript

// 1. 第三方库
import React from 'react';
import { useState } from 'react';
import axios from 'axios';
// 2. 内部模块
import { User } from './models/user';
import { helper } from './utils/helper';
// 3. 样式文件
import './styles/main.css';
import './App.css';

Java

// 1. 静态导入
import static java.lang.Math.PI;
// 2. Java标准库
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
// 3. 第三方库
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
// 4. 本地项目
import com.mycompany.model.User;

自动化工具

Python工具

isort - 自动排序导入

# 安装
pip install isort
# 使用命令行
isort your_file.py
# 配置文件 .isort.cfg
[settings]
profile = black
line_length = 88
known_standard_library = os,sys,json
known_third_party = django,flask,requests
known_first_party = myapp

VS Code 扩展

// settings.json
{
  "python.sortImports.args": ["--profile", "black"],
  "[python]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    }
  }
}

JavaScript工具

ESLint + import插件

// .eslintrc.json
{
  "plugins": ["import"],
  "rules": {
    "import/order": ["error", {
      "groups": [
        "builtin",
        "external",
        "internal",
        "parent",
        "sibling",
        "index"
      ],
      "newlines-between": "always",
      "alphabetize": { "order": "asc" }
    }]
  }
}

编辑器和IDE设置

VS Code

// settings.json
{
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  },
  "editor.formatOnSave": true
}

PyCharm

  • Settings → Editor → Code Style → Python → Imports
  • 启用 "Optimize imports on the fly"
  • 设置导入顺序规则

最佳实践

项目级规范配置

# .style.yapf (Python)
[style]
based_on_style = pep8
import_order = standard,third_party,first_party
# .editorconfig
[*.py]
indent_style = space
indent_size = 4
charset = utf-8
end_of_line = lf
insert_final_newline = true

Hook配置(Git hooks)

// package.json
{
  "husky": {
    "hooks": {
      "pre-commit": "isort --check-only --diff . && lint-staged"
    }
  },
  "lint-staged": {
    "*.py": ["isort", "black"]
  }
}

特殊情况处理

延迟导入

# 函数内部导入 (避免循环导入)
def process_data():
    from myapp.utils import process_helper
    return process_helper(data)

条件导入

try:
    import fast_json as json
except ImportError:
    import json
if sys.platform == 'win32':
    import msvcrt
else:
    import fcntl

检查清单

  • [ ] 标准库导入在所有其他导入之前
  • [ ] 第三方库在标准库之后
  • [ ] 本地应用库在最后
  • [ ] 每个分组内部按字母排序
  • [ ] 分组之间用空行分隔
  • [ ] 没有未使用的导入
  • [ ] 没有循环导入

使用这些工具和规范,可以自动保持导入顺序的正确性和一致性。

抱歉,评论功能暂时关闭!