-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add naming convention checks to eslint config
- Loading branch information
1 parent
3e22f98
commit 1b54bfd
Showing
5 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
export function createRegexForWords(wordList) { | ||
|
||
// This function builds a case-insensitive regex pattern for a given word. | ||
|
||
|
||
const part1List = []; | ||
const part2List = []; | ||
|
||
wordList.forEach(word => { | ||
const part1 = createRegexToCheckIfWordLookAlike(word); | ||
const part2 = createFallbackRegex(word); | ||
part1List.push(part1); | ||
part2List.push(part2); | ||
}); | ||
return `(${part1List.join('|')})|(${part2List.join('')})`; | ||
} | ||
|
||
function getCaseInsensitiveRegexForChar(c){ | ||
return `[${c.toUpperCase()}${c.toLowerCase()}]` | ||
} | ||
|
||
function createCaseInsensitiveRegexForWord(word) { | ||
return word | ||
.match(/[A-Z][a-z]+/g) | ||
.join(".?") | ||
.split("") | ||
.map(c => /[a-zA-Z]/g.test(c) ?getCaseInsensitiveRegexForChar(c) : c) | ||
.join(''); | ||
} | ||
|
||
function transformToUnderscoreUppercase(word) { | ||
return word | ||
.split(/(?=[A-Z])/) // Split at uppercase letters without removing them | ||
.join('_') | ||
.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 | ||
} | ||
|
||
function createRegexToCheckIfWordLookAlike(word) { | ||
let wordLooksLikeRegex = createCaseInsensitiveRegexForWord(word); | ||
wordLooksLikeRegex = getWordRegexWithOptionalLetters(wordLooksLikeRegex) | ||
const wordCorrectRegex = getCaseInsensitiveRegexForChar(word[0]) + word.slice(1); | ||
const wordInUpperCase = transformToUnderscoreUppercase(word) | ||
const wordInLowerCase = transformToHyphenLowercase(word) | ||
return `(?=.*${wordLooksLikeRegex}.*)(.*${wordCorrectRegex}.*|[A-Z_]*${wordInUpperCase}[A-Z_]*|[a-z-]*${wordInLowerCase}[a-z-]*)`; | ||
} | ||
|
||
function createFallbackRegex(word) { | ||
const caseInsensitiveWordRegex = createCaseInsensitiveRegexForWord(word); | ||
return `(?=^(?!.*${caseInsensitiveWordRegex}).*)`; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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_]*|[a-z-]*key-result[a-z-]*)|(?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk].?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*|[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_]*|[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_]*|[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", "CurrentKeyResult", "keyResult", "keyResultId", "key-result", "test-key-result-test"],["Keyresult", "CurrentKeyresult", "keyresult", "keyresultId", "KEyResult", "KeyResUlt", "test-keyresult-test"]], | ||
[["CheckIn"], ["CheckIn", "CurrentCheckIn", "checkIn", "checkInId", "check-in", "test-check-in-test"],["Checkin", "CurrentCheckin", "checkin", "checkinId", "cHeckIn", "checkIN", "test-checkin-test"]], | ||
[["KeyResult", "CheckIn"], ["KeyResult", "CurrentKeyResult", "keyResult", "keyResultId", "key-result", "test-key-result-test", "CheckIn", "CurrentCheckIn", "checkIn", "checkInId", "check-in", "test-check-in-test"],["Keyresult", "CurrentKeyresult", "keyresult", "keyresultId", "KEyResult", "KeyResUlt", "test-keyresult-test", "Checkin", "CurrentCheckin", "checkin", "checkinId", "cHeckIn", "checkIN", "test-checkin-test"]], | ||
])("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) | ||
}); | ||
|
||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters