@sequeljs/ast
Version:
A SQL AST manager for JavaScript
47 lines • 1.64 kB
JavaScript
import Attribute from '../attributes/Attribute';
import VisitorError from '../errors/VisitorError';
export default class Visitor {
accept(object, collector) {
return this.visit(object, collector);
}
visit(object, collector) {
let objectType;
if (object === null) {
objectType = 'Null';
}
else if (typeof object === 'bigint') {
objectType = 'BigInt';
}
else if (typeof object === 'boolean') {
objectType = 'Boolean';
}
else if (typeof object === 'number') {
objectType = 'Number';
}
else if (typeof object === 'string') {
objectType = 'String';
}
else if (typeof object === 'symbol') {
objectType = 'Symbol';
}
else if (object instanceof Attribute) {
objectType = `Attributes${object.constructor.name}`;
}
else {
let prototype = object.constructor;
objectType = prototype.name;
while (!(`visit${objectType}` in this.constructor.prototype) &&
objectType !== '') {
prototype = Object.getPrototypeOf(prototype);
objectType = prototype.name;
}
}
if (objectType !== '' &&
`visit${objectType}` in this.constructor.prototype) {
const visitFunction = this.constructor.prototype[`visit${objectType}`].bind(this);
return visitFunction(object, collector);
}
throw new VisitorError(`Cannot visit ${objectType}`);
}
}
//# sourceMappingURL=Visitor.js.map