From 25cd81000acdf6a532725e210346868f9f9c7ec1 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 8 Nov 2022 20:49:43 +0800 Subject: [PATCH] `prevent-abbreviations`: Improve fix for `retVal` (#1953) --- rules/prevent-abbreviations.js | 10 ++++++++++ test/prevent-abbreviations.mjs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index 8328724c3f..77c8628f0c 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -141,6 +141,16 @@ const getNameReplacements = (name, options, limit = 3) => { samples, } = cartesianProductSamples(combinations, limit); + // `retVal` -> `['returnValue', 'Value']` -> `['returnValue']` + for (const parts of samples) { + for (let index = parts.length - 1; index > 0; index--) { + const word = parts[index]; + if (/^[A-Za-z]+$/.test(word) && parts[index - 1].endsWith(parts[index])) { + parts.splice(index, 1); + } + } + } + return { total, samples: samples.map(words => words.join('')), diff --git a/test/prevent-abbreviations.mjs b/test/prevent-abbreviations.mjs index 87ff815ad5..fd158f8425 100644 --- a/test/prevent-abbreviations.mjs +++ b/test/prevent-abbreviations.mjs @@ -1261,6 +1261,39 @@ const tests = { options: noExtendDefaultAllowListOptions, errors: createErrors(), }, + + // #1937 + { + code: 'const expectedRetVal = "that should be ok";', + output: 'const expectedReturnValue = "that should be ok";', + errors: createErrors(), + }, + { + code: 'const retVal = "that should be ok";', + output: 'const returnValue = "that should be ok";', + errors: createErrors(), + }, + { + code: 'const retValue = "that should be ok";', + output: 'const returnValue = "that should be ok";', + errors: createErrors(), + }, + { + code: 'const returnVal = "that should be ok";', + output: 'const returnValue = "that should be ok";', + errors: createErrors(), + }, + { + code: 'const sendDmMessage = () => {};', + output: 'const sendDirectMessage = () => {};', + options: [{replacements: {dm: {directMessage: true}}}], + errors: createErrors(), + }, + { + code: 'const ret_val = "that should be ok";', + output: 'const returnValue_value = "that should be ok";', + errors: createErrors(), + }, ], };