refactor extra code, duplicates, complexity
- refactor array equality check and add tests - remove OperatingSystem.Unknown causing extra logic, return undefined instead - refactor enum validation to share same logic - refactor scripting language factories to share same logic - refactor too many args in runCodeAsync - refactor ScriptCode constructor to reduce complexity - fix writing useless write to member object since another property write always override it
This commit is contained in:
@@ -7,16 +7,7 @@ export class ScriptCode implements IScriptCode {
|
||||
syntax: ILanguageSyntax) {
|
||||
if (!syntax) { throw new Error('undefined syntax'); }
|
||||
validateCode(execute, syntax);
|
||||
if (revert) {
|
||||
try {
|
||||
validateCode(revert, syntax);
|
||||
if (execute === revert) {
|
||||
throw new Error(`Code itself and its reverting code cannot be the same`);
|
||||
}
|
||||
} catch (err) {
|
||||
throw Error(`(revert): ${err.message}`);
|
||||
}
|
||||
}
|
||||
validateRevertCode(revert, execute, syntax);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +16,20 @@ export interface ILanguageSyntax {
|
||||
readonly commonCodeParts: string[];
|
||||
}
|
||||
|
||||
function validateRevertCode(revertCode: string, execute: string, syntax: ILanguageSyntax) {
|
||||
if (!revertCode) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
validateCode(revertCode, syntax);
|
||||
if (execute === revertCode) {
|
||||
throw new Error(`Code itself and its reverting code cannot be the same`);
|
||||
}
|
||||
} catch (err) {
|
||||
throw Error(`(revert): ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function validateCode(code: string, syntax: ILanguageSyntax): void {
|
||||
if (!code || code.length === 0) {
|
||||
throw new Error(`code is empty or undefined`);
|
||||
|
||||
Reference in New Issue
Block a user