From 346d52b4a4356d3532c1dc0bb032f831222dcfd7 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 9 Feb 2024 19:33:09 +0100 Subject: [PATCH 1/4] infra(unicorn)!: prefer-at --- .eslintrc.js | 2 -- docs/guide/upgrading_v9/2654.md | 6 ++++++ scripts/apidoc/parameter-defaults.ts | 2 +- scripts/apidoc/typedoc.ts | 7 +++++-- scripts/generate-locales.ts | 4 ++-- src/modules/helpers/index.ts | 3 ++- test/modules/color.spec.ts | 4 ++-- test/modules/date.spec.ts | 2 +- test/modules/lorem.spec.ts | 22 +++++++++++----------- test/modules/system.spec.ts | 2 +- 10 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 docs/guide/upgrading_v9/2654.md diff --git a/.eslintrc.js b/.eslintrc.js index f4bb960d373..a2ed141143a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -44,8 +44,6 @@ module.exports = defineConfig({ 'unicorn/number-literal-case': 'off', // incompatible with prettier 'unicorn/prefer-ternary': 'off', // ternaries aren't always better - // TODO @Shinigami92 2023-09-23: prefer-at should be turned on when we drop support for Node 14. - 'unicorn/prefer-at': 'off', // TODO @Shinigami92 2023-09-23: prefer-string-replace-all should be turned on when we drop support for Node 14. 'unicorn/prefer-string-replace-all': 'off', // TODO @ST-DDT 2023-10-28: The following rule should be turned on when we switch to esm. diff --git a/docs/guide/upgrading_v9/2654.md b/docs/guide/upgrading_v9/2654.md new file mode 100644 index 00000000000..5c2b5dcddfa --- /dev/null +++ b/docs/guide/upgrading_v9/2654.md @@ -0,0 +1,6 @@ +### Use `at` over `[length - 1]` in code + +`at` is a feature that is only supported by Node >=v16.6, so this is a breaking change for users that are using Node < v16.6. +However, we dropped Node < v18 anyways. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at diff --git a/scripts/apidoc/parameter-defaults.ts b/scripts/apidoc/parameter-defaults.ts index c2eecdc27c9..a7f53c2bf78 100644 --- a/scripts/apidoc/parameter-defaults.ts +++ b/scripts/apidoc/parameter-defaults.ts @@ -33,7 +33,7 @@ export const parameterDefaultReader: EventCallback = ( reflection.kindOf(reflectionKindFunctionOrMethod) && symbol.declarations?.length ) { - const lastDeclaration = symbol.declarations[symbol.declarations.length - 1]; + const lastDeclaration = symbol.declarations.at(-1); if (TypeScript.isFunctionLike(lastDeclaration)) { (reflection as ParameterDefaultsAware).implementationDefaultParameters = lastDeclaration.parameters.map((param) => diff --git a/scripts/apidoc/typedoc.ts b/scripts/apidoc/typedoc.ts index 4d6a2b6c313..1b3cd79ea85 100644 --- a/scripts/apidoc/typedoc.ts +++ b/scripts/apidoc/typedoc.ts @@ -123,7 +123,8 @@ export function selectApiSignature( throw new Error(`Method ${method.name} has no signature.`); } - return signatures[signatures.length - 1]; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return signatures.at(-1)!; } /** @@ -313,7 +314,9 @@ export function extractSummaryDefault( if (eraseDefault) { summary.splice(-2, 2); - const lastSummaryPart = summary[summary.length - 1]; + const lastSummaryPart = + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + summary.at(-1)!; lastSummaryPart.text = lastSummaryPart.text.replace( /[ \n]Defaults to $/, '' diff --git a/scripts/generate-locales.ts b/scripts/generate-locales.ts index 4228cc91b3b..b459f04f11a 100644 --- a/scripts/generate-locales.ts +++ b/scripts/generate-locales.ts @@ -127,11 +127,11 @@ async function generateLocaleFile(locale: string): Promise { } // TODO @Shinigami92 2023-03-07: Remove 'en' fallback in a separate PR - if (locales[locales.length - 1] !== 'en' && locale !== 'base') { + if (locales.at(-1) !== 'en' && locale !== 'base') { locales.push('en'); } - if (locales[locales.length - 1] !== 'base') { + if (locales.at(-1) !== 'base') { locales.push('base'); } diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 635b3e09d4f..aa522329ee5 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -989,7 +989,8 @@ export class SimpleHelpersModule extends SimpleModuleBase { } // In case of rounding errors, return the last element - return array[array.length - 1].value; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return array.at(-1)!.value; } /** diff --git a/test/modules/color.spec.ts b/test/modules/color.spec.ts index 2bcb5734c0b..9eb64d61c50 100644 --- a/test/modules/color.spec.ts +++ b/test/modules/color.spec.ts @@ -122,8 +122,8 @@ describe('color', () => { format: 'decimal', includeAlpha: true, }); - expect(color[color.length - 1]).toBeGreaterThanOrEqual(0); - expect(color[color.length - 1]).toBeLessThanOrEqual(1); + expect(color.at(-1)).toBeGreaterThanOrEqual(0); + expect(color.at(-1)).toBeLessThanOrEqual(1); for (const value of color.slice(0, 4)) { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(255); diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts index 7cff8d33842..9d4ae9adf4b 100644 --- a/test/modules/date.spec.ts +++ b/test/modules/date.spec.ts @@ -362,7 +362,7 @@ describe('date', () => { expect(dates[i]).greaterThan(dates[i - 1]); } - expect(dates[dates.length - 1]).lessThan(to); + expect(dates.at(-1)).lessThan(to); } ); }); diff --git a/test/modules/lorem.spec.ts b/test/modules/lorem.spec.ts index 3a2062de154..39a9a1442ce 100644 --- a/test/modules/lorem.spec.ts +++ b/test/modules/lorem.spec.ts @@ -117,7 +117,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); }); it.each(times(25))( @@ -127,7 +127,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); const words = actual.split(' '); @@ -182,7 +182,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); }); it.each(times(10))('should return %i sentences', (sentenceCount) => { @@ -190,7 +190,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); const sentences = actual.split('. '); @@ -205,14 +205,14 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); const sentences = actual.split(separator); expect(sentences).toHaveLength(sentenceCount); for (const sentence of sentences) { - expect(sentence[sentence.length - 1]).toBe('.'); + expect(sentence.at(-1)).toBe('.'); } } ); @@ -236,7 +236,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); }); it.each(times(10))( @@ -246,7 +246,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); const sentences = actual.split('. '); @@ -274,7 +274,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); }); it.each(times(5))('should return %i paragraphs', (paragraphCount) => { @@ -282,7 +282,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); const paragraphs = actual.split('\n'); @@ -297,7 +297,7 @@ describe('lorem', () => { expect(actual).toBeTruthy(); expect(actual).toBeTypeOf('string'); - expect(actual[actual.length - 1]).toBe('.'); + expect(actual.at(-1)).toBe('.'); const paragraphs = actual.split(separator); diff --git a/test/modules/system.spec.ts b/test/modules/system.spec.ts index 70a53266000..f2107cb2244 100644 --- a/test/modules/system.spec.ts +++ b/test/modules/system.spec.ts @@ -271,7 +271,7 @@ describe('system', () => { 'generated filePath should start with /' ).toBeTruthy(); expect( - parts[parts.length - 1], + parts.at(-1), 'generated filePath should have a file extension' ).toMatch(/^\w+\.\w+$/); }); From bdfa07194f0b47bbbc839e7dc85eb08b6fb9e6e2 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 10 Feb 2024 10:25:38 +0100 Subject: [PATCH 2/4] removed extra file --- docs/guide/upgrading_v9/2121.md | 7 +++++++ docs/guide/upgrading_v9/2654.md | 6 ------ 2 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 docs/guide/upgrading_v9/2654.md diff --git a/docs/guide/upgrading_v9/2121.md b/docs/guide/upgrading_v9/2121.md index c2a2e8e422f..7ce91f2bf59 100644 --- a/docs/guide/upgrading_v9/2121.md +++ b/docs/guide/upgrading_v9/2121.md @@ -1,3 +1,10 @@ ### Node 14 and 16 No Longer Supported Support for Node.js versions 14 and 16 has been discontinued as these versions have reached their [end-of-life](https://github.com/nodejs/Release). Faker.js 9.0 requires a minimum of Node.js version 18. + +#### Examples of code-upgrades that are now used: + +`at` is a feature that is only supported by Node >=v16.6, so this is a breaking change for users that are using Node < v16.6. +However, we dropped Node < v18 anyways. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at diff --git a/docs/guide/upgrading_v9/2654.md b/docs/guide/upgrading_v9/2654.md deleted file mode 100644 index 5c2b5dcddfa..00000000000 --- a/docs/guide/upgrading_v9/2654.md +++ /dev/null @@ -1,6 +0,0 @@ -### Use `at` over `[length - 1]` in code - -`at` is a feature that is only supported by Node >=v16.6, so this is a breaking change for users that are using Node < v16.6. -However, we dropped Node < v18 anyways. - -https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at From 0c99c32423524d86bc5cedd02921ce31466dee17 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 16 Feb 2024 16:08:43 +0100 Subject: [PATCH 3/4] Reference other migration --- docs/guide/upgrading_v9/2121.md | 7 ------- docs/guide/upgrading_v9/2654.md | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 docs/guide/upgrading_v9/2654.md diff --git a/docs/guide/upgrading_v9/2121.md b/docs/guide/upgrading_v9/2121.md index 7ce91f2bf59..c2a2e8e422f 100644 --- a/docs/guide/upgrading_v9/2121.md +++ b/docs/guide/upgrading_v9/2121.md @@ -1,10 +1,3 @@ ### Node 14 and 16 No Longer Supported Support for Node.js versions 14 and 16 has been discontinued as these versions have reached their [end-of-life](https://github.com/nodejs/Release). Faker.js 9.0 requires a minimum of Node.js version 18. - -#### Examples of code-upgrades that are now used: - -`at` is a feature that is only supported by Node >=v16.6, so this is a breaking change for users that are using Node < v16.6. -However, we dropped Node < v18 anyways. - -https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at diff --git a/docs/guide/upgrading_v9/2654.md b/docs/guide/upgrading_v9/2654.md new file mode 100644 index 00000000000..698d5b854e3 --- /dev/null +++ b/docs/guide/upgrading_v9/2654.md @@ -0,0 +1,7 @@ +### Examples of code-upgrades that are now used + +_This upgrade is an extension to_ [#2121](./2121.md) + +Used Node >= v16.6 feature [`at`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at). + +We could hint users to use polyfill or transpile-down steps. From 4b81671c359239ba1849177571358f4504d3f9ff Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Fri, 16 Feb 2024 16:11:30 +0100 Subject: [PATCH 4/4] Use better code readability --- scripts/apidoc/typedoc.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/apidoc/typedoc.ts b/scripts/apidoc/typedoc.ts index 1b3cd79ea85..1cdcf3e1970 100644 --- a/scripts/apidoc/typedoc.ts +++ b/scripts/apidoc/typedoc.ts @@ -314,9 +314,8 @@ export function extractSummaryDefault( if (eraseDefault) { summary.splice(-2, 2); - const lastSummaryPart = - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - summary.at(-1)!; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const lastSummaryPart = summary.at(-1)!; lastSummaryPart.text = lastSummaryPart.text.replace( /[ \n]Defaults to $/, ''