Skip to content

Commit 44ad2e0

Browse files
committed
refactor: add eslint-plugin-unicorn
1 parent 1cddaac commit 44ad2e0

21 files changed

+582
-62
lines changed

eslint.config.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
// @ts-check
12
import globals from "globals";
23
import pluginJs from "@eslint/js";
34
import tseslint from "typescript-eslint";
5+
import eslintPluginUnicorn from "eslint-plugin-unicorn";
46

57
export default [
68
{ files: ["**/*.{js,mjs,cjs,ts}"] },
79
{ ignores: ["**/dist/", "test/projects/"] },
810
{ languageOptions: { globals: globals.node } },
911
pluginJs.configs.recommended,
1012
...tseslint.configs.recommended,
13+
eslintPluginUnicorn.configs["flat/recommended"],
1114
{
1215
rules: {
1316
"@typescript-eslint/no-empty-object-type": ["error", { allowInterfaces: "with-single-extends" }],
@@ -25,7 +28,21 @@ export default [
2528
},
2629
{
2730
rules: {
31+
// fix these warnings
2832
"@typescript-eslint/no-explicit-any": "warn",
33+
"unicorn/consistent-function-scoping": "warn",
34+
"unicorn/explicit-length-check": "warn",
35+
"unicorn/import-style": "warn",
36+
"unicorn/no-array-reduce": "warn",
37+
"unicorn/no-nested-ternary": "warn",
38+
"unicorn/prefer-regexp-test": "warn",
39+
"unicorn/prefer-string-slice": "warn",
40+
// disable strict rules/not applicable
41+
"unicorn/empty-brace-spaces": "off", // conflict with prettier
42+
"unicorn/no-array-callback-reference": "off",
43+
"unicorn/prefer-module": "off",
44+
"unicorn/prefer-ternary": "off",
45+
"unicorn/prevent-abbreviations": "off",
2946
},
3047
},
3148
];

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@types/typescript-4.7": "npm:[email protected]",
7373
"changelogen": "^0.5.5",
7474
"eslint": "9.x",
75+
"eslint-plugin-unicorn": "^55.0.0",
7576
"globals": "^15.9.0",
7677
"prettier": "^3.3.3",
7778
"prettier-plugin-jsdoc": "^1.3.0",

src/harmony/versions/four-seven.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
4646
const factory = context.tsFactory as unknown as TsFourSevenModule.NodeFactory;
4747

4848
switch (prop) {
49-
case "updateImportDeclaration":
49+
case "updateImportDeclaration": {
5050
return function (
5151
node: ImportDeclaration,
5252
_modifiers: readonly Modifier[] | undefined,
@@ -70,7 +70,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
7070
dsAssertClause,
7171
);
7272
};
73-
case "updateExportDeclaration":
73+
}
74+
case "updateExportDeclaration": {
7475
return function (
7576
node: ExportDeclaration,
7677
_modifiers: readonly Modifier[] | undefined,
@@ -96,7 +97,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
9697
dsAssertClause,
9798
);
9899
};
99-
case "updateModuleDeclaration":
100+
}
101+
case "updateModuleDeclaration": {
100102
return function (
101103
node: ModuleDeclaration,
102104
_modifiers: readonly Modifier[] | undefined,
@@ -107,8 +109,10 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
107109

108110
return factory.updateModuleDeclaration(dsNode, dsNode.decorators, dsNode.modifiers, dsName, dsBody);
109111
};
110-
default:
112+
}
113+
default: {
111114
return (...args: any) => (<any>factory)[prop](...args);
115+
}
112116
}
113117
}
114118

src/harmony/versions/three-eight.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
6565
const ts = context.tsInstance as unknown as typeof TsThreeEightModule;
6666

