Code Formatter
Configures and applies language-specific formatters and linters to establish consistent code style across a codebase — generating configuration files, fixing violations, and setting up pre-commit hooks and CI checks to enforce formatting automatically.
When to Use
- User asks to "format this code", "fix linting errors", or "set up Prettier"
- A new project needs formatter/linter configuration
- CI is failing due to formatting violations
- The team wants to enforce consistent code style in PRs
- Existing linting config needs to be updated or extended
- User asks to add pre-commit hooks for automatic formatting
Process
Detect the language(s) and existing tooling:
- JavaScript/TypeScript: Prettier, ESLint (with
@typescript-eslint, eslint-plugin-react, etc.)
- Python: Black (formatter), Ruff (linter + formatter), isort, mypy, flake8
- Go:
gofmt (built-in), goimports, golangci-lint
- Rust:
rustfmt, clippy
- Java: google-java-format, Checkstyle, Spotless
- Ruby: RuboCop, StandardRB
- Check for existing config files:
.eslintrc.*, prettier.config.*, pyproject.toml, .rubocop.yml
Generate formatter configuration:
Prettier (.prettierrc or prettier.config.js):
printWidth: 100 (adjust to team preference)
tabWidth: 2 for JS/TS, 4 for Python conventions
singleQuote: true/false based on existing codebase
trailingComma: "all" (ES5+ friendly)
semi: true/false
endOfLine: "lf"
- Add
overrides for specific file types (JSON, YAML, markdown)
ESLint (eslint.config.js or .eslintrc.json):
- Extend from
eslint:recommended, @typescript-eslint/recommended
- Add framework-specific plugins (React, Vue, import ordering)
- Configure rules based on existing codebase conventions
- Add
no-console (warn in dev, error in prod)
- Set
no-unused-vars to error
- Add
import/order for consistent import ordering
Black + Ruff (pyproject.toml):
line-length = 100
target-version = ["py311"]
- Ruff rules:
E, F, I (isort), N (naming), UP (pyupgrade), B (bugbear)
- Exclude:
migrations/, __pycache__/, .venv/
Generate .prettierignore / .eslintignore:
- Exclude:
dist/, build/, node_modules/, coverage/, auto-generated files, vendored code
Add formatter scripts to package.json or Makefile:
format: run formatter to fix all files
format:check: check without modifying (for CI)
lint: run linter, exit non-zero on violations
lint:fix: run linter with auto-fix
Set up pre-commit hooks (.pre-commit-config.yaml or lint-staged + Husky):
- Run formatter and linter only on staged files (fast)
- Block commit if violations remain after auto-fix
Set up CI check:
- Add a
lint job to the CI pipeline that runs format check and linting
- This ensures formatting is enforced on all PRs
Fix existing violations if asked:
- Run the formatter on the provided code and show the corrected output
- Explain non-trivial lint rule violations (not just formatting)
Output Format
.prettierrc
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"semi": true,
"endOfLine": "lf",
"arrowParens": "always",
"overrides": [
{ "files": "*.json", "options": { "printWidth": 80 } },
{ "files": "*.md", "options": { "proseWrap": "always" } }
]
}
eslint.config.js (ESLint v9 flat config)
import js from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config(
js.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
'no-console': ['warn', { allow: ['warn', 'error'] }],
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/explicit-function-return-type': 'off',
'prefer-const': 'error',
},
},
{ ignores: ['dist/', 'build/', 'node_modules/'] }
);
pyproject.toml (Ruff + Black)
[tool.black]
line-length = 100
target-version = ["py311"]
[tool.ruff]
line-length = 100
target-version = "py311"
select = ["E", "F", "I", "N", "UP", "B", "C4"]
ignore = ["E501"] # line length handled by Black
exclude = ["migrations/", ".venv/"]
Examples
Example Input
const x=1
const foo = function(a,b,c){
return a+b+c}
var unused = "hello"
Example Output (Prettier + ESLint applied)
// After Prettier formatting:
const x = 1;
const foo = function (a, b, c) {
return a + b + c;
};
// ESLint violations (after formatting):
// Line 5: 'unused' is assigned a value but never used. (@typescript-eslint/no-unused-vars)
// Line 1: Prefer 'const' over 'let'. (prefer-const) ✓ already const
// Line 2: Prefer arrow function expression. (prefer-arrow-callback) — optional rule
Boundaries
- Do NOT apply formatting to auto-generated files (migrations, protobuf output, OpenAPI generated clients).
- Do NOT change linting rules that would cause existing passing CI to fail without flagging the breaking change.
- Do NOT apply
eslint --fix to logic-changing rules (like eqeqeq changing == to ===) without reviewing each case.
- When configuring ESLint, do NOT disable security-relevant rules (
no-eval, no-implied-eval, etc.).
- Do NOT generate formatter configs that conflict with each other (e.g., Prettier and ESLint formatting rules overlapping — use
eslint-config-prettier to disable conflicting ESLint formatting rules).
- If multiple languages are used in the repo, generate separate formatter configs per language — do not try to handle all with one tool.
1---2name: code-formatter3description: Applies language-specific formatting (Prettier, Black, gofmt, rustfmt) and enforces consistent linting rules across a codebase. Invoke when asked to format code, fix linting errors, set up Prettier or Black, configure ESLint, enforce consistent code style, or add pre-commit formatting hooks.4---56# Code Formatter78Configures and applies language-specific formatters and linters to establish consistent code style across a codebase — generating configuration files, fixing violations, and setting up pre-commit hooks and CI checks to enforce formatting automatically.910## When to Use1112- User asks to "format this code", "fix linting errors", or "set up Prettier"13- A new project needs formatter/linter configuration14- CI is failing due to formatting violations15- The team wants to enforce consistent code style in PRs16- Existing linting config needs to be updated or extended17- User asks to add pre-commit hooks for automatic formatting1819## Process20211. **Detect the language(s) and existing tooling**:22 - JavaScript/TypeScript: Prettier, ESLint (with `@typescript-eslint`, `eslint-plugin-react`, etc.)23 - Python: Black (formatter), Ruff (linter + formatter), isort, mypy, flake824 - Go: `gofmt` (built-in), `goimports`, `golangci-lint`25 - Rust: `rustfmt`, `clippy`26 - Java: google-java-format, Checkstyle, Spotless27 - Ruby: RuboCop, StandardRB28 - Check for existing config files: `.eslintrc.*`, `prettier.config.*`, `pyproject.toml`, `.rubocop.yml`29302. **Generate formatter configuration**:3132 **Prettier** (`.prettierrc` or `prettier.config.js`):33 - `printWidth`: 100 (adjust to team preference)34 - `tabWidth`: 2 for JS/TS, 4 for Python conventions35 - `singleQuote`: true/false based on existing codebase36 - `trailingComma`: "all" (ES5+ friendly)37 - `semi`: true/false38 - `endOfLine`: "lf"39 - Add `overrides` for specific file types (JSON, YAML, markdown)4041 **ESLint** (`eslint.config.js` or `.eslintrc.json`):42 - Extend from `eslint:recommended`, `@typescript-eslint/recommended`43 - Add framework-specific plugins (React, Vue, import ordering)44 - Configure rules based on existing codebase conventions45 - Add `no-console` (warn in dev, error in prod)46 - Set `no-unused-vars` to error47 - Add `import/order` for consistent import ordering4849 **Black + Ruff** (`pyproject.toml`):50 - `line-length = 100`51 - `target-version = ["py311"]`52 - Ruff rules: `E`, `F`, `I` (isort), `N` (naming), `UP` (pyupgrade), `B` (bugbear)53 - Exclude: `migrations/`, `__pycache__/`, `.venv/`54553. **Generate `.prettierignore` / `.eslintignore`**:56 - Exclude: `dist/`, `build/`, `node_modules/`, `coverage/`, auto-generated files, vendored code57584. **Add formatter scripts to `package.json`** or `Makefile`:59 - `format`: run formatter to fix all files60 - `format:check`: check without modifying (for CI)61 - `lint`: run linter, exit non-zero on violations62 - `lint:fix`: run linter with auto-fix63645. **Set up pre-commit hooks** (`.pre-commit-config.yaml` or `lint-staged` + Husky):65 - Run formatter and linter only on staged files (fast)66 - Block commit if violations remain after auto-fix67686. **Set up CI check**:69 - Add a `lint` job to the CI pipeline that runs format check and linting70 - This ensures formatting is enforced on all PRs71727. **Fix existing violations** if asked:73 - Run the formatter on the provided code and show the corrected output74 - Explain non-trivial lint rule violations (not just formatting)7576## Output Format7778### `.prettierrc`79```json80{81 "printWidth": 100,82 "tabWidth": 2,83 "singleQuote": true,84 "trailingComma": "all",85 "semi": true,86 "endOfLine": "lf",87 "arrowParens": "always",88 "overrides": [89 { "files": "*.json", "options": { "printWidth": 80 } },90 { "files": "*.md", "options": { "proseWrap": "always" } }91 ]92}93```9495### `eslint.config.js` (ESLint v9 flat config)96```js97import js from '@eslint/js';98import tseslint from 'typescript-eslint';99100export default tseslint.config(101 js.configs.recommended,102 ...tseslint.configs.recommended,103 {104 rules: {105 'no-console': ['warn', { allow: ['warn', 'error'] }],106 'no-unused-vars': 'off',107 '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],108 '@typescript-eslint/explicit-function-return-type': 'off',109 'prefer-const': 'error',110 },111 },112 { ignores: ['dist/', 'build/', 'node_modules/'] }113);114```115116### `pyproject.toml` (Ruff + Black)117```toml118[tool.black]119line-length = 100120target-version = ["py311"]121122[tool.ruff]123line-length = 100124target-version = "py311"125select = ["E", "F", "I", "N", "UP", "B", "C4"]126ignore = ["E501"] # line length handled by Black127exclude = ["migrations/", ".venv/"]128```129130## Examples131132### Example Input133```javascript134const x=1135const foo = function(a,b,c){136return a+b+c}137138var unused = "hello"139```140141### Example Output (Prettier + ESLint applied)142```javascript143// After Prettier formatting:144const x = 1;145const foo = function (a, b, c) {146 return a + b + c;147};148149// ESLint violations (after formatting):150// Line 5: 'unused' is assigned a value but never used. (@typescript-eslint/no-unused-vars)151// Line 1: Prefer 'const' over 'let'. (prefer-const) ✓ already const152// Line 2: Prefer arrow function expression. (prefer-arrow-callback) — optional rule153```154155## Boundaries156157- Do NOT apply formatting to auto-generated files (migrations, protobuf output, OpenAPI generated clients).158- Do NOT change linting rules that would cause existing passing CI to fail without flagging the breaking change.159- Do NOT apply `eslint --fix` to logic-changing rules (like `eqeqeq` changing `==` to `===`) without reviewing each case.160- When configuring ESLint, do NOT disable security-relevant rules (`no-eval`, `no-implied-eval`, etc.).161- Do NOT generate formatter configs that conflict with each other (e.g., Prettier and ESLint formatting rules overlapping — use `eslint-config-prettier` to disable conflicting ESLint formatting rules).162- If multiple languages are used in the repo, generate separate formatter configs per language — do not try to handle all with one tool.