aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2023-12-11 19:42:59 +0100
committerGitHub <[email protected]>2023-12-11 18:42:59 +0000
commit1631115a6a9846c50473fe60ea5bdb7ce9d04084 (patch)
tree6b9dcb203e31aea8ff2233ef60046278139ea71b
parent60dcfe7c6454327740d2253ed283deb59d420462 (diff)
downloadfaker-1631115a6a9846c50473fe60ea5bdb7ce9d04084.tar.xz
faker-1631115a6a9846c50473fe60ea5bdb7ce9d04084.zip
infra(tsconfig): noImplicitAny (#2562)
-rw-r--r--.prettierrc.d.ts4
-rw-r--r--scripts/apidoc/typedoc.ts3
-rw-r--r--scripts/generate-locales.ts22
-rw-r--r--src/internal/keys.ts13
-rw-r--r--test/all-functional.spec.ts10
-rw-r--r--test/faker.spec.ts3
-rw-r--r--test/locale-imports.spec.ts9
-rw-r--r--test/modules/helpers.spec.ts8
-rw-r--r--test/modules/image.spec.ts2
-rw-r--r--test/simple-faker.spec.ts7
-rw-r--r--tsconfig.build.json1
-rw-r--r--tsconfig.json3
12 files changed, 49 insertions, 36 deletions
diff --git a/.prettierrc.d.ts b/.prettierrc.d.ts
new file mode 100644
index 00000000..806a7063
--- /dev/null
+++ b/.prettierrc.d.ts
@@ -0,0 +1,4 @@
+import type { Options } from 'prettier';
+
+declare const options: Options;
+export = options;
diff --git a/scripts/apidoc/typedoc.ts b/scripts/apidoc/typedoc.ts
index a795efa2..1e548c14 100644
--- a/scripts/apidoc/typedoc.ts
+++ b/scripts/apidoc/typedoc.ts
@@ -88,7 +88,8 @@ export function selectApiModules(
.getChildrenByKind(ReflectionKind.Class)
.filter(
(module) =>
- faker[extractModuleFieldName(module)] != null || includeTestModules
+ faker[extractModuleFieldName(module) as keyof typeof faker] != null ||
+ includeTestModules
);
}
diff --git a/scripts/generate-locales.ts b/scripts/generate-locales.ts
index ebc06670..4228cc91 100644
--- a/scripts/generate-locales.ts
+++ b/scripts/generate-locales.ts
@@ -23,10 +23,9 @@ import {
} from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
-import type { Options } from 'prettier';
-import { format } from 'prettier';
-import options from '../.prettierrc.js';
import type { LocaleDefinition, MetadataDefinition } from '../src/definitions';
+import { keys } from '../src/internal/keys';
+import { formatMarkdown, formatTypescript } from './apidoc/format';
// Constants
@@ -78,9 +77,6 @@ const definitionsTypes: DefinitionType = {
word: 'WordDefinition',
};
-const prettierTsOptions: Options = { ...options, parser: 'typescript' };
-const prettierMdOptions: Options = { ...options, parser: 'markdown' };
-
const scriptCommand = 'pnpm run generate:locales';
const autoGeneratedCommentHeader = `/*
@@ -154,7 +150,7 @@ async function generateLocaleFile(locale: string): Promise<void> {
});
`;
- content = await format(content, prettierTsOptions);
+ content = await formatTypescript(content);
writeFileSync(resolve(pathLocale, `${locale}.ts`), content);
}
@@ -195,7 +191,7 @@ async function generateLocalesIndexFile(
writeFileSync(
resolve(path, 'index.ts'),
- await format(content.join('\n'), prettierTsOptions)
+ await formatTypescript(content.join('\n'))
);
}
@@ -301,7 +297,7 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
}
const result = {} as T;
- for (const key of Object.keys(localeData)) {
+ for (const key of keys(localeData)) {
result[key] = normalizeDataRecursive(localeData[key]);
}
@@ -367,7 +363,7 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
// In the long term we should probably define a whether we want those in the files at all.
const newContent = fileContentPreData + JSON.stringify(localeData);
- writeFileSync(filePath, await format(newContent, prettierTsOptions));
+ writeFileSync(filePath, await formatTypescript(newContent));
}
// Start of actual logic
@@ -444,7 +440,7 @@ async function main(): Promise<void> {
} as const;
`;
- localeIndexContent = await format(localeIndexContent, prettierTsOptions);
+ localeIndexContent = await formatTypescript(localeIndexContent);
writeFileSync(pathLocaleIndex, localeIndexContent);
// src/locales/index.ts
@@ -455,12 +451,12 @@ async function main(): Promise<void> {
${localesIndexExports}
`;
- localesIndexContent = await format(localesIndexContent, prettierTsOptions);
+ localesIndexContent = await formatTypescript(localesIndexContent);
writeFileSync(pathLocalesIndex, localesIndexContent);
// docs/guide/localization.md
- localizationLocales = await format(localizationLocales, prettierMdOptions);
+ localizationLocales = await formatMarkdown(localizationLocales);
let localizationContent = readFileSync(pathDocsGuideLocalization, 'utf8');
localizationContent = localizationContent.replace(
diff --git a/src/internal/keys.ts b/src/internal/keys.ts
new file mode 100644
index 00000000..d67d32e7
--- /dev/null
+++ b/src/internal/keys.ts
@@ -0,0 +1,13 @@
+/**
+ * Specialized version of `Object.keys()` which preserves the type information of the keys.
+ *
+ * Please note that the type information might be inaccurate for subtypes of the argument type
+ * and thus should only be used to cover the property access of the object.
+ *
+ * @internal
+ *
+ * @param obj The object to get the keys of.
+ */
+export function keys<T extends object>(obj: T): Array<keyof T> {
+ return Object.keys(obj) as Array<keyof T>;
+}
diff --git a/test/all-functional.spec.ts b/test/all-functional.spec.ts
index d4b1308a..a883da7d 100644
--- a/test/all-functional.spec.ts
+++ b/test/all-functional.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import type { allLocales, Faker, RandomModule } from '../src';
import { allFakers, fakerEN } from '../src';
+import { keys } from '../src/internal/keys';
const IGNORED_MODULES = new Set([
'rawDefinitions',
@@ -35,11 +36,7 @@ function isTestableModule(moduleName: string): moduleName is keyof Faker {
}
function getMethodNamesOf(module: object): string[] {
- return Object.keys(module).filter(isMethodOf(module));
-}
-
-function isMethodOf(module: object): (method: string) => boolean {
- return (method: string) => typeof module[method] === 'function';
+ return keys(module).filter((method) => typeof module[method] === 'function');
}
type SkipConfig<TModule> = Partial<
@@ -81,6 +78,7 @@ function isWorkingLocaleForMethod(
method: string,
locale: string
): boolean {
+ // @ts-expect-error: We don't have types for the dynamic access
const broken = BROKEN_LOCALE_METHODS[module]?.[method] ?? [];
return broken !== '*' && !broken.includes(locale);
}
@@ -104,6 +102,7 @@ describe('BROKEN_LOCALE_METHODS test', () => {
it('should not contain obsolete configuration (methods)', () => {
const existingMethods = modules[module];
const configuredMethods = Object.keys(
+ // @ts-expect-error: We don't have types for the dynamic access
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
BROKEN_LOCALE_METHODS[module] ?? {}
);
@@ -129,6 +128,7 @@ describe('functional tests', () => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
+ // @ts-expect-error: We don't have types for the dynamic access
const result = faker[module][meth]();
if (meth === 'boolean') {
diff --git a/test/faker.spec.ts b/test/faker.spec.ts
index a098d898..bf1bfafc 100644
--- a/test/faker.spec.ts
+++ b/test/faker.spec.ts
@@ -2,6 +2,7 @@ import type { SpyInstance } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { faker, Faker } from '../src';
import { FakerError } from '../src/errors/faker-error';
+import { keys } from '../src/internal/keys';
describe('faker', () => {
it('should throw error if no locales passed', () => {
@@ -13,7 +14,7 @@ describe('faker', () => {
});
it('should not log anything on startup', () => {
- const spies: SpyInstance[] = Object.keys(console)
+ const spies: SpyInstance[] = keys(console)
.filter((key) => typeof console[key] === 'function')
.map((methodName) =>
vi.spyOn(console, methodName as keyof typeof console)
diff --git a/test/locale-imports.spec.ts b/test/locale-imports.spec.ts
index 999a7223..1d7dc475 100644
--- a/test/locale-imports.spec.ts
+++ b/test/locale-imports.spec.ts
@@ -1,8 +1,9 @@
import { describe, expect, it } from 'vitest';
import type { Faker } from '../src';
import { allLocales } from '../src';
+import { keys } from '../src/internal/keys';
-describe.each(Object.keys(allLocales))('locale imports', (locale) => {
+describe.each(keys(allLocales))('locale imports', (locale) => {
it(`should be possible to directly require('@faker-js/faker/locale/${locale}')`, () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
const { faker } = require(`../dist/cjs/locale/${locale}`) as {
@@ -12,7 +13,7 @@ describe.each(Object.keys(allLocales))('locale imports', (locale) => {
expect(faker).toBeDefined();
expect(faker.string.alpha()).toBeTypeOf('string');
expect(faker.definitions.metadata.title).toBe(
- allLocales[locale].metadata.title
+ allLocales[locale].metadata?.title
);
});
@@ -24,12 +25,12 @@ describe.each(Object.keys(allLocales))('locale imports', (locale) => {
expect(faker).toBeDefined();
expect(faker.string.alpha()).toBeTypeOf('string');
expect(faker.definitions.metadata.title).toBe(
- allLocales[locale].metadata.title
+ allLocales[locale].metadata?.title
);
});
it('should have complete metadata values', () => {
- const metadata = allLocales[locale].metadata;
+ const metadata = allLocales[locale].metadata ?? {};
expect(metadata.title).toBeTypeOf('string');
expect(metadata.code).toBeTypeOf('string');
expect(metadata.code).toEqual(locale);
diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts
index 766d0650..1a1db9c5 100644
--- a/test/modules/helpers.spec.ts
+++ b/test/modules/helpers.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { faker, FakerError } from '../../src';
import { luhnCheck } from '../../src/modules/helpers/luhn-check';
+import type { RecordKey } from '../../src/modules/helpers/unique';
import { seededTests } from '../support/seeded-runs';
import { times } from './../support/times';
import './../vitest-extensions';
@@ -1295,8 +1296,9 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`)
const maxTime = 49;
const maxRetries = 49;
const currentIterations = 0;
- const exclude = [];
- const compare = (obj, key) => (obj[key] === undefined ? -1 : 0);
+ const exclude: string[] = [];
+ const compare = (obj: Record<RecordKey, RecordKey>, key: RecordKey) =>
+ obj[key] === undefined ? -1 : 0;
const options = {
startTime,
@@ -1318,7 +1320,7 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`)
});
it('should be possible to pass a user-specific store', () => {
- const store = {};
+ const store: Record<string, string> = {};
const method = () => 'with conflict: 0';
diff --git a/test/modules/image.spec.ts b/test/modules/image.spec.ts
index 938799ef..004905e7 100644
--- a/test/modules/image.spec.ts
+++ b/test/modules/image.spec.ts
@@ -257,7 +257,7 @@ describe('image', () => {
'objects',
'people',
'technology',
- ];
+ ] satisfies Array<keyof typeof faker.image.unsplash>;
describe.each(categories)(`%s()`, (category) => {
it(`should return a random ${category} image url`, () => {
diff --git a/test/simple-faker.spec.ts b/test/simple-faker.spec.ts
index 899bcafe..ec1247cf 100644
--- a/test/simple-faker.spec.ts
+++ b/test/simple-faker.spec.ts
@@ -1,14 +1,13 @@
import type { SpyInstance } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { SimpleFaker, simpleFaker } from '../src';
+import { keys } from '../src/internal/keys';
describe('simpleFaker', () => {
it('should not log anything on startup', () => {
- const spies: SpyInstance[] = Object.keys(console)
+ const spies: SpyInstance[] = keys(console)
.filter((key) => typeof console[key] === 'function')
- .map((methodName) =>
- vi.spyOn(console, methodName as keyof typeof console)
- );
+ .map((methodName) => vi.spyOn(console, methodName));
// 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.
require('..').simpleFaker;
diff --git a/tsconfig.build.json b/tsconfig.build.json
index aa1bfb29..ce2dcfba 100644
--- a/tsconfig.build.json
+++ b/tsconfig.build.json
@@ -8,7 +8,6 @@
"outDir": "dist/types",
// This negates what is set in the extended tsconfig.json
- "noImplicitAny": true,
"skipLibCheck": false,
"allowSyntheticDefaultImports": false,
"resolveJsonModule": false
diff --git a/tsconfig.json b/tsconfig.json
index da5cd4e6..a290fc07 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -8,9 +8,6 @@
"declaration": true,
"stripInternal": true,
- // We need to disable these for now, and need to tackle them in another PR
- "noImplicitAny": false,
-
// These are configs specifically for !build and have to be reverted in the tsconfig.build.json
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,