Skip to content

Commit

Permalink
feat(ProjectParser): add find method (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealShadowNova committed Sep 2, 2022
1 parent 3d87c32 commit adc3fd2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/lib/structures/NamespaceParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,58 @@ export class NamespaceParser extends Parser {
this.typeAliases = typeAliases;
}

/**
* Find a parser by id.
* @since 3.0.0
* @param id The id of the parser to find.
* @returns The parser with the given id, or `null` if none was found.
*/
public find(id: number): SearchResult | null {
for (const classParser of this.classes) {
if (classParser.id === id) return classParser;
if (classParser.construct.id === id) return classParser.construct;

for (const methodParser of classParser.methods) {
if (methodParser.id === id) return methodParser;

for (const signature of methodParser.signatures) {
if (signature.id === id) return signature;

for (const typeParameter of signature.typeParameters) if (typeParameter.id === id) return typeParameter;
for (const parameter of signature.parameters) if (parameter.id === id) return parameter;
}
}

for (const propertyParser of classParser.properties) if (propertyParser.id === id) return propertyParser;
}

for (const constantParser of this.constants) if (constantParser.id === id) return constantParser;
for (const enumParser of this.enums) {
if (enumParser.id === id) return enumParser;

for (const propertyParser of enumParser.properties) if (propertyParser.id === id) return propertyParser;
}

for (const functionParser of this.functions) if (functionParser.id === id) return functionParser;
for (const interfaceParser of this.interfaces) {
if (interfaceParser.id === id) return interfaceParser;

for (const propertyParser of interfaceParser.properties) if (propertyParser.id === id) return propertyParser;
}

for (const namespaceParser of this.namespaces) {
if (namespaceParser.id === id) return namespaceParser;

const found = namespaceParser.find(id);

if (found) return found;
}

for (const typeAliasParser of this.typeAliases) if (typeAliasParser.id === id) return typeAliasParser;

return null;
}

/**
* Search for a parser with a given query.
* @since 3.0.0
Expand Down
52 changes: 52 additions & 0 deletions src/lib/structures/ProjectParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,58 @@ export class ProjectParser {
}
}

/**
* Find a parser by id.
* @since 3.0.0
* @param id The id of the parser to find.
* @returns The parser with the given id, or `null` if none was found.
*/
public find(id: number): SearchResult | null {
for (const classParser of this.classes) {
if (classParser.id === id) return classParser;
if (classParser.construct.id === id) return classParser.construct;

for (const methodParser of classParser.methods) {
if (methodParser.id === id) return methodParser;

for (const signature of methodParser.signatures) {
if (signature.id === id) return signature;

for (const typeParameter of signature.typeParameters) if (typeParameter.id === id) return typeParameter;
for (const parameter of signature.parameters) if (parameter.id === id) return parameter;
}
}

for (const propertyParser of classParser.properties) if (propertyParser.id === id) return propertyParser;
}

for (const constantParser of this.constants) if (constantParser.id === id) return constantParser;
for (const enumParser of this.enums) {
if (enumParser.id === id) return enumParser;

for (const propertyParser of enumParser.properties) if (propertyParser.id === id) return propertyParser;
}

for (const functionParser of this.functions) if (functionParser.id === id) return functionParser;
for (const interfaceParser of this.interfaces) {
if (interfaceParser.id === id) return interfaceParser;

for (const propertyParser of interfaceParser.properties) if (propertyParser.id === id) return propertyParser;
}

for (const namespaceParser of this.namespaces) {
if (namespaceParser.id === id) return namespaceParser;

const found = namespaceParser.find(id);

if (found) return found;
}

for (const typeAliasParser of this.typeAliases) if (typeAliasParser.id === id) return typeAliasParser;

return null;
}

/**
* Search for a parser with a given query.
* @since 3.0.0
Expand Down

0 comments on commit adc3fd2

Please sign in to comment.