Skip to content

Commit

Permalink
infra(unicorn)!: prefer-at
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Feb 9, 2024
1 parent bc3ebb7 commit 2f1f109
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 23 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docs/guide/upgrading_v9/2654.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion scripts/apidoc/parameter-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
4 changes: 2 additions & 2 deletions scripts/apidoc/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function selectApiSignature(
throw new Error(`Method ${method.name} has no signature.`);
}

return signatures[signatures.length - 1];
return signatures.at(-1);

Check failure on line 126 in scripts/apidoc/typedoc.ts

View workflow job for this annotation

GitHub Actions / TS-Check: node-20, ubuntu-latest

Type 'SignatureReflection | undefined' is not assignable to type 'SignatureReflection'.
}

/**
Expand Down Expand Up @@ -313,7 +313,7 @@ export function extractSummaryDefault(

if (eraseDefault) {
summary.splice(-2, 2);
const lastSummaryPart = summary[summary.length - 1];
const lastSummaryPart = summary.at(-1);
lastSummaryPart.text = lastSummaryPart.text.replace(

Check failure on line 317 in scripts/apidoc/typedoc.ts

View workflow job for this annotation

GitHub Actions / TS-Check: node-20, ubuntu-latest

'lastSummaryPart' is possibly 'undefined'.

Check failure on line 317 in scripts/apidoc/typedoc.ts

View workflow job for this annotation

GitHub Actions / TS-Check: node-20, ubuntu-latest

'lastSummaryPart' is possibly 'undefined'.
/[ \n]Defaults to $/,
''
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ async function generateLocaleFile(locale: string): Promise<void> {
}

// 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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ export class SimpleHelpersModule extends SimpleModuleBase {
}

// In case of rounding errors, return the last element
return array[array.length - 1].value;
return array.at(-1).value;

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-18, ubuntu-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-20, ubuntu-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-18, macos-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Timezone Test: node-20, ubuntu-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-20, macos-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-18, windows-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-20, windows-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / TS-Check: node-20, ubuntu-latest

Object is possibly 'undefined'.

Check failure on line 992 in src/modules/helpers/index.ts

View workflow job for this annotation

GitHub Actions / Codecov: node-20, ubuntu-latest

Object is possibly 'undefined'.
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/modules/color.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/modules/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
});
Expand Down
22 changes: 11 additions & 11 deletions test/modules/lorem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))(
Expand All @@ -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(' ');

Expand Down Expand Up @@ -182,15 +182,15 @@ 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) => {
const actual = faker.lorem.sentences(sentenceCount);

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual[actual.length - 1]).toBe('.');
expect(actual.at(-1)).toBe('.');

const sentences = actual.split('. ');

Expand All @@ -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('.');
}
}
);
Expand All @@ -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))(
Expand All @@ -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('. ');

Expand Down Expand Up @@ -274,15 +274,15 @@ 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) => {
const actual = faker.lorem.paragraphs(paragraphCount);

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(actual[actual.length - 1]).toBe('.');
expect(actual.at(-1)).toBe('.');

const paragraphs = actual.split('\n');

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/modules/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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+$/);
});
Expand Down

0 comments on commit 2f1f109

Please # to comment.