Skip to content

Commit

Permalink
docs: deprecation workflow (#1067)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Cheng <ericcheng9316@gmail.com>
  • Loading branch information
xDivisionByZerox and import-brain committed Jun 18, 2022
1 parent c1cc131 commit 6d3f42e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -49,6 +49,28 @@ You can view a generated code coverage report at `coverage/index.html`.

After adding new or updating existing locale data, you need to run `pnpm run generate:locales` to generate/update the related files.

## Deprecation workflow

If you ever find yourself deprecating something in the source code, you can follow these steps to save yourself (and the reviewers) some trouble.

If the code you want to deprecate is a property, convert it to a [getter](https://www.typescriptlang.org/docs/handbook/2/classes.html#getters--setters) first. Now that you have a function, the first thing you want to do is call the internal [`deprecated` function](src/internal/deprecated.ts). Afterwards, add a `@deprecated` parameter to the end of the JSDoc with a human readable description message with a suitable replacement for the deprecated function. Lastly, add a `@see` parameter to the JSDoc with a link to the replacement in the faker library (if it exists). The syntax for the link is `faker.[module].[function]`.

Example:

```ts
/**
* @see faker.cat.random
*
* @deprecated Use faker.cat.random() instead.
*/
get cat() {
deprecated({
deprecated: 'faker.animal.cat',
});
return 'cat';
}
```

## Developing the docs

Before running the docs, build the Faker dist, it's used inside of certain routes.
Expand Down
31 changes: 25 additions & 6 deletions src/internal/deprecated.ts
@@ -1,28 +1,47 @@
/* eslint-disable jsdoc/check-tag-names */
/* eslint-disable jsdoc/require-param */

/**
* A deprecation should never be done in a patch.
*/
type DeprecationSemVer = `${number}.${number}`;

/** @internal */
export interface DeprecatedOptions {
/**
* The name of the function, following the syntax `faker.[module].[function]()`.
*/
deprecated: string;
/**
* An alternative solution.
*/
proposed?: string;
since?: string;
until?: string;
/**
* The semver since when this is deprecated.
*/
since?: DeprecationSemVer;
/**
* The semver when this will be removed.
*/
until?: DeprecationSemVer;
}

/** @internal */
/**
* @internal
*/
export function deprecated(opts: DeprecatedOptions): void {
let message = `[@faker-js/faker]: ${opts.deprecated} is deprecated`;

if (opts.since) {
message += ` since ${opts.since}`;
message += ` since v${opts.since}`;
}

if (opts.until) {
message += ` and will be removed in ${opts.until}`;
message += ` and will be removed in v${opts.until}`;
}

if (opts.proposed) {
message += `. Please use ${opts.proposed} instead`;
message += `. Please use ${opts.proposed} instead.`;
}

console.warn(`${message}.`);
Expand Down
18 changes: 10 additions & 8 deletions src/modules/address/index.ts
Expand Up @@ -76,8 +76,8 @@ export class Address {
deprecated({
deprecated: 'faker.address.city(format)',
proposed: 'faker.address.city() or faker.fake(format)',
since: 'v7.0',
until: 'v8.0',
since: '7.0',
until: '8.0',
});
}
const formats = this.faker.definitions.address.city;
Expand All @@ -98,13 +98,14 @@ export class Address {
* faker.address.cityPrefix() // 'East'
*
* @deprecated
* Use `faker.address.city()` instead.
*/
cityPrefix(): string {
deprecated({
deprecated: 'faker.address.cityPrefix()',
proposed: "faker.address.city() or faker.fake('{{address.city_prefix}}')",
since: 'v7.2',
until: 'v8.0',
since: '7.2',
until: '8.0',
});
return this.faker.helpers.arrayElement(
this.faker.definitions.address.city_prefix
Expand All @@ -120,13 +121,14 @@ export class Address {
* faker.address.citySuffix() // 'mouth'
*
* @deprecated
* Use `faker.address.city()` instead.
*/
citySuffix(): string {
deprecated({
deprecated: 'faker.address.citySuffix()',
proposed: "faker.address.city() or faker.fake('{{address.city_suffix}}')",
since: 'v7.2',
until: 'v8.0',
since: '7.2',
until: '8.0',
});
return this.faker.helpers.arrayElement(
this.faker.definitions.address.city_suffix
Expand Down Expand Up @@ -185,8 +187,8 @@ export class Address {
'faker.address.streetName() without address.street_name definitions',
proposed:
'faker.address.street() or provide address.street_name definitions',
since: 'v7.0',
until: 'v8.0',
since: '7.0',
until: '8.0',
});
return this.street();
}
Expand Down
5 changes: 3 additions & 2 deletions src/modules/commerce/index.ts
Expand Up @@ -22,13 +22,14 @@ export class Commerce {
* faker.commerce.color() // 'red'
*
* @deprecated
* Use `faker.color.human()` instead.
*/
color(): string {
deprecated({
deprecated: 'faker.commerce.color()',
proposed: 'faker.color.human()',
since: 'v7.0.0',
until: 'v8.0.0',
since: '7.0',
until: '8.0',
});
return this.faker.color.human();
}
Expand Down
14 changes: 8 additions & 6 deletions src/modules/phone/index.ts
Expand Up @@ -33,8 +33,8 @@ export class Phone {
deprecated({
deprecated: 'faker.phone.phoneNumber()',
proposed: 'faker.phone.number()',
since: 'v7.3',
until: 'v8.0',
since: '7.3',
until: '8.0',
});
return this.faker.phone.number(format);
}
Expand Down Expand Up @@ -71,14 +71,15 @@ export class Phone {
* faker.phone.phoneNumberFormat(3) // '282.652.3201'
*
* @deprecated
* Use faker.phone.phoneNumber() instead.
*/
phoneNumberFormat(phoneFormatsArrayIndex = 0): string {
deprecated({
deprecated: 'faker.phone.phoneNumberFormat()',
proposed:
'faker.phone.phoneNumber() or faker.helpers.replaceSymbolWithNumber(format)',
since: 'v7.0',
until: 'v8.0',
since: '7.0',
until: '8.0',
});
return this.faker.helpers.replaceSymbolWithNumber(
this.faker.definitions.phone_number.formats[phoneFormatsArrayIndex]
Expand All @@ -95,13 +96,14 @@ export class Phone {
* faker.phone.phoneFormats() // '!##.!##.####'
*
* @deprecated
* Use `faker.phone.phoneNumber()` instead.
*/
phoneFormats(): string {
deprecated({
deprecated: 'faker.phone.phoneFormats()',
proposed: 'faker.phone.phoneNumber()',
since: 'v7.0',
until: 'v8.0',
since: '7.0',
until: '8.0',
});
return this.faker.helpers.arrayElement(
this.faker.definitions.phone_number.formats
Expand Down
4 changes: 2 additions & 2 deletions src/modules/random/index.ts
Expand Up @@ -273,8 +273,8 @@ export class Random {
deprecated({
deprecated: 'faker.random.alpha({ upcase: true })',
proposed: "faker.random.alpha({ casing: 'upper' })",
since: 'v7.0',
until: 'v8.0',
since: '7.0',
until: '8.0',
});
}

Expand Down

0 comments on commit 6d3f42e

Please sign in to comment.