Skip to content

Commit cec7f11

Browse files
authored
prefer-prototype-methods: Fix a missing condition on object literal check (#1332)
1 parent 8ca5b46 commit cec7f11

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

rules/prefer-prototype-methods.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function create(context) {
8484
if (
8585
functionNode &&
8686
functionNode.parent.type === 'Property' &&
87+
functionNode.parent.value === functionNode &&
8788
functionNode.parent.parent.type === 'ObjectExpression'
8889
) {
8990
return;

test/prefer-prototype-methods.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ test.snapshot({
8585
}
8686
}
8787
`,
88+
outdent`
89+
const foo = {
90+
[function() {this.method.bind(this);}]: 1
91+
}
92+
`,
8893
outdent`
8994
const {foo} = {foo: {}};
9095
const bar = foo.method.call(foo, 'property');

test/snapshots/prefer-prototype-methods.mjs.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ Generated by [AVA](https://avajs.dev).
263263
`
264264

265265
## Invalid #2
266+
1 | const foo = {
267+
2 | [function() {this.method.bind(this);}]: 1
268+
3 | }
269+
270+
> Error 1/1
271+
272+
`␊
273+
1 | const foo = {␊
274+
> 2 | [function() {this.method.bind(this);}]: 1␊
275+
| ^^^^^^^^^^^ Prefer using \`method\` method from the constructor prototype.␊
276+
3 | }␊
277+
`
278+
279+
## Invalid #3
266280
1 | const {foo} = {foo: {}};
267281
2 | const bar = foo.method.call(foo, 'property');
268282

@@ -274,7 +288,7 @@ Generated by [AVA](https://avajs.dev).
274288
| ^^^^^^^^^^ Prefer using \`method\` method from the constructor prototype.␊
275289
`
276290

277-
## Invalid #3
291+
## Invalid #4
278292
1 | const [foo] = [{}];
279293
2 | const bar = foo.method.call(foo, 'property');
280294

@@ -286,7 +300,7 @@ Generated by [AVA](https://avajs.dev).
286300
| ^^^^^^^^^^ Prefer using \`method\` method from the constructor prototype.␊
287301
`
288302

289-
## Invalid #4
303+
## Invalid #5
290304
1 | const foo = {
291305
2 | a() {
292306
3 | this.propertyIsEnumerable.apply(this, []);
@@ -304,7 +318,7 @@ Generated by [AVA](https://avajs.dev).
304318
5 | }␊
305319
`
306320

307-
## Invalid #5
321+
## Invalid #6
308322
1 | const foo = {
309323
2 | a() {
310324
3 | function fn() {
@@ -330,7 +344,7 @@ Generated by [AVA](https://avajs.dev).
330344
9 | }␊
331345
`
332346

333-
## Invalid #6
347+
## Invalid #7
334348
1 | this.method = this.method.bind(this)
335349

336350
> Error 1/1
@@ -340,7 +354,7 @@ Generated by [AVA](https://avajs.dev).
340354
| ^^^^^^^^^^^ Prefer using \`method\` method from the constructor prototype.␊
341355
`
342356

343-
## Invalid #7
357+
## Invalid #8
344358
1 | const foo = {};
345359
2 | const bar = foo.hasOwnProperty.call(foo, 'property');
346360

@@ -352,7 +366,7 @@ Generated by [AVA](https://avajs.dev).
352366
| ^^^^^^^^^^^^^^^^^^ Prefer using \`hasOwnProperty\` method from the constructor prototype.␊
353367
`
354368

355-
## Invalid #8
369+
## Invalid #9
356370
1 | for (const foo of []) foo.bar.call(foo)
357371

358372
> Error 1/1
@@ -362,7 +376,7 @@ Generated by [AVA](https://avajs.dev).
362376
| ^^^^^^^ Prefer using \`bar\` method from the constructor prototype.␊
363377
`
364378

365-
## Invalid #9
379+
## Invalid #10
366380
1 | let foo = {};
367381
2 | const bar = foo.method.call(foo, 'property');
368382

@@ -374,7 +388,7 @@ Generated by [AVA](https://avajs.dev).
374388
| ^^^^^^^^^^ Prefer using \`method\` method from the constructor prototype.␊
375389
`
376390

377-
## Invalid #10
391+
## Invalid #11
378392
1 | ({method() {}}).propertyIsEnumerable.call(foo)
379393

380394
> Error 1/1
46 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)