diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 287c1fb4112..941397c9302 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -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 @ST-DDT 2023-10-28: The following rule should be turned on when we switch to esm. 'unicorn/prefer-top-level-await': 'off', 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. 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..1cdcf3e1970 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,8 @@ export function extractSummaryDefault( if (eraseDefault) { summary.splice(-2, 2); - const lastSummaryPart = summary[summary.length - 1]; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const lastSummaryPart = summary.at(-1)!; lastSummaryPart.text = lastSummaryPart.text.replace( /[ \n]Defaults to $/, '' diff --git a/scripts/generate-locales.ts b/scripts/generate-locales.ts index bebfea5fc82..4f35828ff24 100644 --- a/scripts/generate-locales.ts +++ b/scripts/generate-locales.ts @@ -128,11 +128,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 73728f78776..bd9057403e2 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 7b0f1086f30..e98ec2da870 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+$/); });