Skip to content

Commit

Permalink
refactor: remove anys
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpza committed Sep 2, 2024
1 parent 8c6ccf4 commit 07e5c27
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 54 deletions.
6 changes: 0 additions & 6 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,4 @@ export default [
"@typescript-eslint/no-require-imports": "off",
},
},
{
rules: {
// fix these warnings
"@typescript-eslint/no-explicit-any": "warn",
},
},
];
3 changes: 2 additions & 1 deletion src/harmony/harmony-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function createHarmonyFactory(context: TsTransformPathsContext): HarmonyF
} else if (TsFourSeven.predicate(context)) {
return TsFourSeven.handler(context, prop);
} else {
return (<any>target)[prop];
// @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'.
return target[prop];
}
},
}) as HarmonyFactory;
Expand Down
8 changes: 4 additions & 4 deletions src/harmony/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
// @formatter:off

// @prettier-ignore
export type DownSampleTsTypes<TypeMap extends [any, any][], Tuple extends [...unknown[]]> = {
[i in keyof Tuple]: Tuple[i] extends any[]
export type DownSampleTsTypes<TypeMap extends [unknown, unknown][], Tuple extends [...unknown[]]> = {
[i in keyof Tuple]: Tuple[i] extends unknown[]
? DownSampleTsTypes<TypeMap, Tuple[i]>
: DownSampleTsType<TypeMap, Tuple[i]>;
} & {
length: Tuple["length"];
};

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

// @formatter:on
// endregion
6 changes: 4 additions & 2 deletions src/harmony/versions/four-seven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
};
}
default: {
return (...args: any) => (<any>factory)[prop](...args);
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' type.
return (...args) => factory[prop](...args);
}
}
}

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

// endregion
9 changes: 6 additions & 3 deletions src/harmony/versions/three-eight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)

switch (prop) {
case "updateCallExpression": {
return (...args: any) => ts.updateCall.apply(void 0, args);
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' type.
return (...args) => ts.updateCall.apply(void 0, args);
}
case "updateImportClause": {
return function (
Expand Down Expand Up @@ -143,13 +144,15 @@ export function handler(context: TsTransformPathsContext, prop: string | symbol)
};
}
default: {
return (...args: any) => (<any>ts)[prop](...args);
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' type.
return (...args) => ts[prop](...args);
}
}
}

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

// endregion
6 changes: 4 additions & 2 deletions src/utils/elide-import-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export function elideImportOrExportDeclaration(
importClause,
newModuleSpecifier,
// This will be changed in the next release of TypeScript, but by that point we can drop elision entirely
(node as any).attributes || node.assertClause,
// @ts-expect-error TS(2339) FIXME: Property 'attributes' does not exist on type 'ImportDeclaration'.
node.attributes || node.assertClause,
);
else return undefined;
} else {
Expand Down Expand Up @@ -150,7 +151,8 @@ export function elideImportOrExportDeclaration(
exportClause,
newModuleSpecifier,
// This will be changed in the next release of TypeScript, but by that point we can drop elision entirely
(node as any).attributes || node.assertClause,
// @ts-expect-error TS(2339) FIXME: Property 'attributes' does not exist on type 'ExportDeclaration'.
node.attributes || node.assertClause,
)
: undefined;
}
Expand Down
7 changes: 3 additions & 4 deletions src/utils/ts-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function createSyntheticEmitHost(
!tsInstance.sys.useCaseSensitiveFileNames,
),
getCanonicalFileName,
} as unknown as ts.EmitHost;
} as ts.EmitHost;
}

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

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

