Skip to content

Commit 1631115

Browse files
authored
infra(tsconfig): noImplicitAny (#2562)
1 parent 60dcfe7 commit 1631115

File tree

12 files changed

+49
-36
lines changed

12 files changed

+49
-36
lines changed

.prettierrc.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { Options } from 'prettier';
2+
3+
declare const options: Options;
4+
export = options;

scripts/apidoc/typedoc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export function selectApiModules(
8888
.getChildrenByKind(ReflectionKind.Class)
8989
.filter(
9090
(module) =>
91-
faker[extractModuleFieldName(module)] != null || includeTestModules
91+
faker[extractModuleFieldName(module) as keyof typeof faker] != null ||
92+
includeTestModules
9293
);
9394
}
9495

scripts/generate-locales.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import {
2323
} from 'node:fs';
2424
import { dirname, resolve } from 'node:path';
2525
import { fileURLToPath } from 'node:url';
26-
import type { Options } from 'prettier';
27-
import { format } from 'prettier';
28-
import options from '../.prettierrc.js';
2926
import type { LocaleDefinition, MetadataDefinition } from '../src/definitions';
27+
import { keys } from '../src/internal/keys';
28+
import { formatMarkdown, formatTypescript } from './apidoc/format';
3029

3130
// Constants
3231

@@ -78,9 +77,6 @@ const definitionsTypes: DefinitionType = {
7877
word: 'WordDefinition',
7978
};
8079

81-
const prettierTsOptions: Options = { ...options, parser: 'typescript' };
82-
const prettierMdOptions: Options = { ...options, parser: 'markdown' };
83-
8480
const scriptCommand = 'pnpm run generate:locales';
8581

8682
const autoGeneratedCommentHeader = `/*
@@ -154,7 +150,7 @@ async function generateLocaleFile(locale: string): Promise<void> {
154150
});
155151
`;
156152

157-
content = await format(content, prettierTsOptions);
153+
content = await formatTypescript(content);
158154
writeFileSync(resolve(pathLocale, `${locale}.ts`), content);
159155
}
160156

@@ -195,7 +191,7 @@ async function generateLocalesIndexFile(
195191

196192
writeFileSync(
197193
resolve(path, 'index.ts'),
198-
await format(content.join('\n'), prettierTsOptions)
194+
await formatTypescript(content.join('\n'))
199195
);
200196
}
201197

@@ -301,7 +297,7 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
301297
}
302298

303299
const result = {} as T;
304-
for (const key of Object.keys(localeData)) {
300+
for (const key of keys(localeData)) {
305301
result[key] = normalizeDataRecursive(localeData[key]);
306302
}
307303

@@ -367,7 +363,7 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
367363
// In the long term we should probably define a whether we want those in the files at all.
368364
const newContent = fileContentPreData + JSON.stringify(localeData);
369365

370-
writeFileSync(filePath, await format(newContent, prettierTsOptions));
366+
writeFileSync(filePath, await formatTypescript(newContent));
371367
}
372368

373369
// Start of actual logic
@@ -444,7 +440,7 @@ async function main(): Promise<void> {
444440
} as const;
445441
`;
446442

447-
localeIndexContent = await format(localeIndexContent, prettierTsOptions);
443+
localeIndexContent = await formatTypescript(localeIndexContent);
448444
writeFileSync(pathLocaleIndex, localeIndexContent);
449445

450446
// src/locales/index.ts
@@ -455,12 +451,12 @@ async function main(): Promise<void> {
455451
${localesIndexExports}
456452
`;
457453

458-
localesIndexContent = await format(localesIndexContent, prettierTsOptions);
454+
localesIndexContent = await formatTypescript(localesIndexContent);
459455
writeFileSync(pathLocalesIndex, localesIndexContent);
460456

461457
// docs/guide/localization.md
462458

463-
localizationLocales = await format(localizationLocales, prettierMdOptions);
459+
localizationLocales = await formatMarkdown(localizationLocales);
464460

