Skip to content

test(docs): ensure defaults are consistent #2177

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

Merged
merged 37 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b808f72
test(docs): ensure defaults are consistent
ST-DDT May 24, 2023
d9c1bb8
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT May 24, 2023
4e3319e
reenable the other tests and ensure order of execution
ST-DDT May 24, 2023
4065e82
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Jun 17, 2023
a6d97f3
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Jul 15, 2023
620c5d2
chore: add some empty object defaults
ST-DDT Jul 19, 2023
2b7f69d
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Jul 19, 2023
f1ade99
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Sep 15, 2023
685859c
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 5, 2023
27ce286
chore: adjust code to team decision
ST-DDT Oct 5, 2023
57cbc6b
test: improve error message
ST-DDT Oct 5, 2023
2a22da4
chore: add bidirectional code referencing
ST-DDT Oct 5, 2023
f7123b4
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 5, 2023
50d8907
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 6, 2023
287596d
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 6, 2023
c91f813
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 6, 2023
6aa4690
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 7, 2023
dec9180
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 8, 2023
9eebdc7
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 9, 2023
dd544be
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 11, 2023
0d707e5
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 12, 2023
578027f
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 15, 2023
11c0040
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 15, 2023
944c895
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 20, 2023
78fb1b3
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 21, 2023
5844f71
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 22, 2023
741c381
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 25, 2023
ace4639
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 26, 2023
e471705
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 26, 2023
7406bcf
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 28, 2023
1464c44
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 29, 2023
c98ce3e
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Oct 30, 2023
db15010
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Nov 6, 2023
022bfdd
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Nov 7, 2023
4debde6
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Nov 7, 2023
6286f8f
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Nov 12, 2023
c2aff9e
Merge branch 'next' into test/jsdocs/default-assertions
ST-DDT Nov 13, 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 scripts/apidoc/parameterDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function cleanParameterDefault(value?: string): string | undefined {
}

// Strip type casts: "'foobar' as unknown as T" => "'foobar'"
return value.replace(/ as unknown as [A-Za-z<>]+/, '');
return value.replace(/( as unknown)? as [A-Za-z<>]+/, '');
}

/**
Expand Down
58 changes: 29 additions & 29 deletions scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
extractSeeAlsos,
extractSince,
extractSourcePath,
extractSummaryDefault,
extractThrows,
joinTagParts,
toBlock,
} from './typedoc';

Expand Down Expand Up @@ -110,8 +110,7 @@ function analyzeParameter(parameter: ParameterReflection): {
const name = parameter.name;
const declarationName = name + (isOptional(parameter) ? '?' : '');
const type = parameter.type;
const commentDefault = extractDefaultFromComment(parameter.comment);
const defaultValue = parameter.defaultValue ?? commentDefault;
const defaultValue = extractDefaultFromParameter(parameter);

let signatureText = '';
if (defaultValue) {
Expand Down Expand Up @@ -300,38 +299,39 @@ function signatureTypeToText(signature?: SignatureReflection): string {
}

/**
* Extracts and removed the parameter default from the comments.
* Extracts and optionally removes the parameter default from the parameter.
*
* @param parameter The parameter to extract the default from.
* @param eraseDefault Whether to erase the default text from the parameter comment.
* @returns The extracted default value.
*/
function extractDefaultFromParameter(
parameter: ParameterReflection,
eraseDefault = true
): string | undefined {
const commentDefault = extractDefaultFromComment(
parameter.comment,
eraseDefault
);
return parameter.defaultValue ?? commentDefault;
}

