Skip to content

Commit

Permalink
make the regex work for html
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel7373 committed Dec 17, 2024
1 parent 988b5f5 commit dd582ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
11 changes: 1 addition & 10 deletions frontend/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
// },
);


11 changes: 9 additions & 2 deletions frontend/eslintHelper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function createRegexForWords(wordList) {
part1List.push(part1);
part2List.push(part2);
});

return `(${part1List.join('|')})|(${part2List.join('')})`;
}

Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
27 changes: 20 additions & 7 deletions frontend/eslintHelper.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
});

})

0 comments on commit dd582ae

Please sign in to comment.