ts2famix 1.0.13 → 1.0.15

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 (135) hide show
  1. package/.eslintrc.json +24 -24
  2. package/LICENSE +23 -23
  3. package/README.md +109 -109
  4. package/dist/analyze.js +107 -0
  5. package/dist/analyze_functions/processAccesses.js +55 -0
  6. package/dist/analyze_functions/processFiles.js +554 -0
  7. package/dist/analyze_functions/processImportClauses.js +70 -0
  8. package/dist/analyze_functions/processInheritances.js +73 -0
  9. package/dist/analyze_functions/processInvocations.js +49 -0
  10. package/dist/famix2puml.js +125 -0
  11. package/dist/famix_functions/famix_functions.js +525 -0
  12. package/dist/famix_functions/famix_functions_associations.js +223 -0
  13. package/dist/famix_functions/famix_functions_index.js +62 -0
  14. package/dist/famix_functions/famix_functions_types.js +114 -0
  15. package/dist/fqn.js +126 -0
  16. package/dist/lib/famix/src/famix_JSON_exporter.js +54 -0
  17. package/dist/lib/famix/src/famix_base_element.js +13 -0
  18. package/dist/lib/famix/src/famix_repository.js +199 -0
  19. package/dist/lib/famix/src/index.js +30 -0
  20. package/dist/lib/famix/src/model/famix/access.js +39 -0
  21. package/dist/lib/famix/src/model/famix/accessor.js +16 -0
  22. package/dist/lib/famix/src/model/famix/alias.js +32 -0
  23. package/dist/lib/famix/src/model/famix/association.js +36 -0
  24. package/dist/lib/famix/src/model/famix/behavioral_entity.js +81 -0
  25. package/dist/lib/famix/src/model/famix/c_source_language.js +16 -0
  26. package/dist/lib/famix/src/model/famix/class.js +70 -0
  27. package/dist/lib/famix/src/model/famix/comment.js +38 -0
  28. package/dist/lib/famix/src/model/famix/container_entity.js +125 -0
  29. package/dist/lib/famix/src/model/famix/custom_source_language.js +23 -0
  30. package/dist/lib/famix/src/model/famix/decorator.js +31 -0
  31. package/dist/lib/famix/src/model/famix/entity.js +16 -0
  32. package/dist/lib/famix/src/model/famix/enum.js +30 -0
  33. package/dist/lib/famix/src/model/famix/enum_value.js +24 -0
  34. package/dist/lib/famix/src/model/famix/function.js +16 -0
  35. package/dist/lib/famix/src/model/famix/implicit_variable.js +16 -0
  36. package/dist/lib/famix/src/model/famix/import_clause.js +39 -0
  37. package/dist/lib/famix/src/model/famix/index.js +87 -0
  38. package/dist/lib/famix/src/model/famix/indexed_file_anchor.js +37 -0
  39. package/dist/lib/famix/src/model/famix/inheritance.js +32 -0
  40. package/dist/lib/famix/src/model/famix/interface.js +63 -0
  41. package/dist/lib/famix/src/model/famix/invocation.js +53 -0
  42. package/dist/lib/famix/src/model/famix/method.js +66 -0
  43. package/dist/lib/famix/src/model/famix/module.js +31 -0
  44. package/dist/lib/famix/src/model/famix/named_entity.js +77 -0
  45. package/dist/lib/famix/src/model/famix/namespace.js +24 -0
  46. package/dist/lib/famix/src/model/famix/parameter.js +24 -0
  47. package/dist/lib/famix/src/model/famix/parameterizable_class.js +30 -0
  48. package/dist/lib/famix/src/model/famix/parameterizable_interface.js +30 -0
  49. package/dist/lib/famix/src/model/famix/parameterized_type.js +36 -0
  50. package/dist/lib/famix/src/model/famix/primitive_type.js +16 -0
  51. package/dist/lib/famix/src/model/famix/property.js +44 -0
  52. package/dist/lib/famix/src/model/famix/reference.js +32 -0
  53. package/dist/lib/famix/src/model/famix/scoping_entity.js +34 -0
  54. package/dist/lib/famix/src/model/famix/script_entity.js +30 -0
  55. package/dist/lib/famix/src/model/famix/source_anchor.js +26 -0
  56. package/dist/lib/famix/src/model/famix/source_language.js +30 -0
  57. package/dist/lib/famix/src/model/famix/sourced_entity.js +55 -0
  58. package/dist/lib/famix/src/model/famix/structural_entity.js +38 -0
  59. package/dist/lib/famix/src/model/famix/text_anchor.js +37 -0
  60. package/dist/lib/famix/src/model/famix/type.js +71 -0
  61. package/dist/lib/famix/src/model/famix/type_parameter.js +24 -0
  62. package/dist/lib/famix/src/model/famix/variable.js +23 -0
  63. package/dist/lib/ts-complex/cyclomatic-service.js +83 -0
  64. package/dist/ts2famix-cli.js +63 -0
  65. package/dist/ts2famix-tsconfig.js +53 -0
  66. package/jest.config-old.ts +199 -199
  67. package/package.json +47 -47
  68. package/src/analyze.ts +94 -94
  69. package/src/analyze_functions/processAccesses.ts +57 -57
  70. package/src/analyze_functions/processFiles.ts +669 -669
  71. package/src/analyze_functions/processImportClauses.ts +77 -77
  72. package/src/analyze_functions/processInheritances.ts +84 -84
  73. package/src/analyze_functions/processInvocations.ts +51 -51
  74. package/src/famix2puml.ts +119 -119
  75. package/src/famix_functions/famix_functions.ts +565 -559
  76. package/src/famix_functions/famix_functions_associations.ts +225 -215
  77. package/src/famix_functions/famix_functions_index.ts +44 -44
  78. package/src/famix_functions/famix_functions_types.ts +105 -105
  79. package/src/generate_uml.sh +16 -16
  80. package/src/lib/famix/License.md +22 -22
  81. package/src/lib/famix/package-lock.json +301 -301
  82. package/src/lib/famix/package.json +27 -27
  83. package/src/lib/famix/readme.md +4 -4
  84. package/src/lib/famix/src/famix_JSON_exporter.ts +56 -56
  85. package/src/lib/famix/src/famix_base_element.ts +18 -18
  86. package/src/lib/famix/src/famix_repository.ts +212 -199
  87. package/src/lib/famix/src/index.ts +8 -8
  88. package/src/lib/famix/src/model/famix/access.ts +53 -53
  89. package/src/lib/famix/src/model/famix/accessor.ts +15 -15
  90. package/src/lib/famix/src/model/famix/alias.ts +41 -41
  91. package/src/lib/famix/src/model/famix/association.ts +44 -44
  92. package/src/lib/famix/src/model/famix/behavioral_entity.ts +107 -107
  93. package/src/lib/famix/src/model/famix/c_source_language.ts +15 -15
  94. package/src/lib/famix/src/model/famix/class.ts +86 -86
  95. package/src/lib/famix/src/model/famix/comment.ts +50 -50
  96. package/src/lib/famix/src/model/famix/container_entity.ts +165 -165
  97. package/src/lib/famix/src/model/famix/custom_source_language.ts +27 -27
  98. package/src/lib/famix/src/model/famix/decorator.ts +39 -39
  99. package/src/lib/famix/src/model/famix/entity.ts +15 -15
  100. package/src/lib/famix/src/model/famix/enum.ts +31 -31
  101. package/src/lib/famix/src/model/famix/enum_value.ts +29 -29
  102. package/src/lib/famix/src/model/famix/function.ts +15 -15
  103. package/src/lib/famix/src/model/famix/implicit_variable.ts +15 -15
  104. package/src/lib/famix/src/model/famix/import_clause.ts +53 -53
  105. package/src/lib/famix/src/model/famix/index.ts +42 -42
  106. package/src/lib/famix/src/model/famix/indexed_file_anchor.ts +49 -49
  107. package/src/lib/famix/src/model/famix/inheritance.ts +42 -42
  108. package/src/lib/famix/src/model/famix/interface.ts +75 -75
  109. package/src/lib/famix/src/model/famix/invocation.ts +68 -68
  110. package/src/lib/famix/src/model/famix/method.ts +96 -96
  111. package/src/lib/famix/src/model/famix/module.ts +31 -31
  112. package/src/lib/famix/src/model/famix/named_entity.ts +98 -98
  113. package/src/lib/famix/src/model/famix/namespace.ts +28 -28
  114. package/src/lib/famix/src/model/famix/parameter.ts +29 -29
  115. package/src/lib/famix/src/model/famix/parameterizable_class.ts +31 -31
  116. package/src/lib/famix/src/model/famix/parameterizable_interface.ts +31 -31
  117. package/src/lib/famix/src/model/famix/parameterized_type.ts +40 -40
  118. package/src/lib/famix/src/model/famix/primitive_type.ts +15 -15
  119. package/src/lib/famix/src/model/famix/property.ts +54 -54
  120. package/src/lib/famix/src/model/famix/reference.ts +42 -42
  121. package/src/lib/famix/src/model/famix/scoping_entity.ts +34 -31
  122. package/src/lib/famix/src/model/famix/script_entity.ts +38 -38
  123. package/src/lib/famix/src/model/famix/source_anchor.ts +31 -31
  124. package/src/lib/famix/src/model/famix/source_language.ts +31 -31
  125. package/src/lib/famix/src/model/famix/sourced_entity.ts +70 -70
  126. package/src/lib/famix/src/model/famix/structural_entity.ts +44 -44
  127. package/src/lib/famix/src/model/famix/text_anchor.ts +49 -49
  128. package/src/lib/famix/src/model/famix/type.ts +88 -88
  129. package/src/lib/famix/src/model/famix/type_parameter.ts +33 -33
  130. package/src/lib/famix/src/model/famix/variable.ts +28 -28
  131. package/src/lib/famix/tsconfig.json +26 -26
  132. package/src/lib/famix/tslint.json +14 -14
  133. package/src/lib/ts-complex/cyclomatic-service.ts +85 -85
  134. package/src/ts2famix-cli.ts +39 -39
  135. package/tsconfig.json +69 -69
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.FamixFunctionsAssociations = void 0;
27
+ const ts_morph_1 = require("ts-morph");
28
+ const Famix = __importStar(require("../lib/famix/src/model/famix"));
29
+ const fqn_1 = require("../fqn");
30
+ const famix_functions_index_1 = require("./famix_functions_index");
31
+ const famix_1 = require("../lib/famix/src/model/famix");
32
+ /**
33
+ * This class is used to build a Famix model for the associations
34
+ */
35
+ class FamixFunctionsAssociations {
36
+ /**
37
+ * Initializes the FamixFunctionsAssociations object
38
+ * @param famixRep The Famix repository
39
+ * @param fmxClassMap The map of the class names and their Famix model
40
+ * @param fmxInterfaceMap The map of the interface names and their Famix model
41
+ */
42
+ constructor(famixRep, fmxClassMap, fmxInterfaceMap) {
43
+ this.FQNFunctions = new fqn_1.FQNFunctions(); // The fully qualified name functions
44
+ this.famixRep = famixRep;
45
+ this.famixClassMap = fmxClassMap;
46
+ this.famixInterfaceMap = fmxInterfaceMap;
47
+ this.famixFunctionsIndex = new famix_functions_index_1.FamixFunctionsIndex(famixRep);
48
+ }
49
+ /**
50
+ * Creates a Famix access
51
+ * @param node A node
52
+ * @param id An id of a parameter, a variable, a property or an enum member
53
+ */
54
+ createFamixAccess(node, id) {
55
+ const fmxVar = this.famixRep.getFamixEntityById(id);
56
+ const nodeReferenceAncestor = this.findAncestor(node);
57
+ const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(nodeReferenceAncestor);
58
+ const accessor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName);
59
+ const fmxAccess = new Famix.Access(this.famixRep);
60
+ fmxAccess.setAccessor(accessor);
61
+ fmxAccess.setVariable(fmxVar);
62
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(node, fmxAccess);
63
+ }
64
+ /**
65
+ * Creates a Famix invocation
66
+ * @param node A node
67
+ * @param m A method or a function
68
+ * @param id The id of the method or the function
69
+ */
70
+ createFamixInvocation(node, m, id) {
71
+ const fmxMethodOrFunction = this.getFamixEntityById(id);
72
+ const nodeReferenceAncestor = this.findAncestor(node);
73
+ const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(nodeReferenceAncestor);
74
+ const sender = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName);
75
+ const receiverFullyQualifiedName = this.FQNFunctions.getFQN(m.getParent());
76
+ const receiver = this.getFamixEntityByFullyQualifiedName(receiverFullyQualifiedName);
77
+ const fmxInvocation = new Famix.Invocation(this.famixRep);
78
+ fmxInvocation.setSender(sender);
79
+ fmxInvocation.setReceiver(receiver);
80
+ fmxInvocation.addCandidate(fmxMethodOrFunction);
81
+ fmxInvocation.setSignature(fmxMethodOrFunction.getSignature());
82
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(node, fmxInvocation);
83
+ }
84
+ /**
85
+ * Creates a Famix inheritance
86
+ * @param cls A class or an interface (subclass)
87
+ * @param inhClass The inherited class or interface (superclass)
88
+ */
89
+ createFamixInheritance(cls, inhClass) {
90
+ const fmxInheritance = new Famix.Inheritance(this.famixRep);
91
+ // const clsName = cls.getName();
92
+ const classFullyQualifiedName = this.FQNFunctions.getFQN(cls);
93
+ console.info(`createFamixInheritance: classFullyQualifiedName: class fqn = ${classFullyQualifiedName}`);
94
+ let subClass;
95
+ if (cls instanceof ts_morph_1.ClassDeclaration) {
96
+ subClass = this.famixClassMap.get(classFullyQualifiedName);
97
+ }
98
+ else {
99
+ subClass = this.famixInterfaceMap.get(classFullyQualifiedName);
100
+ }
101
+ let inhClassName;
102
+ let inhClassFullyQualifiedName;
103
+ let superClass;
104
+ if (inhClass instanceof ts_morph_1.ClassDeclaration || inhClass instanceof ts_morph_1.InterfaceDeclaration) {
105
+ inhClassName = inhClass.getName();
106
+ inhClassFullyQualifiedName = this.FQNFunctions.getFQN(inhClass);
107
+ if (inhClass instanceof ts_morph_1.ClassDeclaration) {
108
+ superClass = this.famixClassMap.get(inhClassFullyQualifiedName);
109
+ }
110
+ else {
111
+ superClass = this.famixInterfaceMap.get(inhClassFullyQualifiedName);
112
+ }
113
+ }
114
+ else {
115
+ // inhClass is an ExpressionWithTypeArguments
116
+ inhClassName = inhClass.getExpression().getText();
117
+ // what is inhClassFullyQualifiedName? TODO
118
+ inhClassFullyQualifiedName = 'Undefined_Scope_from_importer.' + inhClassName;
119
+ }
120
+ if (superClass === undefined) {
121
+ if (inhClass instanceof ts_morph_1.ClassDeclaration) {
122
+ superClass = new Famix.Class(this.famixRep);
123
+ this.famixClassMap.set(inhClassFullyQualifiedName, superClass);
124
+ }
125
+ else {
126
+ superClass = new Famix.Interface(this.famixRep);
127
+ this.famixInterfaceMap.set(inhClassFullyQualifiedName, superClass);
128
+ }
129
+ superClass.setName(inhClassName);
130
+ superClass.setFullyQualifiedName(inhClassFullyQualifiedName);
131
+ superClass.setIsStub(true);
132
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(inhClass, superClass);
133
+ }
134
+ fmxInheritance.setSubclass(subClass);
135
+ fmxInheritance.setSuperclass(superClass);
136
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(null, fmxInheritance);
137
+ }
138
+ /**
139
+ * Creates a Famix import clause
140
+ * @param importer A source file which is a module
141
+ * @param moduleSpecifier The name of the module where the export declaration is
142
+ * @param moduleSpecifierFilePath The path of the module where the export declaration is
143
+ * @param importElement The imported entity
144
+ * @param isInExports A boolean indicating if the imported entity is in the exports
145
+ * @param isDefaultExport A boolean indicating if the imported entity is a default export
146
+ */
147
+ createFamixImportClause(importer, moduleSpecifier, moduleSpecifierFilePath, importElement, isInExports, isDefaultExport) {
148
+ var _a, _b;
149
+ console.info(`createFamixImportClause: Creating import clause:`);
150
+ const fmxImportClause = new Famix.ImportClause(this.famixRep);
151
+ let importedEntity;
152
+ let importedEntityName;
153
+ let pathName = "\"" + moduleSpecifierFilePath + "\".";
154
+ if (importElement instanceof ts_morph_1.ImportSpecifier) {
155
+ importedEntityName = importElement.getName();
156
+ pathName = pathName + importedEntityName;
157
+ if (isInExports) {
158
+ importedEntity = this.getFamixEntityByFullyQualifiedName(pathName);
159
+ }
160
+ if (importedEntity === undefined) {
161
+ importedEntity = new Famix.NamedEntity(this.famixRep);
162
+ importedEntity.setName(importedEntityName);
163
+ if (!isInExports) {
164
+ importedEntity.setIsStub(true);
165
+ }
166
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
167
+ importedEntity.setFullyQualifiedName(pathName);
168
+ }
169
+ }
170
+ else {
171
+ importedEntityName = importElement.getText();
172
+ if (isDefaultExport) {
173
+ pathName = pathName + "defaultExport";
174
+ }
175
+ else {
176
+ pathName = pathName + "namespaceExport";
177
+ }
178
+ importedEntity = new Famix.NamedEntity(this.famixRep);
179
+ importedEntity.setName(importedEntityName);
180
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
181
+ importedEntity.setFullyQualifiedName(pathName);
182
+ }
183
+ const importerFullyQualifiedName = this.FQNFunctions.getFQN(importer);
184
+ const fmxImporter = this.getFamixEntityByFullyQualifiedName(importerFullyQualifiedName);
185
+ fmxImportClause.setImportingEntity(fmxImporter);
186
+ fmxImportClause.setImportedEntity(importedEntity);
187
+ fmxImportClause.setModuleSpecifier(moduleSpecifier);
188
+ console.info(`createFamixImportClause: ${(_a = fmxImportClause.getImportedEntity()) === null || _a === void 0 ? void 0 : _a.getName()} (of type ${fmxImportClause.getImportedEntity() instanceof famix_1.Class ? 'Class' :
189
+ fmxImportClause.getImportedEntity() instanceof famix_1.Interface ? 'Interface' :
190
+ fmxImportClause.getImportedEntity() instanceof Function ? 'Function' :
191
+ fmxImportClause.getImportedEntity() instanceof famix_1.Enum ? 'Enum' :
192
+ fmxImportClause.getImportedEntity() instanceof famix_1.Alias ? 'Alias' :
193
+ fmxImportClause.getImportedEntity() instanceof famix_1.Variable ? 'Variable' :
194
+ 'NamedEntity'}) is imported by ${(_b = fmxImportClause.getImportingEntity()) === null || _b === void 0 ? void 0 : _b.getName()}`);
195
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(null, fmxImportClause);
196
+ fmxImporter.addOutgoingImport(fmxImportClause);
197
+ }
198
+ /**
199
+ * Gets a Famix entity by id
200
+ * @param famixId An id of a Famix entity
201
+ * @returns The Famix entity corresponding to the id
202
+ */
203
+ getFamixEntityById(famixId) {
204
+ return this.famixRep.getFamixEntityById(famixId);
205
+ }
206
+ /**
207
+ * Gets a Famix entity by fully qualified name
208
+ * @param fullyQualifiedName A fully qualified name
209
+ * @returns The Famix entity corresponding to the fully qualified name
210
+ */
211
+ getFamixEntityByFullyQualifiedName(fullyQualifiedName) {
212
+ return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName);
213
+ }
214
+ /**
215
+ * Finds the ancestor of a node
216
+ * @param node A node
217
+ * @returns The ancestor of the node
218
+ */
219
+ findAncestor(node) {
220
+ return node.getAncestors().find(a => a.getKind() === ts_morph_1.SyntaxKind.MethodDeclaration || a.getKind() === ts_morph_1.SyntaxKind.Constructor || a.getKind() === ts_morph_1.SyntaxKind.FunctionDeclaration || a.getKind() === ts_morph_1.SyntaxKind.FunctionExpression || a.getKind() === ts_morph_1.SyntaxKind.ModuleDeclaration || a.getKind() === ts_morph_1.SyntaxKind.SourceFile || a.getKindName() === "GetAccessor" || a.getKindName() === "SetAccessor" || a.getKind() === ts_morph_1.SyntaxKind.ClassDeclaration);
221
+ }
222
+ }
223
+ exports.FamixFunctionsAssociations = FamixFunctionsAssociations;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.FamixFunctionsIndex = void 0;
27
+ const ts_morph_1 = require("ts-morph");
28
+ const Famix = __importStar(require("../lib/famix/src/model/famix"));
29
+ const fqn_1 = require("../fqn");
30
+ /**
31
+ * This class is used to build a Famix model for the index file anchors
32
+ */
33
+ class FamixFunctionsIndex {
34
+ /**
35
+ * Initializes the FamixFunctionsIndex object
36
+ * @param famixRep The Famix repository
37
+ */
38
+ constructor(famixRep) {
39
+ this.FQNFunctions = new fqn_1.FQNFunctions(); // The fully qualified name functions
40
+ this.famixRep = famixRep;
41
+ }
42
+ /**
43
+ * Makes a Famix index file anchor
44
+ * @param sourceElement A source element
45
+ * @param famixElement The Famix model of the source element
46
+ */
47
+ makeFamixIndexFileAnchor(sourceElement, famixElement) {
48
+ const fmxIndexFileAnchor = new Famix.IndexedFileAnchor(this.famixRep);
49
+ fmxIndexFileAnchor.setElement(famixElement);
50
+ if (sourceElement !== null) {
51
+ fmxIndexFileAnchor.setFileName(sourceElement.getSourceFile().getFilePath());
52
+ if (!(sourceElement instanceof ts_morph_1.CommentRange)) {
53
+ fmxIndexFileAnchor.setStartPos(sourceElement.getStart());
54
+ fmxIndexFileAnchor.setEndPos(sourceElement.getEnd());
55
+ }
56
+ if (!(famixElement instanceof Famix.Association) && !(famixElement instanceof Famix.Comment) && !(sourceElement instanceof ts_morph_1.CommentRange) && !(sourceElement instanceof ts_morph_1.Identifier) && !(sourceElement instanceof ts_morph_1.ImportSpecifier) && !(sourceElement instanceof ts_morph_1.ExpressionWithTypeArguments)) {
57
+ famixElement.setFullyQualifiedName(this.FQNFunctions.getFQN(sourceElement));
58
+ }
59
+ }
60
+ }
61
+ }
62
+ exports.FamixFunctionsIndex = FamixFunctionsIndex;
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.FamixFunctionsTypes = void 0;
27
+ const ts_morph_1 = require("ts-morph");
28
+ const Famix = __importStar(require("../lib/famix/src/model/famix"));
29
+ const fqn_1 = require("../fqn");
30
+ const famix_functions_index_1 = require("./famix_functions_index");
31
+ /**
32
+ * This class is used to build a Famix model for the types
33
+ */
34
+ class FamixFunctionsTypes {
35
+ /**
36
+ * Initializes the FamixFunctionsIndex object
37
+ * @param famixRep The Famix repository
38
+ */
39
+ constructor(famixRep) {
40
+ this.FQNFunctions = new fqn_1.FQNFunctions(); // The fully qualified name functions
41
+ this.fmxTypeMap = new Map(); // Maps the type names to their Famix model
42
+ this.famixRep = famixRep;
43
+ this.famixFunctionsIndex = new famix_functions_index_1.FamixFunctionsIndex(famixRep);
44
+ }
45
+ /**
46
+ * Creates or gets a Famix type
47
+ * @param typeName A type name
48
+ * @param element A ts-morph element
49
+ * @returns The Famix model of the type
50
+ */
51
+ createOrGetFamixType(typeName, element) {
52
+ let fmxType;
53
+ let isPrimitiveType = false;
54
+ let isParameterizedType = false;
55
+ console.info("Creating (or getting) type: " + typeName + "' of element: " + element.getText() + " of kind: " + element.getKindName());
56
+ const typeAncestor = this.findTypeAncestor(element);
57
+ const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(typeAncestor);
58
+ const ancestor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName);
59
+ if (!ancestor) {
60
+ throw new Error(`Ancestor ${ancestorFullyQualifiedName} not found.`);
61
+ }
62
+ if (typeName === "number" || typeName === "string" || typeName === "boolean" || typeName === "bigint" || typeName === "symbol" || typeName === "undefined" || typeName === "null") {
63
+ isPrimitiveType = true;
64
+ }
65
+ if (!isPrimitiveType && typeName.includes("<") && typeName.includes(">") && !(typeName.includes("=>"))) {
66
+ isParameterizedType = true;
67
+ }
68
+ if (!this.fmxTypeMap.has(typeName)) {
69
+ if (isPrimitiveType) {
70
+ fmxType = new Famix.PrimitiveType(this.famixRep);
71
+ fmxType.setIsStub(true);
72
+ }
73
+ else if (isParameterizedType) {
74
+ fmxType = new Famix.ParameterizedType(this.famixRep);
75
+ const parameterTypeNames = typeName.substring(typeName.indexOf("<") + 1, typeName.indexOf(">")).split(",").map(s => s.trim());
76
+ const baseTypeName = typeName.substring(0, typeName.indexOf("<")).trim();
77
+ parameterTypeNames.forEach(parameterTypeName => {
78
+ const fmxParameterType = this.createOrGetFamixType(parameterTypeName, element);
79
+ fmxType.addArgument(fmxParameterType);
80
+ });
81
+ const fmxBaseType = this.createOrGetFamixType(baseTypeName, element);
82
+ fmxType.setBaseType(fmxBaseType);
83
+ }
84
+ else {
85
+ fmxType = new Famix.Type(this.famixRep);
86
+ }
87
+ fmxType.setName(typeName);
88
+ fmxType.setContainer(ancestor);
89
+ this.famixFunctionsIndex.makeFamixIndexFileAnchor(element, fmxType);
90
+ this.fmxTypeMap.set(typeName, fmxType);
91
+ }
92
+ else {
93
+ fmxType = this.fmxTypeMap.get(typeName);
94
+ }
95
+ return fmxType;
96
+ }
97
+ /**
98
+ * Gets a Famix entity by fully qualified name
99
+ * @param fullyQualifiedName A fully qualified name
100
+ * @returns The Famix entity corresponding to the fully qualified name
101
+ */
102
+ getFamixEntityByFullyQualifiedName(fullyQualifiedName) {
103
+ return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName);
104
+ }
105
+ /**
106
+ * Finds the ancestor of a ts-morph element
107
+ * @param element A ts-morph element
108
+ * @returns The ancestor of the ts-morph element
109
+ */
110
+ findTypeAncestor(element) {
111
+ return element.getAncestors().find(a => a.getKind() === ts_morph_1.SyntaxKind.MethodDeclaration || a.getKind() === ts_morph_1.SyntaxKind.Constructor || a.getKind() === ts_morph_1.SyntaxKind.MethodSignature || a.getKind() === ts_morph_1.SyntaxKind.FunctionDeclaration || a.getKind() === ts_morph_1.SyntaxKind.FunctionExpression || a.getKind() === ts_morph_1.SyntaxKind.ModuleDeclaration || a.getKind() === ts_morph_1.SyntaxKind.SourceFile || a.getKindName() === "GetAccessor" || a.getKindName() === "SetAccessor" || a.getKind() === ts_morph_1.SyntaxKind.ClassDeclaration || a.getKind() === ts_morph_1.SyntaxKind.InterfaceDeclaration);
112
+ }
113
+ }
114
+ exports.FamixFunctionsTypes = FamixFunctionsTypes;
package/dist/fqn.js ADDED
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.FQNFunctions = void 0;
27
+ const ts = __importStar(require("ts-morph"));
28
+ /**
29
+ * This class is used to get the fully qualified name of a node
30
+ */
31
+ class FQNFunctions {
32
+ /**
33
+ * Gets the fully qualified name of a node, if it has one
34
+ * @param node A node
35
+ * @returns The fully qualified name of the node, or undefined if it doesn't have one
36
+ */
37
+ getFQN(node) {
38
+ if (node instanceof ts.SourceFile) {
39
+ return `"${node.getFilePath()}"`;
40
+ }
41
+ const symbol = node.getSymbol();
42
+ if (!symbol) {
43
+ return undefined;
44
+ }
45
+ const declarations = symbol.getDeclarations();
46
+ if (!declarations) {
47
+ return undefined;
48
+ }
49
+ const sourceFile = declarations[0].getSourceFile();
50
+ if (!sourceFile) {
51
+ return undefined;
52
+ }
53
+ const sourceFilePath = sourceFile.getFilePath();
54
+ const sourceFileDirectory = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/"));
55
+ const qualifiedNameParts = [];
56
+ const nodeName = this.getNameOfNode(node);
57
+ if (nodeName)
58
+ qualifiedNameParts.push(nodeName);
59
+ const ancestors = node.getAncestors();
60
+ ancestors.forEach(a => {
61
+ const partName = this.getNameOfNode(a);
62
+ if (partName)
63
+ qualifiedNameParts.push(partName);
64
+ });
65
+ if (qualifiedNameParts.length > 0) {
66
+ return `"${sourceFileDirectory}/${qualifiedNameParts.pop()}".${qualifiedNameParts.reverse().join(".")}`;
67
+ }
68
+ else {
69
+ return undefined;
70
+ }
71
+ }
72
+ /**
73
+ * Gets the name of a node, if it has one
74
+ * @param a A node
75
+ * @returns The name of the node, or an empty string if it doesn't have one
76
+ */
77
+ getNameOfNode(a) {
78
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
79
+ switch (a.getKind()) {
80
+ case ts.SyntaxKind.SourceFile:
81
+ return (_a = a.asKind(ts.SyntaxKind.SourceFile)) === null || _a === void 0 ? void 0 : _a.getBaseName();
82
+ case ts.SyntaxKind.ModuleDeclaration:
83
+ return (_b = a.asKind(ts.SyntaxKind.ModuleDeclaration)) === null || _b === void 0 ? void 0 : _b.getName();
84
+ case ts.SyntaxKind.ClassDeclaration:
85
+ return (_c = a.asKind(ts.SyntaxKind.ClassDeclaration)) === null || _c === void 0 ? void 0 : _c.getName();
86
+ case ts.SyntaxKind.InterfaceDeclaration:
87
+ return (_d = a.asKind(ts.SyntaxKind.InterfaceDeclaration)) === null || _d === void 0 ? void 0 : _d.getName();
88
+ case ts.SyntaxKind.PropertyDeclaration:
89
+ return (_e = a.asKind(ts.SyntaxKind.PropertyDeclaration)) === null || _e === void 0 ? void 0 : _e.getName();
90
+ case ts.SyntaxKind.PropertySignature:
91
+ return (_f = a.asKind(ts.SyntaxKind.PropertySignature)) === null || _f === void 0 ? void 0 : _f.getName();
92
+ case ts.SyntaxKind.MethodDeclaration:
93
+ return (_g = a.asKind(ts.SyntaxKind.MethodDeclaration)) === null || _g === void 0 ? void 0 : _g.getName();
94
+ case ts.SyntaxKind.MethodSignature:
95
+ return (_h = a.asKind(ts.SyntaxKind.MethodSignature)) === null || _h === void 0 ? void 0 : _h.getName();
96
+ case ts.SyntaxKind.GetAccessor:
97
+ return (_j = a.asKind(ts.SyntaxKind.GetAccessor)) === null || _j === void 0 ? void 0 : _j.getName();
98
+ case ts.SyntaxKind.SetAccessor:
99
+ return (_k = a.asKind(ts.SyntaxKind.SetAccessor)) === null || _k === void 0 ? void 0 : _k.getName();
100
+ case ts.SyntaxKind.FunctionDeclaration:
101
+ return (_l = a.asKind(ts.SyntaxKind.FunctionDeclaration)) === null || _l === void 0 ? void 0 : _l.getName();
102
+ case ts.SyntaxKind.FunctionExpression:
103
+ return ((_m = a.asKind(ts.SyntaxKind.FunctionExpression)) === null || _m === void 0 ? void 0 : _m.getName()) ? (_o = a.asKind(ts.SyntaxKind.FunctionExpression)) === null || _o === void 0 ? void 0 : _o.getName() : "anonymous";
104
+ case ts.SyntaxKind.Parameter:
105
+ return (_p = a.asKind(ts.SyntaxKind.Parameter)) === null || _p === void 0 ? void 0 : _p.getName();
106
+ case ts.SyntaxKind.VariableDeclaration:
107
+ return (_q = a.asKind(ts.SyntaxKind.VariableDeclaration)) === null || _q === void 0 ? void 0 : _q.getName();
108
+ case ts.SyntaxKind.Decorator:
109
+ return "@" + ((_r = a.asKind(ts.SyntaxKind.Decorator)) === null || _r === void 0 ? void 0 : _r.getName());
110
+ case ts.SyntaxKind.TypeParameter:
111
+ return (_s = a.asKind(ts.SyntaxKind.TypeParameter)) === null || _s === void 0 ? void 0 : _s.getName();
112
+ case ts.SyntaxKind.EnumDeclaration:
113
+ return (_t = a.asKind(ts.SyntaxKind.EnumDeclaration)) === null || _t === void 0 ? void 0 : _t.getName();
114
+ case ts.SyntaxKind.EnumMember:
115
+ return (_u = a.asKind(ts.SyntaxKind.EnumMember)) === null || _u === void 0 ? void 0 : _u.getName();
116
+ case ts.SyntaxKind.TypeAliasDeclaration:
117
+ return (_v = a.asKind(ts.SyntaxKind.TypeAliasDeclaration)) === null || _v === void 0 ? void 0 : _v.getName();
118
+ case ts.SyntaxKind.Constructor:
119
+ return "constructor";
120
+ default:
121
+ // ancestor hasn't got a useful name
122
+ return "";
123
+ }
124
+ }
125
+ }
126
+ exports.FQNFunctions = FQNFunctions;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FamixJSONExporter = void 0;
4
+ const famix_base_element_1 = require("./famix_base_element");
5
+ /**
6
+ * This class is used to export Famix elements to JSON
7
+ */
8
+ class FamixJSONExporter {
9
+ /**
10
+ * Constructor of the FamixJSONExporter class
11
+ * @param packageClass Name of a Famix class
12
+ * @param element A Famix element to export, this element is an instance of the class named "packageClass"
13
+ */
14
+ constructor(packageClass, element) {
15
+ this.bufferArray = {}; // A buffer to store the properties of the Famix element
16
+ this.FamixPrefix = "FamixTypeScript"; // Prefix of the Famix element
17
+ this.element = element;
18
+ this.bufferArray["FM3"] = this.FamixPrefix + "." + packageClass;
19
+ this.bufferArray["id"] = this.element.id;
20
+ }
21
+ /**
22
+ * Adds a property to the Famix element
23
+ * @param name Name of the property
24
+ * @param prop A property
25
+ */
26
+ addProperty(name, prop) {
27
+ if (prop instanceof Set) {
28
+ const valueArray = [];
29
+ for (const value of Array.from(prop.values())) {
30
+ if (value instanceof famix_base_element_1.FamixBaseElement) {
31
+ valueArray.push({ "ref": value.id });
32
+ }
33
+ else {
34
+ valueArray.push(value);
35
+ }
36
+ }
37
+ this.bufferArray[name] = valueArray;
38
+ }
39
+ else if (prop instanceof famix_base_element_1.FamixBaseElement) {
40
+ this.bufferArray[name] = { "ref": prop.id };
41
+ }
42
+ else if (prop !== undefined && !(prop instanceof Set)) {
43
+ this.bufferArray[name] = prop;
44
+ }
45
+ }
46
+ /**
47
+ * Gets a JSON representation of the Famix element
48
+ * @returns A JSON representation of the Famix element
49
+ */
50
+ getJSON() {
51
+ return JSON.stringify(this.bufferArray);
52
+ }
53
+ }
54
+ exports.FamixJSONExporter = FamixJSONExporter;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FamixBaseElement = void 0;
4
+ class FamixBaseElement {
5
+ constructor(repo) {
6
+ repo.addElement(this);
7
+ }
8
+ // @ts-ignore
9
+ // tslint:disable-next-line:no-empty
10
+ addPropertiesToExporter(exporter) {
11
+ }
12
+ }
13
+ exports.FamixBaseElement = FamixBaseElement;