ts2famix 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/.eslintrc.json +24 -0
  2. package/.github/workflows/node.js.yml +60 -0
  3. package/LICENSE +23 -0
  4. package/README.md +111 -0
  5. package/doc-metamodel/skins.include.puml +2 -0
  6. package/jest.config.ts +199 -0
  7. package/package.json +47 -0
  8. package/src/analyze.ts +90 -0
  9. package/src/analyze_functions/processAccesses.ts +50 -0
  10. package/src/analyze_functions/processFiles.ts +656 -0
  11. package/src/analyze_functions/processImportClauses.ts +77 -0
  12. package/src/analyze_functions/processInheritances.ts +84 -0
  13. package/src/analyze_functions/processInvocations.ts +51 -0
  14. package/src/famix2puml.ts +119 -0
  15. package/src/famix_functions/famix_functions.ts +552 -0
  16. package/src/famix_functions/famix_functions_associations.ts +208 -0
  17. package/src/famix_functions/famix_functions_index.ts +44 -0
  18. package/src/famix_functions/famix_functions_types.ts +100 -0
  19. package/src/fqn.ts +127 -0
  20. package/src/fqp_implementation.ts +66 -0
  21. package/src/generate_uml.sh +16 -0
  22. package/src/lib/famix/License.md +23 -0
  23. package/src/lib/famix/package-lock.json +301 -0
  24. package/src/lib/famix/package.json +28 -0
  25. package/src/lib/famix/readme.md +5 -0
  26. package/src/lib/famix/src/famix_JSON_exporter.ts +56 -0
  27. package/src/lib/famix/src/famix_base_element.ts +18 -0
  28. package/src/lib/famix/src/famix_repository.ts +199 -0
  29. package/src/lib/famix/src/index.ts +8 -0
  30. package/src/lib/famix/src/model/famix/access.ts +53 -0
  31. package/src/lib/famix/src/model/famix/accessor.ts +15 -0
  32. package/src/lib/famix/src/model/famix/alias.ts +41 -0
  33. package/src/lib/famix/src/model/famix/association.ts +44 -0
  34. package/src/lib/famix/src/model/famix/behavioral_entity.ts +107 -0
  35. package/src/lib/famix/src/model/famix/c_source_language.ts +15 -0
  36. package/src/lib/famix/src/model/famix/class.ts +86 -0
  37. package/src/lib/famix/src/model/famix/comment.ts +50 -0
  38. package/src/lib/famix/src/model/famix/container_entity.ts +165 -0
  39. package/src/lib/famix/src/model/famix/custom_source_language.ts +27 -0
  40. package/src/lib/famix/src/model/famix/decorator.ts +39 -0
  41. package/src/lib/famix/src/model/famix/entity.ts +15 -0
  42. package/src/lib/famix/src/model/famix/enum.ts +31 -0
  43. package/src/lib/famix/src/model/famix/enum_value.ts +29 -0
  44. package/src/lib/famix/src/model/famix/function.ts +15 -0
  45. package/src/lib/famix/src/model/famix/implicit_variable.ts +15 -0
  46. package/src/lib/famix/src/model/famix/import_clause.ts +53 -0
  47. package/src/lib/famix/src/model/famix/index.ts +42 -0
  48. package/src/lib/famix/src/model/famix/indexed_file_anchor.ts +49 -0
  49. package/src/lib/famix/src/model/famix/inheritance.ts +42 -0
  50. package/src/lib/famix/src/model/famix/interface.ts +75 -0
  51. package/src/lib/famix/src/model/famix/invocation.ts +68 -0
  52. package/src/lib/famix/src/model/famix/method.ts +96 -0
  53. package/src/lib/famix/src/model/famix/module.ts +31 -0
  54. package/src/lib/famix/src/model/famix/named_entity.ts +98 -0
  55. package/src/lib/famix/src/model/famix/namespace.ts +28 -0
  56. package/src/lib/famix/src/model/famix/parameter.ts +29 -0
  57. package/src/lib/famix/src/model/famix/parameterizable_class.ts +31 -0
  58. package/src/lib/famix/src/model/famix/parameterizable_interface.ts +31 -0
  59. package/src/lib/famix/src/model/famix/parameterized_type.ts +40 -0
  60. package/src/lib/famix/src/model/famix/primitive_type.ts +15 -0
  61. package/src/lib/famix/src/model/famix/property.ts +54 -0
  62. package/src/lib/famix/src/model/famix/reference.ts +42 -0
  63. package/src/lib/famix/src/model/famix/scoping_entity.ts +31 -0
  64. package/src/lib/famix/src/model/famix/script_entity.ts +38 -0
  65. package/src/lib/famix/src/model/famix/source_anchor.ts +31 -0
  66. package/src/lib/famix/src/model/famix/source_language.ts +31 -0
  67. package/src/lib/famix/src/model/famix/sourced_entity.ts +70 -0
  68. package/src/lib/famix/src/model/famix/structural_entity.ts +44 -0
  69. package/src/lib/famix/src/model/famix/text_anchor.ts +49 -0
  70. package/src/lib/famix/src/model/famix/type.ts +88 -0
  71. package/src/lib/famix/src/model/famix/type_parameter.ts +33 -0
  72. package/src/lib/famix/src/model/famix/variable.ts +28 -0
  73. package/src/lib/famix/tsconfig.json +27 -0
  74. package/src/lib/famix/tslint.json +15 -0
  75. package/src/lib/ts-complex/cyclomatic-service.ts +85 -0
  76. package/src/ts2famix-cli.ts +26 -0
  77. package/src/ts2famix-tsconfig.ts +30 -0
  78. package/test/abstractClassWithComments.test.ts +58 -0
  79. package/test/abstracts.test.ts +53 -0
  80. package/test/access.test.ts +62 -0
  81. package/test/accesses.test.ts +42 -0
  82. package/test/accessorsWithDecorators.test.ts +98 -0
  83. package/test/alias.test.ts +39 -0
  84. package/test/classExtendsUndefinedClass.test.ts +41 -0
  85. package/test/classImplementsUndefinedInterface.test.ts +45 -0
  86. package/test/classWithDecorators.test.ts +65 -0
  87. package/test/entities.test.ts +232 -0
  88. package/test/entities_json.test.ts +48 -0
  89. package/test/enum.test.ts +55 -0
  90. package/test/functionReturnsFunction.test.ts +53 -0
  91. package/test/functionWithParameters.test.ts +38 -0
  92. package/test/functionWithVariables.test.ts +64 -0
  93. package/test/functions.test.ts +23 -0
  94. package/test/functionsInFunction.test.ts +40 -0
  95. package/test/functionsInMethod.test.ts +42 -0
  96. package/test/genericClass.test.ts +42 -0
  97. package/test/genericClassInheritsInterface.test.ts +47 -0
  98. package/test/genericInterface.test.ts +38 -0
  99. package/test/genericMethod.test.ts +65 -0
  100. package/test/genericWithInvocation.test.ts +71 -0
  101. package/test/generics.test.ts +68 -0
  102. package/test/inheritance.test.ts +50 -0
  103. package/test/interfaceInheritsInterface.test.ts +40 -0
  104. package/test/interfaceInheritsUndefinedInterface.test.ts +41 -0
  105. package/test/invocation.test.ts +94 -0
  106. package/test/invocationWithFunction.test.ts +42 -0
  107. package/test/invocationWithVariable.test.ts +46 -0
  108. package/test/invocation_json.test.ts +63 -0
  109. package/test/invocations.test.ts +131 -0
  110. package/test/jsDoc.test.ts +31 -0
  111. package/test/methodWithDecorator.test.ts +44 -0
  112. package/test/methods.test.ts +42 -0
  113. package/test/metrics.test.ts +51 -0
  114. package/test/module.test.ts +71 -0
  115. package/test/namespaces.test.ts +54 -0
  116. package/test/namespacesAndClasses.test.ts +66 -0
  117. package/test/parameterWithDecorators.test.ts +54 -0
  118. package/test/propertyWithDecorators.test.ts +80 -0
  119. package/test/sample.test.ts +13 -0
  120. package/test/simpleFunction.test.ts +32 -0
  121. package/test/simpleTest.test.ts +18 -0
  122. package/test/simpleTest2.test.ts +36 -0
  123. package/test/types.test.ts +58 -0
  124. package/test_src/sample.ts +103 -0
  125. package/test_src/sampleForModule.ts +10 -0
  126. package/test_src/sampleForModule2.ts +7 -0
  127. package/test_src/sampleForModule3.ts +2 -0
  128. package/tsconfig.json +70 -0
