diff --git a/rules/utils/is-same-reference.js b/rules/utils/is-same-reference.js index 112c807912..6f388a5372 100644 --- a/rules/utils/is-same-reference.js +++ b/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. @@ -128,7 +128,8 @@ function isSameReference(left, right) { return true; } - case 'Identifier': { + case 'Identifier': + case 'PrivateIdentifier': { return left.name === right.name; } @@ -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) ); } diff --git a/test/no-array-push-push.mjs b/test/no-array-push-push.mjs index 572c2e07c4..2f923bdd31 100644 --- a/test/no-array-push-push.mjs +++ b/test/no-array-push-push.mjs @@ -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); @@ -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, + }, ], }); diff --git a/test/prefer-at.mjs b/test/prefer-at.mjs index bdb67c54e2..15253fca34 100644 --- a/test/prefer-at.mjs +++ b/test/prefer-at.mjs @@ -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];', @@ -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]}}', ], }); diff --git a/test/snapshots/prefer-at.mjs.md b/test/snapshots/prefer-at.mjs.md index 9e911d93b5..64bf551ab8 100644 --- a/test/snapshots/prefer-at.mjs.md +++ b/test/snapshots/prefer-at.mjs.md @@ -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); diff --git a/test/snapshots/prefer-at.mjs.snap b/test/snapshots/prefer-at.mjs.snap index cfcec10ed5..269bd260b4 100644 Binary files a/test/snapshots/prefer-at.mjs.snap and b/test/snapshots/prefer-at.mjs.snap differ