From 6399b900193ce13789168deac43a4b9be6e11439 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 19 May 2020 16:11:53 +0800 Subject: [PATCH] DRY --- rules/prefer-array-find.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/rules/prefer-array-find.js b/rules/prefer-array-find.js index 191f4dfe03..779815de7f 100644 --- a/rules/prefer-array-find.js +++ b/rules/prefer-array-find.js @@ -13,6 +13,12 @@ const MESSAGE_ID_DECLARATION = 'prefer-array-find-over-filter'; const MESSAGE_ID_USE_NULLISH_COALESCING_OPERATOR = 'use-nullish-coalescing-operator'; const MESSAGE_ID_USE_LOGICAL_OR_OPERATOR = 'use-logical-or-operator'; +const filterMethodSelectorOptions = { + name: 'filter', + min: 1, + max: 2 +}; + const filterVariableSelector = [ 'VariableDeclaration', // Exclude `export const foo = [];` @@ -27,9 +33,7 @@ const filterVariableSelector = [ 'VariableDeclarator.declarations', '[id.type="Identifier"]', methodSelector({ - name: 'filter', - min: 1, - max: 2, + ...filterMethodSelectorOptions, property: 'init' }) ].join(''); @@ -40,9 +44,7 @@ const zeroIndexSelector = [ '[property.type="Literal"]', '[property.raw="0"]', methodSelector({ - name: 'filter', - min: 1, - max: 2, + ...filterMethodSelectorOptions, property: 'object' }) ].join(''); @@ -53,9 +55,7 @@ const shiftSelector = [ length: 0 }), methodSelector({ - name: 'filter', - min: 1, - max: 2, + ...filterMethodSelectorOptions, property: 'callee.object' }) ].join(''); @@ -66,9 +66,7 @@ const destructuringDeclaratorSelector = [ '[id.elements.length=1]', '[id.elements.0.type!="RestElement"]', methodSelector({ - name: 'filter', - min: 1, - max: 2, + ...filterMethodSelectorOptions, property: 'init' }) ].join(''); @@ -79,9 +77,7 @@ const destructuringAssignmentSelector = [ '[left.elements.length=1]', '[left.elements.0.type!="RestElement"]', methodSelector({ - name: 'filter', - min: 1, - max: 2, + ...filterMethodSelectorOptions, property: 'right' }) ].join('');