Skip to content

Commit 74634bc

Browse files
committed
fix: issues with no-get autofix with array access in nested path
1 parent fcdb389 commit 74634bc

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/rules/no-get.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ function fixGet({
6565
// Replace any array element access (foo.1 => foo[1] or foo?.[1]).
6666
replacementPath = replacementPath
6767
.replace(/\.(\d+)/g, isInLeftSideOfAssignmentExpression ? '[$1]' : '.[$1]') // Usages in middle of path.
68-
.replace(/^(\d+)\?\./, isInLeftSideOfAssignmentExpression ? '[$1]' : '[$1]?.'); // Usage at beginning of path.
68+
.replace(/^(\d+)\??\./, isInLeftSideOfAssignmentExpression ? '[$1].' : '[$1]?.') // Usage at beginning of path.
69+
.replace(/^(\d+)$/, '[$1]'); // Usage as entire string.
6970

7071
// Add parenthesis around the object text in case of something like this: get(foo || {}, 'bar')
7172
const objectTextSafe = isValidJSPath(objectText) ? objectText : `(${objectText})`;

tests/lib/rules/no-get.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ ruleTester.run('no-get', rule, {
425425
],
426426
},
427427
{
428-
// Handle array element access with optional chaining.
429-
code: "this.get('foo1.0.bar1bar.1')",
428+
// Handle array element access with optional chaining (beginning/middle/end of string).
429+
code: "this.get('0.foo1.1.2.bar1bar.3')",
430430
options: [{ useOptionalChaining: true }],
431-
output: 'this.foo1?.[0]?.bar1bar?.[1]',
431+
output: 'this[0]?.foo1?.[1]?.[2]?.bar1bar?.[3]',
432432
errors: [
433433
{
434434
message: ERROR_MESSAGE_GET,
@@ -437,10 +437,10 @@ ruleTester.run('no-get', rule, {
437437
],
438438
},
439439
{
440-
// Handle array element access at beginning of string, with optional chaining.
441-
code: "this.get('0.foo')",
440+
// Handle array element access as entire string.
441+
code: "this.get('0')",
442442
options: [{ useOptionalChaining: true }],
443-
output: 'this[0]?.foo',
443+
output: 'this[0]',
444444
errors: [
445445
{
446446
message: ERROR_MESSAGE_GET,
@@ -449,9 +449,20 @@ ruleTester.run('no-get', rule, {
449449
],
450450
},
451451
{
452-
// Handle array element access (left side of an assignment).
453-
code: "this.get('foo.0.bar')[123] = 'hello world';",
454-
output: "this.foo[0].bar[123] = 'hello world';",
452+
// Handle array element access (left side of an assignment, beginning/middle/end of string).
453+
code: "this.get('0.foo.1.bar.2')[123] = 'hello world';",
454+
output: "this[0].foo[1].bar[2][123] = 'hello world';",
455+
errors: [
456+
{
457+
message: ERROR_MESSAGE_GET,
458+
type: 'CallExpression',
459+
},
460+
],
461+
},
462+
{
463+
// Handle array element access (left side of an assignment, entire string).
464+
code: "this.get('0')[123] = 'hello world';",
465+
output: "this[0][123] = 'hello world';",
455466
errors: [
456467
{
457468
message: ERROR_MESSAGE_GET,

0 commit comments

Comments
 (0)