Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: enable strictNullChecks in tsconfig #2435

Merged
merged 47 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7e89379
infra: enable strictNullChecks in tsconfig
ST-DDT Oct 5, 2023
8d3497b
Apply suggestions from code review
ST-DDT Oct 5, 2023
3ba4b4e
Apply suggestions from code review
ST-DDT Oct 5, 2023
c555cb8
build it
ST-DDT Oct 5, 2023
2c3c7c6
Update src/modules/finance/index.ts
ST-DDT Oct 5, 2023
3caf9d5
Apply suggestions from code review
ST-DDT Oct 5, 2023
2818847
Update test/faker.spec.ts
ST-DDT Oct 6, 2023
50531c8
Update scripts/apidoc/fakerClass.ts
ST-DDT Oct 6, 2023
ebc6ac5
chore: work around ts error in vitest extension
ST-DDT Oct 6, 2023
00f0fcf
chore: simplify version grouping
ST-DDT Oct 6, 2023
3e7d2d0
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 6, 2023
cf2397b
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 6, 2023
0042bdf
chore: prettyPrintIban helper function
ST-DDT Oct 6, 2023
45f6b06
chore: add assertLocaleData
ST-DDT Oct 6, 2023
fa8bb66
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 6, 2023
485cc60
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 7, 2023
a2a303d
chore: apply suggestions
ST-DDT Oct 7, 2023
5d88fb2
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 8, 2023
c9a9229
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 9, 2023
8ca3169
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 10, 2023
487cac2
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 11, 2023
91ad574
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 12, 2023
817d5a0
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 15, 2023
292aa81
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 15, 2023
4fe5153
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 20, 2023
6fe4253
revert: helpers changes
ST-DDT Oct 20, 2023
f36f34e
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 22, 2023
bae7b20
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 25, 2023
bf010a7
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 26, 2023
d3088bd
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 26, 2023
f82a7f6
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 28, 2023
c797146
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 29, 2023
6a54e73
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Oct 30, 2023
d8e078a
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 6, 2023
bf88c58
chore: fix lint issues
ST-DDT Nov 6, 2023
865d4de
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 6, 2023
234c12f
chore: fix another lint warning
ST-DDT Nov 6, 2023
85e9096
chore: fix import issue
ST-DDT Nov 6, 2023
109738f
Update src/modules/date/index.ts
ST-DDT Nov 6, 2023
94f7927
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 6, 2023
e1e6c8a
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 7, 2023
0d73a2a
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 7, 2023
cb4383a
chore: fix lint warning
ST-DDT Nov 10, 2023
6efe430
chore: remove wrong defaults and make the properties required
ST-DDT Nov 10, 2023
0aa69d0
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 13, 2023
c86f261
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 13, 2023
84df921
Merge branch 'next' into infra/tsconfig/enable-strictNullChecks
ST-DDT Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/e2e/api.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('API Test', () => {
cy.get('.api-group li').each(($el) => {
const anchor = $el.find('a');
const text = anchor.text();
const link = anchor.attr('href').split('#')[0];
const link = anchor.attr('href')!.split('#')[0];
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
if (checked.has(link)) {
return;
}
Expand Down
13 changes: 8 additions & 5 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ const config = defineConfig({
{ icon: 'github', link: 'https://github.com/faker-js/faker' },
],

algolia: {
apiKey: process.env.API_KEY,
appId: process.env.APP_ID,
indexName: 'fakerjs',
},
algolia:
process.env.API_KEY == null || process.env.APP_ID == null
? undefined
: {
apiKey: process.env.API_KEY,
appId: process.env.APP_ID,
indexName: 'fakerjs',
},

footer: {
message: 'Released under the MIT License.',
Expand Down
3 changes: 2 additions & 1 deletion docs/.vitepress/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ function readOtherLatestReleaseTagNames(): string[] {
}
return acc;
}, {});

ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
return Object.entries(latestReleaseTagNames)
.map(([major, tags]) => semver.maxSatisfying(tags, `^${major}`))
.map(([major, tags]) => semver.maxSatisfying(tags, `^${major}`)!)
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
.sort(semver.rcompare);
}

Expand Down
6 changes: 5 additions & 1 deletion scripts/apidoc/fakerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export async function processFakerRandomizer(
.getChildrenByKind(ReflectionKind.Interface)
.find((clazz) => clazz.name === 'Randomizer');

if (randomizerClass == null) {
throw new Error('Randomizer class not found');
}

return processClass(randomizerClass);
}

Expand Down Expand Up @@ -62,7 +66,7 @@ async function processClass(
function hasConstructor(clazz: DeclarationReflection): boolean {
return clazz
.getChildrenByKind(ReflectionKind.Constructor)
.some((constructor) => constructor.signatures.length > 0);
.some((constructor) => constructor.signatures!.length > 0);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}

async function processConstructor(
Expand Down
2 changes: 1 addition & 1 deletion scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async function signatureTypeToText(
await Promise.all(
signature.parameters?.map(
async (p) => `${p.name}: ${await typeToText(p.type)}`
)
) ?? []
)
).join(', ')}) => ${await typeToText(signature.type)}`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ export type LocaleDefinition = {
system?: SystemDefinition;
vehicle?: VehicleDefinition;
word?: WordDefinition;
} & Record<string, Record<string, unknown> | undefined>;
} & Record<string, Record<string, unknown>>;
10 changes: 9 additions & 1 deletion src/locales/ar/date/weekday.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export default {
abbr: null,
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
abbr: [
'الأحَد',
'الإثنين',
'الثلاثاء',
'الأربعاء',
'الخميس',
'الجمعة',
'السبت',
],
wide: [
'الأحَد',
'الإثنين',
Expand Down
15 changes: 14 additions & 1 deletion src/locales/ur/date/month.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
export default {
abbr: null,
abbr: [
'جنوری',
'فروری',
'مارچ',
'اپریل',
'مئ',
'جون',
'جولائ',
'اگست',
'ستمبر',
'اکتوبر',
'نومبر',
'دسمبر',
],
wide: [
'جنوری',
'فروری',
Expand Down
2 changes: 1 addition & 1 deletion src/locales/ur/date/weekday.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
abbr: null,
abbr: ['اتور', 'پیر', 'منگل', 'بدھ', 'جمعرات', 'جمعہ', 'ہفتہ'],
wide: ['اتور', 'پیر', 'منگل', 'بدھ', 'جمعرات', 'جمعہ', 'ہفتہ'],
};
2 changes: 1 addition & 1 deletion src/modules/airline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class AirlineModule {
): string {
const { allowNumerics = false, allowVisuallySimilarCharacters = false } =
options;
const excludedChars = [];
const excludedChars: string[] = [];
if (!allowNumerics) {
excludedChars.push(...numerics);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class CommerceModule {

const registrantLength = groupRules.find(
([rangeMaximum]) => elementValue <= rangeMaximum
)[1];
)![1];
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved

const registrant = element.slice(0, registrantLength);
const publication = element.slice(registrantLength);
Expand Down
44 changes: 32 additions & 12 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function toDate(
date: string | Date | number | undefined,
fallback: () => Date
): Date {
if (date == null) {
return fallback();
}

date = new Date(date);
if (isNaN(date.valueOf())) {
date = fallback();
Expand Down Expand Up @@ -387,14 +391,15 @@ export class SimpleDateModule {
},
legacyTo?: string | Date | number
): Date {
if (typeof options !== 'object' || options instanceof Date) {
if (options instanceof Date || typeof options !== 'object') {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
deprecated({
deprecated: 'faker.date.between(from, to)',
proposed: 'faker.date.between({ from, to })',
since: '8.0',
until: '9.0',
});
options = { from: options, to: legacyTo };
// We use options as fallback for legacyTo avoid TS errors for unintended usage.
options = { from: options, to: legacyTo ?? options };
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
}

const { from, to } = options;
Expand Down Expand Up @@ -563,14 +568,15 @@ export class SimpleDateModule {
legacyTo?: string | Date | number,
legacyCount: number = 3
): Date[] {
if (typeof options !== 'object' || options instanceof Date) {
if (options instanceof Date || typeof options !== 'object') {
deprecated({
deprecated: 'faker.date.betweens(from, to, count)',
proposed: 'faker.date.betweens({ from, to, count })',
since: '8.0',
until: '9.0',
});
options = { from: options, to: legacyTo, count: legacyCount };
// We use options as fallback for legacyTo avoid TS errors for unintended usage.
options = { from: options, to: legacyTo ?? options, count: legacyCount };
}

const { from, to, count = 3 } = options;
Expand Down Expand Up @@ -868,12 +874,6 @@ export class SimpleDateModule {
refDate?: string | Date | number;
} = {}
): Date {
if (options.max < options.min) {
throw new FakerError(
`Max ${options.max} should be larger than or equal to min ${options.min}.`
);
}

const mode = options.mode === 'age' ? 'age' : 'year';
const refDate = toDate(options.refDate, this.faker.defaultRefDate);
const refYear = refDate.getUTCFullYear();
Expand All @@ -898,6 +898,12 @@ export class SimpleDateModule {
);
}

if (max < min) {
throw new FakerError(
`Max ${options.max} should be larger than or equal to min ${options.min}.`
);
}

return new Date(this.faker.number.int({ min, max }));
}
}
Expand Down Expand Up @@ -1108,7 +1114,14 @@ export class DateModule extends SimpleDateModule {
type = 'wide';
}

return this.faker.helpers.arrayElement(source[type]);
const values = source[type];

if (values == null) {
// This should never happen due to the checks above, but TS doesn't know that
throw new FakerError(`No month values found for type '${type}'.`);
}

return this.faker.helpers.arrayElement(values);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -1298,6 +1311,13 @@ export class DateModule extends SimpleDateModule {
type = 'wide';
}

return this.faker.helpers.arrayElement(source[type]);
const values = source[type];

if (values == null) {
// This should never happen due to the checks above, but TS doesn't know that
throw new FakerError(`No weekday values found for type '${type}'.`);
}

return this.faker.helpers.arrayElement(values);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}
}
6 changes: 3 additions & 3 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,9 @@ export class FinanceModule {
*/
currencySymbol(): string {
let symbol: string;
while (!symbol) {
do {
symbol = this.currency().symbol;
}
} while (!symbol);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

return symbol;
}
Expand Down Expand Up @@ -1163,7 +1163,7 @@ export class FinanceModule {

const result = `${ibanFormat.country}${checksum}${s}`;

return formatted ? result.match(/.{1,4}/g).join(' ') : result;
return formatted ? result.replace(/(.{4})(?=.)/g, '$1 ') : result;
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export class SimpleHelpersModule {
const quantifierMin: string = token[2];
const quantifierMax: string = token[3];
const quantifierSymbol: string = token[4];
const tokenIndex: number = token.index!;
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved

repetitions = getRepetitionsBasedOnQuantifierParameters(
this.faker,
Expand All @@ -431,9 +432,9 @@ export class SimpleHelpersModule {
);

pattern =
pattern.slice(0, token.index) +
pattern.slice(0, tokenIndex) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
pattern.slice(tokenIndex + token[0].length);
token = pattern.match(SINGLE_CHAR_REG);
}

Expand All @@ -448,6 +449,7 @@ export class SimpleHelpersModule {
const quantifierMin: string = token[4];
const quantifierMax: string = token[5];
const quantifierSymbol: string = token[6];
const tokenIndex: number = token.index!;

const rangeCodes: number[] = [];

Expand Down Expand Up @@ -542,9 +544,9 @@ export class SimpleHelpersModule {
).join('');

pattern =
pattern.slice(0, token.index) +
pattern.slice(0, tokenIndex) +
generatedString +
pattern.slice(token.index + token[0].length);
pattern.slice(tokenIndex + token[0].length);
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
}

Expand All @@ -554,16 +556,17 @@ export class SimpleHelpersModule {
while (token != null) {
min = parseInt(token[2]);
max = parseInt(token[3]);
const tokenIndex = token.index!;
// throw error if min larger than max
if (min > max) {
throw new FakerError('Numbers out of order in {} quantifier.');
}

repetitions = this.faker.number.int({ min, max });
pattern =
pattern.slice(0, token.index) +
pattern.slice(0, tokenIndex) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
pattern.slice(tokenIndex + token[0].length);
token = pattern.match(RANGE_REP_REG);
}

Expand All @@ -572,10 +575,11 @@ export class SimpleHelpersModule {
token = pattern.match(REP_REG);
while (token != null) {
repetitions = parseInt(token[2]);
const tokenIndex = token.index!;
pattern =
pattern.slice(0, token.index) +
pattern.slice(0, tokenIndex) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
pattern.slice(tokenIndex + token[0].length);
token = pattern.match(REP_REG);
}

Expand Down Expand Up @@ -1117,7 +1121,7 @@ export class SimpleHelpersModule {
) => RecordKey,
>(
method: TMethod,
args: Parameters<TMethod> = [] as Parameters<TMethod>,
args: Parameters<TMethod> = [] as unknown as Parameters<TMethod>,
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
options: {
/**
* This parameter does nothing.
Expand Down
4 changes: 2 additions & 2 deletions src/modules/helpers/unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export function exec<
maxTime = 50,
maxRetries = 50,
compare = defaultCompare,
store,
store = {},
} = options;
let { exclude } = options;
let { exclude = [] } = options;
options.currentIterations = options.currentIterations ?? 0;

// Support single exclude argument as string
Expand Down
2 changes: 2 additions & 0 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ export class InternetModule {
firstName + this.faker.helpers.arrayElement(['.', '_']) + lastName;
break;
case 2:
default:
result = `${firstName}${this.faker.helpers.arrayElement([
'.',
'_',
Expand Down Expand Up @@ -810,6 +811,7 @@ export class InternetModule {
firstName + this.faker.helpers.arrayElement(['.', '_']) + lastName;
break;
case 2:
default:
result = `${firstName}${this.faker.helpers.arrayElement([
'.',
'_',
Expand Down
Loading