From a981a13b59a23565b9c2afe5008c480afdc13868 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 5 Oct 2022 13:30:59 +0800 Subject: [PATCH] `prefer-array-index-of`: Add missing message (#1924) --- rules/shared/simple-array-search-rule.js | 5 ++-- test/snapshots/prefer-array-index-of.mjs.md | 26 +++++++++--------- test/snapshots/prefer-array-index-of.mjs.snap | Bin 1653 -> 1686 bytes test/utils/snapshot-rule-tester.mjs | 6 ++++ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/rules/shared/simple-array-search-rule.js b/rules/shared/simple-array-search-rule.js index 1fb34338df..4b648a4034 100644 --- a/rules/shared/simple-array-search-rule.js +++ b/rules/shared/simple-array-search-rule.js @@ -38,10 +38,11 @@ const isIdentifierNamed = ({type, name}, expectName) => type === 'Identifier' && function simpleArraySearchRule({method, replacement}) { // Add prefix to avoid conflicts in `prefer-includes` rule const MESSAGE_ID_PREFIX = `prefer-${replacement}-over-${method}/`; - const ERROR = `${MESSAGE_ID_PREFIX}/error`; - const SUGGESTION = `${MESSAGE_ID_PREFIX}/suggestion`; + const ERROR = `${MESSAGE_ID_PREFIX}error`; + const SUGGESTION = `${MESSAGE_ID_PREFIX}suggestion`; const ERROR_MESSAGES = { findIndex: 'Use `.indexOf()` instead of `.findIndex()` when looking for the index of an item.', + findLastIndex: 'Use `.lastIndexOf()` instead of `findLastIndex() when looking for the index of an item.`', some: `Use \`.${replacement}()\` instead of \`.${method}()\` when checking value existence.`, }; diff --git a/test/snapshots/prefer-array-index-of.mjs.md b/test/snapshots/prefer-array-index-of.mjs.md index 982c408769..4e6c7b0fd4 100644 --- a/test/snapshots/prefer-array-index-of.mjs.md +++ b/test/snapshots/prefer-array-index-of.mjs.md @@ -264,7 +264,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => x === "foo")␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #2 @@ -280,7 +280,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => "foo" === x)␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #3 @@ -296,7 +296,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => {return x === "foo";})␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #4 @@ -312,7 +312,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(function (x) {return x === "foo";})␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #5 @@ -343,7 +343,7 @@ Generated by [AVA](https://avajs.dev). 2 | (0, values)␊ 3 | // 2␊ > 4 | ./* 3 */findLastIndex /* 3 */ (␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 5 | /* 4 */␊ 6 | x /* 5 */ => /* 6 */ x /* 7 */ === /* 8 */ "foo" /* 9 */␊ 7 | ) /* 10 */␊ @@ -366,7 +366,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.findLastIndex(function (element) {␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 2 | return element === bar.findLastIndex(x => x === 1);␊ 3 | });␊ ␊ @@ -380,7 +380,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | foo.findLastIndex(function (element) {␊ > 2 | return element === bar.findLastIndex(x => x === 1);␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 3 | });␊ ` @@ -397,7 +397,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => x === (0, "foo"))␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #8 @@ -413,7 +413,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex((x => x === (0, "foo")))␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #9 @@ -434,7 +434,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | function fn() {␊ > 2 | foo.findLastIndex(x => x === arguments.length)␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 3 | }␊ ` @@ -456,7 +456,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | function fn() {␊ > 2 | foo.findLastIndex(x => x === this[1])␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 3 | }␊ ` @@ -467,7 +467,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => x === foo())␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Replace \`.findLastIndex()\` with \`.lastIndexOf()\`.␊ @@ -485,7 +485,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.findLastIndex(function a(x) {␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 2 | return x === (function (a) {␊ 3 | return a(this) === arguments[1]␊ 4 | }).call(thisObject, anotherFunctionNamedA, secondArgument)␊ diff --git a/test/snapshots/prefer-array-index-of.mjs.snap b/test/snapshots/prefer-array-index-of.mjs.snap index ef25c456aab49c40ca24fed9a58196c969ab1804..15e43093585b1460818d65c6b6262a19269bd33b 100644 GIT binary patch literal 1686 zcmV;H25I?0RzVT-#e(Ew@`Af{|uEGZ|xJ z*&G{-3u7UobCYc@SzN?9=N=j(QL{ud;@fTNHlNY>%FaFaT)DU0mX_MuWHf=)s3wOPc)t>KfHW};~a_5{aD(E z1CUoV!hHY6s-BBo3$Cncow$O{cm^N=H@9RLgOCeWeb#&PrFJ+pDNv7x3hEF$jJ-iNrWE7vPuC!`CPjF z)l(Z26W*HN{!-te_85d-$MW?EUUsSgs5qgW`&mU+%%!vQv!^(k{vr{o8p+EMC{-tu zI$}x>0OGD?`)_q0W)cX^9R+}AQ{TE4^@Re3veToAYjTqawP8s~0N~?&$uo9k?d>RP z%(1o?_P#?R^hGRV5}`fUT2k-q`rg>8-+Ov>TH{0#p|h~uhqATy&us^cFE?C2d3%l9 zlKdu#(Dz33a&Qa)+wVz_=uzRCca$H<$JOkoin!b{mP%^;mMo18|H;JKcS0?3{-D1FMy$wki^# zuVK+o0O0tX4O>;|pSRtM{fNFXAz>7W&^NHmp9sL^ON*2C5I??srJ&?c?$hy!Bto08 z%t`{_YW0c1~x&I&zWdt!CB#`-}(m?5`2Fh%=Yt2^HRLq)~ zdbQcj8eLX9OVR3j4Yg89!K;D$LoF6AYMzs!sbe3@CTsiv%~)5Xvx%5H{u|6K5! zIt#;6HoJY9m92qqa!@V{L!l?R!8A)*T}-VOzGW(Wi^9RU+zytir}TO~)vt=0<~7OU z#t4IGwaa34YS2*HQRA*F|Aa_dbULJ-mENejt-p1FRK49Wkm()U1?ACfUIFO{nn3BhtAg((yrl zNzKywF0pcXHc6g4a@F`<|Fj995{H>F!5Fj2zcDK%{R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j`kU<3Q@nJvm+5N*Z%5 z?ZrJW(wIF)Feed!9haNaukHE5&|27aVtq#AWE!(|1ivEeto`l%gNCOX{_eZF!DUW; zj>arM#)FC^0CwfqkG!1s7CiH3^7SL7dHFPE`v{n^0PN=)U7LH{`;6P0lMVVQ-85!( z1iv8kr1mi#8(dA5y#5c?W?Wm3`@8ne3z9-IKch5jWe`>C&9xL8!yo%cXuGL6|M2tGl$v~10! z)~hSImV~Sk%+z@YXv`ibXt@WqxY(KVcxvIfO+7C+E!NTJY0MrV;3uMatvdYEjlHo& zpfF*_gzuPtVlaD@VD)4VR-^zB1LE+Cphkiux`1Z%PzxKN$!61-EWELlH*)nVlZ!Vv zEjAvqs(LkC%RqEB2!G%b?*$h)IJit>5!zRpRO)5W!aJNCYlJov;(fNzlR7iUL#xfU z(!$rEp6t+R<{*AkXw34^;^b;IsF!J|mv}qpblG`W4+{$mq0bc+>+vKn9J=->=C z-dIeGsF4L7vfx%T%)__%VyavC%&y_tuD@?v9bXMUyz(;fDv`bf35w_V zrnWRzj<0c=)kI)!sZ~CRRlU!uj1=mn@)dO<%wT+=CS?0glOMb)JJX{zzX;H;fH1Tq ze*3lwO5)~#jGMCY3n_aXw%aXKpPN3hibWM5Sn)$S(cULN#WEWZ{Zi=J4P*C-^290` z%+}C@Su07Y*g%2%P7TW>N@7wG_%a!1<#3Mi?KfluX zUzsy#rvdd2*;hh26LjTCJsZBO%zslKWKjN5eiJC0T74wZyt(tsT(Zf)&s%Et;0ey6 zD7n#}g7C825Hnj;DsGu!41}>*f}aay*2(|2k{|lY8!lCSdhq-ktZt$z!mLLIFP#GF zd!?t0MWI~rg;>zCk`x)SUsZ#KC^CQOXT4idRuLkSGd)OHQs}=}tw!lcd`aywLdZ|~E5J~)!Bm)0! zNZx5|KqnYLkw&+ typeof message !== 'string'); + if (invalidMessage) { + throw Object.assign(new Error('Unexpected message.'), {eslintMessage: invalidMessage}); + } + const fatalError = messages.find(({fatal}) => fatal); if (fatalError) { const {line, column, message} = fatalError;