@@ -0,0 +1,208 @@
1
+ import { ClassDeclaration, ConstructorDeclaration, FunctionDeclaration, Identifier, InterfaceDeclaration, MethodDeclaration, SourceFile, GetAccessorDeclaration, SetAccessorDeclaration, Node, ImportSpecifier, SyntaxKind, FunctionExpression, ExpressionWithTypeArguments } from "ts-morph";
2
+ import * as Famix from "../lib/famix/src/model/famix";
3
+ import { FamixRepository } from "../lib/famix/src/famix_repository";
4
+ import { FQNFunctions } from "../fqn";
5
+ import { FamixFunctionsIndex } from "./famix_functions_index";
6
+
7
+ /**
8
+ * This class is used to build a Famix model for the associations
9
+ */
10
+ export class FamixFunctionsAssociations {
11
+
12
+ private famixRep: FamixRepository; // The Famix repository
13
+ private FQNFunctions = new FQNFunctions(); // The fully qualified name functions
14
+ private famixClasses: Map<string, Famix.Class | Famix.ParameterizableClass>; // Maps the class names to their Famix model
15
+ private famixInterfaces: Map<string, Famix.Interface | Famix.ParameterizableInterface>; // Maps the interface names to their Famix model
16
+ private famixFunctionsIndex: FamixFunctionsIndex; // FamixFunctionsIndex object, it contains all the functions needed to create Famix index file anchors
17
+
18
+ /**
19
+ * Initializes the FamixFunctionsAssociations object
20
+ * @param famixRep The Famix repository
21
+ * @param fmxClasses The map of the class names and their Famix model
22
+ * @param fmxInterfaces The map of the interface names and their Famix model
23
+ */
24
+ constructor(famixRep: FamixRepository, fmxClasses: Map<string, Famix.Class | Famix.ParameterizableClass>, fmxInterfaces: Map<string, Famix.Interface | Famix.ParameterizableInterface>) {
25
+ this.famixRep = famixRep;
26
+ this.famixClasses = fmxClasses;
27
+ this.famixInterfaces = fmxInterfaces;
28
+ this.famixFunctionsIndex = new FamixFunctionsIndex(famixRep);
29
+ }
30
+
31
+ /**
32
+ * Creates a Famix access
33
+ * @param node A node
34
+ * @param id An id of a parameter, a variable, a property or an enum member
35
+ */
36
+ public createFamixAccess(node: Identifier, id: number): void {
37
+ const fmxVar = this.famixRep.getFamixEntityById(id) as Famix.StructuralEntity;
38
+ const nodeReferenceAncestor = this.findAncestor(node);
39
+ const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(nodeReferenceAncestor);
40
+ const accessor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName) as Famix.ContainerEntity;
41
+
42
+ const fmxAccess = new Famix.Access(this.famixRep);
43
+ fmxAccess.setAccessor(accessor);
44
+ fmxAccess.setVariable(fmxVar);
45
+
46
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(node, fmxAccess);
47
+ }
48
+
49
+ /**
50
+ * Creates a Famix invocation
51
+ * @param node A node
52
+ * @param m A method or a function
53
+ * @param id The id of the method or the function
54
+ */
55
+ public createFamixInvocation(node: Identifier, m: MethodDeclaration | ConstructorDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | FunctionDeclaration | FunctionExpression, id: number): void {
56
+ const fmxMethodOrFunction = this.getFamixEntityById(id) as Famix.BehavioralEntity;
57
+ const nodeReferenceAncestor = this.findAncestor(node);
58
+ const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(nodeReferenceAncestor);
59
+ const sender = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName) as Famix.ContainerEntity;
60
+ const receiverFullyQualifiedName = this.FQNFunctions.getFQN(m.getParent());
61
+ const receiver = this.getFamixEntityByFullyQualifiedName(receiverFullyQualifiedName) as Famix.NamedEntity;
62
+
63
+ const fmxInvocation = new Famix.Invocation(this.famixRep);
64
+ fmxInvocation.setSender(sender);
65
+ fmxInvocation.setReceiver(receiver);
66
+ fmxInvocation.addCandidate(fmxMethodOrFunction);
67
+ fmxInvocation.setSignature(fmxMethodOrFunction.getSignature());
68
+
69
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(node, fmxInvocation);
70
+ }
71
+
72
+ /**
73
+ * Creates a Famix inheritance
74
+ * @param cls A class or an interface (subclass)
75
+ * @param inhClass The inherited class or interface (superclass)
76
+ */
77
+ public createFamixInheritance(cls: ClassDeclaration | InterfaceDeclaration, inhClass: ClassDeclaration | InterfaceDeclaration | ExpressionWithTypeArguments): void {
78
+ const fmxInheritance = new Famix.Inheritance(this.famixRep);
79
+ const clsName = cls.getName();
80
+
81
+ let subClass: Famix.Class | Famix.Interface;
82
+ if (cls instanceof ClassDeclaration) {
83
+ subClass = this.famixClasses.get(clsName);
84
+ }
85
+ else {
86
+ subClass = this.famixInterfaces.get(clsName);
87
+ }
88
+
89
+ let inhClassName: string;
90
+ let superClass: Famix.Class | Famix.Interface;
91
+ if (inhClass instanceof ClassDeclaration || inhClass instanceof InterfaceDeclaration) {
92
+ inhClassName = inhClass.getName();
93
+ if (inhClass instanceof ClassDeclaration) {
94
+ superClass = this.famixClasses.get(inhClassName);
95
+ }
96
+ else {
97
+ superClass = this.famixInterfaces.get(inhClassName);
98
+ }
99
+ }
100
+ else {
101
+ inhClassName = inhClass.getExpression().getText();
102
+ }
103
+
104
+ if (superClass === undefined) {
105
+ if (inhClass instanceof ClassDeclaration) {
106
+ superClass = new Famix.Class(this.famixRep);
107
+ this.famixClasses.set(inhClassName, superClass);
108
+ }
109
+ else {
110
+ superClass = new Famix.Interface(this.famixRep);
111
+ this.famixInterfaces.set(inhClassName, superClass);
112
+ }
113
+
114
+ superClass.setName(inhClassName);
115
+ superClass.setIsStub(true);
116
+
117
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(inhClass, superClass);
118
+ }
119
+
120
+ fmxInheritance.setSubclass(subClass);
121
+ fmxInheritance.setSuperclass(superClass);
122
+
123
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(null, fmxInheritance);
124
+ }
125
+
126
+ /**
127
+ * Creates a Famix import clause
128
+ * @param importer A source file which is a module
129
+ * @param moduleSpecifier The name of the module where the export declaration is
130
+ * @param moduleSpecifierFilePath The path of the module where the export declaration is
131
+ * @param importElement The imported entity
132
+ * @param isInExports A boolean indicating if the imported entity is in the exports
133
+ * @param isDefaultExport A boolean indicating if the imported entity is a default export
134
+ */
135
+ public createFamixImportClause(importer: SourceFile, moduleSpecifier: string, moduleSpecifierFilePath: string, importElement: ImportSpecifier | Identifier, isInExports: boolean, isDefaultExport: boolean): void {
136
+ const fmxImportClause = new Famix.ImportClause(this.famixRep);
137
+
138
+ let importedEntity: Famix.NamedEntity;
139
+ let importedEntityName: string;
140
+ let pathName = "\"" + moduleSpecifierFilePath + "\".";
141
+ if (importElement instanceof ImportSpecifier) {
142
+ importedEntityName = importElement.getName();
143
+ pathName = pathName + importedEntityName;
144
+ if (isInExports) {
145
+ importedEntity = this.getFamixEntityByFullyQualifiedName(pathName) as Famix.NamedEntity;
146
+ }
147
+ if (importedEntity === undefined) {
148
+ importedEntity = new Famix.NamedEntity(this.famixRep);
149
+ importedEntity.setName(importedEntityName);
150
+ if (!isInExports) {
151
+ importedEntity.setIsStub(true);
152
+ }
153
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
154
+ importedEntity.setFullyQualifiedName(pathName);
155
+ }
156
+ }
157
+ else {
158
+ importedEntityName = importElement.getText();
159
+ if (isDefaultExport) {
160
+ pathName = pathName + "defaultExport";
161
+ }
162
+ else {
163
+ pathName = pathName + "namespaceExport";
164
+ }
165
+ importedEntity = new Famix.NamedEntity(this.famixRep);
166
+ importedEntity.setName(importedEntityName);
167
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
168
+ importedEntity.setFullyQualifiedName(pathName);
169
+ }
170
+
171
+ const importerFullyQualifiedName = this.FQNFunctions.getFQN(importer);
172
+ const fmxImporter = this.getFamixEntityByFullyQualifiedName(importerFullyQualifiedName) as Famix.Module;
173
+ fmxImportClause.setImporter(fmxImporter);
174
+ fmxImportClause.setImportedEntity(importedEntity);
175
+ fmxImportClause.setModuleSpecifier(moduleSpecifier);
176
+
177
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(null, fmxImportClause);
178
+
179
+ fmxImporter.addImportClause(fmxImportClause);
180
+ }
181
+
182
+ /**
183
+ * Gets a Famix entity by id
184
+ * @param famixId An id of a Famix entity
185
+ * @returns The Famix entity corresponding to the id
186
+ */
187
+ private getFamixEntityById(famixId: number): Famix.Entity {
188
+ return this.famixRep.getFamixEntityById(famixId) as Famix.Entity;
189
+ }
190
+
191
+ /**
192
+ * Gets a Famix entity by fully qualified name
193
+ * @param fullyQualifiedName A fully qualified name
194
+ * @returns The Famix entity corresponding to the fully qualified name
195
+ */
196
+ private getFamixEntityByFullyQualifiedName(fullyQualifiedName: string): Famix.Entity {
197
+ return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName) as Famix.Entity;
198
+ }
199
+
200
+ /**
201
+ * Finds the ancestor of a node
202
+ * @param node A node
203
+ * @returns The ancestor of the node
204
+ */
205
+ private findAncestor(node: Identifier): Node {
206
+ return node.getAncestors().find(a => a.getKind() === SyntaxKind.MethodDeclaration || a.getKind() === SyntaxKind.Constructor || a.getKind() === SyntaxKind.FunctionDeclaration || a.getKind() === SyntaxKind.FunctionExpression || a.getKind() === SyntaxKind.ModuleDeclaration || a.getKind() === SyntaxKind.SourceFile || a.getKindName() === "GetAccessor" || a.getKindName() === "SetAccessor" || a.getKind() === SyntaxKind.ClassDeclaration);
207
+ }
208
+ }
@@ -0,0 +1,44 @@
1
+ import { ClassDeclaration, ConstructorDeclaration, FunctionDeclaration, Identifier, InterfaceDeclaration, MethodDeclaration, MethodSignature, ModuleDeclaration, PropertyDeclaration, PropertySignature, SourceFile, TypeParameterDeclaration, VariableDeclaration, ParameterDeclaration, Decorator, GetAccessorDeclaration, SetAccessorDeclaration, ImportSpecifier, CommentRange, EnumDeclaration, EnumMember, TypeAliasDeclaration, FunctionExpression, ExpressionWithTypeArguments } from "ts-morph";
2
+ import * as Famix from "../lib/famix/src/model/famix";
3
+ import { FamixRepository } from "../lib/famix/src/famix_repository";
4
+ import { FQNFunctions } from "../fqn";
5
+
6
+ /**
7
+ * This class is used to build a Famix model for the index file anchors
8
+ */
9
+ export class FamixFunctionsIndex {
10
+
11
+ private famixRep: FamixRepository; // The Famix repository
12
+ private FQNFunctions = new FQNFunctions(); // The fully qualified name functions
13
+
14
+ /**
15
+ * Initializes the FamixFunctionsIndex object
16
+ * @param famixRep The Famix repository
17
+ */
18
+ constructor(famixRep: FamixRepository) {
19
+ this.famixRep = famixRep;
20
+ }
21
+
22
+ /**
23
+ * Makes a Famix index file anchor
24
+ * @param sourceElement A source element
25
+ * @param famixElement The Famix model of the source element
26
+ */
27
+ public makeFamixIndexFileAnchor(sourceElement: SourceFile | ModuleDeclaration | ClassDeclaration | InterfaceDeclaration | MethodDeclaration | ConstructorDeclaration | MethodSignature | FunctionDeclaration | FunctionExpression | ParameterDeclaration | VariableDeclaration | PropertyDeclaration | PropertySignature | TypeParameterDeclaration | Identifier | Decorator | GetAccessorDeclaration | SetAccessorDeclaration | ImportSpecifier | CommentRange | EnumDeclaration | EnumMember | TypeAliasDeclaration | ExpressionWithTypeArguments, famixElement: Famix.SourcedEntity): void {
28
+ const fmxIndexFileAnchor = new Famix.IndexedFileAnchor(this.famixRep);
29
+ fmxIndexFileAnchor.setElement(famixElement);
30
+
31
+ if (sourceElement !== null) {
32
+ fmxIndexFileAnchor.setFileName(sourceElement.getSourceFile().getFilePath());
33
+
34
+ if (!(sourceElement instanceof CommentRange)) {
35
+ fmxIndexFileAnchor.setStartPos(sourceElement.getStart());
36
+ fmxIndexFileAnchor.setEndPos(sourceElement.getEnd());
37
+ }
38
+
39
+ if (!(famixElement instanceof Famix.Association) && !(famixElement instanceof Famix.Comment) && !(sourceElement instanceof CommentRange) && !(sourceElement instanceof Identifier) && !(sourceElement instanceof ImportSpecifier) && !(sourceElement instanceof ExpressionWithTypeArguments)) {
40
+ (famixElement as Famix.NamedEntity).setFullyQualifiedName(this.FQNFunctions.getFQN(sourceElement));
41
+ }
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,100 @@
1
+ import { ConstructorDeclaration, FunctionDeclaration, MethodDeclaration, MethodSignature, PropertyDeclaration, PropertySignature, VariableDeclaration, ParameterDeclaration, GetAccessorDeclaration, SetAccessorDeclaration, EnumMember, TypeAliasDeclaration, Node, SyntaxKind, FunctionExpression } from "ts-morph";
2
+ import * as Famix from "../lib/famix/src/model/famix";
3
+ import { FamixRepository } from "../lib/famix/src/famix_repository";
4
+ import { FQNFunctions } from "../fqn";
5
+ import { FamixFunctionsIndex } from "./famix_functions_index";
6
+
7
+ /**
8
+ * This class is used to build a Famix model for the types
9
+ */
10
+ export class FamixFunctionsTypes {
11
+
12
+ private famixRep: FamixRepository; // The Famix repository
13
+ private FQNFunctions = new FQNFunctions(); // The fully qualified name functions
14
+ private fmxTypes = new Map<string, Famix.Type | Famix.PrimitiveType | Famix.ParameterizedType>(); // Maps the type names to their Famix model
15
+ private famixFunctionsIndex: FamixFunctionsIndex; // FamixFunctionsIndex object, it contains all the functions needed to create Famix index file anchors
16
+
17
+ /**
18
+ * Initializes the FamixFunctionsIndex object
19
+ * @param famixRep The Famix repository
20
+ */
21
+ constructor(famixRep: FamixRepository) {
22
+ this.famixRep = famixRep;
23
+ this.famixFunctionsIndex = new FamixFunctionsIndex(famixRep);
24
+ }
25
+
26
+ /**
27
+ * Creates or gets a Famix type
28
+ * @param typeName A type name
29
+ * @param element A ts-morph element
30
+ * @returns The Famix model of the type
31
+ */
32
+ public createOrGetFamixType(typeName: string, element: TypeAliasDeclaration | PropertyDeclaration | PropertySignature | MethodDeclaration | ConstructorDeclaration | MethodSignature | GetAccessorDeclaration | SetAccessorDeclaration | FunctionDeclaration | FunctionExpression | ParameterDeclaration | VariableDeclaration | EnumMember): Famix.Type | Famix.PrimitiveType | Famix.ParameterizedType {
33
+ let fmxType: Famix.Type | Famix.PrimitiveType | Famix.ParameterizedType;
34
+ let isPrimitiveType = false;
35
+ let isParameterizedType = false;
36
+
37
+ const typeAncestor = this.findTypeAncestor(element);
38
+ const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(typeAncestor);
39
+ const ancestor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName) as Famix.ContainerEntity;
40
+
41
+ if (typeName === "number" || typeName === "string" || typeName === "boolean" || typeName === "bigint" || typeName === "symbol" || typeName === "undefined" || typeName === "null") {
42
+ isPrimitiveType = true;
43
+ }
44
+
45
+ if(!isPrimitiveType && typeName.includes("<") && typeName.includes(">") && !(typeName.includes("=>"))) {
46
+ isParameterizedType = true;
47
+ }
48
+
49
+ if (!this.fmxTypes.has(typeName)) {
50
+ if (isPrimitiveType) {
51
+ fmxType = new Famix.PrimitiveType(this.famixRep);
52
+ fmxType.setIsStub(true);
53
+ }
54
+ else if (isParameterizedType) {
55
+ fmxType = new Famix.ParameterizedType(this.famixRep);
56
+ const parameterTypeNames = typeName.substring(typeName.indexOf("<") + 1, typeName.indexOf(">")).split(",").map(s => s.trim());
57
+ const baseTypeName = typeName.substring(0, typeName.indexOf("<")).trim();
58
+ parameterTypeNames.forEach(parameterTypeName => {
59
+ const fmxParameterType = this.createOrGetFamixType(parameterTypeName, element);
60
+ (fmxType as Famix.ParameterizedType).addArgument(fmxParameterType);
61
+ });
62
+ const fmxBaseType = this.createOrGetFamixType(baseTypeName, element);
63
+ (fmxType as Famix.ParameterizedType).setBaseType(fmxBaseType);
64
+ }
65
+ else {
66
+ fmxType = new Famix.Type(this.famixRep);
67
+ }
68
+
69
+ fmxType.setName(typeName);
70
+ fmxType.setContainer(ancestor);
71
+
72
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(null, fmxType);
73
+
74
+ this.fmxTypes.set(typeName, fmxType);
75
+ }
76
+ else {
77
+ fmxType = this.fmxTypes.get(typeName);
78
+ }
79
+
80
+ return fmxType;
81
+ }
82
+
83
+ /**
84
+ * Gets a Famix entity by fully qualified name
85
+ * @param fullyQualifiedName A fully qualified name
86
+ * @returns The Famix entity corresponding to the fully qualified name
87
+ */
88
+ private getFamixEntityByFullyQualifiedName(fullyQualifiedName: string): Famix.Entity {
89
+ return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName) as Famix.Entity;
90
+ }
91
+
92
+ /**
93
+ * Finds the ancestor of a ts-morph element
94
+ * @param element A ts-morph element
95
+ * @returns The ancestor of the ts-morph element
96
+ */
97
+ private findTypeAncestor(element: TypeAliasDeclaration | PropertyDeclaration | PropertySignature | MethodDeclaration | ConstructorDeclaration | MethodSignature | GetAccessorDeclaration | SetAccessorDeclaration | FunctionDeclaration | FunctionExpression | ParameterDeclaration | VariableDeclaration | EnumMember): Node {
98
+ return element.getAncestors().find(a => a.getKind() === SyntaxKind.MethodDeclaration || a.getKind() === SyntaxKind.Constructor || a.getKind() === SyntaxKind.MethodSignature || a.getKind() === SyntaxKind.FunctionDeclaration || a.getKind() === SyntaxKind.FunctionExpression || a.getKind() === SyntaxKind.ModuleDeclaration || a.getKind() === SyntaxKind.SourceFile || a.getKindName() === "GetAccessor" || a.getKindName() === "SetAccessor" || a.getKind() === SyntaxKind.ClassDeclaration || a.getKind() === SyntaxKind.InterfaceDeclaration);
99
+ }
100
+ }
package/src/fqn.ts ADDED
@@ -0,0 +1,127 @@
1
+ import * as ts from "ts-morph";
2
+
3
+ /**
4
+ * This class is used to get the fully qualified name of a node
5
+ */
6
+ export class FQNFunctions {
7
+
8
+ /**
9
+ * Gets the fully qualified name of a node, if it has one
10
+ * @param node A node
11
+ * @returns The fully qualified name of the node, or undefined if it doesn't have one
12
+ */
13
+ public getFQN(node: ts.Node): string {
14
+ if (node instanceof ts.SourceFile) {
15
+ return `"${node.getFilePath()}"`;
16
+ }
17
+
18
+ const symbol = node.getSymbol();
19
+ if (!symbol) {
20
+ return undefined;
21
+ }
22
+
23
+ const declarations = symbol.getDeclarations();
24
+ if (!declarations) {
25
+ return undefined;
26
+ }
27
+
28
+ const sourceFile = declarations[0].getSourceFile();
29
+ if (!sourceFile) {
30
+ return undefined;
31
+ }
32
+
33
+ const sourceFilePath = sourceFile.getFilePath();
34
+ const sourceFileDirectory = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/"));
35
+
36
+ const qualifiedNameParts: Array<string> = [];
37
+
38
+ const nodeName = this.getNameOfNode(node);
39
+ if (nodeName) qualifiedNameParts.push(nodeName);
40
+
41
+ const ancestors = node.getAncestors();
42
+ ancestors.forEach(a => {
43
+ const partName = this.getNameOfNode(a);
44
+ if (partName) qualifiedNameParts.push(partName);
45
+ });
46
+
47
+ if (qualifiedNameParts.length > 0) {
48
+ return `"${sourceFileDirectory}/${qualifiedNameParts.pop()}".${qualifiedNameParts.reverse().join(".")}`;
49
+ }
50
+ else {
51
+ return undefined;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Gets the name of a node, if it has one
57
+ * @param a A node
58
+ * @returns The name of the node, or an empty string if it doesn't have one
59
+ */
60
+ private getNameOfNode(a: ts.Node<ts.ts.Node>): string {
61
+ switch (a.getKind()) {
62
+ case ts.SyntaxKind.SourceFile:
63
+ return a.asKind(ts.SyntaxKind.SourceFile)?.getBaseName();
64
+
65
+ case ts.SyntaxKind.ModuleDeclaration:
66
+ return a.asKind(ts.SyntaxKind.ModuleDeclaration)?.getName();
67
+
68
+ case ts.SyntaxKind.ClassDeclaration:
69
+ return a.asKind(ts.SyntaxKind.ClassDeclaration)?.getName();
70
+
71
+ case ts.SyntaxKind.InterfaceDeclaration:
72
+ return a.asKind(ts.SyntaxKind.InterfaceDeclaration)?.getName();
73
+
74
+ case ts.SyntaxKind.PropertyDeclaration:
75
+ return a.asKind(ts.SyntaxKind.PropertyDeclaration)?.getName();
76
+
77
+ case ts.SyntaxKind.PropertySignature:
78
+ return a.asKind(ts.SyntaxKind.PropertySignature)?.getName();
79
+
80
+ case ts.SyntaxKind.MethodDeclaration:
81
+ return a.asKind(ts.SyntaxKind.MethodDeclaration)?.getName();
82
+
83
+ case ts.SyntaxKind.MethodSignature:
84
+ return a.asKind(ts.SyntaxKind.MethodSignature)?.getName();
85
+
86
+ case ts.SyntaxKind.GetAccessor:
87
+ return a.asKind(ts.SyntaxKind.GetAccessor)?.getName();
88
+
89
+ case ts.SyntaxKind.SetAccessor:
90
+ return a.asKind(ts.SyntaxKind.SetAccessor)?.getName();
91
+
92
+ case ts.SyntaxKind.FunctionDeclaration:
93
+ return a.asKind(ts.SyntaxKind.FunctionDeclaration)?.getName();
94
+
95
+ case ts.SyntaxKind.FunctionExpression:
96
+ return (a.asKind(ts.SyntaxKind.FunctionExpression)?.getName()) ? a.asKind(ts.SyntaxKind.FunctionExpression)?.getName() : "anonymous";
97
+
98
+ case ts.SyntaxKind.Parameter:
99
+ return a.asKind(ts.SyntaxKind.Parameter)?.getName();
100
+
101
+ case ts.SyntaxKind.VariableDeclaration:
102
+ return a.asKind(ts.SyntaxKind.VariableDeclaration)?.getName();
103
+
104
+ case ts.SyntaxKind.Decorator:
105
+ return "@" + a.asKind(ts.SyntaxKind.Decorator)?.getName();
106
+
107
+ case ts.SyntaxKind.TypeParameter:
108
+ return a.asKind(ts.SyntaxKind.TypeParameter)?.getName();
109
+
110
+ case ts.SyntaxKind.EnumDeclaration:
111
+ return a.asKind(ts.SyntaxKind.EnumDeclaration)?.getName();
112
+
113
+ case ts.SyntaxKind.EnumMember:
114
+ return a.asKind(ts.SyntaxKind.EnumMember)?.getName();
115
+
116
+ case ts.SyntaxKind.TypeAliasDeclaration:
117
+ return a.asKind(ts.SyntaxKind.TypeAliasDeclaration)?.getName();
118
+
119
+ case ts.SyntaxKind.Constructor:
120
+ return "constructor";
121
+
122
+ default:
123
+ // ancestor hasn't got a useful name
124
+ return "";
125
+ }
126
+ }
127
+ }
@@ -0,0 +1,66 @@
1
+ import * as ts from "typescript";
2
+
3
+ function getFullyQualifiedName(
4
+ node: ts.Node,
5
+ checker: ts.TypeChecker,
6
+ ): string | undefined {
7
+ // Traverse the lexical hierarchy to construct the fully qualified name.
8
+ function getQualifiedName(node: ts.Node): string | undefined {
9
+ console.log(`getFullyQualifiedName: ${node}`);
10
+ if (node === undefined) {
11
+ return undefined;
12
+ }
13
+ console.log(`getFullyQualifiedName kind: ${node.kind}, ${ts.SyntaxKind[node.kind]}`);
14
+ if (ts.isSourceFile(node)) {
15
+ return undefined;
16
+ } else if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
17
+ const symbol = checker.getSymbolAtLocation(node.name);
18
+ if (symbol) {
19
+ const name = symbol.getName();
20
+ const parentName = getQualifiedName(node.parent);
21
+ return parentName ? `${parentName}.${name}` : name;
22
+ }
23
+ }
24
+ return getQualifiedName(node.parent);
25
+ }
26
+
27
+ return getQualifiedName(node);
28
+ }
29
+
30
+ const sourceFile = ts.createSourceFile(
31
+ "test.ts",
32
+ "function foo() { const x = 1; }",
33
+ ts.ScriptTarget.ES2015,
34
+ );
35
+
36
+
37
+ // create a program instance, which is needed to create a type checker
38
+ const program = ts.createProgram(["test.ts"], {});
39
+ const typeChecker = program.getTypeChecker();
40
+
41
+ const statements = sourceFile.statements;
42
+
43
+ statements.forEach((statement) => {
44
+ console.log(`statement: ${statement.kind}`);
45
+ if (ts.isFunctionDeclaration(statement)) {
46
+ console.log(`function name: ${statement.name?.text}`);
47
+ }
48
+ });
49
+
50
+ // find all the identifiers in the source file (don't use ts.createTypeChecker here, make it work for TS 5.1.x)
51
+ const identifiers: ts.Identifier[] = [];
52
+ ts.forEachChild(sourceFile, function walk(node) {
53
+ if (ts.isIdentifier(node)) {
54
+ identifiers.push(node);
55
+ }
56
+ ts.forEachChild(node, walk);
57
+ });
58
+
59
+ console.log(identifiers.map((identifier) => identifier.text).join(", "));
60
+
61
+ // find the fully qualified name of each identifier (don't use ts.createTypeChecker here)
62
+ const fullyQualifiedNames = identifiers.map((identifier) => getFullyQualifiedName(identifier, typeChecker));
63
+ console.log(fullyQualifiedNames.join(", "));
64
+
65
+
66
+
@@ -0,0 +1,16 @@
1
+ # Gets latest plantuml
2
+ rm -f plantuml.jar
3
+ wget --no-verbose https://downloads.sourceforge.net/project/plantuml/plantuml.jar
4
+ # Builds metamodel plantuml from TypeScript sources
5
+ npx tplant -i src/lib/famix/**/*.ts -o doc-metamodel/famix-typescript-model.puml
6
+ sed -i 's/@startuml/& metamodel/' doc-metamodel/famix-typescript-model.puml
7
+ # Converts plantuml source to SVG image
8
+ java -jar plantuml.jar -v -tsvg doc-metamodel/famix-typescript-model.puml
9
+ mv doc-metamodel/metamodel.svg doc-metamodel/metamodel-full.svg
10
+ # Inserts include line to customize the file
11
+ sed -i '/@startuml metamodel/a !include skins.include.puml' doc-metamodel/famix-typescript-model.puml
12
+ # Converts plantuml source to SVG image
13
+ java -jar plantuml.jar -v -tsvg doc-metamodel/famix-typescript-model.puml
14
+ # Moves artifacts
15
+ rm -f doc-uml/*
16
+ mv doc-metamodel/*.svg doc-uml
@@ -0,0 +1,23 @@
1
+ Portions of the code in the famix directory:
2
+
3
+ MIT License
4
+
5
+ Copyright (c) 2019 Pascal Erni
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.