Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra(unicorn): prefer-at #2654

Merged
merged 9 commits into from
Feb 19, 2024
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
7 changes: 7 additions & 0 deletions docs/guide/upgrading_v9/2654.md
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
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.
Copy link
Member

@ST-DDT ST-DDT Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note from my future me: We will have at most a very minimum description of "how to transpile down for node < x". I won't help them shoot themselves in the foot and complain to us about it.

And I won't keep track of any node <18 feature usage myself unless specifically requested by our community.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are stuck using node 14/16 you should just stay on faker@8 rather than try to transpile things.

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 @@
}

// 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;

Check warning on line 993 in src/modules/helpers/index.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/index.ts#L992-L993

Added lines #L992 - L993 were not covered by tests
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
}

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