Skip to content

Commit 07e5c27

Browse files
committed
refactor: remove anys
1 parent 8c6ccf4 commit 07e5c27

12 files changed

+91
-54
lines changed

eslint.config.mjs

-6
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,4 @@ export default [
2424
"@typescript-eslint/no-require-imports": "off",
2525
},
2626
},
27-
{
28-
rules: {
29-
// fix these warnings
30-
"@typescript-eslint/no-explicit-any": "warn",
31-
},
32-
},
3327
];

src/harmony/harmony-factory.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function createHarmonyFactory(context: TsTransformPathsContext): HarmonyF
2323
} else if (TsFourSeven.predicate(context)) {
2424
return TsFourSeven.handler(context, prop);
2525
} else {
26-
return (<any>target)[prop];
26+
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expression of type 'string | symbol' can't be used to index type 'typeof import("typescript") | NodeFactory'.
27+
return target[prop];
2728
}
2829
},
2930
}) as HarmonyFactory;

src/harmony/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
// @formatter:off
55

66
// @prettier-ignore
7-
export type DownSampleTsTypes<TypeMap extends [any, any][], Tuple extends [...unknown[]]> = {
8-
[i in keyof Tuple]: Tuple[i] extends any[]
7+
export type DownSampleTsTypes<TypeMap extends [unknown, unknown][], Tuple extends [...unknown[]]> = {
8+
[i in keyof Tuple]: Tuple[i] extends unknown[]
99
? DownSampleTsTypes<TypeMap, Tuple[i]>
1010
: DownSampleTsType<TypeMap, Tuple[i]>;
1111
} & {
1212
length: Tuple["length"];
1313
};
1414

1515
// @prettier-ignore
16-
type DownSampleTsType<TypeMap extends [any, any][], T> =
17-
T extends Exclude<TypeMap[number][0], undefined> ? Extract<TypeMap[number], [T, any]>[1] : T;
16+
type DownSampleTsType<TypeMap extends [unknown, unknown][], T> =
17+
T extends Exclude<TypeMap[number][0], undefined> ? Extract<TypeMap[number], [T, unknown]>[1] : T;
1818

1919
// @formatter:on
2020
// endregion

src/harmony/versions/four-seven.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
111111
};
112112
}
113113
default: {
114-
return (...args: any) => (<any>factory)[prop](...args);
114+
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' type.
115+
return (...args) => factory[prop](...args);
115116
}
116117
}
117118
}
118119

119120
export function downSample<T extends [...unknown[]]>(...args: T): DownSampleTsTypes<TypeMap, T> {
120-
return <any>args;
121+
// @ts-expect-error TS(2322) FIXME: Type 'T' is not assignable to type 'DownSampleTsTypes<TypeMap, T>'.
122+
return args;
121123
}
122124

123125
// endregion

src/harmony/versions/three-eight.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
6666

6767
switch (prop) {
6868
case "updateCallExpression": {
69-
return (...args: any) => ts.updateCall.apply(void 0, args);
69+
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' type.
70+
return (...args) => ts.updateCall.apply(void 0, args);
7071
}
7172
case "updateImportClause": {
7273
return function (
@@ -143,13 +144,15 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
143144
};
144145
}
145146
default: {
146-
return (...args: any) => (<any>ts)[prop](...args);
147+
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' type.
148+
return (...args) => ts[prop](...args);
147149
}
148150
}
149151
}
150152

151153
export function downSample<T extends [...unknown[]]>(...args: T): DownSampleTsTypes<TypeMap, T> {
152-
return <any>args;
154+
// @ts-expect-error TS(2322) FIXME: Type 'T' is not assignable to type 'DownSampleTsTypes<TypeMap, T>'.
155+
return args;
153156
}
154157

155158
// endregion

