Skip to content

Commit 3915dc2

Browse files
committed
feat(json): Support formatted .json files
1 parent 31b3857 commit 3915dc2

21 files changed

+339
-88
lines changed

.github/renovate.json5

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": ["github>coderwyd/renovate-config"]
2+
extends: ['github>coderwyd/renovate-config'],
33
}

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ interface OptionsConfig {
218218
* {
219219
* "html": true,
220220
* "css": true,
221-
* "graphql": true,
222-
* "markdown": true
223-
* "yaml": true
224-
* "toml": true
221+
* "json": true,
222+
* "graphql": false,
223+
* "markdown": false
224+
* "yaml": false
225+
* "toml": false
225226
* }
226227
*/
227228
formatter?: OptionsFormatters

example/html.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
ga('create', 'UA-XXXXX-Y', 'auto')
2121
ga('send', 'pageview')
2222
</script>
23-
<script src="https://www.google-analytics.com/analytics.js" async defer></script>
23+
<script
24+
src="https://www.google-analytics.com/analytics.js"
25+
async
26+
defer
27+
></script>
2428
</body>
2529
</html>

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@coderwyd/eslint-config",
33
"type": "module",
4-
"version": "2.1.4",
4+
"version": "2.1.5",
55
"packageManager": "pnpm@8.15.1",
66
"description": "Donny's ESLint config",
77
"author": "Donny Wang <donny526@outlook.com> (https://github.com/coderwyd/)",
@@ -26,10 +26,7 @@
2626
"module": "./dist/index.js",
2727
"types": "./dist/index.d.ts",
2828
"bin": "./bin/index.js",
29-
"files": [
30-
"bin",
31-
"dist"
32-
],
29+
"files": ["bin", "dist"],
3330
"publishConfig": {
3431
"access": "public"
3532
},

src/cli/run.ts

+32-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import c from 'picocolors'
88

99
// @ts-expect-error missing types
1010
import parse from 'parse-gitignore'
11-
import { ARROW, CHECK, WARN, eslintVersion, version, vscodeSettingsString } from './constants'
11+
import {
12+
ARROW,
13+
CHECK,
14+
WARN,
15+
eslintVersion,
16+
version,
17+
vscodeSettingsString,
18+
} from './constants'
1219
import { isGitClean } from './utils'
1320

1421
export interface RuleOptions {
@@ -29,14 +36,19 @@ export async function run(options: RuleOptions = {}) {
2936
const pathESLintIngore = path.join(cwd, '.eslintignore')
3037

3138
if (fs.existsSync(pathFlatConfig)) {
32-
console.log(c.yellow(`${WARN} eslint.config.js already exists, migration wizard exited.`))
39+
console.log(
40+
c.yellow(
41+
`${WARN} eslint.config.js already exists, migration wizard exited.`,
42+
),
43+
)
3344
return process.exit(1)
3445
}
3546

3647
if (!SKIP_GIT_CHECK && !isGitClean()) {
3748
const { confirmed } = await prompts({
3849
initial: false,
39-
message: 'There are uncommitted changes in the current repository, are you sure to continue?',
50+
message:
51+
'There are uncommitted changes in the current repository, are you sure to continue?',
4052
name: 'confirmed',
4153
type: 'confirm',
4254
})
@@ -67,7 +79,10 @@ export async function run(options: RuleOptions = {}) {
6779

6880
for (const glob of globs) {
6981
if (glob.type === 'ignore') eslintIgnores.push(...glob.patterns)
70-
else if (glob.type === 'unignore') eslintIgnores.push(...glob.patterns.map((pattern: string) => `!${pattern}`))
82+
else if (glob.type === 'unignore')
83+
eslintIgnores.push(
84+
...glob.patterns.map((pattern: string) => `!${pattern}`),
85+
)
7186
}
7287
}
7388

@@ -94,7 +109,8 @@ module.exports = defineConfig({\n${coderwydConfig}\n})
94109
const files = fs.readdirSync(cwd)
95110
const legacyConfig: string[] = []
96111
files.forEach(file => {
97-
if (file.includes('eslint') || file.includes('prettier')) legacyConfig.push(file)
112+
if (file.includes('eslint') || file.includes('prettier'))
113+
legacyConfig.push(file)
98114
})
99115
if (legacyConfig.length > 0) {
100116
console.log(`${WARN} you can now remove those files manually:`)
@@ -112,7 +128,8 @@ module.exports = defineConfig({\n${coderwydConfig}\n})
112128
promptResult = await prompts(
113129
{
114130
initial: true,
115-
message: 'Update .vscode/settings.json for better VS Code experience?',
131+
message:
132+
'Update .vscode/settings.json for better VS Code experience?',
116133
name: 'updateVscodeSettings',
117134
type: 'confirm',
118135
},
@@ -132,7 +149,8 @@ module.exports = defineConfig({\n${coderwydConfig}\n})
132149
const dotVscodePath: string = path.join(cwd, '.vscode')
133150
const settingsPath: string = path.join(dotVscodePath, 'settings.json')
134151

135-
if (!fs.existsSync(dotVscodePath)) await fsp.mkdir(dotVscodePath, { recursive: true })
152+
if (!fs.existsSync(dotVscodePath))
153+
await fsp.mkdir(dotVscodePath, { recursive: true })
136154

137155
if (!fs.existsSync(settingsPath)) {
138156
await fsp.writeFile(settingsPath, `{${vscodeSettingsString}}\n`, 'utf-8')
@@ -141,7 +159,10 @@ module.exports = defineConfig({\n${coderwydConfig}\n})
141159
let settingsContent = await fsp.readFile(settingsPath, 'utf8')
142160

143161
settingsContent = settingsContent.trim().replace(/\s*}$/, '')
144-
settingsContent += settingsContent.endsWith(',') || settingsContent.endsWith('{') ? '' : ','
162+
settingsContent +=
163+
settingsContent.endsWith(',') || settingsContent.endsWith('{')
164+
? ''
165+
: ','
145166
settingsContent += `${vscodeSettingsString}}\n`
146167

147168
await fsp.writeFile(settingsPath, settingsContent, 'utf-8')
@@ -151,5 +172,7 @@ module.exports = defineConfig({\n${coderwydConfig}\n})
151172

152173
// End update .vscode/settings.json
153174
console.log(c.green(`${CHECK} migration completed`))
154-
console.log(`Now you can update the dependencies and run ${c.blue('eslint . --fix')}\n`)
175+
console.log(
176+
`Now you can update the dependencies and run ${c.blue('eslint . --fix')}\n`,
177+
)
155178
}

src/configs/formatter.ts

+39-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import {
22
GLOB_CSS,
33
GLOB_GRAPHQL,
44
GLOB_HTML,
5+
GLOB_JSON,
6+
GLOB_JSON5,
7+
GLOB_JSONC,
58
GLOB_LESS,
69
GLOB_MARKDOWN,
710
GLOB_POSTCSS,
@@ -11,19 +14,36 @@ import {
1114
} from '../constants/glob'
1215

1316
import { ensurePackages, interopDefault, parserPlain } from '../shared'
14-
import type { FlatConfigItem, OptionsFormatters, PartialPrettierExtendedOptions, PrettierParser } from '../types'
17+
import type {
18+
FlatConfigItem,
19+
OptionsFormatters,
20+
PartialPrettierExtendedOptions,
21+
PrettierParser,
22+
} from '../types'
1523

1624
export async function formatter(
1725
options: OptionsFormatters = {},
1826
prettierRules: PartialPrettierExtendedOptions = {},
1927
): Promise<FlatConfigItem[]> {
2028
await ensurePackages(['eslint-plugin-prettier'])
2129

22-
const { css = true, graphql, html = true, markdown, toml, yaml } = options || {}
30+
const {
31+
css = true,
32+
graphql,
33+
html = true,
34+
json = true,
35+
markdown,
36+
toml,
37+
yaml,
38+
} = options || {}
2339

2440
const pluginPrettier = await interopDefault(import('eslint-plugin-prettier'))
2541

26-
function createPrettierFormatter(files: string[], parser: PrettierParser, plugins?: string[]) {
42+
function createPrettierFormatter(
43+
files: string[],
44+
parser: PrettierParser,
45+
plugins?: string[],
46+
) {
2747
const rules = {
2848
...prettierRules,
2949
parser,
@@ -49,7 +69,10 @@ export async function formatter(
4969
prettier: pluginPrettier,
5070
},
5171
rules: {
52-
'prettier/prettier': ['warn', parser === 'markdown' ? markdownRules : rules],
72+
'prettier/prettier': [
73+
'warn',
74+
parser === 'markdown' ? markdownRules : rules,
75+
],
5376
},
5477
}
5578

@@ -65,6 +88,11 @@ export async function formatter(
6588
},
6689
]
6790

91+
if (html) {
92+
const htmlConfig = createPrettierFormatter([GLOB_HTML], 'html')
93+
configs.push(htmlConfig)
94+
}
95+
6896
if (css) {
6997
const cssConfig = createPrettierFormatter([GLOB_CSS, GLOB_POSTCSS], 'css')
7098
const scssConfig = createPrettierFormatter([GLOB_SCSS], 'scss')
@@ -73,9 +101,10 @@ export async function formatter(
73101
configs.push(cssConfig, scssConfig, lessConfig)
74102
}
75103

76-
if (html) {
77-
const htmlConfig = createPrettierFormatter([GLOB_HTML], 'html')
78-
configs.push(htmlConfig)
104+
if (json) {
105+
const jsonConfig = createPrettierFormatter([GLOB_JSON, GLOB_JSONC], 'json')
106+
const json5Config = createPrettierFormatter([GLOB_JSON5], 'json5')
107+
configs.push(jsonConfig, json5Config)
79108
}
80109

81110
if (markdown) {
@@ -96,7 +125,9 @@ export async function formatter(
96125
if (toml) {
97126
await ensurePackages(['@toml-tools/parser', 'prettier-plugin-toml'])
98127

99-
const tomlConfig = createPrettierFormatter([GLOB_TOML], 'toml', ['prettier-plugin-toml'])
128+
const tomlConfig = createPrettierFormatter([GLOB_TOML], 'toml', [
129+
'prettier-plugin-toml',
130+
])
100131

101132
configs.push(tomlConfig)
102133
}

src/configs/imports.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export async function imports(): Promise<FlatConfigItem[]> {
1616
'antfu/no-import-node-modules-by-path': 'error',
1717

1818
'import/first': 'error',
19-
'import/newline-after-import': ['error', { considerComments: true, count: 1 }],
19+
'import/newline-after-import': [
20+
'error',
21+
{ considerComments: true, count: 1 },
22+
],
2023
'import/no-duplicates': 'error',
2124
'import/no-mutable-exports': 'error',
2225
'import/no-named-default': 'error',
@@ -25,7 +28,16 @@ export async function imports(): Promise<FlatConfigItem[]> {
2528
'import/order': [
2629
'error',
2730
{
28-
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
31+
groups: [
32+
'builtin',
33+
'external',
34+
'internal',
35+
'parent',
36+
'sibling',
37+
'index',
38+
'object',
39+
'type',
40+
],
2941
pathGroups: [{ group: 'internal', pattern: '{{@,~}/,#}**' }],
3042
pathGroupsExcludedImportTypes: ['type'],
3143
},

src/configs/javascript.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import globals from 'globals'
22
import { pluginUnusedImports } from '../plugins'
33
import { GLOB_SRC, GLOB_SRC_EXT } from '../constants/glob'
4-
import type { FlatConfigItem, OptionsIsInEditor, OptionsOverrides } from '../types'
4+
import type {
5+
FlatConfigItem,
6+
OptionsIsInEditor,
7+
OptionsOverrides,
8+
} from '../types'
59

6-
export async function javascript(options: OptionsIsInEditor & OptionsOverrides = {}): Promise<FlatConfigItem[]> {
10+
export async function javascript(
11+
options: OptionsIsInEditor & OptionsOverrides = {},
12+
): Promise<FlatConfigItem[]> {
713
const { isInEditor = false, overrides = {} } = options
814

915
return [
@@ -35,15 +41,21 @@ export async function javascript(options: OptionsIsInEditor & OptionsOverrides =
3541
'unused-imports': pluginUnusedImports,
3642
},
3743
rules: {
38-
'accessor-pairs': ['error', { enforceForClassMembers: true, setWithoutGet: true }],
44+
'accessor-pairs': [
45+
'error',
46+
{ enforceForClassMembers: true, setWithoutGet: true },
47+
],
3948

4049
'array-callback-return': 'error',
4150
'block-scoped-var': 'error',
4251
'constructor-super': 'error',
4352
'default-case-last': 'error',
4453
'dot-notation': ['error', { allowKeywords: true }],
4554
eqeqeq: ['error', 'smart'],
46-
'new-cap': ['error', { capIsNew: false, newIsCap: true, properties: true }],
55+
'new-cap': [
56+
'error',
57+
{ capIsNew: false, newIsCap: true, properties: true },
58+
],
4759
'no-alert': 'error',
4860
'no-array-constructor': 'error',
4961
'no-async-promise-executor': 'error',
@@ -102,7 +114,8 @@ export async function javascript(options: OptionsIsInEditor & OptionsOverrides =
102114
'no-restricted-properties': [
103115
'error',
104116
{
105-
message: 'Use `Object.getPrototypeOf` or `Object.setPrototypeOf` instead.',
117+
message:
118+
'Use `Object.getPrototypeOf` or `Object.setPrototypeOf` instead.',
106119
property: '__proto__',
107120
},
108121
{
@@ -164,7 +177,10 @@ export async function javascript(options: OptionsIsInEditor & OptionsOverrides =
164177
vars: 'all',
165178
},
166179
],
167-
'no-use-before-define': ['error', { classes: false, functions: false, variables: true }],
180+
'no-use-before-define': [
181+
'error',
182+
{ classes: false, functions: false, variables: true },
183+
],
168184
'no-useless-backreference': 'error',
169185
'no-useless-call': 'error',
170186
'no-useless-catch': 'error',
@@ -227,7 +243,10 @@ export async function javascript(options: OptionsIsInEditor & OptionsOverrides =
227243
varsIgnorePattern: '^_',
228244
},
229245
],
230-
'use-isnan': ['error', { enforceForIndexOf: true, enforceForSwitchCase: true }],
246+
'use-isnan': [
247+
'error',
248+
{ enforceForIndexOf: true, enforceForSwitchCase: true },
249+
],
231250
'valid-typeof': ['error', { requireStringLiterals: true }],
232251
'vars-on-top': 'error',
233252
yoda: ['error', 'never'],

src/configs/jsonc.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { GLOB_JSON, GLOB_JSON5, GLOB_JSONC } from '../constants/glob'
22
import { interopDefault } from '../shared'
33
import type { FlatConfigItem, OptionsFiles, OptionsOverrides } from '../types'
44

5-
export async function jsonc(options: OptionsFiles & OptionsOverrides = {}): Promise<FlatConfigItem[]> {
6-
const { files = [GLOB_JSON, GLOB_JSON5, GLOB_JSONC], overrides = {} } = options || {}
5+
export async function jsonc(
6+
options: OptionsFiles & OptionsOverrides = {},
7+
): Promise<FlatConfigItem[]> {
8+
const { files = [GLOB_JSON, GLOB_JSON5, GLOB_JSONC], overrides = {} } =
9+
options || {}
710

811
const [pluginJsonc, parserJsonc] = await Promise.all([
912
interopDefault(import('eslint-plugin-jsonc')),

0 commit comments

Comments
 (0)