Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
prefer-at: Support private field (#1929)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
regseb and fisker committed Nov 8, 2022
1 parent ba21fc0 commit 60bb455
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
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.

0 comments on commit 60bb455

Please sign in to comment.