src/utils/elide-import-export.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ export function elideImportOrExportDeclaration(
117117
importClause,
118118
newModuleSpecifier,
119119
// This will be changed in the next release of TypeScript, but by that point we can drop elision entirely
120-
(node as any).attributes || node.assertClause,
120+
// @ts-expect-error TS(2339) FIXME: Property 'attributes' does not exist on type 'ImportDeclaration'.
121+
node.attributes || node.assertClause,
121122
);
122123
else return undefined;
123124
} else {
@@ -150,7 +151,8 @@ export function elideImportOrExportDeclaration(
150151
exportClause,
151152
newModuleSpecifier,
152153
// This will be changed in the next release of TypeScript, but by that point we can drop elision entirely
153-
(node as any).attributes || node.assertClause,
154+
// @ts-expect-error TS(2339) FIXME: Property 'attributes' does not exist on type 'ExportDeclaration'.
155+
node.attributes || node.assertClause,
154156
)
155157
: undefined;
156158
}

src/utils/ts-helpers.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function createSyntheticEmitHost(
6868
!tsInstance.sys.useCaseSensitiveFileNames,
6969
),
7070
getCanonicalFileName,
71-
} as unknown as ts.EmitHost;
71+
} as ts.EmitHost;
7272
}
7373

7474
/** Get ts-node register info */
@@ -86,9 +86,8 @@ export function getTsNodeRegistrationProperties(tsInstance: typeof ts) {
8686
const { config, options } = global.process[tsNodeSymbol]!;
8787

8888
const { configFilePath } = config.options;
89-
const pcl = configFilePath
90-
? tsInstance.getParsedCommandLineOfConfigFile(configFilePath, {}, <any>tsInstance.sys)
91-
: void 0;
89+
// @ts-expect-error TS(2345) FIXME: Argument of type 'System' is not assignable to parameter of type 'ParseConfigFileHost'.
90+
const pcl = configFilePath ? tsInstance.getParsedCommandLineOfConfigFile(configFilePath, {}, tsInstance.sys) : void 0;
9291

9392
const fileNames = pcl?.fileNames || config.fileNames;
9493
const compilerOptions = Object.assign({}, config.options, options.compilerOptions, { outDir: pcl?.options.outDir });

test/tests/nx.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ describe(`NX Transformer`, () => {
1717
describe("Plugin", () => {
1818
let mockedTransformer: jest.SpyInstance;
1919

20-
const program: any = { x: 1 };
20+
const program = { x: 1 };
2121

2222
beforeAll(async () => {
23-
mockedTransformer = jest.spyOn(transformerModule, "default").mockReturnValue(<any>(() => {}));
23+
// @ts-expect-error TS(2345) FIXME: Argument of type '() => void' is not assignable to parameter of type '(transformationContext: TransformationContext) => (sourceFile: SourceFile) => SourceFile'.
24+
mockedTransformer = jest.spyOn(transformerModule, "default").mockReturnValue(() => {});
2425
});
2526
afterAll(() => {
2627
mockedTransformer.mockClear();
@@ -30,8 +31,9 @@ describe(`NX Transformer`, () => {
3031
});
3132

3233
test(`Before properly routes transform`, () => {
33-
const config: any = { a: 2 };
34+
const config = { a: 2 };
3435

36+
// @ts-expect-error TS(2559) FIXME: Type '{ a: number; }' has no properties in common with type 'Omit<TsTransformPathsConfig, "transform">'.
3537
nxTransformerPlugin.before(config, program);
3638

3739
expect(mockedTransformer).toHaveBeenCalledTimes(1);
@@ -43,8 +45,9 @@ describe(`NX Transformer`, () => {
4345
});
4446

4547
test(`After properly routes transform`, () => {
46-
const config: any = { a: 2, afterDeclarations: true };
48+
const config = { a: 2, afterDeclarations: true };
4749

50+
// @ts-expect-error TS(2345) FIXME: Argument of type '{ x: number; }' is not assignable to parameter of type 'Program'.
4851
nxTransformerPlugin.afterDeclarations(config, program);
4952

5053
expect(mockedTransformer).toHaveBeenCalledTimes(1);

test/tests/register.test.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ const configMap = Object.entries(configs).map(([label, cfg]) => {
2626
let hasAfterDeclarations: boolean = false;
2727
const transformers = [
2828
...[cfg].flat().map((c) => {
29-
if ((<any>c).before || !(<any>c).afterDeclarations) hasBefore = true;
30-
if ((<any>c).afterDeclarations) hasAfterDeclarations = true;
29+
// @ts-expect-error TS(2339) FIXME: Property 'before' does not exist on type '{} | { readonly before: true; } | { readonly afterDeclarations: true; } | {} | { readonly afterDeclarations: true; } | { readonly before: true; } | { readonly afterDeclarations: true; }'.
30+
if (c.before || !c.afterDeclarations) hasBefore = true;
31+
// @ts-expect-error TS(2339) FIXME: Property 'afterDeclarations' does not exist on type '{} | { readonly before: true; } | { readonly afterDeclarations: true; } | {} | { readonly afterDeclarations: true; } | { readonly before: true; } | { readonly afterDeclarations: true; }'.
32+
if (c.afterDeclarations) hasAfterDeclarations = true;
3133
return { transform: "typescript-transform-paths", ...c, ...pluginOptions } as PluginConfig;
3234
}),
3335
otherTransformer,
@@ -63,9 +65,10 @@ describe(`Register script`, () => {
6365
}
6466
});
6567
test(`Uses existing ts-node if found`, () => {
66-
const fakeInstance: any = {};
68+
const fakeInstance: unknown = {};
6769

6870
const originalTsNodeInstance = global.process[instanceSymbol];
71+
// @ts-expect-error TS(2322) FIXME: Type 'unknown' is not assignable to type 'Service | undefined'.
6972
global.process[instanceSymbol] = fakeInstance;
7073
let registerSpy: jest.SpyInstance | undefined;
7174
try {
@@ -128,15 +131,17 @@ describe(`Register script`, () => {
128131
"Existing Transformer Config Factory",
129132
"No Existing Transformers",
130133
] as const)(`%s`, (configKind) => {
131-
const fakeExistingTransformer = function fakeExistingTransformer(): any {};
132-
const fakeTransformer = function fakeTransformer(): any {};
134+
// @ts-expect-error TS(2355) FIXME: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
135+
const fakeExistingTransformer = function fakeExistingTransformer(): unknown {};
136+
// @ts-expect-error TS(2355) FIXME: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
137+
const fakeTransformer = function fakeTransformer(): unknown {};
133138
const fakeTransformerConfig = {
134139
before: [fakeExistingTransformer],
135140
after: [fakeExistingTransformer],
136141
afterDeclarations: [fakeExistingTransformer],
137142
};
138143
const transformerFactoryFn = jest.fn().mockReturnValue(fakeTransformerConfig);
139-
const fakeProgram: any = {};
144+
const fakeProgram: unknown = {};
140145

141146
let existingTransformers: CustomTransformers | ((p: Program) => CustomTransformers) | undefined;
142147
switch (configKind) {
@@ -145,6 +150,7 @@ describe(`Register script`, () => {
145150
break;
146151
}
147152
case "Existing Transformer Config": {
153+
// @ts-expect-error TS(2322) FIXME: Type '{ before: (() => unknown)[]; after: (() => unknown)[]; afterDeclarations: (() => unknown)[]; }' is not assignable to type 'CustomTransformers | ((p: Program) => CustomTransformers) | undefined'.
148154
existingTransformers = { ...fakeTransformerConfig };
149155
break;
150156
}
@@ -161,6 +167,7 @@ describe(`Register script`, () => {
161167
let mergedTransformers: CustomTransformers;
162168

163169
beforeAll(() => {
170+
// @ts-expect-error TS(2345) FIXME: Argument of type '() => unknown' is not assignable to parameter of type '(transformationContext: TransformationContext) => (sourceFile: SourceFile) => SourceFile'.
164171
mockTransformer = jest.spyOn(transformerModule, "default").mockReturnValue(fakeTransformer);
165172

166173
global.process[instanceSymbol] = void 0;
@@ -183,7 +190,8 @@ describe(`Register script`, () => {
183190

184191
mergedTransformers =
185192
typeof registerResult.transformers === "function"
186-
? registerResult.transformers(fakeProgram)
193+
? // @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to parameter of type 'Program'.
194+
registerResult.transformers(fakeProgram)
187195
: registerResult.transformers!;
188196
});
189197
afterAll(() => {

test/tests/transformer/general.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ describe(`Transformer -> General Tests`, () => {
5050
beforeAll(() => {
5151
transformed = transformedFiles[file];
5252
expected = {
53-
js: getExpected(<any>tsInstance, file, originalFiles[file].js, projectRoot),
54-
dts: getExpected(<any>tsInstance, file, originalFiles[file].dts, projectRoot),
53+
// @ts-expect-error TS(2345) FIXME: Argument of type 'typeof ts | typeof ts | typeof import("typescript")' is not assignable to parameter of type 'typeof import("typescript")'.
54+
js: getExpected(tsInstance, file, originalFiles[file].js, projectRoot),
55+
// @ts-expect-error TS(2345) FIXME: Argument of type 'typeof ts | typeof ts | typeof import("typescript")' is not assignable to parameter of type 'typeof import("typescript")'.
56+
dts: getExpected(tsInstance, file, originalFiles[file].dts, projectRoot),
5557
};
5658
});
5759

test/tests/transformer/specific.test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const baseConfig: TsTransformPathsConfig = { exclude: ["**/excluded/**", "exclud
1919

2020
/* Test Mapping */
2121
const modes = ["program", "manual", "ts-node"] as const;
22-
const testConfigs: { label: string; tsInstance: any; mode: (typeof modes)[number]; tsSpecifier: string }[] = [];
22+
const testConfigs: { label: string; tsInstance: unknown; mode: (typeof modes)[number]; tsSpecifier: string }[] = [];
2323
for (const cfg of tsModules)
2424
testConfigs.push(...modes.map((mode) => ({ label: cfg[0], tsInstance: cfg[1], mode, tsSpecifier: cfg[2] })));
2525

@@ -53,6 +53,7 @@ declare global {
5353

5454
describe(`Specific Tests`, () => {
5555
describe.each(testConfigs)(`TypeScript $label - Mode: $mode`, ({ tsInstance, mode, tsSpecifier }) => {
56+
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
5657
const tsVersion = +tsInstance.versionMajorMinor.split(".").slice(0, 2).join("");
5758
let normalEmit: EmittedFiles;
5859
let rootDirsEmit: EmittedFiles;
@@ -62,6 +63,7 @@ describe(`Specific Tests`, () => {
6263
switch (mode) {
6364
case "program": {
6465
const program = createTsProgram({
66+
// @ts-expect-error TS(2322) FIXME: Type 'unknown' is not assignable to type 'typeof import("typescript")'.
6567
tsInstance,
6668
tsConfigFile,
6769
pluginOptions: {
@@ -72,6 +74,7 @@ describe(`Specific Tests`, () => {
7274
normalEmit = getEmitResultFromProgram(program);
7375

7476
const rootDirsProgram = createTsProgram({
77+
// @ts-expect-error TS(2322) FIXME: Type 'unknown' is not assignable to type 'typeof import("typescript")'.
7578
tsInstance,
7679
tsConfigFile,
7780
pluginOptions: {
@@ -84,20 +87,24 @@ describe(`Specific Tests`, () => {
8487
}
8588
case "manual": {
8689
skipDts = true;
90+
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
8791
const pcl = tsInstance.getParsedCommandLineOfConfigFile(
8892
tsConfigFile,
8993
{},
90-
<any>tsInstance.sys,
94+
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
95+
<unknown>tsInstance.sys,
9196
)! as TS.ParsedCommandLine;
9297
normalEmit = getManualEmitResult({ ...baseConfig, useRootDirs: false }, tsInstance, pcl);
9398
rootDirsEmit = getManualEmitResult({ ...baseConfig, useRootDirs: true }, tsInstance, pcl);
9499
break;
95100
}
96101
case "ts-node": {
102+
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
97103
const pcl = tsInstance.getParsedCommandLineOfConfigFile(
98104
tsConfigFile,
99105
{},
100-
<any>tsInstance.sys,
106+
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
107+
<unknown>tsInstance.sys,
101108
)! as TS.ParsedCommandLine;
102109
skipDts = true;
103110
normalEmit = getTsNodeEmitResult({ ...baseConfig, useRootDirs: false }, pcl, tsSpecifier);

0 commit comments

Comments
 (0)