Skip to content

Commit

Permalink
infra(unicorn): prefer-at (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Feb 19, 2024
1 parent fd31ec6 commit 9882760
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 23 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.cjs
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 @ST-DDT 2023-10-28: The following rule should be turned on when we switch to esm.
'unicorn/prefer-top-level-await': 'off',

Expand Down
7 changes: 7 additions & 0 deletions docs/guide/upgrading_v9/2654.md
Original file line number Diff line number Diff line change
@@ -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.
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
6 changes: 4 additions & 2 deletions scripts/apidoc/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!;
}

/**
Expand Down Expand Up @@ -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 $/,
''
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,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
3 changes: 2 additions & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
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 9882760

Please sign in to comment.