cordbuilder
Version:
Simple dsl to create discord builders simple.
63 lines (62 loc) • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ast_1 = require("../types/ast");
class CordBuilder {
constructor(propertiesData, program, name, subPropertiesData) {
this.propertiesData = propertiesData;
this.program = program;
this.name = name;
this.subPropertiesData = subPropertiesData;
}
validateProperties() {
this.program.statements.forEach(prop => {
const token = prop.identifier.token;
const data = this.propertiesData.find(p => p.name == prop.identifier.name);
if (!data)
throw new Error(`${this.name} (l:${token.line}, c:${token.col}): Invalid ${this.name} property has found -> ${prop.identifier.name}`);
const expression = prop.expression.kind == ast_1.ExpressionKind.ParenthesizedExpression ?
prop.expression.expression : prop.expression;
const isValidKind = expression.kind == data.kind;
if (!isValidKind)
throw new TypeError(`${this.name} (l: ${token.line}, c:${token.col}): Invalid ${this.name} property (${prop.identifier.name}) value has found: ${expression.kind}, expected -> ${data.kind}`);
});
this.propertiesData
.filter(prop => prop.required)
.forEach(property => {
const builderDefinedProperties = this.program.statements.map(p => p.identifier.name);
const hasCurrentRequiredProp = builderDefinedProperties.find(definedProp => definedProp == property.name);
if (!hasCurrentRequiredProp)
throw new Error(`${this.name}: Missing ${this.name} required property (${property.name}).`);
});
}
validateSubProperties() {
this.program.statements
.filter(prop => prop.expression.kind == ast_1.ExpressionKind.ArgumentsExpression)
.forEach(prop => {
const _arguments = prop.expression.args;
_arguments.forEach(argument => {
const token = argument.identifier.token;
const data = this.subPropertiesData.find(p => p.name == prop.identifier.name)?.props
.find(p => p.name == argument.identifier.name);
if (!data)
throw new Error(`${this.name} (l: ${token.line}, c:${token.col}): Invalid ${this.name} (${prop.identifier.name}) sub-property has found -> ${argument.identifier.name}`);
const expression = argument.expression.kind == ast_1.ExpressionKind.ParenthesizedExpression ?
argument.expression.expression : argument.expression;
const isValidKind = expression.kind == data.kind;
if (!isValidKind)
throw new TypeError(`${this.name} (l: ${token.line}, c:${token.col}): Invalid ${this.name} (${prop.identifier.name}) sub-property (${argument.identifier.name}) value has found: ${expression.kind}, expected -> ${data.kind}`);
});
const propertyData = this.subPropertiesData.find(property => property.name == prop.identifier.name);
propertyData?.props
.filter(subProperty => subProperty.required)
.forEach(subProperty => {
const currentBuilderPropertySubProps = _arguments.map(p => p.identifier.name);
const hasCurrentRequiredSubProp = currentBuilderPropertySubProps
.find(definedSubProp => definedSubProp == subProperty.name);
if (!hasCurrentRequiredSubProp)
throw new Error(`${this.name}: Missing ${this.name} (${propertyData.name}) required sub-property (${subProperty.name}).`);
});
});
}
}
exports.default = CordBuilder;