Skip to content

Commit

Permalink
refactor: configure eslint unicorn (#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 27, 2024
1 parent 11d5e57 commit b59dd3f
Show file tree
Hide file tree
Showing 34 changed files with 565 additions and 160 deletions.
28 changes: 28 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { readGitignoreFiles } = require('eslint-gitignore');

/// <reference types="@eslint-types/prettier" />
/// <reference types="@eslint-types/typescript-eslint" />
/// <reference types="@eslint-types/unicorn" />

module.exports = defineConfig({
ignorePatterns: [
Expand All @@ -19,6 +20,7 @@ module.exports = defineConfig({
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/strict-type-checked',
'plugin:unicorn/recommended',
'plugin:prettier/recommended',
],
parserOptions: {
Expand All @@ -32,6 +34,30 @@ module.exports = defineConfig({
'prefer-template': 'error',
quotes: 'off',

'unicorn/consistent-function-scoping': 'off',
'unicorn/filename-case': 'off',
'unicorn/import-style': [
'error',
{ styles: { 'node:path': { named: true } } },
],
'unicorn/no-array-callback-reference': 'off', // reduces readability
'unicorn/no-array-reduce': 'off',
'unicorn/no-nested-ternary': 'off', // incompatible with prettier
'unicorn/no-null': 'off', // incompatible with TypeScript
'unicorn/no-zero-fractions': 'off', // deactivated to raise awareness of floating operations
'unicorn/number-literal-case': 'off', // incompatible with prettier
'unicorn/prefer-module': 'off',
'unicorn/prefer-ternary': 'off', // ternaries aren't always better
'unicorn/prefer-type-error': 'off',

// TODO @Shinigami92 2024-04-27: Potentially enable later
'unicorn/no-process-exit': 'off',
'unicorn/prefer-at': 'off',
'unicorn/prefer-native-coercion-functions': 'off',
'unicorn/prefer-string-replace-all': 'off',
'unicorn/prefer-top-level-await': 'off',
'unicorn/prevent-abbreviations': 'off',

'@typescript-eslint/array-type': [
'error',
{ default: 'array-simple', readonly: 'generic' },
Expand Down Expand Up @@ -108,6 +134,8 @@ module.exports = defineConfig({
{
files: ['test/migrations/*.js'],
rules: {
'unicorn/prefer-module': 'off',

'@typescript-eslint/no-throw-literal': 'off',
},
},
Expand Down
34 changes: 19 additions & 15 deletions bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ process.on('uncaughtException', (err) => {
function tryRequire<TModule = unknown>(moduleName: string): TModule | null {
try {
return require(moduleName);
} catch (err) {
} catch (error) {
if (
// @ts-expect-error: TS doesn't know about code property
err?.code !== 'MODULE_NOT_FOUND'
error?.code !== 'MODULE_NOT_FOUND'
) {
throw err;
throw error;
}

return null;
Expand Down Expand Up @@ -255,7 +255,7 @@ function readTsconfig() {

try {
const config = readFileSync(resolve(process.cwd(), tsconfigPath), {
encoding: 'utf-8',
encoding: 'utf8',
});
tsconfig = json5 ? json5.parse(config) : JSON.parse(config);

Expand All @@ -264,13 +264,15 @@ function readTsconfig() {
...tsconfig,
...tsconfig['ts-node'],
compilerOptions: {
// eslint-disable-next-line unicorn/no-useless-fallback-in-spread
...(tsconfig.compilerOptions ?? {}),
// eslint-disable-next-line unicorn/no-useless-fallback-in-spread
...(tsconfig['ts-node'].compilerOptions ?? {}),
},
};
}
} catch (err) {
console.error("Can't load tsconfig.json:", err);
} catch (error) {
console.error("Can't load tsconfig.json:", error);
}

const tsnode = tryRequire<typeof import('ts-node')>('ts-node');
Expand Down Expand Up @@ -440,9 +442,9 @@ VERBOSE ??= true;

if (action === 'create') {
// replaces spaces with dashes - should help fix some errors
let newMigrationName = argv._.length ? argv._.join('-') : '';
let newMigrationName = argv._.length > 0 ? argv._.join('-') : '';
// forces use of dashes in names - keep thing clean
newMigrationName = newMigrationName.replace(/[_ ]+/g, '-');
newMigrationName = newMigrationName.replace(/[ _]+/g, '-');

if (!newMigrationName) {
console.error("'migrationName' is required.");
Expand All @@ -463,8 +465,8 @@ if (action === 'create') {
console.log(format('Created migration -- %s', migrationPath));
process.exit(0);
})
.catch((err: unknown) => {
console.error(err);
.catch((error: unknown) => {
console.error(error);
process.exit(1);
});
} else if (action === 'up' || action === 'down' || action === 'redo') {
Expand Down Expand Up @@ -495,12 +497,12 @@ if (action === 'create') {
console.log('no lock');
}

const upDownArg = argv._.length ? argv._[0] : null;
const upDownArg = argv._.length > 0 ? argv._[0] : null;
let numMigrations: number;
let migrationName: string;

if (upDownArg !== null) {
const parsedUpDownArg = parseInt(`${upDownArg}`, 10);
const parsedUpDownArg = Number.parseInt(`${upDownArg}`, 10);
// eslint-disable-next-line eqeqeq
if (parsedUpDownArg == upDownArg) {
numMigrations = parsedUpDownArg;
Expand Down Expand Up @@ -537,12 +539,14 @@ if (action === 'create') {
}
: undefined),
},
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
dir: MIGRATIONS_DIR!,
ignorePattern: IGNORE_PATTERN,
schema: SCHEMA,
createSchema: CREATE_SCHEMA,
migrationsSchema: MIGRATIONS_SCHEMA,
createMigrationsSchema: CREATE_MIGRATIONS_SCHEMA,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
migrationsTable: MIGRATIONS_TABLE!,
count,
timestamp,
Expand All @@ -560,16 +564,16 @@ if (action === 'create') {
const promise =
action === 'redo'
? migrationRunner(options('down')).then(() =>
migrationRunner(options('up', Infinity, false))
migrationRunner(options('up', Number.POSITIVE_INFINITY, false))
)
: migrationRunner(options(action));
promise
.then(() => {
console.log('Migrations complete!');
process.exit(0);
})
.catch((err: unknown) => {
console.error(err);
.catch((error: unknown) => {
console.error(error);
process.exit(1);
});
} else {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"devDependencies": {
"@eslint-types/prettier": "5.1.3",
"@eslint-types/typescript-eslint": "7.5.0",
"@eslint-types/unicorn": "52.0.0",
"@types/config": "3.3.4",
"@types/node": "18.19.31",
"@types/pg": "8.11.5",
Expand All @@ -118,6 +119,7 @@
"eslint-define-config": "2.1.0",
"eslint-gitignore": "0.1.0",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-unicorn": "52.0.0",
"json5": "2.2.3",
"npm-run-all2": "6.1.2",
"pg": "8.11.5",
Expand Down

0 comments on commit b59dd3f

Please sign in to comment.