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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions rules/utils/is-same-reference.js
@@ -1,7 +1,7 @@
'use strict';
const {getStaticValue} = require('eslint-utils');

// Copied from https://github.com/eslint/eslint/blob/c3e9accce2f61b04ab699fd37c90703305281aa3/lib/rules/utils/ast-utils.js#L379
// Copied from https://github.com/eslint/eslint/blob/94ba68d76a6940f68ff82eea7332c6505f93df76/lib/rules/utils/ast-utils.js#L392

/**
Gets the property name of a given node.
Expand Down Expand Up @@ -128,7 +128,8 @@ function isSameReference(left, right) {
return true;
}

case 'Identifier': {
case 'Identifier':
case 'PrivateIdentifier': {
return left.name === right.name;
}

Expand All @@ -143,11 +144,23 @@ function isSameReference(left, right) {
case 'MemberExpression': {
const nameA = getStaticPropertyName(left);

// X.y = x["y"]
// `x.y = x["y"]`
if (typeof nameA !== 'undefined') {
return (
isSameReference(left.object, right.object)
&& nameA === getStaticPropertyName(right)
);
}

/*
`x[0] = x[0]`
`x[y] = x[y]`
`x.y = x.y`
*/
return (
typeof nameA !== 'undefined'
left.computed === right.computed
&& isSameReference(left.object, right.object)
&& nameA === getStaticPropertyName(right)
&& isSameReference(left.property, right.property)
);
}

Expand Down
13 changes: 10 additions & 3 deletions test/no-array-push-push.mjs
Expand Up @@ -209,9 +209,6 @@ test.snapshot({
test({
valid: [
outdent`
a[x].push(1);
a[x].push(2);

'1'.someMagicPropertyReturnsAnArray.push(1);
(1).someMagicPropertyReturnsAnArray.push(2);

Expand Down Expand Up @@ -284,5 +281,15 @@ test({
`,
errors: 9,
},
{
code: outdent`
a[x].push(1);
a[x].push(2);
`,
output: outdent`
a[x].push(1, 2);
`,
errors: 1,
},
],
});
3 changes: 3 additions & 0 deletions test/prefer-at.mjs
Expand Up @@ -18,6 +18,7 @@ test.snapshot({
'++ array[array.length - 1]',
'array[array.length - 1] --',
'delete array[array.length - 1]',
'class Foo {bar; #bar; baz() {return this.#bar[this.bar.length - 1]}}',
],
invalid: [
'array[array.length - 1];',
Expand All @@ -38,6 +39,8 @@ test.snapshot({
'const {a = array[array.length - 1]} = {}',
'typeof array[array.length - 1]',
'function foo() {return arguments[arguments.length - 1]}',
'class Foo {bar; baz() {return this.bar[this.bar.length - 1]}}',
'class Foo {#bar; baz() {return this.#bar[this.#bar.length - 1]}}',
],
});

Expand Down
32 changes: 32 additions & 0 deletions test/snapshots/prefer-at.mjs.md
Expand Up @@ -286,6 +286,38 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^ Prefer \`.at(…)\` over \`[….length - index]\`.␊
`

## Invalid #19
1 | class Foo {bar; baz() {return this.bar[this.bar.length - 1]}}

> Output

`␊
1 | class Foo {bar; baz() {return this.bar.at(-1)}}␊
`

> Error 1/1

`␊
> 1 | class Foo {bar; baz() {return this.bar[this.bar.length - 1]}}␊
| ^^^^^^^^^^^^^^^^^^^ Prefer \`.at(…)\` over \`[….length - index]\`.␊
`

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

> Output

`␊
1 | class Foo {#bar; baz() {return this.#bar.at(-1)}}␊
`

> Error 1/1

`␊
> 1 | class Foo {#bar; baz() {return this.#bar[this.#bar.length - 1]}}␊
| ^^^^^^^^^^^^^^^^^^^^ Prefer \`.at(…)\` over \`[….length - index]\`.␊
`

## Invalid #1
1 | string.charAt(string.length - 1);

Expand Down
Binary file modified test/snapshots/prefer-at.mjs.snap
Binary file not shown.