Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-prototype-methods: Update tests #1734

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rules/prefer-prototype-methods.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.