/**
* Extracts and optionally removes the parameter default from the comments.
*
* @param comment The comment to extract the default from.
* @param eraseDefault Whether to erase the default text from the comment.
* @returns The extracted default value.
*/
function extractDefaultFromComment(comment?: Comment): string | undefined {
function extractDefaultFromComment(
comment?: Comment,
eraseDefault = true
): string | undefined {
if (!comment) {
return;
}

const defaultTag = comment.getTag('@default');
if (defaultTag) {
return extractRawDefault({ comment });
}

const summary = comment.summary;
const text = joinTagParts(summary).trim();
if (!text) {
return;
}

const result = /^(.*)[ \n]Defaults to `([^`]+)`\.(.*)$/s.exec(text);
if (!result) {
return;
}

if (result[3].trim()) {
throw new Error(`Found description text after the default value:\n${text}`);
}

summary.splice(summary.length - 2, 2);
const lastSummaryPart = summary[summary.length - 1];
lastSummaryPart.text = lastSummaryPart.text.replace(/[ \n]Defaults to $/, '');
return result[2];
const tagDefault = extractRawDefault({ comment });
const summaryDefault = extractSummaryDefault(comment, eraseDefault);
return tagDefault || summaryDefault;
}
42 changes: 42 additions & 0 deletions scripts/apidoc/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,48 @@ export function extractRawDefault(reflection?: CommentHolder): string {
return extractRawCode('@default', reflection)[0] ?? '';
}

/**
* Extracts and optionally removes the default from the comment summary.
*
* @param comment The comment to extract the default from.
* @param eraseDefault Whether to erase the default text from the comment.
* @returns The extracted default value.
*/
export function extractSummaryDefault(
comment?: Comment,
eraseDefault = true
): string | undefined {
if (!comment) {
return;
}

const summary = comment.summary;
const text = joinTagParts(summary).trim();
if (!text) {
return;
}

const result = /^(.*)[ \n]Defaults to `([^`]+)`\.(.*)$/s.exec(text);
if (!result) {
return;
}

if (result[3].trim()) {
throw new Error(`Found description text after the default value:\n${text}`);
}

if (eraseDefault) {
summary.splice(summary.length - 2, 2);
const lastSummaryPart = summary[summary.length - 1];
lastSummaryPart.text = lastSummaryPart.text.replace(
/[ \n]Defaults to $/,
''
);
}

return result[2];
}

/**
* Extracts the examples from the jsdocs without the surrounding md code block.
*
Expand Down
20 changes: 20 additions & 0 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,29 @@ export class CommerceModule {
options?:
| number
| {
/**
* The minimum price.
*
* @default 1
*/
min?: number;
/**
* The maximum price.
*
* @default 1000
*/
max?: number;
/**
* The number of decimal places.
*
* @default 2
*/
dec?: number;
/**
* The currency value to use.
*
* @default ''
*/
symbol?: string;
},
legacyMax?: number,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class DatatypeModule {
* Returns a single random number between zero and the given max value or the given range with the specified precision.
* The bounds are inclusive.
*
* @param options Maximum value or options object.
* @param options Maximum value or options object. Defaults to `99999`.
* @param options.min Lower bound for generated number. Defaults to `0`.
* @param options.max Upper bound for generated number. Defaults to `min + 99999`.
* @param options.precision Precision of the generated number. Defaults to `1`.
Expand Down Expand Up @@ -298,7 +298,7 @@ export class DatatypeModule {
* If the probability is `>= 1.0`, it will always return `true`.
* The probability is limited to two decimal places.
*
* @param options The optional options object or the probability (`[0.00, 1.00]`) of returning `true`. Defaults to `0.5`.
* @param options The optional options object or the probability (`[0.00, 1.00]`) of returning `true`.
* @param options.probability The probability (`[0.00, 1.00]`) of returning `true`. Defaults to `0.5`.
*
* @example
Expand Down
2 changes: 1 addition & 1 deletion src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export class DateModule {
* @param options.to The late date boundary.
* @param options.count The number of dates to generate. Defaults to `3`.
* @param legacyTo Deprecated, use `options.to` instead.
* @param legacyCount Deprecated, use `options.count` instead.
* @param legacyCount Deprecated, use `options.count` instead. Defaults to `3`.
*
* @example
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' })
Expand Down
12 changes: 6 additions & 6 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class FinanceModule {
* @param max The upper bound for the amount. Defaults to `1000`.
* @param dec The number of decimal places for the amount. Defaults to `2`.
* @param symbol The symbol used to prefix the amount. Defaults to `''`.
* @param autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
* @param autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. Defaults to `false`.
*
* @example
* faker.finance.amount() // '617.87'
Expand All @@ -412,7 +412,7 @@ export class FinanceModule {
* @param options.max The upper bound for the amount. Defaults to `1000`.
* @param options.dec The number of decimal places for the amount. Defaults to `2`.
* @param options.symbol The symbol used to prefix the amount. Defaults to `''`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. Defaults to `false`.
*
* @example
* faker.finance.amount() // '617.87'
Expand Down Expand Up @@ -463,11 +463,11 @@ export class FinanceModule {
* @param options.max The upper bound for the amount. Defaults to `1000`.
* @param options.dec The number of decimal places for the amount. Defaults to `2`.
* @param options.symbol The symbol used to prefix the amount. Defaults to `''`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. Defaults to `false`.
* @param legacyMax The upper bound for the amount. Defaults to `1000`.
* @param legacyDec The number of decimal places for the amount. Defaults to `2`.
* @param legacySymbol The symbol used to prefix the amount. Defaults to `''`.
* @param legacyAutoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
* @param legacyAutoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. Defaults to `false`.
*
* @example
* faker.finance.amount() // '617.87'
Expand Down Expand Up @@ -530,11 +530,11 @@ export class FinanceModule {
* @param options.max The upper bound for the amount. Defaults to `1000`.
* @param options.dec The number of decimal places for the amount. Defaults to `2`.
* @param options.symbol The symbol used to prefix the amount. Defaults to `''`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. Defaults to `false`.
* @param legacyMax The upper bound for the amount. Defaults to `1000`.
* @param legacyDec The number of decimal places for the amount. Defaults to `2`.
* @param legacySymbol The symbol used to prefix the amount. Defaults to `''`.
* @param legacyAutoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
* @param legacyAutoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. Defaults to `false`.
*
* @example
* faker.finance.amount() // '617.87'
Expand Down
14 changes: 7 additions & 7 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class HelpersModule {
* For that all spaces (` `) are replaced by hyphens (`-`)
* and most non word characters except for dots and hyphens will be removed.
*
* @param string The input to slugify.
* @param string The input to slugify. Defaults to `''`.
*
* @example
* faker.helpers.slugify() // ''
Expand All @@ -129,7 +129,7 @@ export class HelpersModule {
* Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`).
* `!` will be replaced by digits >=2 (`2` - `9`).
*
* @param string The template string to parse.
* @param string The template string to parse. Defaults to `''`.
* @param symbol The symbol to replace with digits. Defaults to `'#'`.
*
* @example
Expand Down Expand Up @@ -162,7 +162,7 @@ export class HelpersModule {
* - `?` will be replaced with an upper letter ('A' - 'Z')
* - and `*` will be replaced with either a digit or letter.
*
* @param string The template string to parse.
* @param string The template string to parse. Defaults to `''`.
*
* @example
* faker.helpers.replaceSymbols() // ''
Expand Down Expand Up @@ -227,8 +227,8 @@ export class HelpersModule {
* This method supports both range patterns `[4-9]` as well as the patterns used by `replaceSymbolWithNumber()`.
* `L` will be replaced with the appropriate Luhn checksum.
*
* @param string The credit card format pattern. Defaults to `6453-####-####-####-###L`.
* @param symbol The symbol to replace with a digit.
* @param string The credit card format pattern. Defaults to `'6453-####-####-####-###L'`.
* @param symbol The symbol to replace with a digit. Defaults to `'#'`.
*
* @example
* faker.helpers.replaceCreditCardSymbols() // '6453-4876-8626-8995-3771'
Expand Down Expand Up @@ -257,7 +257,7 @@ export class HelpersModule {
* - `.{min,max}` => Repeat the character `min` to `max` times.
* - `[min-max]` => Generate a number between min and max (inclusive).
*
* @param string The template string to to parse.
* @param string The template string to to parse. Defaults to `''`.
*
* @example
* faker.helpers.regexpStyleStringParse() // ''
Expand Down Expand Up @@ -1283,7 +1283,7 @@ export class HelpersModule {
* @template TMethod The type of the method to execute.
*
* @param method The method used to generate the values.
* @param args The arguments used to call the method.
* @param args The arguments used to call the method. Defaults to `[]`.
* @param options The optional options used to configure this method.
* @param options.startTime This parameter does nothing.
* @param options.maxTime The time in milliseconds this method may take before throwing an error. Defaults to `50`.
Expand Down
32 changes: 30 additions & 2 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,22 @@ export class LocationModule {
* @since 8.0.0
*/
nearbyGPSCoordinate(options?: {
/**
* The original coordinate to get a new coordinate close to.
* If no coordinate is given, a random one will be chosen.
*/
origin?: [latitude: number, longitude: number];
/**
* The maximum distance from the given coordinate to the new coordinate.
*
* @default 10
*/
radius?: number;
/**
* If `true` assume the radius to be in kilometers. If `false` for miles.
*
* @default false
*/
isMetric?: boolean;
}): [latitude: number, longitude: number];
/**
Expand Down Expand Up @@ -1115,8 +1129,8 @@ export class LocationModule {
* If no coordinate is given, a random one will be chosen.
* @param options.radius The maximum distance from the given coordinate to the new coordinate. Defaults to `10`.
* @param options.isMetric If `true` assume the radius to be in kilometers. If `false` for miles. Defaults to `false`.
* @param legacyRadius Deprecated, use `options.radius` instead.
* @param legacyIsMetric Deprecated, use `options.isMetric` instead.
* @param legacyRadius Deprecated, use `options.radius` instead. Defaults to `10`.
* @param legacyIsMetric Deprecated, use `options.isMetric` instead. Defaults to `false`.
*
* @example
* faker.location.nearbyGPSCoordinate() // [ 33.8475, -170.5953 ]
Expand All @@ -1129,8 +1143,22 @@ export class LocationModule {
options?:
| [latitude: number, longitude: number]
| {
/**
* The original coordinate to get a new coordinate close to.
* If no coordinate is given, a random one will be chosen.
*/
origin?: [latitude: number, longitude: number];
/**
* The maximum distance from the given coordinate to the new coordinate.
*
* @default 10
*/
radius?: number;
/**
* If `true` assume the radius to be in kilometers. If `false` for miles.
*
* @default false
*/
isMetric?: boolean;
},
legacyRadius?: number,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/random/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class RandomModule {
/**
* Generating a string consisting of letters in the English alphabet.
*
* @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'mixed', bannedChars: [] }`.
* @param options Either the number of characters or an options instance.
* @param options.count The number of characters to generate. Defaults to `1`.
* @param options.casing The casing of the characters. Defaults to `'mixed'`.
* @param options.bannedChars An array with characters to exclude. Defaults to `[]`.
Expand Down Expand Up @@ -291,7 +291,7 @@ export class RandomModule {
* Generating a string consisting of alpha characters and digits.
*
* @param count The number of characters and digits to generate. Defaults to `1`.
* @param options The options to use. Defaults to `{ bannedChars: [] }`.
* @param options The options to use.
* @param options.casing The casing of the characters. Defaults to `'lower'`.
* @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`.
*
Expand Down
Loading