Prettier Plugin for PostgreSQL 使用教程
1. 项目目录结构及介绍
prettier-plugin-pg/
├── scripts/
├── src/
├── tests/
├── tests_config/
├── typings/
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .prettierrc.js
├── LICENSE
├── README.md
├── jest.eslint.config.js
├── jest.test.config.js
├── package.json
├── tsconfig.json
└── yarn.lock
目录结构介绍
- scripts/: 存放项目的脚本文件。
- src/: 存放项目的主要源代码。
- tests/: 存放项目的测试代码。
- tests_config/: 存放测试配置文件。
- typings/: 存放TypeScript类型定义文件。
- .eslintignore: ESLint忽略文件配置。
- .eslintrc.js: ESLint配置文件。
- .gitignore: Git忽略文件配置。
- .prettierrc.js: Prettier配置文件。
- LICENSE: 项目许可证文件。
- README.md: 项目说明文档。
- jest.eslint.config.js: Jest ESLint配置文件。
- jest.test.config.js: Jest测试配置文件。
- package.json: 项目依赖和脚本配置文件。
- tsconfig.json: TypeScript配置文件。
- yarn.lock: Yarn锁定文件,用于确保依赖版本一致性。
2. 项目启动文件介绍
项目的主要启动文件是 src/
目录下的文件。具体来说,项目的核心逻辑和功能实现都在这个目录中。
主要文件
- src/index.ts: 项目的入口文件,负责初始化和配置Prettier插件。
- src/parser.ts: 负责解析PostgreSQL SQL语句。
- src/printer.ts: 负责格式化解析后的SQL语句。
3. 项目配置文件介绍
.prettierrc.js
这是Prettier的配置文件,用于定义代码格式化的规则。例如:
module.exports = {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
trailingComma: 'es5',
bracketSpacing: true,
arrowParens: 'always',
};
tsconfig.json
这是TypeScript的配置文件,用于定义TypeScript编译器的选项。例如:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
package.json
这是项目的依赖和脚本配置文件。例如:
{
"name": "prettier-plugin-pg",
"version": "1.0.0",
"scripts": {
"build": "tsc",
"test": "jest"
},
"dependencies": {
"prettier": "^2.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.0",
"jest": "^26.0.0",
"typescript": "^4.0.0"
}
}
通过以上配置文件,可以确保项目在开发和测试过程中的一致性和规范性。