-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
configurationProject setup and configuration filesProject setup and configuration filesenhancementNew feature or requestNew feature or request
Milestone
Description
feat(eslint): add plugins for sorting keys and imports
| ⏱️ Estimate | 📊 Priority | 📏 Size | 📅 Start | 📅 End |
|---|---|---|---|---|
| 1h | P2 | S | 24-01-2026 | 24-01-2026 |
📸 Screenshots
| Current | Expected |
|---|---|
| N/A — This change has no visual impact. | N/A — This change has no visual impact. |
📝 Summary
- Add ESLint plugins to automatically sort imports/exports in JS/Vue files and keys in JS objects
- Replace legacy
eslint-plugin-jsonwith moderneslint-plugin-jsonc - Add
sort-package-jsonto sortpackage.jsonwith standard conventional order - All plugins support auto-fix for seamless integration with
npm run lint
💡 Why this change?
- Currently
sort-importsandsort-keysrules are disabled (0) in.eslintrc.js - Native ESLint rules don't have auto-fix capability
- Using plugins with auto-fix ensures consistent code formatting automatically
eslint-plugin-jsonis legacy and doesn't support sorting or auto-fixsort-package-jsonuses the standard conventional order without manual configuration
✅ Benefits
- Automatic sorting of imports/exports in JS/Vue files
- Automatic sorting of keys in JSON files
- Automatic sorting of keys in JS objects (configs, etc.)
- Automatic sorting of
package.jsonwith conventional order (name, version, scripts, dependencies...) - Consistent code style across the project
- Integration with pre-commit hooks via
lint-staged
📋 Steps
Phase 1: Install packages
- Install new packages as
devDependencies:
npm install -D eslint-plugin-jsonc jsonc-eslint-parser eslint-plugin-simple-import-sort eslint-plugin-sort-keys-fix sort-package-jsonPhase 2: Remove legacy package
- Uninstall legacy package:
npm uninstall eslint-plugin-jsonPhase 3: Update ESLint configuration
3.1 Update extends array
- Remove
plugin:json/recommendedand addplugin:jsonc/recommended-with-json:
"extends": [
"prettier",
"eslint:recommended",
"plugin:vue/recommended",
- "plugin:json/recommended",
+ "plugin:jsonc/recommended-with-json",
],3.2 Update plugins array
- Remove
jsonand add new plugins:
"plugins": [
"vue",
"prettier",
- "json",
+ "jsonc",
+ "simple-import-sort",
+ "sort-keys-fix",
],3.3 Update overrides for JSON and JSONC files
- Replace existing overrides with complete configuration for all file types:
"overrides": [
// Existing Vue override
{
"files": ["*.vue"],
"rules": {
"indent": "off",
},
},
+ // JSON files - alphabetical order (excludes package.json and JSONC files)
+ {
+ "files": ["*.json", ".prettierrc"],
+ "excludedFiles": ["package.json", "package-lock.json", "jsconfig.json", "tsconfig*.json", ".vscode/*.json"],
+ "parser": "jsonc-eslint-parser",
+ "rules": {
+ "jsonc/comma-dangle": "off",
+ "jsonc/indent": [2, "tab"],
+ "jsonc/key-spacing": 2,
+ "jsonc/sort-keys": [
+ 2,
+ {
+ "order": { "type": "asc" },
+ "pathPattern": ".*"
+ }
+ ]
+ }
+ },
+ // JSONC files - allows comments (jsconfig, tsconfig, .vscode)
+ {
+ "files": ["jsconfig.json", "tsconfig*.json", ".vscode/*.json"],
+ "parser": "jsonc-eslint-parser",
+ "extends": ["plugin:jsonc/recommended-with-jsonc"],
+ "rules": {
+ "jsonc/comma-dangle": "off",
+ "jsonc/indent": [2, "tab"],
+ "jsonc/key-spacing": 2,
+ "jsonc/sort-keys": [
+ 2,
+ {
+ "order": { "type": "asc" },
+ "pathPattern": ".*"
+ }
+ ]
+ }
+ },
+ // package.json - no sorting (handled by sort-package-json)
+ {
+ "files": ["package.json"],
+ "parser": "jsonc-eslint-parser",
+ "rules": {
+ "jsonc/comma-dangle": "off",
+ "jsonc/indent": [2, "tab"],
+ "jsonc/key-spacing": 2,
+ "jsonc/sort-keys": "off"
+ }
+ },
],3.4 Add sorting rules for JS/Vue files
- Add the new sorting rules to the
rulessection:
"rules": {
// ... existing rules ...
// Keep native rules disabled (no auto-fix)
"sort-imports": 0,
"sort-keys": 0,
+ // Import/export sorting (with auto-fix)
+ "simple-import-sort/imports": 2,
+ "simple-import-sort/exports": 2,
+
+ // Object keys sorting (with auto-fix)
+ "sort-keys-fix/sort-keys-fix": 2,
// ... rest of existing rules ...
},Phase 4: Update package.json scripts
- Add
sort-pkgscript and updatelintscript:
"scripts": {
"install:clean": "rm -rf node_modules package-lock.json",
"postinstall": "npm run lint",
"prepare": "husky",
"env:create": "grep -v '^# ' .env.local.sample > .env.local",
"serve": "vue-cli-service serve",
"build": "dotenv -e .env.local -- vue-cli-service build",
"prettier:fix": "prettier --write \"./**/*.{css,scss,sass,json,js,cjs,mjs,vue}\" .prettierrc",
"eslint:fix": "vue-cli-service lint \"**/*.{json,js,cjs,mjs,vue}\" --fix --ignore-path .eslintignore",
"stylelint:fix": "stylelint \"**/*.{css,scss,sass,vue}\" --fix --ignore-path .stylelintignore",
- "lint": "npm run prettier:fix && npm run stylelint:fix && npm run eslint:fix",
+ "sort-pkg": "sort-package-json",
+ "lint": "npm run prettier:fix && npm run stylelint:fix && npm run eslint:fix && npm run sort-pkg",
"deploy": "node gh-pages-deploy.mjs"
},Phase 5: Run linter
- Auto-fix all files:
npm run lint🧪 Tests
- Linting runs without errors:
npm run lint- Imports are sorted alphabetically in JS/Vue files
- Exports are sorted alphabetically in JS/Vue files
- Keys are sorted in JS objects
- Keys are sorted alphabetically in JSON files
- Keys are sorted alphabetically in
.prettierrc - Keys are sorted alphabetically in
jsconfig.json(JSONC with comments support) -
package.jsonis sorted with conventional order bysort-package-json - Pre-commit hook works correctly
📌 Notes
Sorting rules summary
| Rule | Applies to | Behavior |
|---|---|---|
simple-import-sort/imports |
JS/Vue files | Sort imports alphabetically |
simple-import-sort/exports |
JS/Vue files | Sort exports alphabetically |
sort-keys-fix/sort-keys-fix |
JS/Vue files | Sort object keys alphabetically |
jsonc/sort-keys |
JSON files | Sort keys alphabetically |
sort-package-json |
package.json | Sort with conventional order |
JSONC rules applied to all JSON files
| Rule | Value | Description |
|---|---|---|
jsonc/comma-dangle |
"off" |
Disable trailing commas in JSON |
jsonc/indent |
[2, "tab"] |
Use tabs for indentation |
jsonc/key-spacing |
2 |
Enforce correct spacing around colons |
jsonc/sort-keys |
[2, ...] |
Sort keys alphabetically |
JSON vs JSONC files
| File type | Config used | Comments allowed | Sorting |
|---|---|---|---|
*.json, .prettierrc |
recommended-with-json |
❌ | Alphabetical |
jsconfig.json, tsconfig*.json, .vscode/*.json |
recommended-with-jsonc |
✓ | Alphabetical |
package.json |
Custom | ❌ | Conventional (sort-package-json) |
package-lock.json |
Excluded | - | None |
How eslint-plugin-jsonc and sort-package-json work together
Both packages are compatible and complement each other:
| Package | Root level | Nested objects (scripts, dependencies...) |
|---|---|---|
eslint-plugin-json (current) |
❌ No sorting | ❌ No sorting |
eslint-plugin-jsonc |
Alphabetical | Alphabetical |
sort-package-json |
Conventional | Alphabetical |
Execution order:
eslint:fix→eslint-plugin-jsoncsorts all keys alphabetically (exceptpackage.json)sort-pkg→sort-package-jsonsortspackage.jsonwith conventional order
Final result for package.json:
- Root level: conventional order (name, version, description, scripts, dependencies...)
- Nested objects: alphabetical order (scripts, dependencies, devDependencies...)
Other JSON files (jsconfig.json, .prettierrc, etc.):
- All keys sorted alphabetically by
eslint-plugin-jsonc
🔗 References
Files to modify
.eslintrc.jspackage.jsonpackage-lock.json
Documentation
- https://github.com/ota-meshi/eslint-plugin-jsonc
- https://ota-meshi.github.io/eslint-plugin-jsonc/rules/
- https://github.com/lydell/eslint-plugin-simple-import-sort
- https://github.com/leo-sipos/eslint-plugin-sort-keys-fix
- https://github.com/keithamus/sort-package-json
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
configurationProject setup and configuration filesProject setup and configuration filesenhancementNew feature or requestNew feature or request