Skip to content

Commit

Permalink
prefer-prototype-methods: Update tests (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 17, 2022
1 parent e6de810 commit 5ffdf5d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 35 deletions.
2 changes: 1 addition & 1 deletion rules/prefer-prototype-methods.js
Expand Up @@ -46,7 +46,7 @@ function create(context) {
return {
node,
messageId: methodName ? 'known-method' : 'unknown-method',
data: {constructorName, methodName: String(methodName)},
data: {constructorName, methodName},
* fix(fixer) {
yield fixer.replaceText(node.object, `${constructorName}.prototype`);

Expand Down
39 changes: 5 additions & 34 deletions test/prefer-prototype-methods.mjs
Expand Up @@ -50,39 +50,10 @@ test.snapshot({
'const foo = ({}.toString.call)(bar);',
'function foo(){return[].slice.call(bar);}',
'function foo(){return{}.toString.call(bar)}',
],
});

test.babel({
testerOptions: {
env: {es2021: true},
},
valid: [],
invalid: [
{
code: 'Reflect.apply({}[Symbol()], baz, [])',
output: 'Reflect.apply(Object.prototype[Symbol()], baz, [])',
errors: [{message: 'Prefer using method from `Object.prototype`.'}],
},
{
code: 'Reflect.apply({}[Symbol("symbol description")], baz, [])',
output: 'Reflect.apply(Object.prototype[Symbol("symbol description")], baz, [])',
errors: [{message: 'Prefer using method from `Object.prototype`.'}],
},
{
code: 'Reflect.apply([][Symbol()], baz, [])',
output: 'Reflect.apply(Array.prototype[Symbol()], baz, [])',
errors: [{message: 'Prefer using method from `Array.prototype`.'}],
},
{
code: 'Reflect.apply({}[Symbol("symbol description")], baz, [])',
output: 'Reflect.apply(Object.prototype[Symbol("symbol description")], baz, [])',
errors: [{message: 'Prefer using method from `Object.prototype`.'}],
},
{
code: '[][Symbol.iterator].call(foo)',
output: 'Array.prototype[Symbol.iterator].call(foo)',
errors: [{message: 'Prefer using `Array.prototype.Symbol(Symbol.iterator)`.'}],
},
'Reflect.apply({}[Symbol()], baz, [])',
'Reflect.apply({}[Symbol("symbol description")], baz, [])',
'Reflect.apply([][Symbol()], baz, [])',
'Reflect.apply({}[Symbol("symbol description")], baz, [])',
'[][Symbol.iterator].call(foo)',
],
});
80 changes: 80 additions & 0 deletions test/snapshots/prefer-prototype-methods.mjs.md
Expand Up @@ -259,3 +259,83 @@ Generated by [AVA](https://avajs.dev).
> 1 | function foo(){return{}.toString.call(bar)}␊
| ^^^^^^^^^^^ Prefer using \`Object.prototype.toString\`.␊
`

## Invalid #17
1 | Reflect.apply({}[Symbol()], baz, [])

> Output
`␊
1 | Reflect.apply(Object.prototype[Symbol()], baz, [])␊
`

> Error 1/1
`␊
> 1 | Reflect.apply({}[Symbol()], baz, [])␊
| ^^^^^^^^^^^^ Prefer using method from \`Object.prototype\`.␊
`

## Invalid #18
1 | Reflect.apply({}[Symbol("symbol description")], baz, [])

> Output
`␊
1 | Reflect.apply(Object.prototype[Symbol("symbol description")], baz, [])␊
`

> Error 1/1
`␊
> 1 | Reflect.apply({}[Symbol("symbol description")], baz, [])␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using method from \`Object.prototype\`.␊
`

## Invalid #19
1 | Reflect.apply([][Symbol()], baz, [])

> Output
`␊
1 | Reflect.apply(Array.prototype[Symbol()], baz, [])␊
`

> Error 1/1
`␊
> 1 | Reflect.apply([][Symbol()], baz, [])␊
| ^^^^^^^^^^^^ Prefer using method from \`Array.prototype\`.␊
`

## Invalid #20
1 | Reflect.apply({}[Symbol("symbol description")], baz, [])

> Output
`␊
1 | Reflect.apply(Object.prototype[Symbol("symbol description")], baz, [])␊
`

> Error 1/1
`␊
> 1 | Reflect.apply({}[Symbol("symbol description")], baz, [])␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using method from \`Object.prototype\`.␊
`

## Invalid #21
1 | [][Symbol.iterator].call(foo)

> Output
`␊
1 | Array.prototype[Symbol.iterator].call(foo)␊
`

> Error 1/1
`␊
> 1 | [][Symbol.iterator].call(foo)␊
| ^^^^^^^^^^^^^^^^^^^ Prefer using method from \`Array.prototype\`.␊
`
Binary file modified test/snapshots/prefer-prototype-methods.mjs.snap
Binary file not shown.

0 comments on commit 5ffdf5d

Please sign in to comment.