Rework code validation to be bound to a context and not context-independent. It means that the generated code is validated based on different phases during the compilation. This is done by moving validation from `ScriptCode` constructor to a different callable function. It removes duplicate detection for function calls once a call is fully compiled, but still checks for duplicates inside each function body that has inline code. This allows for having duplicates in final scripts (thus relaxing the duplicate detection), e.g., when multiple calls to the same function is made. It fixes non-duplicates (when using common syntax) being misrepresented as duplicate lines. It improves the output of errors, such as printing valid lines, to give more context. This improvement also fixes empty line validation not showing the right empty lines in the error output. Empty line validation shows tabs and whitespaces more clearly. Finally, it adds more tests including tests for existing logic, such as singleton factories.
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { ISharedFunction, ISharedFunctionBody, FunctionBodyType } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
|
|
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
|
|
import { IFunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/IFunctionCall';
|
|
import { FunctionParameterCollectionStub } from './FunctionParameterCollectionStub';
|
|
import { FunctionCallStub } from './FunctionCallStub';
|
|
|
|
export class SharedFunctionStub implements ISharedFunction {
|
|
public name = 'shared-function-stub-name';
|
|
|
|
public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub()
|
|
.withParameterName('shared-function-stub-parameter-name');
|
|
|
|
private code = 'shared-function-stub-code';
|
|
|
|
private revertCode = 'shared-function-stub-revert-code';
|
|
|
|
private bodyType: FunctionBodyType = FunctionBodyType.Code;
|
|
|
|
private calls: IFunctionCall[] = [new FunctionCallStub()];
|
|
|
|
constructor(type: FunctionBodyType) {
|
|
this.bodyType = type;
|
|
}
|
|
|
|
public get body(): ISharedFunctionBody {
|
|
return {
|
|
type: this.bodyType,
|
|
code: this.bodyType === FunctionBodyType.Code ? {
|
|
execute: this.code,
|
|
revert: this.revertCode,
|
|
} : undefined,
|
|
calls: this.bodyType === FunctionBodyType.Calls ? this.calls : undefined,
|
|
};
|
|
}
|
|
|
|
public withName(name: string) {
|
|
this.name = name;
|
|
return this;
|
|
}
|
|
|
|
public withCode(code: string) {
|
|
this.code = code;
|
|
return this;
|
|
}
|
|
|
|
public withRevertCode(revertCode: string) {
|
|
this.revertCode = revertCode;
|
|
return this;
|
|
}
|
|
|
|
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
|
|
this.parameters = parameters;
|
|
return this;
|
|
}
|
|
|
|
public withCalls(...calls: readonly IFunctionCall[]) {
|
|
this.calls = [...calls];
|
|
return this;
|
|
}
|
|
|
|
public withParameterNames(...parameterNames: readonly string[]) {
|
|
let collection = new FunctionParameterCollectionStub();
|
|
for (const name of parameterNames) {
|
|
collection = collection.withParameterName(name);
|
|
}
|
|
return this.withParameters(collection);
|
|
}
|
|
}
|