@vlocode/apex
Version:
Salesforce APEX Parser and Grammar
56 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeRefCollector = void 0;
const types_1 = require("../types");
const syntaxTreeVisitor_1 = require("./syntaxTreeVisitor");
const typeListVisitor_1 = require("./typeListVisitor");
const typeRefVisitor_1 = require("./typeRefVisitor");
/**
* Collects all type references in a given Apex syntax tree and returns them as an array of unique `ApexTypeRef` objects that are referenced in the tree.
* Useful for getting all external types referenced in a given Apex syntax tree.
*/
class TypeRefCollector extends syntaxTreeVisitor_1.ApexSyntaxTreeVisitor {
options;
distinctTypes = new Set();
constructor(options) {
super([]);
this.options = options;
}
visitIdCreatedNamePair(ctx) {
const typeRef = {
name: ctx.anyId().getText(),
isSystemType: types_1.ApexTypeRef.isSystemType(ctx.getText()),
genericArguments: (ctx.typeList() && new typeListVisitor_1.TypeListVisitor().visit(ctx.typeList())) ?? undefined
};
this.addDistinct(typeRef);
return this.state;
}
visitIdPrimary(ctx) {
const name = ctx.getText();
this.addDistinct({ name, isSystemType: types_1.ApexTypeRef.isSystemType(name) });
return this.visitChildren(ctx);
}
visitTypeName(ctx) {
this.addDistinct(new typeRefVisitor_1.TypeRefVisitor().visit(ctx));
return this.state;
}
addDistinct(typeRef) {
if (!typeRef) {
return;
}
if (!this.options?.excludeSystemTypes || !typeRef.isSystemType) {
const typeName = `${typeRef.name}`;
if (!this.distinctTypes.has(typeName)) {
this.distinctTypes.add(typeName);
this.state.push(typeRef);
}
}
if (typeRef.genericArguments && typeRef.genericArguments.length > 0) {
for (const genericArgument of typeRef.genericArguments) {
this.addDistinct(genericArgument);
}
}
}
}
exports.TypeRefCollector = TypeRefCollector;
//# sourceMappingURL=typeRefCollector.js.map