Skip to content

Commit

Permalink
chore: update to eslint v9 with flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Dec 5, 2024
1 parent d50e106 commit fa18e1e
Show file tree
Hide file tree
Showing 24 changed files with 859 additions and 1,746 deletions.
14 changes: 0 additions & 14 deletions .eslintignore

This file was deleted.

51 changes: 0 additions & 51 deletions .eslintrc.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion bin/check-dependency-version-consistency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
/* eslint node/shebang:"off" -- shebang needed so compiled code gets interpreted as JS */
/* eslint n/hashbang:"off" -- shebang needed so compiled code gets interpreted as JS */

import { run } from '../lib/cli.js';

Expand Down
74 changes: 74 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// @ts-check

import js from '@eslint/js';
import eslintPluginN from 'eslint-plugin-n';
import eslintPluginJest from 'eslint-plugin-jest';
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'; // eslint-disable-line import/extensions -- false positive
import * as eslintPluginImport from 'eslint-plugin-import';
import tseslint from 'typescript-eslint';

export default tseslint.config(
// Configs:
js.configs.recommended,
eslintPluginImport.flatConfigs
? eslintPluginImport.flatConfigs.recommended // TODO: use typescript config
: {},
eslintPluginJest.configs['flat/recommended'],
eslintPluginN.configs['flat/recommended'],
eslintPluginPrettierRecommended,
eslintPluginUnicorn.configs['flat/recommended'],
...tseslint.configs.recommendedTypeChecked,

// Individual rules:
{
rules: {
'import/extensions': ['error', 'always'],
'import/no-unresolved': 'off',

'n/no-missing-import': 'off', // bug with recognizing node: prefix https://github.com/mysticatea/eslint-plugin-node/issues/275
'n/no-unsupported-features/es-syntax': [
'error',
{ ignores: ['modules'] },
],

'prettier/prettier': [
'error',
{
singleQuote: true,
},
],

'unicorn/import-style': 'off',
'unicorn/no-useless-undefined': 'off', // We use a lot of `return undefined` to satisfy the `consistent-return` rule.

'@typescript-eslint/prefer-readonly': 'error',
'@typescript-eslint/require-array-sort-compare': 'error',
},

// typescript-eslint parser options:
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},

// Disabling type-checking for JS files:
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
},

// Ignore files:
{
ignores: [
// compiled output
'dist/**',

// coverage
'coverage/**',
],
},
);
8 changes: 5 additions & 3 deletions jest.config.cjs → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/
import type { Config } from 'jest';

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
// https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/
const config: Config = {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
testMatch: ['<rootDir>/test/**/*-test.ts'],
Expand All @@ -24,3 +24,5 @@ module.exports = {
},
},
};

