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

no-array-method-this-argument: Check Array.fromAsync() #2330

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rules/no-array-method-this-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ const create = context => {
});
});

// `Array.from()`
// `Array.from()` and `Array.fromAsync()`
context.on('CallExpression', callExpression => {
if (
!isMethodCall(callExpression, {
object: 'Array',
method: 'from',
methods: ['from', 'fromAsync'],
argumentsLength: 3,
optionalCall: false,
optionalMember: false,
Expand Down
31 changes: 31 additions & 0 deletions test/no-array-method-this-argument.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ test.snapshot({
'Array.from?.(iterableOrArrayLike, () => {}, thisArgument)',
'Array?.from(iterableOrArrayLike, () => {}, thisArgument)',
'NotArray.from(iterableOrArrayLike, () => {}, thisArgument)',
'new Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument)',
'Array.fromAsync?.(iterableOrArrayLike, () => {}, thisArgument)',
'Array?.fromAsync(iterableOrArrayLike, () => {}, thisArgument)',
'NotArray.fromAsync(iterableOrArrayLike, () => {}, thisArgument)',

// More or less arguments
'array.map()',
Expand All @@ -28,6 +32,13 @@ test.snapshot({
'Array.from(iterableOrArrayLike, ...() => {}, thisArgument)',
'Array.from(...iterableOrArrayLike, () => {}, thisArgument)',
'Array.from(iterableOrArrayLike, () => {}, thisArgument, extraArgument)',
'Array.fromAsync()',
'Array.fromAsync(iterableOrArrayLike)',
'Array.fromAsync(iterableOrArrayLike, () => {},)',
'Array.fromAsync(iterableOrArrayLike, () => {}, ...thisArgument)',
'Array.fromAsync(iterableOrArrayLike, ...() => {}, thisArgument)',
'Array.fromAsync(...iterableOrArrayLike, () => {}, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument, extraArgument)',

// Ignored
'lodash.every(array, () => {})',
Expand All @@ -52,8 +63,11 @@ test.snapshot({
'array.map(1, thisArgument)',
'async () => array.map(await callback, thisArgument)',
'Array.from(iterableOrArrayLike, new Callback, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, new Callback, thisArgument)',
'Array.from(iterableOrArrayLike, 1, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, 1, thisArgument)',
'Array.from(iterableOrArrayLike, await callback, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, await callback, thisArgument)',
],
invalid: [
'array.every(() => {}, thisArgument)',
Expand All @@ -66,13 +80,16 @@ test.snapshot({
'array.forEach(() => {}, thisArgument)',
'array.map(() => {}, thisArgument)',
'Array.from(iterableOrArrayLike, () => {}, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument)',
// Comma
'array.map(() => {}, thisArgument,)',
'array.map(() => {}, (0, thisArgument),)',
'Array.from(iterableOrArrayLike, () => {}, thisArgument,)',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument,)',
// Side effect
'array.map(() => {}, thisArgumentHasSideEffect())',
'Array.from(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect())',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect())',
],
});

Expand All @@ -82,12 +99,15 @@ test.snapshot({
invalid: [
'array.map(callback, thisArgument)',
'Array.from(iterableOrArrayLike, callback, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, callback, thisArgument)',
'array.map(callback, (0, thisArgument))',
'Array.from(iterableOrArrayLike, callback, (0, thisArgument))',
'Array.fromAsync(iterableOrArrayLike, callback, (0, thisArgument))',
'array.map(function () {}, thisArgument)',
'Array.from(iterableOrArrayLike, function () {}, thisArgument)',
'array.map(function callback () {}, thisArgument)',
'Array.from(iterableOrArrayLike, function callback () {}, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, function callback () {}, thisArgument)',
{
code: 'array.map( foo as bar, (( thisArgument )),)',
languageOptions: {parser: parsers.typescript},
Expand All @@ -96,6 +116,10 @@ test.snapshot({
code: 'Array.from(iterableOrArrayLike, foo as bar, (( thisArgument )),)',
languageOptions: {parser: parsers.typescript},
},
{
code: 'Array.fromAsync(iterableOrArrayLike, foo as bar, (( thisArgument )),)',
languageOptions: {parser: parsers.typescript},
},
{
code: 'array.map( (( foo as bar )), (( thisArgument )),)',
languageOptions: {parser: parsers.typescript},
Expand All @@ -104,13 +128,20 @@ test.snapshot({
code: 'Array.from(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),)',
languageOptions: {parser: parsers.typescript},
},
{
code: 'Array.fromAsync(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),)',
languageOptions: {parser: parsers.typescript},
},
'array.map( (( 0, callback )), (( thisArgument )),)',
'Array.from(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),)',
'Array.fromAsync(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),)',
// This callback is actually arrow function, but we don't know
'array.map((0, () => {}), thisArgument)',
'Array.from(iterableOrArrayLike, (0, () => {}), thisArgument)',
'Array.fromAsync(iterableOrArrayLike, (0, () => {}), thisArgument)',
// This callback is a bound function, but we don't know
'array.map(callback.bind(foo), thisArgument)',
'Array.from(iterableOrArrayLike, callback.bind(foo), thisArgument)',
'Array.fromAsync(iterableOrArrayLike, callback.bind(foo), thisArgument)',
],
});
Loading