Skip to content

Commit

Permalink
feat(eslint-config)!: add a config preset for validating dependencies…
Browse files Browse the repository at this point in the history
… in package.json files (#813)
  • Loading branch information
marcusrbrown authored Dec 28, 2024
1 parent d673453 commit 167311e
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-bulldogs-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/eslint-config": minor
---

Add a config preset for validating dependencies in package.json files.

1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {defineConfig} from './packages/eslint-config/src'
export const config = defineConfig({
name: '@bfra.me/works',
ignores: ['**/test/fixtures', '**/test/_fixtures'],
packageJson: true,
typescript: {
parserOptions: {
project: [
Expand Down
14 changes: 6 additions & 8 deletions packages/eslint-config/eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import config from '../../eslint.config'
import {composeConfig} from './src/compose-config'

export default composeConfig(
config,
{
export default composeConfig(config)
.insertAfter('@bfra.me/ignores', {
name: '@bfra.me/eslint-config/ignores',
ignores: ['.eslint-config-inspector/', 'lib', 'src/rules.d.ts'],
},
{
ignores: ['.eslint-config-inspector', 'lib', 'src/rules.d.ts'],
})
.append({
name: '@bfra.me/eslint-config',
files: ['src/**/*.ts'],
rules: {
Expand All @@ -19,5 +18,4 @@ export default composeConfig(
},
],
},
},
)
})
1 change: 1 addition & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"eslint": "9.17.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-no-only-tests": "3.3.0",
"eslint-plugin-node-dependencies": "0.12.0",
"eslint-plugin-prettier": "5.2.1",
"eslint-typegen": "0.3.2",
"tsup": "8.3.5",
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {defineConfig} from '../src/define-config'

const configs = await defineConfig(
{
packageJson: true,
plugins: {
'': {
rules: Object.fromEntries(builtinRules),
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-config/src/config.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/eslint-config/src/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './jsdoc'
export * from './jsonc'
export * from './markdown'
export * from './node'
export * from './package-json'
export * from './perfectionist'
export * from './prettier'
export * from './regexp'
Expand Down
43 changes: 43 additions & 0 deletions packages/eslint-config/src/configs/package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {Config} from '../config'
import type {Flatten, OptionsFiles} from '../options'
import {anyParser} from '../parsers/any-parser'
import {interopDefault} from '../plugins'
import {requireOf} from '../require-of'
import {fallback} from './fallback'
import {jsonSchema} from './json-schema'

export type PackageJsonOptions = Flatten<OptionsFiles>

export const packageJsonFiles = ['package.json', 'package.json5', 'package.jsonc'].flatMap(file => [
file,
`**/${file}`,
])

export async function packageJson(options: PackageJsonOptions = {}): Promise<Config[]> {
const {files = packageJsonFiles} = options
return requireOf(
['eslint-plugin-node-dependencies'],
async () => {
// @ts-expect-error: Missing type definitions
const pluginNodeDependencies = await interopDefault(import('eslint-plugin-node-dependencies'))
return [
...pluginNodeDependencies.configs['flat/recommended'].map(
(config: Config, index: number) => ({
...config,
name: config.plugins
? `@bfra.me/package-json/plugins`
: `@bfra.me/${config.name || `package-json/unnamed${index}`}`,
files,
}),
),
...(await jsonSchema('package-json', files as string[])),
]
},
async missingList =>
fallback(missingList, {
name: '@bfra.me/package-json/fallback',
files,
languageOptions: {parser: anyParser},
}),
)
}
6 changes: 6 additions & 0 deletions packages/eslint-config/src/define-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
jsonc,
markdown,
node,
packageJson,
perfectionist,
prettier,
regexp,
Expand Down Expand Up @@ -57,6 +58,7 @@ export async function defineConfig(
const {
gitignore: enableGitignore = true,
jsx: enableJsx = true,
packageJson: enablePackageJson = false,
perfectionist: enablePerfectionist = true,
prettier: enablePrettier = isPackageExists('prettier'),
regexp: enableRegexp = true,
Expand Down Expand Up @@ -96,6 +98,10 @@ export async function defineConfig(
command(),
)

if (enablePackageJson) {
configs.push(packageJson(resolveSubOptions(options, 'packageJson')))
}

if (enablePrettier) {
configs.push(
prettier({
Expand Down
7 changes: 7 additions & 0 deletions packages/eslint-config/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ export type Options = Flatten<
*/
markdown?: boolean | (OptionsOverrides & OptionsPrettier)

/**
* Enable support for package.json files.
*
* @default false
*/
packageJson?: boolean | OptionsFiles

/**
* Options to override the behavior of Perfectionist sorting rules.
*/
Expand Down
82 changes: 82 additions & 0 deletions packages/eslint-config/src/rules.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 167311e

Please sign in to comment.