Skip to content

Commit d741be5

Browse files
committed
refactor API
1 parent 876ec23 commit d741be5

File tree

2 files changed

+361
-394
lines changed

2 files changed

+361
-394
lines changed

packages/helpers/lib/class/explorer.ts

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class Explorer {
396396
}
397397

398398
// Finds all variable statements
399-
getVariables(): { [key: string]: Explorer } {
399+
get variables(): { [key: string]: Explorer } {
400400
const variables = this.getAll(SyntaxKind.VariableStatement);
401401
const result: { [key: string]: Explorer } = {};
402402
variables.forEach((variable) => {
@@ -409,7 +409,7 @@ class Explorer {
409409
}
410410

411411
// Retrieves the assigned value of a variable, property, parameter, or property assignment
412-
getValue(): Explorer {
412+
get value(): Explorer {
413413
if (this.isEmpty()) {
414414
return new Explorer();
415415
}
@@ -436,7 +436,7 @@ class Explorer {
436436
}
437437

438438
// Retrieves the type annotation of the current node if it exists, otherwise returns an empty Explorer
439-
getAnnotation(): Explorer {
439+
get annotation(): Explorer {
440440
if (this.isEmpty()) {
441441
return new Explorer();
442442
}
@@ -470,7 +470,7 @@ class Explorer {
470470

471471
// Checks if the current node has a union type annotation that includes all specified types ignoring order
472472
isUnionOf(types: string[]): boolean {
473-
const currentAnnotation = this.getAnnotation();
473+
const currentAnnotation = this.annotation;
474474
if (currentAnnotation.isEmpty()) {
475475
return false;
476476
}
@@ -501,7 +501,7 @@ class Explorer {
501501

502502
// Checks if the current node has a type annotation that matches the provided annotation string
503503
hasAnnotation(annotation: string): boolean {
504-
const currentAnnotation = this.getAnnotation();
504+
const currentAnnotation = this.annotation;
505505
if (currentAnnotation.isEmpty()) {
506506
return false;
507507
}
@@ -510,8 +510,8 @@ class Explorer {
510510
return currentAnnotation.matches(annotationExplorer);
511511
}
512512

513-
// Finds all functions in the current tree. If withVariables is true, it includes function expressions and arrow functions assigned to variables
514-
getFunctions(withVariables: boolean = false): { [key: string]: Explorer } {
513+
// Finds all function declarations in the current tree
514+
get functions(): { [key: string]: Explorer } {
515515
const result: { [key: string]: Explorer } = {};
516516
const functionDeclarations = this.getAll(SyntaxKind.FunctionDeclaration);
517517
functionDeclarations.forEach((func) => {
@@ -521,10 +521,15 @@ class Explorer {
521521
}
522522
});
523523

524-
if (withVariables) {
525-
const functionVariables = this.getAll(
526-
SyntaxKind.VariableStatement,
527-
).filter((v) => {
524+
return result;
525+
}
526+
527+
// Finds all function declarations, arrow functions, and function expressions assigned to variables
528+
get allFunctions(): { [key: string]: Explorer } {
529+
const result = { ...this.functions };
530+
531+
const functionVariables = this.getAll(SyntaxKind.VariableStatement).filter(
532+
(v) => {
528533
const declaration = (v.tree as VariableStatement).declarationList
529534
.declarations[0];
530535
if (!declaration.initializer) return false;
@@ -533,14 +538,14 @@ class Explorer {
533538
isArrowFunction(declaration.initializer) ||
534539
isFunctionExpression(declaration.initializer)
535540
);
536-
});
537-
functionVariables.forEach((v) => {
538-
const declaration = (v.tree as VariableStatement).declarationList
539-
.declarations[0];
540-
const name = (declaration.name as Identifier).text;
541-
result[name] = v;
542-
});
543-
}
541+
},
542+
);
543+
functionVariables.forEach((v) => {
544+
const declaration = (v.tree as VariableStatement).declarationList
545+
.declarations[0];
546+
const name = (declaration.name as Identifier).text;
547+
result[name] = v;
548+
});
544549

545550
return result;
546551
}
@@ -590,7 +595,7 @@ class Explorer {
590595
}
591596

592597
// Retrieves the parameters of a function or method
593-
getParameters(): Explorer[] {
598+
get parameters(): Explorer[] {
594599
if (!this.tree) {
595600
return [];
596601
}
@@ -617,7 +622,7 @@ class Explorer {
617622
}
618623

619624
// Retrieves the type parameters (<T>, <T extends U>) of a function, method, class, or interface
620-
getTypeParameters(): Explorer[] {
625+
get typeParameters(): Explorer[] {
621626
if (!this.tree) {
622627
return [];
623628
}
@@ -650,7 +655,7 @@ class Explorer {
650655
}
651656

652657
// Retrieves the type arguments (<string>, <K, V>) of a type reference or call expression
653-
getTypeArguments(): Explorer[] {
658+
get typeArguments(): Explorer[] {
654659
if (!this.tree) {
655660
return [];
656661
}
@@ -701,7 +706,7 @@ class Explorer {
701706
}
702707

703708
// Finds all type alias declarations in the current tree
704-
getTypes(): { [key: string]: Explorer } {
709+
get types(): { [key: string]: Explorer } {
705710
const typeDeclarations = this.getAll(SyntaxKind.TypeAliasDeclaration);
706711
const result: { [key: string]: Explorer } = {};
707712
typeDeclarations.forEach((t) => {
@@ -712,7 +717,7 @@ class Explorer {
712717
}
713718

714719
// Finds all interface declarations in the current tree
715-
getInterfaces(): { [key: string]: Explorer } {
720+
get interfaces(): { [key: string]: Explorer } {
716721
const interfaceDeclarations = this.getAll(SyntaxKind.InterfaceDeclaration);
717722
const result: { [key: string]: Explorer } = {};
718723
interfaceDeclarations.forEach((i) => {
@@ -723,7 +728,7 @@ class Explorer {
723728
}
724729

725730
// Finds all class declarations in the current tree
726-
getClasses(): { [key: string]: Explorer } {
731+
get classes(): { [key: string]: Explorer } {
727732
const classDeclarations = this.getAll(SyntaxKind.ClassDeclaration);
728733
const result: { [key: string]: Explorer } = {};
729734
classDeclarations.forEach((c) => {
@@ -736,7 +741,7 @@ class Explorer {
736741
}
737742

738743
// Finds all method declarations within a class
739-
getMethods(): { [key: string]: Explorer } {
744+
get methods(): { [key: string]: Explorer } {
740745
const result: { [key: string]: Explorer } = {};
741746
if (this.tree && isClassDeclaration(this.tree)) {
742747
this.getAll(SyntaxKind.MethodDeclaration).forEach((member) => {
@@ -750,7 +755,7 @@ class Explorer {
750755
}
751756

752757
// Finds and returns the constructor of a class as an Explorer object
753-
getConstructor(): Explorer | null {
758+
get classConstructor(): Explorer | null {
754759
if (!this.tree || !isClassDeclaration(this.tree)) {
755760
return null;
756761
}
@@ -760,7 +765,7 @@ class Explorer {
760765
}
761766

762767
// Finds all properties in a class
763-
getClassProps(includeConstructor = false): { [key: string]: Explorer } {
768+
get classProps(): { [key: string]: Explorer } {
764769
if (!this.tree || !isClassDeclaration(this.tree)) {
765770
return {};
766771
}
@@ -773,43 +778,51 @@ class Explorer {
773778
result[name] = new Explorer(prop);
774779
});
775780

776-
if (includeConstructor) {
777-
const constructor = this.getConstructor();
778-
if (constructor && !constructor.isEmpty()) {
779-
const constructorNode = constructor.tree as Node;
780-
const body = getBody(constructorNode);
781-
if (body && isBlock(body)) {
782-
body.statements.forEach((stmt: Node) => {
783-
if (isExpressionStatement(stmt)) {
784-
const expr = stmt.expression;
785-
// Check for property assignments: this.propertyName = value
786-
if (
787-
isBinaryExpression(expr) &&
788-
isPropertyAccessExpression(expr.left)
789-
) {
790-
const propAccess = expr.left;
791-
// Ensure it's accessing a property on 'this'
792-
if (
793-
propAccess.expression.kind === SyntaxKind.ThisKeyword &&
794-
isIdentifier(propAccess.name)
795-
) {
796-
const propName = propAccess.name.text;
797-
// Only add if not already defined as a PropertyDeclaration
798-
if (!(propName in result)) {
799-
result[propName] = new Explorer(stmt);
800-
}
801-
}
802-
}
781+
return result;
782+
}
783+
784+
get constructorProps(): { [key: string]: Explorer } {
785+
if (!this.tree || !isClassDeclaration(this.tree)) {
786+
return {};
787+
}
788+
789+
const constructor = this.classConstructor;
790+
if (!constructor || constructor.isEmpty()) {
791+
return {};
792+
}
793+
794+
const constructorNode = constructor.tree as Node;
795+
const body = getBody(constructorNode);
796+
if (!body || !isBlock(body)) {
797+
return {};
798+
}
799+
800+
const result: { [key: string]: Explorer } = {};
801+
body.statements.forEach((stmt: Node) => {
802+
if (isExpressionStatement(stmt)) {
803+
const expr = stmt.expression;
804+
// Check for property assignments: this.propertyName = value
805+
if (isBinaryExpression(expr) && isPropertyAccessExpression(expr.left)) {
806+
const propAccess = expr.left;
807+
// Ensure it's accessing a property on 'this'
808+
if (
809+
propAccess.expression.kind === SyntaxKind.ThisKeyword &&
810+
isIdentifier(propAccess.name)
811+
) {
812+
const propName = propAccess.name.text;
813+
// Only add if not already defined as a PropertyDeclaration
814+
if (!(propName in result)) {
815+
result[propName] = new Explorer(stmt);
803816
}
804-
});
817+
}
805818
}
806819
}
807-
}
820+
});
808821

809822
return result;
810823
}
811824

812-
getTypeProps(): { [key: string]: Explorer } {
825+
get typeProps(): { [key: string]: Explorer } {
813826
if (!this.tree) {
814827
return {};
815828
}
@@ -825,7 +838,7 @@ class Explorer {
825838
}
826839

827840
// Finds all properties in an object literal and returns them as Explorer instances
828-
getObjectProps(): { [key: string]: Explorer } {
841+
get objectProps(): { [key: string]: Explorer } {
829842
if (!this.tree) {
830843
return {};
831844
}
@@ -934,7 +947,7 @@ class Explorer {
934947
props = [props];
935948
}
936949

937-
const members = this.getTypeProps();
950+
const members = this.typeProps;
938951
if (Object.keys(members).length === 0 || props.length === 0) {
939952
return false;
940953
}

0 commit comments

Comments
 (0)