From dd582aeb1f6e69679f7fab3d61aee43731d1d8f0 Mon Sep 17 00:00:00 2001 From: Miguel Lehmann Date: Tue, 17 Dec 2024 15:27:27 +0100 Subject: [PATCH] make the regex work for html --- frontend/eslint.config.mjs | 11 +---------- frontend/eslintHelper.mjs | 11 +++++++++-- frontend/eslintHelper.spec.ts | 27 ++++++++++++++++++++------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/frontend/eslint.config.mjs b/frontend/eslint.config.mjs index ebd9c18703..339b054f73 100644 --- a/frontend/eslint.config.mjs +++ b/frontend/eslint.config.mjs @@ -42,25 +42,16 @@ export default tsEslint.config( "id-match": [ "error", createRegexForWords(["KeyResult", "CheckIn", "TeamManagement", "StretchGoal"]) - // "((?=.*[Kk]?[Ee]?[Yy].?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*)|(?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk].?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*))|((?=^(?!.*[Kk][Ee][Yy].?[Rr][Ee][Ss][Uu][Ll][Tt]).*)(?=^(?!.*[Cc][Hh][Ee][Cc][Kk].?[Ii][Nn]).*))" - // "((?=.*[Ss]?[Tt]?[Rr]?[Ee]?[Tt]?[Cc]?[Hh].?[Gg]?[Oo]?[Aa]?[Ll]?.*)(.*[Ss]tretchGoal.*|[A-Z_]*STRETCH_GOAL[A-Z_]*)|^(?!.*[Ss][Tt][Rr][Ee][Tt][Cc][Hh].?[Gg][Oo][Aa][Ll]).*)" - // "((?=.*[Kk]?[Ee]?[Yy].?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*)|^(?!.*[Kk][Ee][Yy].?[Rr][Ee][Ss][Uu][Ll][Tt]).*)" - // "((?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk].?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*)|^(?!.*[Cc][Hh][Ee][Cc][Kk].?[Ii][Nn]).*)" - // "((?=.*[Tt]?[Ee]?[Aa]?[Mm].?[Mm]?[Aa]?[Nn]?[Aa]?[Gg]?[Ee]?[Mm]?[Ee]?[Nn]?[Tt]?.*)(.*[Tt]eamManagement.*|[A-Z_]*TEAM_MANAGEMENT[A-Z_]*)|^(?!.*[Tt][Ee][Aa][Mm].?[Mm][Aa][Nn][Aa][Gg][Ee][Mm][Ee][Nn][Tt]).*)" ] } }, { ...html.configs["flat/recommended"], files: ["**/*.html"], - rules: { - "@html-eslint/id-naming-convention": ["error", "kebab-case"], + "@html-eslint/id-naming-convention": ["error", "regex", {pattern: `(?=(^[a-z]+(-[a-z]+)*$))(?=(${createRegexForWords(["KeyResult", "CheckIn", "TeamManagement", "StretchGoal"])}))`}], } }, - // { - // files: ["**/*.scss"], rules: {} - // }, ); diff --git a/frontend/eslintHelper.mjs b/frontend/eslintHelper.mjs index ed3018c2ac..0c91d4e22f 100644 --- a/frontend/eslintHelper.mjs +++ b/frontend/eslintHelper.mjs @@ -13,7 +13,6 @@ export function createRegexForWords(wordList) { part1List.push(part1); part2List.push(part2); }); - return `(${part1List.join('|')})|(${part2List.join('')})`; } @@ -37,6 +36,13 @@ function transformToUnderscoreUppercase(word) { .toUpperCase(); } +function transformToHyphenLowercase(word) { + return word + .split(/(?=[A-Z])/) // Split at uppercase letters without removing them + .join('-') + .toLowerCase(); +} + function getWordRegexWithOptionalLetters(word) { return word.replace(/(\[[^\[\]]+\])(?![.?])/g, "$1?"); // Puts a "?" between the case-insensitive braces if there is no "?" or "." already } @@ -46,7 +52,8 @@ function createRegexToCheckIfWordLookAlike(word) { wordLooksLikeRegex = getWordRegexWithOptionalLetters(wordLooksLikeRegex) const wordCorrectRegex = getCaseInsensitiveRegexForChar(word[0]) + word.slice(1); const wordInUpperCase = transformToUnderscoreUppercase(word) - return `(?=.*${wordLooksLikeRegex}.*)(.*${wordCorrectRegex}.*|[A-Z_]*${wordInUpperCase}[A-Z_]*)`; + const wordInLowerCase = transformToHyphenLowercase(word) + return `(?=.*${wordLooksLikeRegex}.*)(.*${wordCorrectRegex}.*|[A-Z_]*${wordInUpperCase}[A-Z_]*|[a-z-]*${wordInLowerCase}[a-z-]*)`; } function createFallbackRegex(word) { diff --git a/frontend/eslintHelper.spec.ts b/frontend/eslintHelper.spec.ts index 9744ea613d..3c9a9364c1 100644 --- a/frontend/eslintHelper.spec.ts +++ b/frontend/eslintHelper.spec.ts @@ -1,18 +1,31 @@ import * as eslintHelper from "./eslintHelper.mjs" +import { $locationShim } from "@angular/common/upgrade"; describe("eslintHelper", () => { const combinedRegex:string = "((?=.*[Kk]?[Ee]?[Yy].?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*)|(?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk].?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*))|((?=^(?!.*[Kk][Ee][Yy].?[Rr][Ee][Ss][Uu][Ll][Tt]).*)(?=^(?!.*[Cc][Hh][Ee][Cc][Kk].?[Ii][Nn]).*))" - const keyResultRegex:string = "((?=.*[Kk]?[Ee]?[Yy]?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*))|((?=^(?!.*[Kk][Ee][Yy][Rr][Ee][Ss][Uu][Ll][Tt]).*))" - const CheckInRegex:string = "((?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk]?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*))|((?=^(?!.*[Cc][Hh][Ee][Cc][Kk][Ii][Nn]).*))" + const keyResultRegex:string = "((?=.*[Kk]?[Ee]?[Yy].?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*))|((?=^(?!.*[Kk][Ee][Yy].?[Rr][Ee][Ss][Uu][Ll][Tt]).*))" + const checkInRegex:string = "((?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk].?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*))|((?=^(?!.*[Cc][Hh][Ee][Cc][Kk].?[Ii][Nn]).*))" + it.each([ + [["KeyResult"], keyResultRegex], + [["CheckIn"], checkInRegex], + [["KeyResult", "CheckIn"], combinedRegex], + ])("should return regex %p", (wordToRegex, expectedRegex) => { + expect(eslintHelper.createRegexForWords(wordToRegex)).toEqual(expectedRegex); + }); it.each([ - // [["KeyResult"], /KeyResult/], - // [["CheckIn"], /CheckIn/], - [["KeyResult", "CheckIn"], /KeyResult|CheckIn/], - ])("should return regex %p", (wordToRegex) => { - expect(eslintHelper.createRegexForWords(wordToRegex)).toEqual(combinedRegex); + [["KeyResult"], ["KeyResult", "CurrentKeyResult", "keyResult", "keyResultId"],["Keyresult", "CurrentKeyresult", "keyresult", "keyresultId", "KEyResult", "KeyResUlt"]], + [["CheckIn"], ["CheckIn", "CurrentCheckIn", "checkIn", "checkInId"],["Checkin", "CurrentCheckin", "checkin", "checkinId", "cHeckIn", "checkIN"]], + [["KeyResult", "CheckIn"], ["KeyResult", "CurrentKeyResult", "keyResult", "keyResultId", "CheckIn", "CurrentCheckIn", "checkIn", "checkInId"],["Keyresult", "CurrentKeyresult", "keyresult", "keyresultId", "KEyResult", "KeyResUlt", "Checkin", "CurrentCheckin", "checkin", "checkinId", "cHeckIn", "checkIN"]], + ])("should run regex %p threw the matching and not matching list", (wordToRegex:string[], matchingListToRegex:string[], notMatchingListToRegex:string[]) => { + const regexOfCustomWord = new RegExp(eslintHelper.createRegexForWords(wordToRegex)); + matchingListToRegex = matchingListToRegex.filter(word => regexOfCustomWord.test(word)); + notMatchingListToRegex = notMatchingListToRegex.filter(word => regexOfCustomWord.test(word)); + expect(matchingListToRegex.length).toBe(matchingListToRegex.length); + expect(notMatchingListToRegex.length).toBe(0) }); + })