Skip to content

Commit

Permalink
no-array-method-this-argument: Check Array.fromAsync() (#2330)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 7, 2024
1 parent 497519e commit 99489b9
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 23 deletions.
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

0 comments on commit 99489b9

Please sign in to comment.