6767
switch (prop) {
68-
case "updateCallExpression":
68+
case "updateCallExpression": {
6969
return (...args: any) => ts.updateCall.apply(void 0, args);
70-
case "updateImportClause":
70+
}
71+
case "updateImportClause": {
7172
return function (
7273
node: ImportClause,
7374
_isTypeOnly: boolean,
@@ -77,7 +78,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
7778
// @ts-expect-error TODO investigate type issue
7879
return ts.updateImportClause.apply(void 0, downSample(node, name, namedBindings));
7980
};
80-
case "updateImportDeclaration":
81+
}
82+
case "updateImportDeclaration": {
8183
return function (
8284
node: ImportDeclaration,
8385
_modifiers: readonly Modifier[] | undefined,
@@ -94,7 +96,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
9496
dsModuleSpecifier,
9597
);
9698
};
97-
case "updateExportDeclaration":
99+
}
100+
case "updateExportDeclaration": {
98101
return function (
99102
node: ExportDeclaration,
100103
_modifiers: readonly Modifier[] | undefined,
@@ -112,7 +115,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
112115
dsNode.isTypeOnly,
113116
);
114117
};
115-
case "updateModuleDeclaration":
118+
}
119+
case "updateModuleDeclaration": {
116120
return function (
117121
node: ModuleDeclaration,
118122
_modifiers: readonly Modifier[] | undefined,
@@ -123,7 +127,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
123127

124128
return ts.updateModuleDeclaration(dsNode, dsNode.decorators, dsNode.modifiers, dsName, dsBody);
125129
};
126-
case "updateImportTypeNode":
130+
}
131+
case "updateImportTypeNode": {
127132
return function (
128133
node: ImportTypeNode,
129134
argument: TypeNode,
@@ -136,8 +141,10 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
136141

137142
return ts.updateImportTypeNode(dsNode, dsArgument, dsQualifier, dsTypeArguments, isTypeOf);
138143
};
139-
default:
144+
}
145+
default: {
140146
return (...args: any) => (<any>ts)[prop](...args);
147+
}
141148
}
142149
}
143150

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import transformer from "./transformer";
2-
export default transformer;
3-
41
export type { TsTransformPathsConfig } from "./types";
52
export { register } from "./register";
63
export { nxTransformerPlugin } from "./plugins";
4+
5+
export { default } from "./transformer";

src/plugins/nx-transformer-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export const before: NxTransformerFactory = (pluginConfig, program) =>
3333
pluginConfig?.afterDeclarations ? voidTransformer : transformer(program, { ...pluginConfig });
3434

3535
export const afterDeclarations: NxTransformerFactory = (pluginConfig, program) =>
36-
!pluginConfig?.afterDeclarations ? voidTransformer : transformer(program, { ...pluginConfig });
36+
pluginConfig?.afterDeclarations ? transformer(program, { ...pluginConfig }) : voidTransformer;

src/transformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from "path";
1+
import path from "node:path";
22
import ts, { CompilerOptions } from "typescript";
33
import { RunMode, TsNodeState, TsTransformPathsConfig, TsTransformPathsContext, VisitorContext } from "./types";
44
import { nodeVisitor } from "./visitor";
@@ -52,7 +52,7 @@ function getTsProperties(args: Parameters<typeof transformer>) {
5252
tsNodeState === TsNodeState.Full
5353
? compilerOptions!
5454
: {
55-
...(program?.getCompilerOptions() ?? {}),
55+
...program?.getCompilerOptions(),
5656
...tsNodeProps!.compilerOptions,
5757
};
5858
} else {

src/utils/general-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import url from "url";
2-
import path from "path";
1+
import url from "node:url";
2+
import path from "node:path";
33

44
/* ****************************************************************************************************************** *
55
* General Utilities & Helpers

src/utils/get-relative-path.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import fs from "fs";
2-
import * as os from "os";
3-
import path from "path";
1+
import fs from "node:fs";
2+
import * as os from "node:os";
3+
import path from "node:path";
44

55
/* ****************************************************************************************************************** */
66
// region: Locals
@@ -21,7 +21,7 @@ function tryRmFile(fileName: string) {
2121
}
2222

2323
function getIsFsCaseSensitive() {
24-
if (isCaseSensitiveFilesystem != null) return isCaseSensitiveFilesystem;
24+
if (isCaseSensitiveFilesystem != undefined) return isCaseSensitiveFilesystem;
2525

2626
for (let i = 0; i < 1000; i++) {
2727
const tmpFileName = path.join(os.tmpdir(), `tstp~${i}.tmp`);

src/utils/resolve-module-name.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VisitorContext } from "../types";
22
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
3-
import * as path from "path";
3+
import * as path from "node:path";
44
import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript";
55
import { getOutputDirForSourceFile } from "./ts-helpers";
66
import { getRelativePath } from "./get-relative-path";
@@ -32,7 +32,7 @@ function getPathDetail(moduleName: string, resolvedModule: ResolvedModuleFull) {
3232
const resolvedBaseNameNoExtension = resolvedBaseName && removeFileExtension(resolvedBaseName);
3333
const resolvedExtName = resolvedBaseName && path.extname(resolvedFileName);
3434

35-
let baseName = !implicitPackageIndex ? path.basename(moduleName) : void 0;
35+
let baseName = implicitPackageIndex ? void 0 : path.basename(moduleName);
3636
let baseNameNoExtension = baseName && removeFileExtension(baseName);
3737
let extName = baseName && path.extname(moduleName);
3838

0 commit comments

Comments
 (0)