const fileNames = pcl?.fileNames || config.fileNames;
const compilerOptions = Object.assign({}, config.options, options.compilerOptions, { outDir: pcl?.options.outDir });
Expand Down
11 changes: 7 additions & 4 deletions test/tests/nx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ describe(`NX Transformer`, () => {
describe("Plugin", () => {
let mockedTransformer: jest.SpyInstance;

const program: any = { x: 1 };
const program = { x: 1 };

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

test(`Before properly routes transform`, () => {
const config: any = { a: 2 };
const config = { a: 2 };

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

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

test(`After properly routes transform`, () => {
const config: any = { a: 2, afterDeclarations: true };
const config = { a: 2, afterDeclarations: true };

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

expect(mockedTransformer).toHaveBeenCalledTimes(1);
Expand Down
22 changes: 15 additions & 7 deletions test/tests/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const configMap = Object.entries(configs).map(([label, cfg]) => {
let hasAfterDeclarations: boolean = false;
const transformers = [
...[cfg].flat().map((c) => {
if ((<any>c).before || !(<any>c).afterDeclarations) hasBefore = true;
if ((<any>c).afterDeclarations) hasAfterDeclarations = true;
// @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; }'.
if (c.before || !c.afterDeclarations) hasBefore = true;
// @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; }'.
if (c.afterDeclarations) hasAfterDeclarations = true;
return { transform: "typescript-transform-paths", ...c, ...pluginOptions } as PluginConfig;
}),
otherTransformer,
Expand Down Expand Up @@ -63,9 +65,10 @@ describe(`Register script`, () => {
}
});
test(`Uses existing ts-node if found`, () => {
const fakeInstance: any = {};
const fakeInstance: unknown = {};

const originalTsNodeInstance = global.process[instanceSymbol];
// @ts-expect-error TS(2322) FIXME: Type 'unknown' is not assignable to type 'Service | undefined'.
global.process[instanceSymbol] = fakeInstance;
let registerSpy: jest.SpyInstance | undefined;
try {
Expand Down Expand Up @@ -128,15 +131,17 @@ describe(`Register script`, () => {
"Existing Transformer Config Factory",
"No Existing Transformers",
] as const)(`%s`, (configKind) => {
const fakeExistingTransformer = function fakeExistingTransformer(): any {};
const fakeTransformer = function fakeTransformer(): any {};
// @ts-expect-error TS(2355) FIXME: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
const fakeExistingTransformer = function fakeExistingTransformer(): unknown {};
// @ts-expect-error TS(2355) FIXME: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
const fakeTransformer = function fakeTransformer(): unknown {};
const fakeTransformerConfig = {
before: [fakeExistingTransformer],
after: [fakeExistingTransformer],
afterDeclarations: [fakeExistingTransformer],
};
const transformerFactoryFn = jest.fn().mockReturnValue(fakeTransformerConfig);
const fakeProgram: any = {};
const fakeProgram: unknown = {};

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

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

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

mergedTransformers =
typeof registerResult.transformers === "function"
? registerResult.transformers(fakeProgram)
? // @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to parameter of type 'Program'.
registerResult.transformers(fakeProgram)
: registerResult.transformers!;
});
afterAll(() => {
Expand Down
6 changes: 4 additions & 2 deletions test/tests/transformer/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ describe(`Transformer -> General Tests`, () => {
beforeAll(() => {
transformed = transformedFiles[file];
expected = {
js: getExpected(<any>tsInstance, file, originalFiles[file].js, projectRoot),
dts: getExpected(<any>tsInstance, file, originalFiles[file].dts, projectRoot),
// @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")'.
js: getExpected(tsInstance, file, originalFiles[file].js, projectRoot),
// @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")'.
dts: getExpected(tsInstance, file, originalFiles[file].dts, projectRoot),
};
});

Expand Down
13 changes: 10 additions & 3 deletions test/tests/transformer/specific.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const baseConfig: TsTransformPathsConfig = { exclude: ["**/excluded/**", "exclud

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

Expand Down Expand Up @@ -53,6 +53,7 @@ declare global {

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

const rootDirsProgram = createTsProgram({
// @ts-expect-error TS(2322) FIXME: Type 'unknown' is not assignable to type 'typeof import("typescript")'.
tsInstance,
tsConfigFile,
pluginOptions: {
Expand All @@ -84,20 +87,24 @@ describe(`Specific Tests`, () => {
}
case "manual": {
skipDts = true;
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
const pcl = tsInstance.getParsedCommandLineOfConfigFile(
tsConfigFile,
{},
<any>tsInstance.sys,
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
<unknown>tsInstance.sys,
)! as TS.ParsedCommandLine;
normalEmit = getManualEmitResult({ ...baseConfig, useRootDirs: false }, tsInstance, pcl);
rootDirsEmit = getManualEmitResult({ ...baseConfig, useRootDirs: true }, tsInstance, pcl);
break;
}
case "ts-node": {
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
const pcl = tsInstance.getParsedCommandLineOfConfigFile(
tsConfigFile,
{},
<any>tsInstance.sys,
// @ts-expect-error TS(18046) FIXME: 'tsInstance' is of type 'unknown'.
<unknown>tsInstance.sys,
)! as TS.ParsedCommandLine;
skipDts = true;
normalEmit = getTsNodeEmitResult({ ...baseConfig, useRootDirs: false }, pcl, tsSpecifier);
Expand Down
Loading

0 comments on commit 07e5c27

Please sign in to comment.