ts2famix 1.3.1 → 1.4.0

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 (40) hide show
  1. package/dist/analyze.js +37 -27
  2. package/dist/analyze_functions/process_functions.js +723 -0
  3. package/dist/famix_functions/EntityDictionary.js +798 -0
  4. package/dist/famix_functions/helpers_creation.js +97 -0
  5. package/dist/fqn.js +106 -95
  6. package/dist/lib/famix/src/famix_base_element.js +8 -4
  7. package/dist/lib/famix/src/famix_repository.js +32 -15
  8. package/dist/lib/ts-complex/cyclomatic-service.js +2 -3
  9. package/doc-uml/metamodel-full.svg +1 -0
  10. package/doc-uml/metamodel.svg +1 -0
  11. package/package.json +1 -1
  12. package/plantuml.jar +0 -0
  13. package/src/analyze.ts +46 -29
  14. package/src/analyze_functions/process_functions.ts +838 -0
  15. package/src/famix_functions/EntityDictionary.ts +915 -0
  16. package/src/famix_functions/helpers_creation.ts +77 -0
  17. package/src/fqn.ts +101 -92
  18. package/src/lib/famix/src/famix_base_element.ts +9 -5
  19. package/src/lib/famix/src/famix_repository.ts +45 -23
  20. package/src/lib/ts-complex/cyclomatic-service.ts +2 -4
  21. package/src/ts2famix-cli.ts +1 -0
  22. package/dist/analyze_functions/processAccesses.js +0 -56
  23. package/dist/analyze_functions/processFiles.js +0 -554
  24. package/dist/analyze_functions/processImportClauses.js +0 -88
  25. package/dist/analyze_functions/processInheritances.js +0 -74
  26. package/dist/analyze_functions/processInvocations.js +0 -50
  27. package/dist/famix_functions/famix_functions.js +0 -523
  28. package/dist/famix_functions/famix_functions_associations.js +0 -238
  29. package/dist/famix_functions/famix_functions_index.js +0 -135
  30. package/dist/famix_functions/famix_functions_types.js +0 -115
  31. package/docs/.gitkeep +0 -0
  32. package/src/analyze_functions/processAccesses.ts +0 -58
  33. package/src/analyze_functions/processFiles.ts +0 -667
  34. package/src/analyze_functions/processImportClauses.ts +0 -95
  35. package/src/analyze_functions/processInheritances.ts +0 -85
  36. package/src/analyze_functions/processInvocations.ts +0 -52
  37. package/src/famix_functions/famix_functions.ts +0 -562
  38. package/src/famix_functions/famix_functions_associations.ts +0 -242
  39. package/src/famix_functions/famix_functions_index.ts +0 -120
  40. package/src/famix_functions/famix_functions_types.ts +0 -106
