Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Inheritance and reference type search for name filtering (#104)
* feat: name filtering include heritage & properties reference type * test: add test case for type search * feat: update traverse type defined Co-authored-by: Fabien BERNARD <fabien0102@hotmail.com> * Extract isTypeNode utils Co-authored-by: Fabien BERNARD <fabien0102@hotmail.com> Co-authored-by: Fabien BERNARD <fabien0102@gmail.com>
- Loading branch information
1 parent
958d5a5
commit 038b9f6
Showing
3 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ts from "typescript"; | ||
|
||
export type TypeNode = ( | ||
| ts.InterfaceDeclaration | ||
| ts.TypeAliasDeclaration | ||
| ts.EnumDeclaration | ||
) & { visited?: boolean }; | ||
|
||
export function isTypeNode(node: ts.Node): node is TypeNode { | ||
return ( | ||
ts.isInterfaceDeclaration(node) || | ||
ts.isTypeAliasDeclaration(node) || | ||
ts.isEnumDeclaration(node) | ||
); | ||
} | ||
|
||
export function getExtractedTypeNames( | ||
node: TypeNode, | ||
sourceFile: ts.SourceFile, | ||
typeNameMapping: Map<string, TypeNode> | ||
) { | ||
const referenceTypeNames: string[] = []; | ||
|
||
const recursiveExtract = (node: TypeNode) => { | ||
if (node.visited) { | ||
return; | ||
} | ||
|
||
const heritageClauses = (node as ts.InterfaceDeclaration).heritageClauses; | ||
|
||
if (heritageClauses) { | ||
heritageClauses.forEach((clause) => { | ||
const extensionTypes = clause.types; | ||
extensionTypes.forEach((extensionTypeNode) => { | ||
const typeName = extensionTypeNode.expression.getText(sourceFile); | ||
const typeNode = typeNameMapping.get(typeName); | ||
|
||
referenceTypeNames.push(typeName); | ||
|
||
if (typeNode) { | ||
typeNode.visited = true; | ||
recursiveExtract(typeNode); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
node.forEachChild((child) => { | ||
const childNode = child as ts.PropertySignature; | ||
if (childNode.kind !== ts.SyntaxKind.PropertySignature) { | ||
return; | ||
} | ||
|
||
if (childNode.type?.kind === ts.SyntaxKind.TypeReference) { | ||
const typeNode = typeNameMapping.get( | ||
childNode.type.getText(sourceFile) | ||
); | ||
|
||
referenceTypeNames.push(childNode.type.getText(sourceFile)); | ||
|
||
if (typeNode) { | ||
typeNode.visited = true; | ||
recursiveExtract(typeNode); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
recursiveExtract(node); | ||
return [node.name.text, ...referenceTypeNames]; | ||
} |