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

src/utils/resolve-path-update-node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export function resolvePathAndUpdateNode(
2222

2323
// Accommodate direct override via @transform-path tag
2424
if (tags.overridePath) {
25-
const transformedPath = !isURL(tags.overridePath)
26-
? maybeAddRelativeLocalPrefix(normalizePath(tags.overridePath))
27-
: tags.overridePath;
25+
const transformedPath = isURL(tags.overridePath)
26+
? tags.overridePath
27+
: maybeAddRelativeLocalPrefix(normalizePath(tags.overridePath));
2828
return updaterFn(factory.createStringLiteral(transformedPath));
2929
}
3030

@@ -63,7 +63,7 @@ export function resolvePathAndUpdateNode(
6363
if (targetNode.pos >= 0) {
6464
try {
6565
const trivia = targetNode.getFullText(sourceFile).slice(0, targetNode.getLeadingTriviaWidth(sourceFile));
66-
const regex = /^\s*\/\/\/?\s*@(transform-path|no-transform-path)(?:[^\S\r\n](.+?))?$/gim;
66+
const regex = /^\s*\/{2,3}\s*@(transform-path|no-transform-path)(?:[^\S\n\r](.+?))?$/gim;
6767

6868
for (let match = regex.exec(trivia); match; match = regex.exec(trivia))
6969
if (match[1]) commentTags.set(match[1], match[2]);

src/utils/ts-helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ts, { GetCanonicalFileName, SourceFile } from "typescript";
2-
import path from "path";
2+
import path from "node:path";
33
import { VisitorContext } from "../types";
44
import type { REGISTER_INSTANCE } from "ts-node";
55

@@ -78,10 +78,10 @@ export function getTsNodeRegistrationProperties(tsInstance: typeof ts) {
7878
// eslint-disable-next-line @typescript-eslint/no-require-imports
7979
tsNodeSymbol = require("ts-node")?.["REGISTER_INSTANCE"];
8080
} catch {
81-
return undefined;
81+
return;
8282
}
8383

84-
if (!global.process[tsNodeSymbol]) return undefined;
84+
if (!global.process[tsNodeSymbol]) return;
8585

8686
const { config, options } = global.process[tsNodeSymbol]!;
8787

test/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import ts from "typescript";
22
import tsThree from "typescript-3";
33
import tsFourSeven from "typescript-4.7";
4-
import path from "path";
5-
6-
export { ts };
4+
import path from "node:path";
75

86
export const tsModules = <const>[
97
["3.6.5", tsThree, "typescript-3"],
@@ -16,3 +14,5 @@ export const transformerPath = require.resolve("typescript-transform-paths");
1614
export const builtTransformerPath = require.resolve("typescript-transform-paths");
1715

1816
Error.stackTraceLimit = 120;
17+
18+
export { default as ts } from "typescript";

test/jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { JestConfigWithTsJest } from "ts-jest";
33
const config: JestConfigWithTsJest = {
44
testEnvironment: "node",
55
preset: "ts-jest",
6-
testRegex: ".*(test|spec)\\.tsx?$",
6+
testRegex: String.raw`.*(test|spec)\.tsx?$`,
77
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
88
};
99

test/tests/extras.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createTsProgram, getEmitResultFromProgram, ModuleNotFoundError } from "../utils";
22
import { projectsPaths } from "../config";
3-
import path from "path";
3+
import path from "node:path";
44
import ts from "typescript";
55
import * as config from "../config";
6-
import { execSync } from "child_process";
6+
import { execSync } from "node:child_process";
77

88
/* ****************************************************************************************************************** *
99
* Tests

test/tests/project-ref.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// noinspection ES6UnusedImports
2-
import * as path from "path";
2+
import * as path from "node:path";
33
import { createTsSolutionBuilder, EmittedFiles } from "../utils";
44
import { projectsPaths, ts } from "../config";
55

test/tests/register.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ const configs = {
2424
const configMap = Object.entries(configs).map(([label, cfg]) => {
2525
let hasBefore: boolean = false;
2626
let hasAfterDeclarations: boolean = false;
27-
const transformers = [cfg]
28-
.flat()
29-
.map((c) => {
27+
const transformers = [
28+
...[cfg].flat().map((c) => {
3029
if ((<any>c).before || !(<any>c).afterDeclarations) hasBefore = true;
3130
if ((<any>c).afterDeclarations) hasAfterDeclarations = true;
3231
return { transform: "typescript-transform-paths", ...c, ...pluginOptions } as PluginConfig;
33-
})
34-
.concat([otherTransformer]);
32+
}),
33+
otherTransformer,
34+
] as PluginConfig[];
3535

3636
return { label, transformers, hasBefore, hasAfterDeclarations };
3737
});
@@ -140,14 +140,17 @@ describe(`Register script`, () => {
140140

141141
let existingTransformers: CustomTransformers | ((p: Program) => CustomTransformers) | undefined;
142142
switch (configKind) {
143-
case "Existing Transformer Config Factory":
143+
case "Existing Transformer Config Factory": {
144144
existingTransformers = transformerFactoryFn;
145145
break;
146-
case "Existing Transformer Config":
146+
}
147+
case "Existing Transformer Config": {
147148
existingTransformers = { ...fakeTransformerConfig };
148149
break;
149-
case "No Existing Transformers":
150+
}
151+
case "No Existing Transformers": {
150152
existingTransformers = void 0;
153+
}
151154
}
152155

153156
describe.each(configMap)(`$label`, ({ transformers, hasBefore, hasAfterDeclarations }) => {

test/tests/transformer/general.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// noinspection ES6UnusedImports
2-
import * as path from "path";
2+
import * as path from "node:path";
33
import { createTsProgram, EmittedFiles, getEmitResultFromProgram } from "../../utils";
44
import { ts, tsModules, projectsPaths } from "../../config";
55

@@ -15,8 +15,8 @@ const makeRelative = (tsInstance: typeof ts, fileName: string, p: string, rootDi
1515

1616
const getExpected = (tsInstance: typeof ts, fileName: string, original: string, rootDir: string): string =>
1717
original
18-
.replace(/"@(.*)"/g, (_, p) => makeRelative(tsInstance, fileName, p, rootDir))
19-
.replace(/"#utils\/(.*)"/g, (_, p) =>
18+
.replaceAll(/"@(.*)"/g, (_, p) => makeRelative(tsInstance, fileName, p, rootDir))
19+
.replaceAll(/"#utils\/(.*)"/g, (_, p) =>
2020
makeRelative(tsInstance, fileName, path.join(p === "hello" ? "secondary" : "utils", p), rootDir),
2121
)
2222
.replace('"path"', '"https://external.url/path.js"')

0 commit comments

Comments
 (0)