export default config;
6 changes: 3 additions & 3 deletions lib/cdvc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CDVC {

public getDependencies(): readonly Dependency[] {
return Object.keys(this.dependencies).map((dependency) =>
this.getDependency(dependency)
this.getDependency(dependency),
);
}

Expand All @@ -72,13 +72,13 @@ export class CDVC {

public get hasMismatchingDependenciesFixable(): boolean {
return Object.values(this.dependencies).some(
(dep) => dep.isMismatching && dep.isFixable
(dep) => dep.isMismatching && dep.isFixable,
);
}

public get hasMismatchingDependenciesNotFixable(): boolean {
return Object.values(this.dependencies).some(
(dep) => dep.isMismatching && !dep.isFixable
(dep) => dep.isMismatching && !dep.isFixable,
);
}
}
24 changes: 12 additions & 12 deletions lib/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { DEFAULT_DEP_TYPES } from './defaults.js';
*/
export function check(
path: string,
options?: Options
options?: Options,
): {
dependencies: Dependencies;
} {
Expand All @@ -36,8 +36,8 @@ export function check(
) {
throw new Error(
`Invalid depType provided. Choices are: ${Object.keys(
DEPENDENCY_TYPE
).join(', ')}.`
DEPENDENCY_TYPE,
).join(', ')}.`,
);
}

Expand All @@ -64,37 +64,37 @@ export function check(
optionsWithDefaults.ignorePackage,
optionsWithDefaults.ignorePackagePattern.map((s) => new RegExp(s)),
optionsWithDefaults.ignorePath,
optionsWithDefaults.ignorePathPattern.map((s) => new RegExp(s))
optionsWithDefaults.ignorePathPattern.map((s) => new RegExp(s)),
);

const dependencies = calculateVersionsForEachDependency(
packages,
optionsWithDefaults.depType.map((dt) => DEPENDENCY_TYPE[dt]) // Convert string to enum.
optionsWithDefaults.depType.map((dt) => DEPENDENCY_TYPE[dt]), // Convert string to enum.
);
const dependenciesAndVersions =
calculateDependenciesAndVersions(dependencies);
const dependenciesAndVersionsWithMismatches = dependenciesAndVersions.filter(
({ versions }) => versions.length > 1
({ versions }) => versions.length > 1,
);

// Information about all dependencies.
const dependenciesAndVersionsWithoutIgnored = filterOutIgnoredDependencies(
dependenciesAndVersions,
optionsWithDefaults.ignoreDep,
optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s))
optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s)),
);

// Information about mismatches.
const dependenciesAndVersionsMismatchesWithoutIgnored =
filterOutIgnoredDependencies(
dependenciesAndVersionsWithMismatches,
optionsWithDefaults.ignoreDep,
optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s))
optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s)),
);
const resultsAfterFix = fixVersionsMismatching(
packages,
dependenciesAndVersionsMismatchesWithoutIgnored,
!optionsWithDefaults.fix // Do dry-run if not fixing.
!optionsWithDefaults.fix, // Do dry-run if not fixing.
);
const versionsMismatchingFixable = resultsAfterFix.fixable;

Expand All @@ -106,15 +106,15 @@ export function check(
dependency,
{
isFixable: versionsMismatchingFixable.some(
(dep) => dep.dependency === dependency
(dep) => dep.dependency === dependency,
),
isMismatching: dependenciesAndVersionsMismatchesWithoutIgnored.some(
(dep) => dep.dependency === dependency
(dep) => dep.dependency === dependency,
),
versions,
},
];
})
}),
),
};
}
28 changes: 14 additions & 14 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));

function getCurrentPackageVersion(): string {
const packageJson = JSON.parse(
readFileSync(join(__dirname, '..', '..', 'package.json'), 'utf8') // Relative to compiled version of this file in the dist folder.
readFileSync(join(__dirname, '..', '..', 'package.json'), 'utf8'), // Relative to compiled version of this file in the dist folder.
) as PackageJson;
if (!packageJson.version) {
throw new Error('Could not find package.json `version`');
Expand All @@ -25,7 +25,7 @@ function getCurrentPackageVersion(): string {
*/
function collect(
value: string,
previous: readonly string[]
previous: readonly string[],
): readonly string[] {
return [...previous, value];
}
Expand All @@ -36,7 +36,7 @@ function collect(
* */
function collectCSV(
value: string,
previous: readonly string[]
previous: readonly string[],
): readonly string[] {
return [...previous, ...value.split(',')];
}
Expand All @@ -47,60 +47,60 @@ export function run() {

program
.description(
'CLI tool which checks that dependencies are on consistent versions across a monorepo / npm/pnpm/Yarn workspace.'
'CLI tool which checks that dependencies are on consistent versions across a monorepo / npm/pnpm/Yarn workspace.',
)
.version(getCurrentPackageVersion())
.addArgument(new Argument('[path]', 'path to workspace root').default('.'))
.option(
'--dep-type <dependency-type>',
`Type of dependency to check (choices: ${Object.keys(
DEPENDENCY_TYPE
DEPENDENCY_TYPE,
).join(', ')}) (default: ${DEFAULT_DEP_TYPES.join(
', '
', ',
)}) (option can be repeated)`,
collectCSV,
[]
[],
)
.option(
'--fix',
'Whether to autofix inconsistencies (using highest version present)',
false
false,
)
.option(
'--ignore-dep <dependency-name>',
'Dependency to ignore (option can be repeated)',
collect,
[]
[],
)
.option(
'--ignore-dep-pattern <dependency-name-pattern>',
'RegExp of dependency names to ignore (option can be repeated)',
collect,
[]
[],
)
.option(
'--ignore-package <package-name>',
'Workspace package to ignore (option can be repeated)',
collect,
[]
[],
)
.option(
'--ignore-package-pattern <package-name-pattern>',
'RegExp of package names to ignore (option can be repeated)',
collect,
[]
[],
)
.option(
'--ignore-path <path>',
'Workspace-relative path of packages to ignore (option can be repeated)',
collect,
[]
[],
)
.option(
'--ignore-path-pattern <path-pattern>',
'RegExp of workspace-relative path of packages to ignore (option can be repeated)',
collect,
[]
[],
)
.action((path: string, options: Options) => {
const cdvc = new CDVC(path, options);
Expand Down
Loading

0 comments on commit fa18e1e

Please sign in to comment.