ts2famix 1.3.1 → 1.4.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 (40) hide show
  1. package/dist/analyze.js +38 -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 +48 -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,238 +0,0 @@
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 analyze_1 = require("../analyze");
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
- analyze_1.logger.debug(`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 importClauseInfo The information needed to create a Famix import clause
141
- * @param importDeclaration The import declaration
142
- * @param importer A source file which is a module
143
- * @param moduleSpecifierFilePath The path of the module where the export declaration is
144
- * @param importElement The imported entity
145
- * @param isInExports A boolean indicating if the imported entity is in the exports
146
- * @param isDefaultExport A boolean indicating if the imported entity is a default export
147
- */
148
- createFamixImportClause(importClauseInfo) {
149
- var _a, _b;
150
- const { importDeclaration, importer, moduleSpecifierFilePath, importElement, isInExports, isDefaultExport } = importClauseInfo;
151
- analyze_1.logger.debug(`createFamixImportClause: Creating import clause:`);
152
- const fmxImportClause = new Famix.ImportClause(this.famixRep);
153
- let importedEntity;
154
- let importedEntityName;
155
- let pathName = "\"" + moduleSpecifierFilePath + "\".";
156
- if (importElement instanceof ts_morph_1.ImportSpecifier) {
157
- importedEntityName = importElement.getName();
158
- pathName = pathName + importedEntityName;
159
- if (isInExports) {
160
- importedEntity = this.getFamixEntityByFullyQualifiedName(pathName);
161
- }
162
- if (importedEntity === undefined) {
163
- importedEntity = new Famix.NamedEntity(this.famixRep);
164
- importedEntity.setName(importedEntityName);
165
- if (!isInExports) {
166
- importedEntity.setIsStub(true);
167
- }
168
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
169
- importedEntity.setFullyQualifiedName(pathName);
170
- }
171
- }
172
- else {
173
- importedEntityName = importElement.getText();
174
- if (isDefaultExport) {
175
- pathName = pathName + "defaultExport";
176
- }
177
- else {
178
- pathName = pathName + "namespaceExport";
179
- }
180
- importedEntity = new Famix.NamedEntity(this.famixRep);
181
- importedEntity.setName(importedEntityName);
182
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(importElement, importedEntity);
183
- importedEntity.setFullyQualifiedName(pathName);
184
- }
185
- const importerFullyQualifiedName = this.FQNFunctions.getFQN(importer);
186
- const fmxImporter = this.getFamixEntityByFullyQualifiedName(importerFullyQualifiedName);
187
- fmxImportClause.setImportingEntity(fmxImporter);
188
- fmxImportClause.setImportedEntity(importedEntity);
189
- fmxImportClause.setModuleSpecifier(importDeclaration === null || importDeclaration === void 0 ? void 0 : importDeclaration.getModuleSpecifierValue());
190
- analyze_1.logger.debug(`createFamixImportClause: ${(_a = fmxImportClause.getImportedEntity()) === null || _a === void 0 ? void 0 : _a.getName()} (of type ${getSubTypeName(fmxImportClause.getImportedEntity())}) is imported by ${(_b = fmxImportClause.getImportingEntity()) === null || _b === void 0 ? void 0 : _b.getName()}`);
191
- // make an index file anchor for the import clause
192
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(importDeclaration, fmxImportClause);
193
- fmxImporter.addOutgoingImport(fmxImportClause);
194
- }
195
- /**
196
- * Gets a Famix entity by id
197
- * @param famixId An id of a Famix entity
198
- * @returns The Famix entity corresponding to the id
199
- */
200
- getFamixEntityById(famixId) {
201
- return this.famixRep.getFamixEntityById(famixId);
202
- }
203
- /**
204
- * Gets a Famix entity by fully qualified name
205
- * @param fullyQualifiedName A fully qualified name
206
- * @returns The Famix entity corresponding to the fully qualified name
207
- */
208
- getFamixEntityByFullyQualifiedName(fullyQualifiedName) {
209
- return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName);
210
- }
211
- /**
212
- * Finds the ancestor of a node
213
- * @param node A node
214
- * @returns The ancestor of the node
215
- */
216
- findAncestor(node) {
217
- 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);
218
- }
219
- }
220
- exports.FamixFunctionsAssociations = FamixFunctionsAssociations;
221
- function getSubTypeName(fmxNamedEntity) {
222
- const name = fmxNamedEntity instanceof Famix.Class ? 'Class' :
223
- fmxNamedEntity instanceof Famix.Interface ? 'Interface' :
224
- fmxNamedEntity instanceof Famix.Function ? 'Function' :
225
- fmxNamedEntity instanceof Famix.Enum ? 'Enum' :
226
- fmxNamedEntity instanceof Famix.EnumValue ? 'EnumValue' :
227
- fmxNamedEntity instanceof Famix.Alias ? 'Alias' :
228
- fmxNamedEntity instanceof Famix.Variable ? 'Variable' :
229
- fmxNamedEntity instanceof Famix.Type ? 'Type' :
230
- fmxNamedEntity instanceof Famix.Method ? 'Method' :
231
- fmxNamedEntity instanceof Famix.Decorator ? 'Decorator' :
232
- fmxNamedEntity instanceof Famix.Accessor ? 'Accessor' :
233
- fmxNamedEntity instanceof Famix.Parameter ? 'Parameter' :
234
- fmxNamedEntity instanceof Famix.Property ? 'Property' :
235
- 'NamedEntity';
236
- analyze_1.logger.debug(`${fmxNamedEntity.getName()} is of type ${name}`);
237
- return name;
238
- }
@@ -1,135 +0,0 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.FamixFunctionsIndex = void 0;
30
- const ts_morph_1 = require("ts-morph");
31
- const Famix = __importStar(require("../lib/famix/src/model/famix"));
32
- const fqn_1 = require("../fqn");
33
- const grapheme_splitter_1 = __importDefault(require("grapheme-splitter"));
34
- const analyze_1 = require("../analyze");
35
- /**
36
- * This class is used to build a Famix model for the index file anchors
37
- */
38
- class FamixFunctionsIndex {
39
- /**
40
- * Initializes the FamixFunctionsIndex object
41
- * @param famixRep The Famix repository
42
- */
43
- constructor(famixRep) {
44
- this.FQNFunctions = new fqn_1.FQNFunctions(); // The fully qualified name functions
45
- this.famixRep = famixRep;
46
- }
47
- /**
48
- * Makes a Famix index file anchor
49
- * @param sourceElement A source element
50
- * @param famixElement The Famix model of the source element
51
- */
52
- makeFamixIndexFileAnchor(sourceElement, famixElement) {
53
- analyze_1.logger.debug("making index file anchor for '" + (sourceElement === null || sourceElement === void 0 ? void 0 : sourceElement.getText()) + "' with famixElement " + famixElement.getJSON());
54
- const fmxIndexFileAnchor = new Famix.IndexedFileAnchor(this.famixRep);
55
- fmxIndexFileAnchor.setElement(famixElement);
56
- if (sourceElement !== null) {
57
- fmxIndexFileAnchor.setFileName(sourceElement.getSourceFile().getFilePath());
58
- let sourceStart, sourceEnd, sourceLineStart, sourceLineEnd;
59
- if (!(sourceElement instanceof ts_morph_1.CommentRange)) {
60
- sourceStart = sourceElement.getStart();
61
- sourceEnd = sourceElement.getEnd();
62
- sourceLineStart = sourceElement.getStartLineNumber();
63
- sourceLineEnd = sourceElement.getEndLineNumber();
64
- }
65
- else {
66
- sourceStart = sourceElement.getPos();
67
- sourceEnd = sourceElement.getEnd();
68
- }
69
- if (analyze_1.config.expectGraphemes) {
70
- /**
71
- * The following logic handles the case of multi-code point characters (e.g. emoji) in the source text.
72
- * This is needed because Pharo/Smalltalk treats multi-code point characters as a single character,
73
- * but JavaScript treats them as multiple characters. This means that the start and end positions
74
- * of a source element in Pharo/Smalltalk will be different than the start and end positions of the
75
- * same source element in JavaScript. This logic finds the start and end positions of the source
76
- * element in JavaScript and then uses those positions to set the start and end positions of the
77
- * Famix index file anchor.
78
- * It depends on code in the 'grapheme-splitter' package in npm.
79
- */
80
- const splitter = new grapheme_splitter_1.default();
81
- const sourceFileText = sourceElement.getSourceFile().getFullText();
82
- const hasGraphemeClusters = splitter.countGraphemes(sourceFileText) > 1;
83
- if (hasGraphemeClusters) {
84
- const sourceElementText = sourceFileText.substring(sourceStart, sourceEnd);
85
- const sourceElementTextGraphemes = splitter.splitGraphemes(sourceElementText);
86
- const sourceFileTextGraphemes = splitter.splitGraphemes(sourceFileText);
87
- const numberOfGraphemeClustersBeforeStart = splitter.countGraphemes(sourceFileText.substring(0, sourceStart));
88
- // find the start of the sourceElementTextGraphemes array in the sourceFileTextGraphemes array
89
- sourceStart = indexOfSplitArray({ searchArray: sourceFileTextGraphemes,
90
- targetArray: sourceElementTextGraphemes,
91
- start: sourceStart - numberOfGraphemeClustersBeforeStart });
92
- sourceEnd = sourceStart + sourceElementTextGraphemes.length;
93
- }
94
- }
95
- // note: the +1 is because the source anchor is 1-based, but ts-morph is 0-based
96
- fmxIndexFileAnchor.setStartPos(sourceStart + 1);
97
- fmxIndexFileAnchor.setEndPos(sourceEnd + 1);
98
- if (!(sourceElement instanceof ts_morph_1.CommentRange)) {
99
- fmxIndexFileAnchor.setStartLine(sourceLineStart);
100
- fmxIndexFileAnchor.setEndLine(sourceLineEnd);
101
- }
102
- 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)) {
103
- famixElement.setFullyQualifiedName(this.FQNFunctions.getFQN(sourceElement));
104
- }
105
- }
106
- else {
107
- // sourceElement is null
108
- analyze_1.logger.warn("sourceElement is null for famixElement " + famixElement.getJSON());
109
- fmxIndexFileAnchor.setFileName("unknown");
110
- fmxIndexFileAnchor.setStartPos(0);
111
- fmxIndexFileAnchor.setEndPos(0);
112
- }
113
- }
114
- }
115
- exports.FamixFunctionsIndex = FamixFunctionsIndex;
116
- /**
117
- * This function works like indexOf, but it works with arrays of grapheme clusters.
118
- * @param targetArray
119
- */
120
- function indexOfSplitArray(params) {
121
- const { searchArray, targetArray, start = 0 } = params;
122
- for (let i = start; i <= searchArray.length - targetArray.length; i++) {
123
- let found = true;
124
- for (let j = 0; j < targetArray.length; j++) {
125
- if (searchArray[i + j] !== targetArray[j]) {
126
- found = false;
127
- break;
128
- }
129
- }
130
- if (found) {
131
- return i; // Return the index where the target array was found
132
- }
133
- }
134
- return -1; // Return -1 if the target array was not found in the search array
135
- }
@@ -1,115 +0,0 @@
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
- const analyze_1 = require("../analyze");
32
- /**
33
- * This class is used to build a Famix model for the types
34
- */
35
- class FamixFunctionsTypes {
36
- /**
37
- * Initializes the FamixFunctionsIndex object
38
- * @param famixRep The Famix repository
39
- */
40
- constructor(famixRep) {
41
- this.FQNFunctions = new fqn_1.FQNFunctions(); // The fully qualified name functions
42
- this.fmxTypeMap = new Map(); // Maps the type names to their Famix model
43
- this.famixRep = famixRep;
44
- this.famixFunctionsIndex = new famix_functions_index_1.FamixFunctionsIndex(famixRep);
45
- }
46
- /**
47
- * Creates or gets a Famix type
48
- * @param typeName A type name
49
- * @param element A ts-morph element
50
- * @returns The Famix model of the type
51
- */
52
- createOrGetFamixType(typeName, element) {
53
- let fmxType;
54
- let isPrimitiveType = false;
55
- let isParameterizedType = false;
56
- analyze_1.logger.debug("Creating (or getting) type: '" + typeName + "' of element: " + element.getText() + " of kind: " + element.getKindName());
57
- const typeAncestor = this.findTypeAncestor(element);
58
- const ancestorFullyQualifiedName = this.FQNFunctions.getFQN(typeAncestor);
59
- const ancestor = this.getFamixEntityByFullyQualifiedName(ancestorFullyQualifiedName);
60
- if (!ancestor) {
61
- throw new Error(`Ancestor ${ancestorFullyQualifiedName} not found.`);
62
- }
63
- if (typeName === "number" || typeName === "string" || typeName === "boolean" || typeName === "bigint" || typeName === "symbol" || typeName === "undefined" || typeName === "null" || typeName === "any" || typeName === "unknown" || typeName === "never" || typeName === "void") {
64
- isPrimitiveType = true;
65
- }
66
- if (!isPrimitiveType && typeName.includes("<") && typeName.includes(">") && !(typeName.includes("=>"))) {
67
- isParameterizedType = true;
68
- }
69
- if (!this.fmxTypeMap.has(typeName)) {
70
- if (isPrimitiveType) {
71
- fmxType = new Famix.PrimitiveType(this.famixRep);
72
- fmxType.setIsStub(true);
73
- }
74
- else if (isParameterizedType) {
75
- fmxType = new Famix.ParameterizedType(this.famixRep);
76
- const parameterTypeNames = typeName.substring(typeName.indexOf("<") + 1, typeName.indexOf(">")).split(",").map(s => s.trim());
77
- const baseTypeName = typeName.substring(0, typeName.indexOf("<")).trim();
78
- parameterTypeNames.forEach(parameterTypeName => {
79
- const fmxParameterType = this.createOrGetFamixType(parameterTypeName, element);
80
- fmxType.addArgument(fmxParameterType);
81
- });
82
- const fmxBaseType = this.createOrGetFamixType(baseTypeName, element);
83
- fmxType.setBaseType(fmxBaseType);
84
- }
85
- else {
86
- fmxType = new Famix.Type(this.famixRep);
87
- }
88
- fmxType.setName(typeName);
89
- fmxType.setContainer(ancestor);
90
- this.famixFunctionsIndex.makeFamixIndexFileAnchor(element, fmxType);
91
- this.fmxTypeMap.set(typeName, fmxType);
92
- }
93
- else {
94
- fmxType = this.fmxTypeMap.get(typeName);
95
- }
96
- return fmxType;
97
- }
98
- /**
99
- * Gets a Famix entity by fully qualified name
100
- * @param fullyQualifiedName A fully qualified name
101
- * @returns The Famix entity corresponding to the fully qualified name
102
- */
103
- getFamixEntityByFullyQualifiedName(fullyQualifiedName) {
104
- return this.famixRep.getFamixEntityByFullyQualifiedName(fullyQualifiedName);
105
- }
106
- /**
107
- * Finds the ancestor of a ts-morph element
108
- * @param element A ts-morph element
109
- * @returns The ancestor of the ts-morph element
110
- */
111
- findTypeAncestor(element) {
112
- 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);
113
- }
114
- }
115
- exports.FamixFunctionsTypes = FamixFunctionsTypes;
package/docs/.gitkeep DELETED
File without changes
@@ -1,58 +0,0 @@
1
- import { Identifier, ParameterDeclaration, VariableDeclaration, PropertyDeclaration, EnumMember } from "ts-morph";
2
- import { FamixFunctions } from "../famix_functions/famix_functions";
3
- import { logger } from "../analyze";
4
-
5
- /**
6
- * This class is used to build a Famix model for the accesses
7
- */
8
- export class ProcessAccesses {
9
-
10
- private famixFunctions: FamixFunctions; // FamixFunctions object, it contains all the functions needed to create Famix entities
11
-
12
- /**
13
- * Initializes the ProcessAccesses object
14
- * @param famixFunctions FamixFunctions object, it contains all the functions needed to create Famix entities
15
- */
16
- constructor(famixFunctions: FamixFunctions) {
17
- this.famixFunctions = famixFunctions;
18
- }
19
-
20
- /**
21
- * Builds a Famix model for the accesses on the parameters, variables, properties and enum members of the source files
22
- * @param accessMap A map of parameters, variables, properties and enum members with their id
23
- */
24
- public processAccesses(accessMap: Map<number, ParameterDeclaration | VariableDeclaration | PropertyDeclaration | EnumMember>): void {
25
- logger.debug(`processAccesses: Creating accesses:`);
26
- accessMap.forEach((v, id) => {
27
- logger.debug(`processAccesses: Accesses to ${v.getName()}`);
28
- try {
29
- const temp_nodes = v.findReferencesAsNodes() as Array<Identifier>;
30
- temp_nodes.forEach(node => this.processNodeForAccesses(node, id));
31
- } catch (error) {
32
- logger.error(`> WARNING: got exception ${error}. Continuing...`);
33
- }
34
- });
35
- }
36
-
37
- /**
38
- * Builds a Famix model for an access on a parameter, variable, property or enum member
39
- * @param n A node
40
- * @param id An id of a parameter, a variable, a property or an enum member
41
- */
42
- private processNodeForAccesses(n: Identifier, id: number): void {
43
- try {
44
- // sometimes node's first ancestor is a PropertyDeclaration, which is not an access
45
- // see https://github.com/fuhrmanator/FamixTypeScriptImporter/issues/9
46
- // check for a node whose first ancestor is a property declaration and bail?
47
- // This may be a bug in ts-morph?
48
- if (n.getFirstAncestorOrThrow().getKindName() === "PropertyDeclaration") {
49
- logger.debug(`processNodeForAccesses: node kind: ${n.getKindName()}, ${n.getText()}, (${n.getType().getText()})'s first ancestor is a PropertyDeclaration. Skipping...`);
50
- return;
51
- }
52
- this.famixFunctions.createFamixAccess(n, id);
53
- logger.debug(`processNodeForAccesses: node kind: ${n.getKindName()}, ${n.getText()}, (${n.getType().getText()})`);
54
- } catch (error) {
55
- logger.error(`> WARNING: got exception ${error}. ScopeDeclaration invalid for ${n.getSymbol().getFullyQualifiedName()}. Continuing...`);
56
- }
57
- }
58
- }