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

prefer-at: Support private field #1929

Merged
merged 7 commits into from Nov 8, 2022
Merged

prefer-at: Support private field #1929

merged 7 commits into from Nov 8, 2022

Conversation

regseb
Copy link
Contributor

@regseb regseb commented Oct 9, 2022

Fix #1928

I added case 'PrivateIdentifier': in isSameReference() (cf. #1928 (comment)) but the private fields are still not supported.

prefer-at.mjs › Invalid #20
        1 | class Foo {#bar; baz() {return this.#bar[this.#bar.length - 1]}}

  Invalid case should have at least one error.

  Value is deeply equal:

  []

  › file://test/utils/snapshot-rule-tester.mjs:188:8

  ─

  1 test failed

@fisker
Copy link
Collaborator

fisker commented Oct 9, 2022

MemberExpression need update too.

@regseb
Copy link
Contributor Author

regseb commented Oct 9, 2022

Tests passed with "PrivateIdentifier" in getStaticPropertyName(). 2a9e359#diff

There isn't "PrivateIdentifier" in ESLint. There is a bug in ESLlint? (I don't find a testcase)

@fisker
Copy link
Collaborator

fisker commented Oct 9, 2022

I mean this part https://github.com/eslint/eslint/blob/94ba68d76a6940f68ff82eea7332c6505f93df76/lib/rules/utils/ast-utils.js#L420-L443, your solution isn't correct, it will cause this.#foo[this.foo.length - 1] pass.

@regseb
Copy link
Contributor Author

regseb commented Nov 1, 2022

With my last commit e95cec1, the tests on the private fields are successful.

But 4 tests fail because messages are reported for valid codes:

# Source Message
1 a[x].push(1); a[x].push(2); no-array-push-push: Do not call Array#push() multiple times.
2 foo[bar++].slice(foo[bar++].length - 1) prefer-negative-index: Prefer negative index over length minus index for slice.
3 foo[a + b].slice(foo[a + b].length - 1) prefer-negative-index: Prefer negative index over length minus index for slice.
4 foo[`${bar}`].slice(foo[`${bar}`].length - 1) prefer-negative-index: Prefer negative index over length minus index for slice._

I don't know how to correct this problem. 😞


I tested with the no-self-assign rule of ESLint (which uses the isSameReference function). Only case 1 has a problem.

ESLint Playground:

/* eslint no-self-assign: "error" */

let foo = ["a", "b", "c"];
let bar = 0;
let baz = 0;
/* 1 */ foo[bar] = foo[bar];
/* 2 */ foo[bar++] = foo[bar++];
/* 3 */ foo[bar + baz] = foo[bar + baz];
/* 4 */ foo[`${bar}`] = foo[`${bar}`];

6:20 'foo[bar]' is assigned to itself. (no-self-assign)

@fisker fisker changed the title prefer-at: Support private field. prefer-at: Support private field Nov 7, 2022
@sindresorhus sindresorhus merged commit 60bb455 into sindresorhus:main Nov 8, 2022
@regseb regseb deleted the prefer-at-private branch November 10, 2022 17:28
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.

prefer-at rule doesn't support private class members
3 participants