ts2famix 2.0.1 → 2.1.0-beta.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 (190) hide show
  1. package/.eslintrc.json +24 -24
  2. package/.vscode/settings.json +4 -0
  3. package/LICENSE +24 -24
  4. package/README.md +78 -78
  5. package/TODO +1 -0
  6. package/arwea-fix.json +1 -0
  7. package/bogus.ts +3 -0
  8. package/class-diagram.puml +792 -0
  9. package/debug.txt +13332 -0
  10. package/debuglog.txt +12073 -0
  11. package/dist/analyze.js +2 -2
  12. package/dist/analyze_functions/process_functions.js +84 -64
  13. package/dist/famix2puml.js +126 -0
  14. package/dist/famix_functions/EntityDictionary.js +865 -503
  15. package/dist/famix_functions/helpers_creation.js +1 -1
  16. package/dist/fqn.js +44 -11
  17. package/dist/getClasses-arrow-body.js +43 -0
  18. package/dist/lib/famix/famix_JSON_exporter.js +1 -1
  19. package/dist/lib/famix/famix_base_element.js +1 -1
  20. package/dist/lib/famix/famix_repository.js +57 -2
  21. package/dist/lib/famix/index.js +1 -1
  22. package/dist/lib/famix/model/famix/access.js +1 -1
  23. package/dist/lib/famix/model/famix/accessor.js +1 -1
  24. package/dist/lib/famix/model/famix/alias.js +1 -1
  25. package/dist/lib/famix/model/famix/arrow_function.js +1 -1
  26. package/dist/lib/famix/model/famix/behavioral_entity.js +1 -1
  27. package/dist/lib/famix/model/famix/class.js +1 -1
  28. package/dist/lib/famix/model/famix/comment.js +1 -1
  29. package/dist/lib/famix/model/famix/concretisation.js +1 -1
  30. package/dist/lib/famix/model/famix/container_entity.js +1 -1
  31. package/dist/lib/famix/model/famix/decorator.js +1 -1
  32. package/dist/lib/famix/model/famix/entity.js +1 -1
  33. package/dist/lib/famix/model/famix/enum.js +1 -1
  34. package/dist/lib/famix/model/famix/enum_value.js +1 -1
  35. package/dist/lib/famix/model/famix/function.js +1 -1
  36. package/dist/lib/famix/model/famix/import_clause.js +1 -1
  37. package/dist/lib/famix/model/famix/index.js +1 -1
  38. package/dist/lib/famix/model/famix/indexed_file_anchor.js +1 -1
  39. package/dist/lib/famix/model/famix/inheritance.js +1 -1
  40. package/dist/lib/famix/model/famix/interface.js +1 -1
  41. package/dist/lib/famix/model/famix/invocation.js +1 -1
  42. package/dist/lib/famix/model/famix/method.js +1 -1
  43. package/dist/lib/famix/model/famix/module.js +1 -1
  44. package/dist/lib/famix/model/famix/named_entity.js +1 -1
  45. package/dist/lib/famix/model/famix/parameter.js +1 -1
  46. package/dist/lib/famix/model/famix/parameter_concretisation.js +1 -1
  47. package/dist/lib/famix/model/famix/parameter_type.js +1 -1
  48. package/dist/lib/famix/model/famix/parametric_arrow_function.js +1 -1
  49. package/dist/lib/famix/model/famix/parametric_class.js +1 -1
  50. package/dist/lib/famix/model/famix/parametric_function.js +1 -1
  51. package/dist/lib/famix/model/famix/parametric_interface.js +1 -1
  52. package/dist/lib/famix/model/famix/parametric_method.js +1 -1
  53. package/dist/lib/famix/model/famix/primitive_type.js +1 -1
  54. package/dist/lib/famix/model/famix/property.js +1 -1
  55. package/dist/lib/famix/model/famix/reference.js +1 -1
  56. package/dist/lib/famix/model/famix/scoping_entity.js +1 -1
  57. package/dist/lib/famix/model/famix/script_entity.js +1 -1
  58. package/dist/lib/famix/model/famix/source_anchor.js +1 -1
  59. package/dist/lib/famix/model/famix/source_language.js +1 -1
  60. package/dist/lib/famix/model/famix/sourced_entity.js +1 -1
  61. package/dist/lib/famix/model/famix/structural_entity.js +1 -1
  62. package/dist/lib/famix/model/famix/type.js +1 -1
  63. package/dist/lib/famix/model/famix/variable.js +1 -1
  64. package/dist/lib/famix/src/famix_JSON_exporter.js +55 -0
  65. package/dist/lib/famix/src/famix_base_element.js +18 -0
  66. package/dist/lib/famix/src/famix_repository.js +224 -0
  67. package/dist/lib/famix/src/index.js +31 -0
  68. package/dist/lib/famix/src/model/famix/access.js +40 -0
  69. package/dist/lib/famix/src/model/famix/accessor.js +17 -0
  70. package/dist/lib/famix/src/model/famix/alias.js +33 -0
  71. package/dist/lib/famix/src/model/famix/arrowFunction.js +17 -0
  72. package/dist/lib/famix/src/model/famix/arrow_function.js +17 -0
  73. package/dist/lib/famix/src/model/famix/behavioral_entity.js +79 -0
  74. package/dist/lib/famix/src/model/famix/class.js +71 -0
  75. package/dist/lib/famix/src/model/famix/comment.js +39 -0
  76. package/dist/lib/famix/src/model/famix/concretisation.js +31 -0
  77. package/dist/lib/famix/src/model/famix/container_entity.js +126 -0
  78. package/dist/lib/famix/src/model/famix/decorator.js +32 -0
  79. package/dist/lib/famix/src/model/famix/entity.js +17 -0
  80. package/dist/lib/famix/src/model/famix/enum.js +31 -0
  81. package/dist/lib/famix/src/model/famix/enum_value.js +25 -0
  82. package/dist/lib/famix/src/model/famix/function.js +17 -0
  83. package/dist/lib/famix/src/model/famix/implicit_variable.js +17 -0
  84. package/dist/lib/famix/src/model/famix/import_clause.js +41 -0
  85. package/dist/lib/famix/src/model/famix/index.js +86 -0
  86. package/dist/lib/famix/src/model/famix/indexed_file_anchor.js +38 -0
  87. package/dist/lib/famix/src/model/famix/inheritance.js +33 -0
  88. package/dist/lib/famix/src/model/famix/interface.js +64 -0
  89. package/dist/lib/famix/src/model/famix/invocation.js +54 -0
  90. package/dist/lib/famix/src/model/famix/method.js +67 -0
  91. package/dist/lib/famix/src/model/famix/module.js +60 -0
  92. package/dist/lib/famix/src/model/famix/named_entity.js +78 -0
  93. package/dist/lib/famix/src/model/famix/parameter.js +25 -0
  94. package/dist/lib/famix/src/model/famix/parameterConcretisation.js +44 -0
  95. package/dist/lib/famix/src/model/famix/parameter_concretisation.js +44 -0
  96. package/dist/lib/famix/src/model/famix/parameter_type.js +45 -0
  97. package/dist/lib/famix/src/model/famix/parametricArrowFunction.js +29 -0
  98. package/dist/lib/famix/src/model/famix/parametric_arrow_function.js +31 -0
  99. package/dist/lib/famix/src/model/famix/parametric_class.js +44 -0
  100. package/dist/lib/famix/src/model/famix/parametric_function.js +31 -0
  101. package/dist/lib/famix/src/model/famix/parametric_interface.js +44 -0
  102. package/dist/lib/famix/src/model/famix/parametric_method.js +31 -0
  103. package/dist/lib/famix/src/model/famix/primitive_type.js +17 -0
  104. package/dist/lib/famix/src/model/famix/property.js +73 -0
  105. package/dist/lib/famix/src/model/famix/reference.js +33 -0
  106. package/dist/lib/famix/src/model/famix/scoping_entity.js +36 -0
  107. package/dist/lib/famix/src/model/famix/script_entity.js +29 -0
  108. package/dist/lib/famix/src/model/famix/source_anchor.js +27 -0
  109. package/dist/lib/famix/src/model/famix/source_language.js +35 -0
  110. package/dist/lib/famix/src/model/famix/sourced_entity.js +60 -0
  111. package/dist/lib/famix/src/model/famix/structural_entity.js +39 -0
  112. package/dist/lib/famix/src/model/famix/text_anchor.js +38 -0
  113. package/dist/lib/famix/src/model/famix/type.js +73 -0
  114. package/dist/lib/famix/src/model/famix/variable.js +24 -0
  115. package/dist/lib/ts-complex/cyclomatic-service.js +1 -1
  116. package/dist/refactorer/refactor-getter-setter.js +1 -1
  117. package/dist/ts2famix-cli-wrapper.js +1 -1
  118. package/dist/ts2famix-cli.js +1 -1
  119. package/doc-uml/famix-typescript-model.puml +607 -607
  120. package/eslint.config.mjs +28 -0
  121. package/fqn-model.json +1 -0
  122. package/iterateGenericTypes.ts +69 -0
  123. package/out/class-diagram/class-diagram.svg +1 -0
  124. package/package.json +70 -50
  125. package/sample.json +1 -0
  126. package/sample.ts +1 -0
  127. package/src/analyze.ts +120 -120
  128. package/src/analyze_functions/process_functions.ts +1040 -1019
  129. package/src/famix_functions/EntityDictionary.ts +2016 -1593
  130. package/src/famix_functions/helpers_creation.ts +135 -135
  131. package/src/fqn.ts +50 -16
  132. package/src/generate_uml.sh +20 -20
  133. package/src/lib/famix/License.md +22 -22
  134. package/src/lib/famix/famix_JSON_exporter.ts +56 -56
  135. package/src/lib/famix/famix_base_element.ts +22 -22
  136. package/src/lib/famix/famix_repository.ts +278 -243
  137. package/src/lib/famix/index.ts +8 -8
  138. package/src/lib/famix/model/famix/access.ts +50 -50
  139. package/src/lib/famix/model/famix/accessor.ts +15 -15
  140. package/src/lib/famix/model/famix/alias.ts +39 -39
  141. package/src/lib/famix/model/famix/arrow_function.ts +15 -15
  142. package/src/lib/famix/model/famix/behavioral_entity.ts +97 -97
  143. package/src/lib/famix/model/famix/class.ts +85 -85
  144. package/src/lib/famix/model/famix/comment.ts +47 -47
  145. package/src/lib/famix/model/famix/concretisation.ts +40 -40
  146. package/src/lib/famix/model/famix/container_entity.ts +160 -160
  147. package/src/lib/famix/model/famix/decorator.ts +37 -37
  148. package/src/lib/famix/model/famix/entity.ts +15 -15
  149. package/src/lib/famix/model/famix/enum.ts +30 -30
  150. package/src/lib/famix/model/famix/enum_value.ts +28 -28
  151. package/src/lib/famix/model/famix/function.ts +15 -15
  152. package/src/lib/famix/model/famix/import_clause.ts +51 -51
  153. package/src/lib/famix/model/famix/index.ts +41 -41
  154. package/src/lib/famix/model/famix/indexed_file_anchor.ts +46 -46
  155. package/src/lib/famix/model/famix/inheritance.ts +40 -40
  156. package/src/lib/famix/model/famix/interface.ts +75 -75
  157. package/src/lib/famix/model/famix/invocation.ts +65 -65
  158. package/src/lib/famix/model/famix/method.ts +89 -89
  159. package/src/lib/famix/model/famix/module.ts +71 -71
  160. package/src/lib/famix/model/famix/named_entity.ts +95 -95
  161. package/src/lib/famix/model/famix/parameter.ts +28 -28
  162. package/src/lib/famix/model/famix/parameter_concretisation.ts +51 -51
  163. package/src/lib/famix/model/famix/parameter_type.ts +58 -58
  164. package/src/lib/famix/model/famix/parametric_arrow_function.ts +32 -32
  165. package/src/lib/famix/model/famix/parametric_class.ts +49 -49
  166. package/src/lib/famix/model/famix/parametric_function.ts +32 -32
  167. package/src/lib/famix/model/famix/parametric_interface.ts +49 -49
  168. package/src/lib/famix/model/famix/parametric_method.ts +32 -32
  169. package/src/lib/famix/model/famix/primitive_type.ts +15 -15
  170. package/src/lib/famix/model/famix/property.ts +94 -94
  171. package/src/lib/famix/model/famix/reference.ts +40 -40
  172. package/src/lib/famix/model/famix/scoping_entity.ts +35 -35
  173. package/src/lib/famix/model/famix/script_entity.ts +34 -34
  174. package/src/lib/famix/model/famix/source_anchor.ts +30 -30
  175. package/src/lib/famix/model/famix/source_language.ts +35 -35
  176. package/src/lib/famix/model/famix/sourced_entity.ts +70 -70
  177. package/src/lib/famix/model/famix/structural_entity.ts +43 -43
  178. package/src/lib/famix/model/famix/type.ts +87 -87
  179. package/src/lib/famix/model/famix/variable.ts +27 -27
  180. package/src/lib/famix/package.json +28 -28
  181. package/src/lib/ts-complex/cyclomatic-service.ts +83 -83
  182. package/src/refactorer/refactor-getter-setter.ts +140 -140
  183. package/src/ts2famix-cli-wrapper.ts +21 -21
  184. package/src/ts2famix-cli.ts +60 -60
  185. package/stats.txt +3091 -0
  186. package/tabby-debug-output.txt +19433 -0
  187. package/ts2famix.log +22656 -0
  188. package/tsconfig.check-tests.json +14 -14
  189. package/tsconfig.json +72 -72
  190. package/validate-references.js +103 -0
