make compiler throw if a function call includes an unexpected parameter

This commit is contained in:
undergroundwires
2021-02-05 13:27:40 +01:00
parent f1e21babbf
commit 15353d0e25
2 changed files with 34 additions and 1 deletions

View File

@@ -30,6 +30,7 @@ export class ScriptCompiler implements IScriptCompiler {
calls.forEach((currentCall, currentCallIndex) => {
ensureValidCall(currentCall, script.name);
const commonFunction = this.getFunctionByName(currentCall.function);
ensureExpectedParameters(commonFunction, currentCall);
let functionCode = compileCode(commonFunction, currentCall.parameters);
if (currentCallIndex !== calls.length - 1) {
functionCode = appendLine(functionCode);
@@ -57,6 +58,18 @@ export class ScriptCompiler implements IScriptCompiler {
}
}
function ensureExpectedParameters(func: FunctionData, call: FunctionCallData) {
if (!func.parameters && !call.parameters) {
return;
}
const unexpectedParameters = Object.keys(call.parameters || {})
.filter((callParam) => !func.parameters.includes(callParam));
if (unexpectedParameters.length) {
throw new Error(
`function "${func.name}" has unexpected parameter(s) provided: "${unexpectedParameters.join('", "')}"`);
}
}
function getDuplicates(texts: readonly string[]): string[] {
return texts.filter((item, index) => texts.indexOf(item) !== index);
}