Skip to content

Add custom eslint rule 'no-array-mutating-method-expressions' #59526

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

Merged
merged 6 commits into from
Aug 14, 2024

Conversation

jakebailey
Copy link
Member

Motivated by the bug found in #59523.

Needs tests.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Aug 3, 2024
@RyanCavanaugh
Copy link
Member

We could instead add an earlier overload via declaration merging to return void. Might be less work and/or better, haven't tried it

@jakebailey
Copy link
Member Author

I'm not totally sure how we'd add an overload before the lib.d.ts, but I guess it must be possible because ts-reset or whatever can do it?

But, that wouldn't allow us to ignore the calls that happen after we're sure the array has been cloned (which is what this PR does); we don't have that many calls to sort but most of them are actually ignored by this rule.

@RyanCavanaugh
Copy link
Member

Overloads from declaration merging to in "before" the overloads they merge with

@jakebailey
Copy link
Member Author

Planning on going through the list of mutating methods from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#copying_methods_and_mutating_methods and adding them here too; same kind of bug.

@jakebailey jakebailey changed the title Add custom eslint rule 'no-array-sort-return' Add custom eslint rule 'no-array-mutation-method-returns' Aug 6, 2024
@jakebailey jakebailey marked this pull request as ready for review August 9, 2024 20:00
@jakebailey jakebailey force-pushed the eslint-no-array-sort-return branch from 5ed0892 to eb5e5ab Compare August 13, 2024 21:58
@@ -93,7 +94,7 @@ export function patchServiceForStateBaseline(service: ProjectService) {
function sendLogsToLogger(title: string, logs: StateItemLog[] | undefined) {
if (!logs) return;
logger.log(title);
logs.sort((a, b) => compareStringsCaseSensitive(a[0], b[0]))
toSorted(logs, (a, b) => compareStringsCaseSensitive(a[0], b[0]))
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a bug.

@@ -4058,7 +4058,7 @@ export class TestState {
public verifyRefactorKindsAvailable(kind: string, expected: string[], preferences = ts.emptyOptions) {
const refactors = this.getApplicableRefactorsAtSelection("invoked", kind, preferences);
const availableKinds = ts.flatMap(refactors, refactor => refactor.actions).map(action => action.kind);
assert.deepEqual(availableKinds.sort(), expected.sort(), `Expected kinds to be equal`);
assert.deepEqual(availableKinds.slice().sort(), expected.slice().sort(), `Expected kinds to be equal`);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a bug, more or less.

@@ -12,7 +12,7 @@ function testExtractRangeFailed(caption: string, s: string, expectedErrors: stri
const result = ts.refactor.extractSymbol.getRangeToExtract(file, ts.createTextSpanFromRange(selectionRange), /*invoked*/ false);
assert(result.targetRange === undefined, "failure expected");
const sortedErrors = result.errors.map(e => e.messageText as string).sort();
assert.deepEqual(sortedErrors, expectedErrors.sort(), "unexpected errors");
assert.deepEqual(sortedErrors, expectedErrors.slice().sort(), "unexpected errors");
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a bug.

@jakebailey jakebailey changed the title Add custom eslint rule 'no-array-mutation-method-returns' Add custom eslint rule 'no-array-mutating-method-expressions' Aug 13, 2024
@jakebailey jakebailey merged commit 25e09d9 into microsoft:main Aug 14, 2024
32 checks passed
@jakebailey jakebailey deleted the eslint-no-array-sort-return branch August 14, 2024 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants