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

Fix FP S2871 (no-alphabetical-sort): Rework the rule to emit a dedicated message for arrays of strings #4539

Merged
merged 4 commits into from Jan 31, 2024

Conversation

ericmorand-sonarsource
Copy link
Contributor

Fixes #4531

@@ -53,6 +53,8 @@ export const rule: Rule.RuleModule = {
messages: {
provideCompareFunction:
'Provide a compare function to avoid sorting elements alphabetically.',
provideCompareFunctionForArrayOfStrings:
'Provide a compare function that depends on String.localeCompare, to reliably sort elements alphabetically.',

Choose a reason for hiding this comment

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

This looks great!

Choose a reason for hiding this comment

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

Suggested change
'Provide a compare function that depends on String.localeCompare, to reliably sort elements alphabetically.',
'Provide a compare function that depends on "String.localeCompare", to reliably sort elements alphabetically.',

@@ -606,6 +606,8 @@ ruleTester.run(
code: 'const array = ["foo", "bar"]; const sortedArray = array.toSorted();',
errors: [
{
message:
'Provide a compare function that depends on String.localeCompare, to reliably sort elements alphabetically.',

Choose a reason for hiding this comment

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

Might need to apply this suggestion if you applied the one I suggested above.

Suggested change
'Provide a compare function that depends on String.localeCompare, to reliably sort elements alphabetically.',
'Provide a compare function that depends on "String.localeCompare", to reliably sort elements alphabetically.',

Comment on lines 34 to 43
<h3>Exceptions</h3>
<p>The rule ignores arrays that only contain strings, or are declared as such.</p>
<pre>
const names = ['foo', 'bar'];
names.sort(); // Compliant
</pre>
<pre>
const names: string[] = [];
names.sort(); // Compliant
</pre>

Choose a reason for hiding this comment

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

I guess the exception no longer holds and we can get rid of this from the RSPEC, right?

@yassin-kammoun-sonarsource
Copy link
Contributor

We could also update the PR's title since it's no longer about adding an exception for string arrays but rather having a specific message for string arrays.

@ericmorand-sonarsource ericmorand-sonarsource changed the title Fix FP S2871 (no-alphabetical-sort): Add exception for string arrays Fix FP S2871 (no-alphabetical-sort): Rework the rule to emit a dedicated message for arrays of strings Jan 31, 2024
Copy link
Contributor

@ilia-kebets-sonarsource ilia-kebets-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM!

@ericmorand-sonarsource ericmorand-sonarsource merged commit 9870bba into master Jan 31, 2024
16 checks passed
@ericmorand-sonarsource ericmorand-sonarsource deleted the issue-4531-S2871 branch January 31, 2024 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix FP S2871 (no-alphabetical-sort): Add exception for string arrays
3 participants