@@ -1,242 +0,0 @@
1
- import { ClassDeclaration, ConstructorDeclaration, FunctionDeclaration, Identifier, InterfaceDeclaration, MethodDeclaration, SourceFile, GetAccessorDeclaration, SetAccessorDeclaration, Node, ImportSpecifier, SyntaxKind, FunctionExpression, ExpressionWithTypeArguments, ImportDeclaration } 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
- import { logger } from "../analyze";
7
-
8
- /**
9
- * This class is used to build a Famix model for the associations
10
- */
11
- export class FamixFunctionsAssociations {
12
-
13
- private famixRep: FamixRepository; // The Famix repository
14
- private FQNFunctions = new FQNFunctions(); // The fully qualified name functions
15
- private famixClassMap: Map<string, Famix.Class | Famix.ParameterizableClass>; // Maps the class names to their Famix model
16
- private famixInterfaceMap: Map<string, Famix.Interface | Famix.ParameterizableInterface>; // Maps the interface names to their Famix model
17
- private famixFunctionsIndex: FamixFunctionsIndex; // FamixFunctionsIndex object, it contains all the functions needed to create Famix index file anchors
18
-
19
- /**
20
- * Initializes the FamixFunctionsAssociations object
21
- * @param famixRep The Famix repository
22
- * @param fmxClassMap The map of the class names and their Famix model
23
- * @param fmxInterfaceMap The map of the interface names and their Famix model
24
- */
25
- constructor(famixRep: FamixRepository, fmxClassMap: Map<string, Famix.Class | Famix.ParameterizableClass>, fmxInterfaceMap: Map<string, Famix.Interface | Famix.ParameterizableInterface>) {
26
- this.famixRep = famixRep;
27
- this.famixClassMap = fmxClassMap;
28
- this.famixInterfaceMap = fmxInterfaceMap;
29
- this.famixFunctionsIndex = new FamixFunctionsIndex(famixRep);
30
- }
31
-
32
- /**
33
- * Creates a Famix access
34
- * @param node A node
35
- * @param id An id of a parameter, a variable, a property or an enum member
36
- */
37
- public createFamixAccess(node: Identifier, id: number): void {
38
- const fmxVar = this.famixRep.getFamixEntityById(id) as Famix.StructuralEntity;
39
- const nodeReferenceAncestor = this.findAncestor(node);
40
- const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(nodeReferenceAncestor);
41
- const accessor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName) as Famix.ContainerEntity;
42
-
43
- const fmxAccess = new Famix.Access(this.famixRep);
44
- fmxAccess.setAccessor(accessor);
45
- fmxAccess.setVariable(fmxVar);
46
-
47
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(node, fmxAccess);
48
- }
49
-
50
- /**
51
- * Creates a Famix invocation
52
- * @param node A node
53
- * @param m A method or a function
54
- * @param id The id of the method or the function
55
- */
56
- public createFamixInvocation(node: Identifier, m: MethodDeclaration | ConstructorDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | FunctionDeclaration | FunctionExpression, id: number): void {
57
- const fmxMethodOrFunction = this.getFamixEntityById(id) as Famix.BehavioralEntity;
58
- const nodeReferenceAncestor = this.findAncestor(node);
59
- const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(nodeReferenceAncestor);
60
- const sender = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName) as Famix.ContainerEntity;
61
- const receiverFullyQualifiedName = this.FQNFunctions.getFQN(m.getParent());
62
- const receiver = this.getFamixEntityByFullyQualifiedName(receiverFullyQualifiedName) as Famix.NamedEntity;
63
-
64
- const fmxInvocation = new Famix.Invocation(this.famixRep);
65
- fmxInvocation.setSender(sender);
66
- fmxInvocation.setReceiver(receiver);
67
- fmxInvocation.addCandidate(fmxMethodOrFunction);
68
- fmxInvocation.setSignature(fmxMethodOrFunction.getSignature());
69
-
70
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(node, fmxInvocation);
71
- }
72
-
73
- /**
74
- * Creates a Famix inheritance
75
- * @param cls A class or an interface (subclass)
76
- * @param inhClass The inherited class or interface (superclass)
77
- */
78
- public createFamixInheritance(cls: ClassDeclaration | InterfaceDeclaration, inhClass: ClassDeclaration | InterfaceDeclaration | ExpressionWithTypeArguments): void {
79
- const fmxInheritance = new Famix.Inheritance(this.famixRep);
80
- // const clsName = cls.getName();
81
- const classFullyQualifiedName = this.FQNFunctions.getFQN(cls);
82
- logger.debug(`createFamixInheritance: classFullyQualifiedName: class fqn = ${classFullyQualifiedName}`);
83
- let subClass: Famix.Class | Famix.Interface;
84
- if (cls instanceof ClassDeclaration) {
85
- subClass = this.famixClassMap.get(classFullyQualifiedName);
86
- }
87
- else {
88
- subClass = this.famixInterfaceMap.get(classFullyQualifiedName);
89
- }
90
-
91
- let inhClassName: string;
92
- let inhClassFullyQualifiedName: string;
93
- let superClass: Famix.Class | Famix.Interface;
94
- if (inhClass instanceof ClassDeclaration || inhClass instanceof InterfaceDeclaration) {
95
- inhClassName = inhClass.getName();
96
- inhClassFullyQualifiedName = this.FQNFunctions.getFQN(inhClass);
97
- if (inhClass instanceof ClassDeclaration) {
98
- superClass = this.famixClassMap.get(inhClassFullyQualifiedName);
99
- }
100
- else {
101
- superClass = this.famixInterfaceMap.get(inhClassFullyQualifiedName);
102
- }
103
- }
104
- else {
105
- // inhClass is an ExpressionWithTypeArguments
106
- inhClassName = inhClass.getExpression().getText();
107
- // what is inhClassFullyQualifiedName? TODO
108
- inhClassFullyQualifiedName = 'Undefined_Scope_from_importer.' + inhClassName;
109
- }
110
-
111
- if (superClass === undefined) {
112
- if (inhClass instanceof ClassDeclaration) {
113
- superClass = new Famix.Class(this.famixRep);
114
- this.famixClassMap.set(inhClassFullyQualifiedName, superClass);
115
- }
116
- else {
117
- superClass = new Famix.Interface(this.famixRep);
118
- this.famixInterfaceMap.set(inhClassFullyQualifiedName, superClass);
119
- }
120
-
121
- superClass.setName(inhClassName);
122
- superClass.setFullyQualifiedName(inhClassFullyQualifiedName);
123
- superClass.setIsStub(true);
124
-
125
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(inhClass, superClass);
126
- }
127
-
128
- fmxInheritance.setSubclass(subClass);
129
- fmxInheritance.setSuperclass(superClass);
130
-
131
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(null, fmxInheritance);
132
- }
133
-
134
- /**
135
- * Creates a Famix import clause
136
- * @param importClauseInfo The information needed to create a Famix import clause
137
- * @param importDeclaration The import declaration
138
- * @param importer A source file which is a module
139
- * @param moduleSpecifierFilePath The path of the module where the export declaration is
140
- * @param importElement The imported entity
141
- * @param isInExports A boolean indicating if the imported entity is in the exports
142
- * @param isDefaultExport A boolean indicating if the imported entity is a default export
143
- */
144
- public createFamixImportClause(importClauseInfo: {importDeclaration?: ImportDeclaration, importer: SourceFile, moduleSpecifierFilePath: string, importElement: ImportSpecifier | Identifier, isInExports: boolean, isDefaultExport: boolean}): void {
145
- const {importDeclaration, importer, moduleSpecifierFilePath, importElement, isInExports, isDefaultExport} = importClauseInfo;
146
- logger.debug(`createFamixImportClause: Creating import clause:`);
147
- const fmxImportClause = new Famix.ImportClause(this.famixRep);
148
-
149
- let importedEntity: Famix.NamedEntity;
150
- let importedEntityName: string;
151
- let pathName = "\"" + moduleSpecifierFilePath + "\".";
152
- if (importElement instanceof ImportSpecifier) {
153
- importedEntityName = importElement.getName();
154
- pathName = pathName + importedEntityName;
155
- if (isInExports) {
156
- importedEntity = this.getFamixEntityByFullyQualifiedName(pathName) as Famix.NamedEntity;
157
- }
158
- if (importedEntity === undefined) {
159
- importedEntity = new Famix.NamedEntity(this.famixRep);
160
- importedEntity.setName(importedEntityName);
161
- if (!isInExports) {
162
- importedEntity.setIsStub(true);
163
- }
164
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
165
- importedEntity.setFullyQualifiedName(pathName);
166
- }
167
- }
168
- else {
169
- importedEntityName = importElement.getText();
170
- if (isDefaultExport) {
171
- pathName = pathName + "defaultExport";
172
- }
173
- else {
174
- pathName = pathName + "namespaceExport";
175
- }
176
- importedEntity = new Famix.NamedEntity(this.famixRep);
177
- importedEntity.setName(importedEntityName);
178
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
179
- importedEntity.setFullyQualifiedName(pathName);
180
- }
181
-
182
- const importerFullyQualifiedName = this.FQNFunctions.getFQN(importer);
183
- const fmxImporter = this.getFamixEntityByFullyQualifiedName(importerFullyQualifiedName) as Famix.Module;
184
- fmxImportClause.setImportingEntity(fmxImporter);
185
- fmxImportClause.setImportedEntity(importedEntity);
186
- fmxImportClause.setModuleSpecifier(importDeclaration?.getModuleSpecifierValue() as string);
187
-
188
- logger.debug(`createFamixImportClause: ${fmxImportClause.getImportedEntity()?.getName()} (of type ${
189
- getSubTypeName(fmxImportClause.getImportedEntity())}) is imported by ${fmxImportClause.getImportingEntity()?.getName()}`);
190
-
191
- // make an index file anchor for the import clause
192
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(importDeclaration, fmxImportClause);
193
-
194
- fmxImporter.addOutgoingImport(fmxImportClause);
195
- }
196
-
197
- /**
198
- * Gets a Famix entity by id
199
- * @param famixId An id of a Famix entity
200
- * @returns The Famix entity corresponding to the id
201
- */
202
- private getFamixEntityById(famixId: number): Famix.Entity {
203
- return this.famixRep.getFamixEntityById(famixId) as Famix.Entity;
204
- }
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
- private getFamixEntityByFullyQualifiedName(fullyQualifiedName: string): Famix.Entity {
212
- return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName) as Famix.Entity;
213
- }
214
-
215
- /**
216
- * Finds the ancestor of a node
217
- * @param node A node
218
- * @returns The ancestor of the node
219
- */
220
- private findAncestor(node: Identifier): Node {
221
- 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);
222
- }
223
- }
224
- function getSubTypeName(fmxNamedEntity: Famix.NamedEntity) {
225
- const name = fmxNamedEntity instanceof Famix.Class ? 'Class' :
226
- fmxNamedEntity instanceof Famix.Interface ? 'Interface' :
227
- fmxNamedEntity instanceof Famix.Function ? 'Function' :
228
- fmxNamedEntity instanceof Famix.Enum ? 'Enum' :
229
- fmxNamedEntity instanceof Famix.EnumValue ? 'EnumValue' :
230
- fmxNamedEntity instanceof Famix.Alias ? 'Alias' :
231
- fmxNamedEntity instanceof Famix.Variable ? 'Variable' :
232
- fmxNamedEntity instanceof Famix.Type ? 'Type' :
233
- fmxNamedEntity instanceof Famix.Method ? 'Method' :
234
- fmxNamedEntity instanceof Famix.Decorator ? 'Decorator' :
235
- fmxNamedEntity instanceof Famix.Accessor ? 'Accessor' :
236
- fmxNamedEntity instanceof Famix.Parameter ? 'Parameter' :
237
- fmxNamedEntity instanceof Famix.Property ? 'Property' :
238
- 'NamedEntity';
239
- logger.debug(`${fmxNamedEntity.getName()} is of type ${name}`);
240
- return name;
241
- }
242
-
@@ -1,120 +0,0 @@
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, ImportDeclaration } 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 GraphemeSplitter from "grapheme-splitter";
6
- import { logger, config } from "../analyze";
7
-
8
- /**
9
- * This class is used to build a Famix model for the index file anchors
10
- */
11
- export class FamixFunctionsIndex {
12
-
13
- private famixRep: FamixRepository; // The Famix repository
14
- private FQNFunctions = new FQNFunctions(); // The fully qualified name functions
15
-
16
- /**
17
- * Initializes the FamixFunctionsIndex object
18
- * @param famixRep The Famix repository
19
- */
20
- constructor(famixRep: FamixRepository) {
21
- this.famixRep = famixRep;
22
- }
23
-
24
- /**
25
- * Makes a Famix index file anchor
26
- * @param sourceElement A source element
27
- * @param famixElement The Famix model of the source element
28
- */
29
- public makeFamixIndexFileAnchor(sourceElement: ImportDeclaration | 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 {
30
- logger.debug("making index file anchor for '" + sourceElement?.getText() + "' with famixElement " + famixElement.getJSON());
31
- const fmxIndexFileAnchor = new Famix.IndexedFileAnchor(this.famixRep);
32
- fmxIndexFileAnchor.setElement(famixElement);
33
-
34
- if (sourceElement !== null) {
35
- fmxIndexFileAnchor.setFileName(sourceElement.getSourceFile().getFilePath());
36
- let sourceStart, sourceEnd, sourceLineStart, sourceLineEnd: number;
37
- if (!(sourceElement instanceof CommentRange)) {
38
- sourceStart = sourceElement.getStart();
39
- sourceEnd = sourceElement.getEnd();
40
- sourceLineStart = sourceElement.getStartLineNumber();
41
- sourceLineEnd = sourceElement.getEndLineNumber();
42
- } else {
43
- sourceStart = sourceElement.getPos();
44
- sourceEnd = sourceElement.getEnd();
45
- }
46
- if (config.expectGraphemes) {
47
- /**
48
- * The following logic handles the case of multi-code point characters (e.g. emoji) in the source text.
49
- * This is needed because Pharo/Smalltalk treats multi-code point characters as a single character,
50
- * but JavaScript treats them as multiple characters. This means that the start and end positions
51
- * of a source element in Pharo/Smalltalk will be different than the start and end positions of the
52
- * same source element in JavaScript. This logic finds the start and end positions of the source
53
- * element in JavaScript and then uses those positions to set the start and end positions of the
54
- * Famix index file anchor.
55
- * It depends on code in the 'grapheme-splitter' package in npm.
56
- */
57
- const splitter = new GraphemeSplitter();
58
- const sourceFileText = sourceElement.getSourceFile().getFullText();
59
- const hasGraphemeClusters = splitter.countGraphemes(sourceFileText) > 1;
60
- if (hasGraphemeClusters) {
61
- const sourceElementText = sourceFileText.substring(sourceStart, sourceEnd);
62
- const sourceElementTextGraphemes = splitter.splitGraphemes(sourceElementText);
63
- const sourceFileTextGraphemes = splitter.splitGraphemes(sourceFileText);
64
- const numberOfGraphemeClustersBeforeStart = splitter.countGraphemes(sourceFileText.substring(0, sourceStart));
65
-
66
- // find the start of the sourceElementTextGraphemes array in the sourceFileTextGraphemes array
67
- sourceStart = indexOfSplitArray({searchArray: sourceFileTextGraphemes,
68
- targetArray: sourceElementTextGraphemes,
69
- start: sourceStart - numberOfGraphemeClustersBeforeStart});
70
- sourceEnd = sourceStart + sourceElementTextGraphemes.length;
71
- }
72
- }
73
- // note: the +1 is because the source anchor is 1-based, but ts-morph is 0-based
74
- fmxIndexFileAnchor.setStartPos(sourceStart + 1);
75
- fmxIndexFileAnchor.setEndPos(sourceEnd + 1);
76
- if (!(sourceElement instanceof CommentRange)) {
77
- fmxIndexFileAnchor.setStartLine(sourceLineStart);
78
- fmxIndexFileAnchor.setEndLine(sourceLineEnd);
79
- }
80
-
81
- if (!(famixElement instanceof Famix.Association) && !(famixElement instanceof Famix.Comment) && !(sourceElement instanceof CommentRange) && !(sourceElement instanceof Identifier) && !(sourceElement instanceof ImportSpecifier) && !(sourceElement instanceof ExpressionWithTypeArguments)) {
82
- (famixElement as Famix.NamedEntity).setFullyQualifiedName(this.FQNFunctions.getFQN(sourceElement));
83
- }
84
- } else {
85
- // sourceElement is null
86
- logger.warn("sourceElement is null for famixElement " + famixElement.getJSON());
87
- fmxIndexFileAnchor.setFileName("unknown");
88
- fmxIndexFileAnchor.setStartPos(0);
89
- fmxIndexFileAnchor.setEndPos(0);
90
-
91
- }
92
- }
93
- }
94
-
95
-
96
- interface SearchParameters {
97
- searchArray: string[];
98
- targetArray: string[];
99
- start?: number;
100
- }
101
- /**
102
- * This function works like indexOf, but it works with arrays of grapheme clusters.
103
- * @param targetArray
104
- */
105
- function indexOfSplitArray(params: SearchParameters): number {
106
- const {searchArray, targetArray, start = 0} = params;
107
- for (let i = start; i <= searchArray.length - targetArray.length; i++) {
108
- let found = true;
109
- for (let j = 0; j < targetArray.length; j++) {
110
- if (searchArray[i + j] !== targetArray[j]) {
111
- found = false;
112
- break;
113
- }
114
- }
115
- if (found) {
116
- return i; // Return the index where the target array was found
117
- }
118
- }
119
- return -1; // Return -1 if the target array was not found in the search array
120
- }
@@ -1,106 +0,0 @@
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
- import { logger } from "../analyze";
7
-
8
- /**
9
- * This class is used to build a Famix model for the types
10
- */
11
- export class FamixFunctionsTypes {
12
-
13
- private famixRep: FamixRepository; // The Famix repository
14
- private FQNFunctions = new FQNFunctions(); // The fully qualified name functions
15
- private fmxTypeMap = new Map<string, Famix.Type | Famix.PrimitiveType | Famix.ParameterizedType>(); // Maps the type 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 FamixFunctionsIndex object
20
- * @param famixRep The Famix repository
21
- */
22
- constructor(famixRep: FamixRepository) {
23
- this.famixRep = famixRep;
24
- this.famixFunctionsIndex = new FamixFunctionsIndex(famixRep);
25
- }
26
-
27
- /**
28
- * Creates or gets a Famix type
29
- * @param typeName A type name
30
- * @param element A ts-morph element
31
- * @returns The Famix model of the type
32
- */
33
- public createOrGetFamixType(typeName: string, element: TypeAliasDeclaration | PropertyDeclaration | PropertySignature | MethodDeclaration | ConstructorDeclaration | MethodSignature | GetAccessorDeclaration | SetAccessorDeclaration | FunctionDeclaration | FunctionExpression | ParameterDeclaration | VariableDeclaration | EnumMember): Famix.Type | Famix.PrimitiveType | Famix.ParameterizedType {
34
- let fmxType: Famix.Type | Famix.PrimitiveType | Famix.ParameterizedType;
35
- let isPrimitiveType = false;
36
- let isParameterizedType = false;
37
-
38
- logger.debug("Creating (or getting) type: '" + typeName + "' of element: " + element.getText() + " of kind: " + element.getKindName());
39
-
40
- const typeAncestor = this.findTypeAncestor(element);
41
- const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(typeAncestor);
42
- const ancestor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName) as Famix.ContainerEntity;
43
- if (!ancestor) {
44
- throw new Error(`Ancestor ${ancestorFullyQualifiedName} not found.`);
45
- }
46
-
47
- if (typeName === "number" || typeName === "string" || typeName === "boolean" || typeName === "bigint" || typeName === "symbol" || typeName === "undefined" || typeName === "null" || typeName === "any" || typeName === "unknown" || typeName === "never" || typeName === "void") {
48
- isPrimitiveType = true;
49
- }
50
-
51
- if(!isPrimitiveType && typeName.includes("<") && typeName.includes(">") && !(typeName.includes("=>"))) {
52
- isParameterizedType = true;
53
- }
54
-
55
- if (!this.fmxTypeMap.has(typeName)) {
56
- if (isPrimitiveType) {
57
- fmxType = new Famix.PrimitiveType(this.famixRep);
58
- fmxType.setIsStub(true);
59
- }
60
- else if (isParameterizedType) {
61
- fmxType = new Famix.ParameterizedType(this.famixRep);
62
- const parameterTypeNames = typeName.substring(typeName.indexOf("<") + 1, typeName.indexOf(">")).split(",").map(s => s.trim());
63
- const baseTypeName = typeName.substring(0, typeName.indexOf("<")).trim();
64
- parameterTypeNames.forEach(parameterTypeName => {
65
- const fmxParameterType = this.createOrGetFamixType(parameterTypeName, element);
66
- (fmxType as Famix.ParameterizedType).addArgument(fmxParameterType);
67
- });
68
- const fmxBaseType = this.createOrGetFamixType(baseTypeName, element);
69
- (fmxType as Famix.ParameterizedType).setBaseType(fmxBaseType);
70
- }
71
- else {
72
- fmxType = new Famix.Type(this.famixRep);
73
- }
74
-
75
- fmxType.setName(typeName);
76
- fmxType.setContainer(ancestor);
77
-
78
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(element, fmxType);
79
-
80
- this.fmxTypeMap.set(typeName, fmxType);
81
- }
82
- else {
83
- fmxType = this.fmxTypeMap.get(typeName);
84
- }
85
-
86
- return fmxType;
87
- }
88
-
89
- /**
90
- * Gets a Famix entity by fully qualified name
91
- * @param fullyQualifiedName A fully qualified name
92
- * @returns The Famix entity corresponding to the fully qualified name
93
- */
94
- private getFamixEntityByFullyQualifiedName(fullyQualifiedName: string): Famix.Entity {
95
- return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName) as Famix.Entity;
96
- }
97
-
98
- /**
99
- * Finds the ancestor of a ts-morph element
100
- * @param element A ts-morph element
101
- * @returns The ancestor of the ts-morph element
102
- */
103
- private findTypeAncestor(element: TypeAliasDeclaration | PropertyDeclaration | PropertySignature | MethodDeclaration | ConstructorDeclaration | MethodSignature | GetAccessorDeclaration | SetAccessorDeclaration | FunctionDeclaration | FunctionExpression | ParameterDeclaration | VariableDeclaration | EnumMember): Node {
104
- 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);
105
- }
106
- }