Additional validation of custom macro keys

This commit is contained in:
Cohee 2024-06-25 22:02:05 +03:00
parent 8dab4ecb06
commit 01d38f9218
1 changed files with 13 additions and 2 deletions

View File

@ -30,14 +30,25 @@ export class MacrosParser {
throw new Error('Macro key must be a string');
}
if (this.#macros.has(key)) {
console.warn(`Macro ${key} is already registered`);
// Allowing surrounding whitespace would just create more confusion...
key = key.trim();
if (!key) {
throw new Error('Macro key must not be empty or whitespace only');
}
if (key.startsWith('{{') || key.endsWith('}}')) {
throw new Error('Macro key must not include the surrounding braces');
}
if (typeof value !== 'string' && typeof value !== 'function') {
throw new Error('Macro value must be a string or a function that returns a string');
}
if (this.#macros.has(key)) {
console.warn(`Macro ${key} is already registered`);
}
this.#macros.set(key, value);
}