diff --git a/rules/no-array-method-this-argument.js b/rules/no-array-method-this-argument.js index 31f70fe04d..d6330a7c35 100644 --- a/rules/no-array-method-this-argument.js +++ b/rules/no-array-method-this-argument.js @@ -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, diff --git a/test/no-array-method-this-argument.mjs b/test/no-array-method-this-argument.mjs index c50143a9f9..9d5feba658 100644 --- a/test/no-array-method-this-argument.mjs +++ b/test/no-array-method-this-argument.mjs @@ -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()', @@ -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, () => {})', @@ -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)', @@ -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())', ], }); @@ -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}, @@ -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}, @@ -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)', ], }); diff --git a/test/snapshots/no-array-method-this-argument.mjs.md b/test/snapshots/no-array-method-this-argument.mjs.md index 9691c03253..aa1fc0514d 100644 --- a/test/snapshots/no-array-method-this-argument.mjs.md +++ b/test/snapshots/no-array-method-this-argument.mjs.md @@ -214,7 +214,28 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.from()\`.␊ ` -## invalid(11): array.map(() => {}, thisArgument,) +## invalid(11): Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument)␊ + ` + +> Output + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, () => {})␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ` + +## invalid(12): array.map(() => {}, thisArgument,) > Input @@ -235,7 +256,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array#map()\`.␊ ` -## invalid(12): array.map(() => {}, (0, thisArgument),) +## invalid(13): array.map(() => {}, (0, thisArgument),) > Input @@ -256,7 +277,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array#map()\`.␊ ` -## invalid(13): Array.from(iterableOrArrayLike, () => {}, thisArgument,) +## invalid(14): Array.from(iterableOrArrayLike, () => {}, thisArgument,) > Input @@ -277,7 +298,28 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.from()\`.␊ ` -## invalid(14): array.map(() => {}, thisArgumentHasSideEffect()) +## invalid(15): Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument,) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument,)␊ + ` + +> Output + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, () => {},)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument,)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ` + +## invalid(16): array.map(() => {}, thisArgumentHasSideEffect()) > Input @@ -296,7 +338,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map(() => {})␊ ` -## invalid(15): Array.from(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect()) +## invalid(17): Array.from(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect()) > Input @@ -315,6 +357,25 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, () => {})␊ ` +## invalid(18): Array.fromAsync(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect()) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect())␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect())␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, () => {})␊ + ` + ## invalid(1): array.map(callback, thisArgument) > Input @@ -361,7 +422,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, callback.bind(thisArgument))␊ ` -## invalid(3): array.map(callback, (0, thisArgument)) +## invalid(3): Array.fromAsync(iterableOrArrayLike, callback, thisArgument) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback, thisArgument)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, callback, thisArgument)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback.bind(thisArgument))␊ + ` + +## invalid(4): array.map(callback, (0, thisArgument)) > Input @@ -384,7 +468,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map(callback.bind((0, thisArgument)))␊ ` -## invalid(4): Array.from(iterableOrArrayLike, callback, (0, thisArgument)) +## invalid(5): Array.from(iterableOrArrayLike, callback, (0, thisArgument)) > Input @@ -407,7 +491,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, callback.bind((0, thisArgument)))␊ ` -## invalid(5): array.map(function () {}, thisArgument) +## invalid(6): Array.fromAsync(iterableOrArrayLike, callback, (0, thisArgument)) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback, (0, thisArgument))␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, callback, (0, thisArgument))␊ + | ^^^^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback.bind((0, thisArgument)))␊ + ` + +## invalid(7): array.map(function () {}, thisArgument) > Input @@ -430,7 +537,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map(function () {}.bind(thisArgument))␊ ` -## invalid(6): Array.from(iterableOrArrayLike, function () {}, thisArgument) +## invalid(8): Array.from(iterableOrArrayLike, function () {}, thisArgument) > Input @@ -453,7 +560,7 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, function () {}.bind(thisArgument))␊ ` -## invalid(7): array.map(function callback () {}, thisArgument) +## invalid(9): array.map(function callback () {}, thisArgument) > Input @@ -476,7 +583,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map(function callback () {}.bind(thisArgument))␊ ` -## invalid(8): Array.from(iterableOrArrayLike, function callback () {}, thisArgument) +## invalid(10): Array.from(iterableOrArrayLike, function callback () {}, thisArgument) > Input @@ -499,7 +606,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, function callback () {}.bind(thisArgument))␊ ` -## invalid(9): array.map( foo as bar, (( thisArgument )),) +## invalid(11): Array.fromAsync(iterableOrArrayLike, function callback () {}, thisArgument) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, function callback () {}, thisArgument)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, function callback () {}, thisArgument)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, function callback () {})␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, function callback () {}.bind(thisArgument))␊ + ` + +## invalid(12): array.map( foo as bar, (( thisArgument )),) > Input @@ -522,7 +652,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map( (foo as bar).bind((( thisArgument ))),)␊ ` -## invalid(10): Array.from(iterableOrArrayLike, foo as bar, (( thisArgument )),) +## invalid(13): Array.from(iterableOrArrayLike, foo as bar, (( thisArgument )),) > Input @@ -545,7 +675,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, (foo as bar).bind((( thisArgument ))),)␊ ` -## invalid(11): array.map( (( foo as bar )), (( thisArgument )),) +## invalid(14): Array.fromAsync(iterableOrArrayLike, foo as bar, (( thisArgument )),) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, foo as bar, (( thisArgument )),)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, foo as bar, (( thisArgument )),)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, foo as bar,)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (foo as bar).bind((( thisArgument ))),)␊ + ` + +## invalid(15): array.map( (( foo as bar )), (( thisArgument )),) > Input @@ -568,7 +721,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map( (( foo as bar )).bind((( thisArgument ))),)␊ ` -## invalid(12): Array.from(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),) +## invalid(16): Array.from(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),) > Input @@ -591,7 +744,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, (( foo as bar )).bind((( thisArgument ))),)␊ ` -## invalid(13): array.map( (( 0, callback )), (( thisArgument )),) +## invalid(17): Array.fromAsync(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, (( foo as bar )), (( thisArgument )),)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (( foo as bar )),)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (( foo as bar )).bind((( thisArgument ))),)␊ + ` + +## invalid(18): array.map( (( 0, callback )), (( thisArgument )),) > Input @@ -614,7 +790,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map( (( 0, callback )).bind((( thisArgument ))),)␊ ` -## invalid(14): Array.from(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),) +## invalid(19): Array.from(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),) > Input @@ -637,7 +813,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, (( 0, callback )).bind((( thisArgument ))),)␊ ` -## invalid(15): array.map((0, () => {}), thisArgument) +## invalid(20): Array.fromAsync(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, (( 0, callback )), (( thisArgument )),)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (( 0, callback )),)␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (( 0, callback )).bind((( thisArgument ))),)␊ + ` + +## invalid(21): array.map((0, () => {}), thisArgument) > Input @@ -660,7 +859,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map((0, () => {}).bind(thisArgument))␊ ` -## invalid(16): Array.from(iterableOrArrayLike, (0, () => {}), thisArgument) +## invalid(22): Array.from(iterableOrArrayLike, (0, () => {}), thisArgument) > Input @@ -683,7 +882,30 @@ Generated by [AVA](https://avajs.dev). 1 | Array.from(iterableOrArrayLike, (0, () => {}).bind(thisArgument))␊ ` -## invalid(17): array.map(callback.bind(foo), thisArgument) +## invalid(23): Array.fromAsync(iterableOrArrayLike, (0, () => {}), thisArgument) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, (0, () => {}), thisArgument)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, (0, () => {}), thisArgument)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (0, () => {}))␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, (0, () => {}).bind(thisArgument))␊ + ` + +## invalid(24): array.map(callback.bind(foo), thisArgument) > Input @@ -706,7 +928,7 @@ Generated by [AVA](https://avajs.dev). 1 | array.map(callback.bind(foo).bind(thisArgument))␊ ` -## invalid(18): Array.from(iterableOrArrayLike, callback.bind(foo), thisArgument) +## invalid(25): Array.from(iterableOrArrayLike, callback.bind(foo), thisArgument) > Input @@ -728,3 +950,26 @@ Generated by [AVA](https://avajs.dev). Suggestion 2/2: Use a bound function.␊ 1 | Array.from(iterableOrArrayLike, callback.bind(foo).bind(thisArgument))␊ ` + +## invalid(26): Array.fromAsync(iterableOrArrayLike, callback.bind(foo), thisArgument) + +> Input + + `␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback.bind(foo), thisArgument)␊ + ` + +> Error 1/1 + + `␊ + > 1 | Array.fromAsync(iterableOrArrayLike, callback.bind(foo), thisArgument)␊ + | ^^^^^^^^^^^^ Do not use the \`this\` argument in \`Array.fromAsync()\`.␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/2: Remove this argument.␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback.bind(foo))␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 2/2: Use a bound function.␊ + 1 | Array.fromAsync(iterableOrArrayLike, callback.bind(foo).bind(thisArgument))␊ + ` diff --git a/test/snapshots/no-array-method-this-argument.mjs.snap b/test/snapshots/no-array-method-this-argument.mjs.snap index fbad733253..4ea59be7bc 100644 Binary files a/test/snapshots/no-array-method-this-argument.mjs.snap and b/test/snapshots/no-array-method-this-argument.mjs.snap differ