465461
let localizationContent = readFileSync(pathDocsGuideLocalization, 'utf8');
466462
localizationContent = localizationContent.replace(

src/internal/keys.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Specialized version of `Object.keys()` which preserves the type information of the keys.
3+
*
4+
* Please note that the type information might be inaccurate for subtypes of the argument type
5+
* and thus should only be used to cover the property access of the object.
6+
*
7+
* @internal
8+
*
9+
* @param obj The object to get the keys of.
10+
*/
11+
export function keys<T extends object>(obj: T): Array<keyof T> {
12+
return Object.keys(obj) as Array<keyof T>;
13+
}

test/all-functional.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22
import type { allLocales, Faker, RandomModule } from '../src';
33
import { allFakers, fakerEN } from '../src';
4+
import { keys } from '../src/internal/keys';
45

56
const IGNORED_MODULES = new Set([
67
'rawDefinitions',
@@ -35,11 +36,7 @@ function isTestableModule(moduleName: string): moduleName is keyof Faker {
3536
}
3637

3738
function getMethodNamesOf(module: object): string[] {
38-
return Object.keys(module).filter(isMethodOf(module));
39-
}
40-
41-
function isMethodOf(module: object): (method: string) => boolean {
42-
return (method: string) => typeof module[method] === 'function';
39+
return keys(module).filter((method) => typeof module[method] === 'function');
4340
}
4441

4542
type SkipConfig<TModule> = Partial<
@@ -81,6 +78,7 @@ function isWorkingLocaleForMethod(
8178
method: string,
8279
locale: string
8380
): boolean {
81+
// @ts-expect-error: We don't have types for the dynamic access
8482
const broken = BROKEN_LOCALE_METHODS[module]?.[method] ?? [];
8583
return broken !== '*' && !broken.includes(locale);
8684
}
@@ -104,6 +102,7 @@ describe('BROKEN_LOCALE_METHODS test', () => {
104102
it('should not contain obsolete configuration (methods)', () => {
105103
const existingMethods = modules[module];
106104
const configuredMethods = Object.keys(
105+
// @ts-expect-error: We don't have types for the dynamic access
107106
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
108107
BROKEN_LOCALE_METHODS[module] ?? {}
109108
);
@@ -129,6 +128,7 @@ describe('functional tests', () => {
129128
const testAssertion = () => {
130129
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
131130
faker.seed(1);
131+
// @ts-expect-error: We don't have types for the dynamic access
132132
const result = faker[module][meth]();
133133

134134
if (meth === 'boolean') {

test/faker.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { SpyInstance } from 'vitest';
22
import { describe, expect, it, vi } from 'vitest';
33
import { faker, Faker } from '../src';
44
import { FakerError } from '../src/errors/faker-error';
5+
import { keys } from '../src/internal/keys';
56

67
describe('faker', () => {
78
it('should throw error if no locales passed', () => {
@@ -13,7 +14,7 @@ describe('faker', () => {
1314
});
1415

1516
it('should not log anything on startup', () => {
16-
const spies: SpyInstance[] = Object.keys(console)
17+
const spies: SpyInstance[] = keys(console)
1718
.filter((key) => typeof console[key] === 'function')
1819
.map((methodName) =>
1920
vi.spyOn(console, methodName as keyof typeof console)

test/locale-imports.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { describe, expect, it } from 'vitest';
22
import type { Faker } from '../src';
33
import { allLocales } from '../src';
4+
import { keys } from '../src/internal/keys';
45

5-
describe.each(Object.keys(allLocales))('locale imports', (locale) => {
6+
describe.each(keys(allLocales))('locale imports', (locale) => {
67
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
78
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
89
const { faker } = require(`../dist/cjs/locale/${locale}`) as {
@@ -12,7 +13,7 @@ describe.each(Object.keys(allLocales))('locale imports', (locale) => {
1213
expect(faker).toBeDefined();
1314
expect(faker.string.alpha()).toBeTypeOf('string');
1415
expect(faker.definitions.metadata.title).toBe(
15-
allLocales[locale].metadata.title
16+
allLocales[locale].metadata?.title
1617
);
1718
});
1819

@@ -24,12 +25,12 @@ describe.each(Object.keys(allLocales))('locale imports', (locale) => {
2425
expect(faker).toBeDefined();
2526
expect(faker.string.alpha()).toBeTypeOf('string');
2627
expect(faker.definitions.metadata.title).toBe(
27-
allLocales[locale].metadata.title
28+
allLocales[locale].metadata?.title
2829
);
2930
});
3031

3132
it('should have complete metadata values', () => {
32-
const metadata = allLocales[locale].metadata;
33+
const metadata = allLocales[locale].metadata ?? {};
3334
expect(metadata.title).toBeTypeOf('string');
3435
expect(metadata.code).toBeTypeOf('string');
3536
expect(metadata.code).toEqual(locale);

test/modules/helpers.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22
import { faker, FakerError } from '../../src';
33
import { luhnCheck } from '../../src/modules/helpers/luhn-check';
4+
import type { RecordKey } from '../../src/modules/helpers/unique';
45
import { seededTests } from '../support/seeded-runs';
56
import { times } from './../support/times';
67
import './../vitest-extensions';
@@ -1295,8 +1296,9 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`)
12951296
const maxTime = 49;
12961297
const maxRetries = 49;
12971298
const currentIterations = 0;
1298-
const exclude = [];
1299-
const compare = (obj, key) => (obj[key] === undefined ? -1 : 0);
1299+
const exclude: string[] = [];
1300+
const compare = (obj: Record<RecordKey, RecordKey>, key: RecordKey) =>
1301+
obj[key] === undefined ? -1 : 0;
13001302

13011303
const options = {
13021304
startTime,
@@ -1318,7 +1320,7 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`)
13181320
});
13191321

13201322
it('should be possible to pass a user-specific store', () => {
1321-
const store = {};
1323+
const store: Record<string, string> = {};
13221324

13231325
const method = () => 'with conflict: 0';
13241326

test/modules/image.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe('image', () => {
257257
'objects',
258258
'people',
259259
'technology',
260-
];
260+
] satisfies Array<keyof typeof faker.image.unsplash>;
261261

262262
describe.each(categories)(`%s()`, (category) => {
263263
it(`should return a random ${category} image url`, () => {

test/simple-faker.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { SpyInstance } from 'vitest';
22
import { describe, expect, it, vi } from 'vitest';
33
import { SimpleFaker, simpleFaker } from '../src';
4+
import { keys } from '../src/internal/keys';
45

56
describe('simpleFaker', () => {
67
it('should not log anything on startup', () => {
7-
const spies: SpyInstance[] = Object.keys(console)
8+
const spies: SpyInstance[] = keys(console)
89
.filter((key) => typeof console[key] === 'function')
9-
.map((methodName) =>
10-
vi.spyOn(console, methodName as keyof typeof console)
11-
);
10+
.map((methodName) => vi.spyOn(console, methodName));
1211

1312
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module -- Using import() requires types being build but the CI / TS-Check runs without them.
1413
require('..').simpleFaker;

tsconfig.build.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"outDir": "dist/types",
99

1010
// This negates what is set in the extended tsconfig.json
11-
"noImplicitAny": true,
1211
"skipLibCheck": false,
1312
"allowSyntheticDefaultImports": false,
1413
"resolveJsonModule": false

tsconfig.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
"declaration": true,
99
"stripInternal": true,
1010

11-
// We need to disable these for now, and need to tackle them in another PR
12-
"noImplicitAny": false,
13-
1411
// These are configs specifically for !build and have to be reverted in the tsconfig.build.json
1512
"skipLibCheck": true,
1613
"allowSyntheticDefaultImports": true,

0 commit comments

Comments
 (0)