Add "with" expression for templating #53
Allows optionally rendering content if an argument is given. The expression is designed to be used with `optional` parameters. Goal is to allow using `RunPowerShell` function on every function that consists of PowerShell code. Before this commit, they were all required to provide revertCode, or none of them could be able to have it. It would not work because some scripts can be reverted, meanwhile some are one-way scripts that cannot be reverted (such as cleaning scripts). In this case a way to optionally render revertCode was required. `with` expression give each callee script ability to turn off `revertCode` if not needed, therefore enables using `RunPowerShell` everywhere. This commit also improves error message for script code for better debugging and refactors parser tests for more code reuse. It also adds more tests to parameter substitution, and renames some tests of both expressions for consistency.
This commit is contained in:
@@ -39,23 +39,37 @@ function validateCode(code: string, syntax: ILanguageSyntax): void {
|
||||
}
|
||||
|
||||
function ensureNoEmptyLines(code: string): void {
|
||||
if (code.split('\n').some((line) => line.trim().length === 0)) {
|
||||
throw Error(`script has empty lines`);
|
||||
const lines = code.split(/\r\n|\r|\n/);
|
||||
if (lines.some((line) => line.trim().length === 0)) {
|
||||
throw Error(`Script has empty lines:\n${lines.map((part, index) => `\n (${index}) ${part || '❌'}`).join('')}`);
|
||||
}
|
||||
}
|
||||
|
||||
function ensureCodeHasUniqueLines(code: string, syntax: ILanguageSyntax): void {
|
||||
const lines = code.split('\n')
|
||||
.filter((line) => !shouldIgnoreLine(line, syntax));
|
||||
if (lines.length === 0) {
|
||||
const allLines = code.split(/\r\n|\r|\n/);
|
||||
const checkedLines = allLines.filter((line) => !shouldIgnoreLine(line, syntax));
|
||||
if (checkedLines.length === 0) {
|
||||
return;
|
||||
}
|
||||
const duplicateLines = lines.filter((e, i, a) => a.indexOf(e) !== i);
|
||||
const duplicateLines = checkedLines.filter((e, i, a) => a.indexOf(e) !== i);
|
||||
if (duplicateLines.length !== 0) {
|
||||
throw Error(`Duplicates detected in script:\n${duplicateLines.map((line, index) => `(${index}) - ${line}`).join('\n')}`);
|
||||
throw Error(`Duplicates detected in script:\n${printDuplicatedLines(allLines)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function printDuplicatedLines(allLines: string[]) {
|
||||
return allLines
|
||||
.map((line, index) => {
|
||||
const occurrenceIndices = allLines
|
||||
.map((e, i) => e === line ? i : '')
|
||||
.filter(String);
|
||||
const isDuplicate = occurrenceIndices.length > 1;
|
||||
const indicator = isDuplicate ? `❌ (${occurrenceIndices.join(',')})\t` : '✅ ';
|
||||
return `${indicator}[${index}] ${line}`;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
function shouldIgnoreLine(codeLine: string, syntax: ILanguageSyntax): boolean {
|
||||
codeLine = codeLine.toLowerCase();
|
||||
const isCommentLine = () => syntax.commentDelimiters.some((delimiter) => codeLine.startsWith(delimiter));
|
||||
|
||||
Reference in New Issue
Block a user