From b3d71584e540702fb5580be7015e3e477e21b64f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 7 Nov 2022 19:46:11 +0800 Subject: [PATCH 1/4] `prevent-abbreviations`: Improve fix for `retVal` --- rules/prevent-abbreviations.js | 13 ++++++++++++- test/prevent-abbreviations.mjs | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index 8328724c3f..3114e6ca5f 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -119,6 +119,18 @@ const getNameReplacements = (name, options, limit = 3) => { // Split words const words = name.split(/(?=[^a-z])|(?<=[^A-Za-z])/).filter(Boolean); + // `retVal` + // eslint-disable-next-line unicorn/prevent-abbreviations + const valueAfterRetIndex = words.findIndex( + (word, index) => + index > 0 + && (word === 'Val' || word === 'Value') + && /^[Rr]et$/.test(words[index - 1]), + ); + if (valueAfterRetIndex !== -1) { + words.splice(valueAfterRetIndex, 1); + } + let hasReplacements = false; const combinations = words.map(word => { const wordReplacements = getWordReplacements(word, options); @@ -420,7 +432,6 @@ const create = context => { } const variableReplacements = getNameReplacements(variable.name, options); - if (variableReplacements.total === 0) { return; } diff --git a/test/prevent-abbreviations.mjs b/test/prevent-abbreviations.mjs index 87ff815ad5..1f6672cf14 100644 --- a/test/prevent-abbreviations.mjs +++ b/test/prevent-abbreviations.mjs @@ -1261,6 +1261,33 @@ 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 ret_val = "that should be ok";', + output: 'const returnValue_value = "that should be ok";', + errors: createErrors(), + }, ], }; From d07b25de57f367e287e0ad6937535bcb883f23fb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 8 Nov 2022 18:50:41 +0800 Subject: [PATCH 2/4] More general check --- rules/prevent-abbreviations.js | 22 ++++++++++------------ test/prevent-abbreviations.mjs | 6 ++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index 3114e6ca5f..ceae74589e 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -119,18 +119,6 @@ const getNameReplacements = (name, options, limit = 3) => { // Split words const words = name.split(/(?=[^a-z])|(?<=[^A-Za-z])/).filter(Boolean); - // `retVal` - // eslint-disable-next-line unicorn/prevent-abbreviations - const valueAfterRetIndex = words.findIndex( - (word, index) => - index > 0 - && (word === 'Val' || word === 'Value') - && /^[Rr]et$/.test(words[index - 1]), - ); - if (valueAfterRetIndex !== -1) { - words.splice(valueAfterRetIndex, 1); - } - let hasReplacements = false; const combinations = words.map(word => { const wordReplacements = getWordReplacements(word, options); @@ -153,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 1f6672cf14..fd158f8425 100644 --- a/test/prevent-abbreviations.mjs +++ b/test/prevent-abbreviations.mjs @@ -1283,6 +1283,12 @@ const tests = { 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";', From 6770e3b678cac63e36e42be01b849882277167b2 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 8 Nov 2022 18:52:05 +0800 Subject: [PATCH 3/4] Reduce diff --- rules/prevent-abbreviations.js | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index ceae74589e..79fd1f7aaa 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -430,6 +430,7 @@ const create = context => { } const variableReplacements = getNameReplacements(variable.name, options); + if (variableReplacements.total === 0) { return; } From 16483ce46bf8e5ffc6652b87cc44b7cc1771e0e9 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 8 Nov 2022 19:40:23 +0800 Subject: [PATCH 4/4] Linting --- rules/prevent-abbreviations.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js index 79fd1f7aaa..77c8628f0c 100644 --- a/rules/prevent-abbreviations.js +++ b/rules/prevent-abbreviations.js @@ -143,10 +143,10 @@ const getNameReplacements = (name, options, limit = 3) => { // `retVal` -> `['returnValue', 'Value']` -> `['returnValue']` for (const parts of samples) { - for (let index = parts.length - 1; index > 0; index --) { + 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) + if (/^[A-Za-z]+$/.test(word) && parts[index - 1].endsWith(parts[index])) { + parts.splice(index, 1); } } }