@@ -1,22 +1,22 @@
1
- import { logger } from "../../analyze";
2
- import { FamixJSONExporter } from "./famix_JSON_exporter";
3
- // import { FamixRepository } from "./famix_repository";
4
-
5
- export abstract class FamixBaseElement {
6
-
7
- public id!: number;
8
-
9
- // constructor(repo: FamixRepository) {
10
- // repo.addElement(this);
11
- // }
12
-
13
- constructor() {
14
- }
15
-
16
- public abstract getJSON(): string;
17
-
18
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
- public addPropertiesToExporter(exporter: FamixJSONExporter): void {
20
- logger.debug("addPropertiesToExporter not implemented for " + this.constructor.name + `(${exporter})`);
21
- }
22
- }
1
+ import { logger } from "../../analyze";
2
+ import { FamixJSONExporter } from "./famix_JSON_exporter";
3
+ // import { FamixRepository } from "./famix_repository";
4
+
5
+ export abstract class FamixBaseElement {
6
+
7
+ public id!: number;
8
+
9
+ // constructor(repo: FamixRepository) {
10
+ // repo.addElement(this);
11
+ // }
12
+
13
+ constructor() {
14
+ }
15
+
16
+ public abstract getJSON(): string;
17
+
18
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
+ public addPropertiesToExporter(exporter: FamixJSONExporter): void {
20
+ logger.debug("addPropertiesToExporter not implemented for " + this.constructor.name + `(${exporter})`);
21
+ }
22
+ }
@@ -1,243 +1,278 @@
1
- import { FamixBaseElement } from "./famix_base_element";
2
- import { Class, Interface, Variable, Method, ArrowFunction, Function as FamixFunctionEntity, Type, NamedEntity, ScriptEntity, Module, SourceLanguage } from "./model/famix";
3
- import * as Famix from "./model/famix";
4
- import { TSMorphObjectType } from "../../famix_functions/EntityDictionary";
5
- /**
6
- * This class is used to store all Famix elements
7
- */
8
- export class FamixRepository {
9
- private elements = new Set<FamixBaseElement>(); // All Famix elements
10
- private famixClasses = new Set<Class>(); // All Famix classes
11
- private famixInterfaces = new Set<Interface>(); // All Famix interfaces
12
- private famixModules = new Set<Module>(); // All Famix namespaces
13
- private famixMethods = new Set<Method>(); // All Famix methods
14
- private famixVariables = new Set<Variable>(); // All Famix variables
15
- private famixFunctions = new Set<FamixFunctionEntity>(); // All Famix functions
16
- private famixFiles = new Set<ScriptEntity | Module>(); // All Famix files
17
- private idCounter = 1; // Id counter
18
- private absolutePath: string = "";
19
- private fmxElementObjectMap = new Map<Famix.Entity, TSMorphObjectType>();
20
- private tsMorphObjectMap = new Map<TSMorphObjectType, Famix.Entity>(); // TODO: add this map to have two-way mapping between Famix and TS Morph objects
21
-
22
- constructor() {
23
- this.addElement(new SourceLanguage()); // add the source language entity (TypeScript)
24
- }
25
-
26
- public setFmxElementObjectMap(fmxElementObjectMap: Map<Famix.Entity, TSMorphObjectType>) {
27
- this.fmxElementObjectMap = fmxElementObjectMap;
28
- }
29
-
30
- public getFmxElementObjectMap() {
31
- return this.fmxElementObjectMap;
32
- }
33
-
34
- public getAbsolutePath(): string {
35
- return this.absolutePath;
36
- }
37
-
38
- public setAbsolutePath(path: string) {
39
- this.absolutePath = path;
40
- }
41
-
42
- /**
43
- * Gets a Famix entity by id
44
- * @param id An id of a Famix entity
45
- * @returns The Famix entity corresponding to the id or undefined if it doesn't exist
46
- */
47
- public getFamixEntityById(id: number): FamixBaseElement | undefined {
48
- const entity = Array.from(this.elements.values()).find(e => e.id === id);
49
- return entity;
50
- }
51
-
52
- /**
53
- * Gets a Famix entity by fully qualified name
54
- * @param fullyQualifiedName A fully qualified name
55
- * @returns The Famix entity corresponding to the fully qualified name or undefined if it doesn't exist
56
- */
57
- public getFamixEntityByFullyQualifiedName(fullyQualifiedName: string): FamixBaseElement | undefined {
58
- const allEntities = Array.from(this.elements.values()).filter(e => e instanceof NamedEntity) as Array<NamedEntity>;
59
- const entity = allEntities.find(e => e.fullyQualifiedName === fullyQualifiedName);
60
- return entity;
61
- }
62
-
63
- export(arg0: { format: string; }) {
64
- if (arg0.format === "json") {
65
- return this.getJSON();
66
- } else {
67
- throw new Error("Unsupported format");
68
- }
69
- }
70
-
71
-
72
- // Only for tests
73
-
74
- /**
75
- * Gets all Famix entities
76
- * @returns All Famix entities
77
- */
78
- public _getAllEntities(): Set<FamixBaseElement> {
79
- return new Set(Array.from(this.elements.values()));
80
- }
81
-
82
- /**
83
- * Gets all Famix entities of a given type
84
- * @param theType A type of Famix entity
85
- * @returns All Famix entities of the given type
86
- */
87
- public _getAllEntitiesWithType(theType: string): Set<FamixBaseElement> {
88
- return new Set(Array.from(this.elements.values()).filter(e => (e as FamixBaseElement).constructor.name === theType));
89
- }
90
-
91
- /**
92
- * Gets a Famix class by name
93
- * @param name A class name
94
- * @returns The Famix class corresponding to the name or undefined if it doesn't exist
95
- */
96
- public _getFamixClass(fullyQualifiedName: string): Class | undefined {
97
- return Array.from(this.famixClasses.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
98
- }
99
-
100
- /**
101
- * Gets a Famix interface by name
102
- * @param name An interface name
103
- * @returns The Famix interface corresponding to the name or undefined if it doesn't exist
104
- */
105
- public _getFamixInterface(fullyQualifiedName: string): Interface | undefined {
106
- return Array.from(this.famixInterfaces.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
107
- }
108
-
109
- /**
110
- * Gets a Famix method by name
111
- * @param name A method name
112
- * @returns The Famix method corresponding to the name or undefined if it doesn't exist
113
- */
114
- public _getFamixMethod(fullyQualifiedName: string): Method | undefined {
115
- return Array.from(this.famixMethods.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
116
- }
117
-
118
- /**
119
- * Gets a Famix function by name
120
- * @param name A function name
121
- * @returns The Famix function corresponding to the name or undefined if it doesn't exist
122
- */
123
- public _getFamixFunction(fullyQualifiedName: string): FamixFunctionEntity | undefined {
124
- return Array.from(this.famixFunctions.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
125
- }
126
-
127
-
128
- /**
129
- * Gets a Famix variable by name
130
- * @param name A variable name
131
- * @returns The Famix variable corresponding to the name or undefined if it doesn't exist
132
- */
133
- public _getFamixVariable(fullyQualifiedName: string): Variable | undefined {
134
- return Array.from(this.famixVariables.values()).find(v => v.fullyQualifiedName === fullyQualifiedName);
135
- }
136
-
137
- /**
138
- * Gets a Famix namespace by name
139
- * @param name A namespace name
140
- * @returns The Famix namespace corresponding to the name or undefined if it doesn't exist
141
- */
142
- public _getFamixModule(fullyQualifiedName: string): Module | undefined {
143
- return Array.from(this.famixModules.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
144
- }
145
-
146
- /**
147
- * Gets all Famix namespaces
148
- * @returns All Famix namespaces
149
- */
150
- public _getFamixModules(): Set<Module> {
151
- return new Set(Array.from(this.famixModules.values()));
152
- }
153
-
154
- /**
155
- * Gets a Famix file by name
156
- * @param name A file name
157
- * @returns The Famix file corresponding to the name or undefined if it doesn't exist
158
- */
159
- public _getFamixFile(fullyQualifiedName: string): ScriptEntity | Module | undefined {
160
- return Array.from(this.famixFiles.values()).find(ns => ns.name === fullyQualifiedName);
161
- }
162
-
163
- /**
164
- * Gets all Famix files
165
- * @returns All Famix files
166
- */
167
- public _getFamixFiles(): Set<ScriptEntity | Module> {
168
- return new Set(Array.from(this.famixFiles.values()));
169
- }
170
-
171
- /**
172
- * Gets all method names as a set from a class
173
- * @param className A class name
174
- * @returns The set of class "className" method names
175
- */
176
- public _methodNamesAsSetFromClass(className: string): Set<string> {
177
- const theClass = this._getFamixClass(className) as Class;
178
- return new Set(Array.from(theClass.methods).map(m => m.name));
179
- }
180
-
181
- /**
182
- * Gets all method parents as a set from a class
183
- * @param className A class name
184
- * @returns The set of class "className" method parents
185
- */
186
- public _methodParentsAsSetFromClass(className: string): Set<Type> {
187
- const theClass = this._getFamixClass(className) as Class;
188
- return new Set(Array.from(theClass.methods).map(m => m.parentEntity));
189
- }
190
-
191
- /**
192
- * Gets the map of Famix element ids and their Famix element from a JSON model
193
- * @param model A JSON model
194
- * @returns The map of Famix element ids and their Famix element from the JSON model
195
- */
196
- public _initMapFromModel(model: string): Map<number, unknown> {
197
- const parsedModel: Array<FamixBaseElement> = JSON.parse(model);
198
- const idToElementMap: Map<number, unknown> = new Map();
199
- parsedModel.forEach(element => {
200
- idToElementMap.set(element.id, element);
201
- });
202
- return idToElementMap;
203
- }
204
-
205
-
206
- /**
207
- * Adds a Famix element to the repository
208
- * @param element A Famix element
209
- */
210
- public addElement(element: FamixBaseElement): void {
211
- if (element instanceof Class) {
212
- this.famixClasses.add(element);
213
- } else if (element instanceof Interface) {
214
- this.famixInterfaces.add(element);
215
- } else if (element instanceof Module) {
216
- this.famixModules.add(element);
217
- } else if (element instanceof Variable) {
218
- this.famixVariables.add(element);
219
- } else if (element instanceof Method) {
220
- this.famixMethods.add(element);
221
- } else if (element instanceof FamixFunctionEntity || element instanceof ArrowFunction) {
222
- this.famixFunctions.add(element);
223
- } else if (element instanceof ScriptEntity || element instanceof Module) {
224
- this.famixFiles.add(element);
225
- }
226
- this.elements.add(element);
227
- element.id = this.idCounter;
228
- this.idCounter++;
229
- }
230
-
231
- /**
232
- * Gets a JSON representation of the repository
233
- * @returns A JSON representation of the repository
234
- */
235
- public getJSON(): string {
236
- let ret = "[";
237
- for (const element of Array.from(this.elements.values())) {
238
- ret = ret + element.getJSON() + ",";
239
- }
240
- ret = ret.substring(0, ret.length - 1);
241
- return ret + "]";
242
- }
243
- }
1
+ import { FamixBaseElement } from "./famix_base_element";
2
+ import { Class, Interface, Variable, Method, ArrowFunction, Function as FamixFunctionEntity, Type, NamedEntity, ScriptEntity, Module, SourceLanguage } from "./model/famix";
3
+ import * as Famix from "./model/famix";
4
+ import { TSMorphObjectType } from "../../famix_functions/EntityDictionary";
5
+ /**
6
+ * This class is used to store all Famix elements
7
+ */
8
+ export class FamixRepository {
9
+ private elements = new Set<FamixBaseElement>(); // All Famix elements
10
+ private famixClasses = new Set<Class>(); // All Famix classes
11
+ private famixInterfaces = new Set<Interface>(); // All Famix interfaces
12
+ private famixModules = new Set<Module>(); // All Famix namespaces
13
+ private famixMethods = new Set<Method>(); // All Famix methods
14
+ private famixVariables = new Set<Variable>(); // All Famix variables
15
+ private famixFunctions = new Set<FamixFunctionEntity>(); // All Famix functions
16
+ private famixFiles = new Set<ScriptEntity | Module>(); // All Famix files
17
+ private idCounter = 1; // Id counter
18
+ private absolutePath: string = "";
19
+ private fmxElementObjectMap = new Map<Famix.Entity, TSMorphObjectType>();
20
+ private tsMorphObjectMap = new Map<TSMorphObjectType, Famix.Entity>(); // TODO: add this map to have two-way mapping between Famix and TS Morph objects
21
+
22
+ constructor() {
23
+ this.addElement(new SourceLanguage()); // add the source language entity (TypeScript)
24
+ }
25
+
26
+ public setFmxElementObjectMap(fmxElementObjectMap: Map<Famix.Entity, TSMorphObjectType>) {
27
+ this.fmxElementObjectMap = fmxElementObjectMap;
28
+ }
29
+
30
+ public getFmxElementObjectMap() {
31
+ return this.fmxElementObjectMap;
32
+ }
33
+
34
+ public getAbsolutePath(): string {
35
+ return this.absolutePath;
36
+ }
37
+
38
+ public setAbsolutePath(path: string) {
39
+ this.absolutePath = path;
40
+ }
41
+
42
+ /**
43
+ * Gets a Famix entity by id
44
+ * @param id An id of a Famix entity
45
+ * @returns The Famix entity corresponding to the id or undefined if it doesn't exist
46
+ */
47
+ public getFamixEntityById(id: number): FamixBaseElement | undefined {
48
+ const entity = Array.from(this.elements.values()).find(e => e.id === id);
49
+ return entity;
50
+ }
51
+
52
+ /**
53
+ * Gets a Famix entity by fully qualified name
54
+ * @param fullyQualifiedName A fully qualified name
55
+ * @returns The Famix entity corresponding to the fully qualified name or undefined if it doesn't exist
56
+ */
57
+ public getFamixEntityByFullyQualifiedName(fullyQualifiedName: string): FamixBaseElement | undefined {
58
+ const allEntities = Array.from(this.elements.values()).filter(e => e instanceof NamedEntity) as Array<NamedEntity>;
59
+ const entity = allEntities.find(e =>
60
+ // {console.log(`namedEntity: ${e.fullyQualifiedName}`);
61
+ // return
62
+ e.fullyQualifiedName === fullyQualifiedName
63
+ // }
64
+ );
65
+ return entity;
66
+ }
67
+
68
+ // Method to get Famix access by accessor and variable
69
+ public getFamixAccessByAccessorAndVariable(accessor: Famix.ContainerEntity, variable: Famix.StructuralEntity): Famix.Access | undefined {
70
+ // Iterate through the list of Famix accesses to find the matching one
71
+ for (const access of Array.from(this.elements.values()).filter(e => e instanceof Famix.Access) as Array<Famix.Access>) {
72
+ if (access.accessor === accessor && access.variable === variable) {
73
+ return access;
74
+ }
75
+ }
76
+ // Return undefined if no matching access is found
77
+ return undefined;
78
+ }
79
+
80
+
81
+ export(arg0: { format: string; }) {
82
+ if (arg0.format === "json") {
83
+ return this.getJSON();
84
+ } else {
85
+ throw new Error("Unsupported format");
86
+ }
87
+ }
88
+
89
+
90
+ // Only for tests
91
+
92
+ /**
93
+ * Gets all Famix entities
94
+ * @returns All Famix entities
95
+ */
96
+ public _getAllEntities(): Set<FamixBaseElement> {
97
+ return new Set(Array.from(this.elements.values()));
98
+ }
99
+
100
+ /**
101
+ * Gets all Famix entities of a given type
102
+ * @param theType A type of Famix entity
103
+ * @returns All Famix entities of the given type
104
+ */
105
+ public _getAllEntitiesWithType(theType: string): Set<FamixBaseElement> {
106
+ return new Set(Array.from(this.elements.values()).filter(e => (e as FamixBaseElement).constructor.name === theType));
107
+ }
108
+
109
+ /**
110
+ * Gets a Famix class by name
111
+ * @param name A class name
112
+ * @returns The Famix class corresponding to the name or undefined if it doesn't exist
113
+ */
114
+ public _getFamixClass(fullyQualifiedName: string): Class | undefined {
115
+ return Array.from(this.famixClasses.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
116
+ }
117
+
118
+ /**
119
+ * Gets a Famix interface by name
120
+ * @param name An interface name
121
+ * @returns The Famix interface corresponding to the name or undefined if it doesn't exist
122
+ */
123
+ public _getFamixInterface(fullyQualifiedName: string): Interface | undefined {
124
+ return Array.from(this.famixInterfaces.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
125
+ }
126
+
127
+ /**
128
+ * Gets a Famix method by name
129
+ * @param name A method name
130
+ * @returns The Famix method corresponding to the name or undefined if it doesn't exist
131
+ */
132
+ public _getFamixMethod(fullyQualifiedName: string): Method | undefined {
133
+ return Array.from(this.famixMethods.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
134
+ }
135
+
136
+ /**
137
+ * Gets a Famix function by name
138
+ * @param name A function name
139
+ * @returns The Famix function corresponding to the name or undefined if it doesn't exist
140
+ */
141
+ public _getFamixFunction(fullyQualifiedName: string): FamixFunctionEntity | undefined {
142
+ return Array.from(this.famixFunctions.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
143
+ }
144
+
145
+
146
+ /**
147
+ * Gets a Famix variable by name
148
+ * @param name A variable name
149
+ * @returns The Famix variable corresponding to the name or undefined if it doesn't exist
150
+ */
151
+ public _getFamixVariable(fullyQualifiedName: string): Variable | undefined {
152
+ return Array.from(this.famixVariables.values()).find(v => v.fullyQualifiedName === fullyQualifiedName);
153
+ }
154
+
155
+ /**
156
+ * Gets a Famix namespace by name
157
+ * @param name A namespace name
158
+ * @returns The Famix namespace corresponding to the name or undefined if it doesn't exist
159
+ */
160
+ public _getFamixModule(fullyQualifiedName: string): Module | undefined {
161
+ return Array.from(this.famixModules.values()).find(ns => ns.fullyQualifiedName === fullyQualifiedName);
162
+ }
163
+
164
+ /**
165
+ * Gets all Famix namespaces
166
+ * @returns All Famix namespaces
167
+ */
168
+ public _getFamixModules(): Set<Module> {
169
+ return new Set(Array.from(this.famixModules.values()));
170
+ }
171
+
172
+ /**
173
+ * Gets a Famix file by name
174
+ * @param name A file name
175
+ * @returns The Famix file corresponding to the name or undefined if it doesn't exist
176
+ */
177
+ public _getFamixFile(fullyQualifiedName: string): ScriptEntity | Module | undefined {
178
+ return Array.from(this.famixFiles.values()).find(ns => ns.name === fullyQualifiedName);
179
+ }
180
+
181
+ /**
182
+ * Gets all Famix files
183
+ * @returns All Famix files
184
+ */
185
+ public _getFamixFiles(): Set<ScriptEntity | Module> {
186
+ return new Set(Array.from(this.famixFiles.values()));
187
+ }
188
+
189
+ /**
190
+ * Gets all method names as a set from a class
191
+ * @param className A class name
192
+ * @returns The set of class "className" method names
193
+ */
194
+ public _methodNamesAsSetFromClass(className: string): Set<string> {
195
+ const theClass = this._getFamixClass(className) as Class;
196
+ return new Set(Array.from(theClass.methods).map(m => m.name));
197
+ }
198
+
199
+ /**
200
+ * Gets all method parents as a set from a class
201
+ * @param className A class name
202
+ * @returns The set of class "className" method parents
203
+ */
204
+ public _methodParentsAsSetFromClass(className: string): Set<Type> {
205
+ const theClass = this._getFamixClass(className) as Class;
206
+ return new Set(Array.from(theClass.methods).map(m => m.parentEntity));
207
+ }
208
+
209
+ /**
210
+ * Gets the map of Famix element ids and their Famix element from a JSON model
211
+ * @param model A JSON model
212
+ * @returns The map of Famix element ids and their Famix element from the JSON model
213
+ */
214
+ public _initMapFromModel(model: string): Map<number, unknown> {
215
+ const parsedModel: Array<FamixBaseElement> = JSON.parse(model);
216
+ const idToElementMap: Map<number, unknown> = new Map();
217
+ parsedModel.forEach(element => {
218
+ idToElementMap.set(element.id, element);
219
+ });
220
+ return idToElementMap;
221
+ }
222
+
223
+
224
+ /**
225
+ * Adds a Famix element to the repository
226
+ * @param element A Famix element
227
+ */
228
+ public addElement(element: FamixBaseElement): void {
229
+ if (element instanceof Class) {
230
+ this.famixClasses.add(element);
231
+ } else if (element instanceof Interface) {
232
+ this.famixInterfaces.add(element);
233
+ } else if (element instanceof Module) {
234
+ this.famixModules.add(element);
235
+ } else if (element instanceof Variable) {
236
+ this.famixVariables.add(element);
237
+ } else if (element instanceof Method) {
238
+ this.famixMethods.add(element);
239
+ } else if (element instanceof FamixFunctionEntity || element instanceof ArrowFunction) {
240
+ this.famixFunctions.add(element);
241
+ } else if (element instanceof ScriptEntity || element instanceof Module) {
242
+ this.famixFiles.add(element);
243
+ }
244
+ this.elements.add(element);
245
+ element.id = this.idCounter;
246
+ this.idCounter++;
247
+ this.validateFQNs();
248
+ }
249
+
250
+ /**
251
+ * Validates the fully qualified names of all Famix elements
252
+ */
253
+ private validateFQNs(): void {
254
+ // make sure all elements have unique fully qualified names
255
+ const fqns = new Set<string>();
256
+ for (const element of Array.from(this.elements.values())) {
257
+ if (element instanceof NamedEntity && element.fullyQualifiedName && fqns.has(element.fullyQualifiedName)) {
258
+ const theExistingElement = Array.from(this.elements.values()).find(e => (e as NamedEntity).fullyQualifiedName === element.fullyQualifiedName);
259
+ throw new Error(`The fully qualified name ${element.fullyQualifiedName} is not unique.\nIt exists for ${theExistingElement?.getJSON()}`);
260
+ }
261
+ fqns.add((element as NamedEntity).fullyQualifiedName);
262
+ }
263
+ }
264
+
265
+
266
+ /**
267
+ * Gets a JSON representation of the repository
268
+ * @returns A JSON representation of the repository
269
+ */
270
+ public getJSON(): string {
271
+ let ret = "[";
272
+ for (const element of Array.from(this.elements.values())) {
273
+ ret = ret + element.getJSON() + ",";
274
+ }
275
+ ret = ret.substring(0, ret.length - 1);
276
+ return ret + "]";
277
+ }
278
+ }
@@ -1,8 +1,8 @@
1
- import * as Famix from "./model/famix";
2
- import { FamixBaseElement } from "./famix_base_element";
3
- import { FamixJSONExporter } from "./famix_JSON_exporter";
4
- import { FamixRepository } from "./famix_repository";
5
-
6
- const model = { Famix };
7
-
8
- export = { FamixBaseElement, FamixJSONExporter, FamixRepository, model };
1
+ import * as Famix from "./model/famix";
2
+ import { FamixBaseElement } from "./famix_base_element";
3
+ import { FamixJSONExporter } from "./famix_JSON_exporter";
4
+ import { FamixRepository } from "./famix_repository";
5
+
6
+ const model = { Famix };
7
+
8
+ export = { FamixBaseElement, FamixJSONExporter, FamixRepository, model };