Skip to content

Commit bafe799

Browse files
error handle for variable types
1 parent b078abf commit bafe799

File tree

1 file changed

+38
-9
lines changed
  • packages/core/src/formatters

1 file changed

+38
-9
lines changed

packages/core/src/formatters/base.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,28 @@ export abstract class BaseFormatter {
659659
fullStringModifiers: FullStringModifiers
660660
): ParseValueToVariable {
661661
// get variableType and propertyName from baseString without regex
662-
const variableType = baseString.split('.')[0].toLowerCase();
662+
let variableType = baseString.split('.')[0].toLowerCase();
663+
let foundVariableType: boolean = false;
664+
Object.keys(this.regexBuilder.hardcodedParseValueKeysForRegexMatching).forEach((key) => {
665+
if (variableType == key.toLowerCase()) {
666+
variableType = key;
667+
foundVariableType = true;
668+
}
669+
});
670+
if (!foundVariableType) return () => ({ error: `{unknown_variableType(${variableType})}` }); // should never happen
663671
baseString = baseString.substring(variableType.length + 1);
664-
const propertyName = baseString.split('::')[0].toLowerCase();
672+
673+
let propertyName = baseString.split('::')[0].toLowerCase(); // TODO: replace with this.regexBuilder.modifierPrefix || this.regexBuilder.comparatorWrapper
674+
let foundPropertyName: boolean = false;
675+
const sectionOfVariableType = this.regexBuilder.hardcodedParseValueKeysForRegexMatching[variableType as keyof ParseValue]!;
676+
Object.keys(sectionOfVariableType).forEach((key) => {
677+
if (propertyName == key.toLowerCase()) {
678+
propertyName = key;
679+
foundPropertyName = true;
680+
}
681+
});
682+
if (!foundPropertyName) return () => ({ error: `{unknown_propertyName(${variableType}.${propertyName})}` }); // should never happen
683+
665684
const allModifiers = baseString.substring(propertyName.length);
666685
let sortedModMatches: string[] = [];
667686
if (allModifiers.length) {
@@ -872,7 +891,7 @@ class BaseFormatterRegexBuilder {
872891
public checkTFSplit = '"||"';
873892
public modifierPrefix = '::';
874893
public comparatorWrapper = '::';
875-
private hardcodedParseValueKeysForRegexMatching: ParseValue;
894+
public hardcodedParseValueKeysForRegexMatching: ParseValue;
876895
constructor(hardcodedParseValueKeysForRegexMatching: ParseValue) {
877896
this.hardcodedParseValueKeysForRegexMatching =
878897
hardcodedParseValueKeysForRegexMatching;
@@ -884,19 +903,29 @@ class BaseFormatterRegexBuilder {
884903
*/
885904
public buildVariableRegexPattern(): string {
886905
// Get all valid variable names (keys as well as subkeys) from ParseValue structure
887-
const validVariableNames = Object.keys(
906+
907+
// enforce non-duplicate section keys (case-insensitive)
908+
const sectionKeys = new Set<string>();
909+
Object.keys(
888910
this.hardcodedParseValueKeysForRegexMatching
889-
).flatMap((sectionKey) => {
911+
).forEach((key) => {
912+
if (sectionKeys.has(key.toLowerCase())) throw new Error(`Must Remove Case-Insensitive Duplicate: '${key}' in ParseValue`);
913+
sectionKeys.add(key.toLowerCase());
914+
});
915+
916+
const validVariableNames = [...sectionKeys].flatMap((sectionKey) => {
890917
const section =
891918
this.hardcodedParseValueKeysForRegexMatching[
892919
sectionKey as keyof ParseValue
893920
];
894-
if (section && typeof section === 'object' && section !== null) {
895-
if (sectionKey.toLowerCase() != sectionKey) throw new Error(`Key '${sectionKey}' in ParseValue must be lowercased`);
921+
if (section) {
922+
const sectionSubKeys = new Set<string>();
896923
Object.keys(section).forEach((key) => {
897-
if (key.toLowerCase() != key) throw new Error(`Section key '${sectionKey}.${key}' in ParseValue must be lowercased`);
924+
if (sectionSubKeys.has(key.toLowerCase())) throw new Error(`Must Remove Case-Insensitive Duplicate: '${sectionKey}.${key}' in ParseValue`);
925+
sectionSubKeys.add(key.toLowerCase());
898926
});
899-
return `${sectionKey}\\.(${Object.keys(section).join('|')})`;
927+
928+
return `${sectionKey}\\.(${[...sectionSubKeys].join('|')})`;
900929
}
901930
return []; // @flatMap
902931
});

0 commit comments

Comments
 (0)