ts2famix 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/.eslintrc.json +24 -0
  2. package/.github/workflows/node.js.yml +60 -0
  3. package/LICENSE +23 -0
  4. package/README.md +111 -0
  5. package/doc-metamodel/skins.include.puml +2 -0
  6. package/jest.config.ts +199 -0
  7. package/package.json +47 -0
  8. package/src/analyze.ts +90 -0
  9. package/src/analyze_functions/processAccesses.ts +50 -0
  10. package/src/analyze_functions/processFiles.ts +656 -0
  11. package/src/analyze_functions/processImportClauses.ts +77 -0
  12. package/src/analyze_functions/processInheritances.ts +84 -0
  13. package/src/analyze_functions/processInvocations.ts +51 -0
  14. package/src/famix2puml.ts +119 -0
  15. package/src/famix_functions/famix_functions.ts +552 -0
  16. package/src/famix_functions/famix_functions_associations.ts +208 -0
  17. package/src/famix_functions/famix_functions_index.ts +44 -0
  18. package/src/famix_functions/famix_functions_types.ts +100 -0
  19. package/src/fqn.ts +127 -0
  20. package/src/fqp_implementation.ts +66 -0
  21. package/src/generate_uml.sh +16 -0
  22. package/src/lib/famix/License.md +23 -0
  23. package/src/lib/famix/package-lock.json +301 -0
  24. package/src/lib/famix/package.json +28 -0
  25. package/src/lib/famix/readme.md +5 -0
  26. package/src/lib/famix/src/famix_JSON_exporter.ts +56 -0
  27. package/src/lib/famix/src/famix_base_element.ts +18 -0
  28. package/src/lib/famix/src/famix_repository.ts +199 -0
  29. package/src/lib/famix/src/index.ts +8 -0
  30. package/src/lib/famix/src/model/famix/access.ts +53 -0
  31. package/src/lib/famix/src/model/famix/accessor.ts +15 -0
  32. package/src/lib/famix/src/model/famix/alias.ts +41 -0
  33. package/src/lib/famix/src/model/famix/association.ts +44 -0
  34. package/src/lib/famix/src/model/famix/behavioral_entity.ts +107 -0
  35. package/src/lib/famix/src/model/famix/c_source_language.ts +15 -0
  36. package/src/lib/famix/src/model/famix/class.ts +86 -0
  37. package/src/lib/famix/src/model/famix/comment.ts +50 -0
  38. package/src/lib/famix/src/model/famix/container_entity.ts +165 -0
  39. package/src/lib/famix/src/model/famix/custom_source_language.ts +27 -0
  40. package/src/lib/famix/src/model/famix/decorator.ts +39 -0
  41. package/src/lib/famix/src/model/famix/entity.ts +15 -0
  42. package/src/lib/famix/src/model/famix/enum.ts +31 -0
  43. package/src/lib/famix/src/model/famix/enum_value.ts +29 -0
  44. package/src/lib/famix/src/model/famix/function.ts +15 -0
  45. package/src/lib/famix/src/model/famix/implicit_variable.ts +15 -0
  46. package/src/lib/famix/src/model/famix/import_clause.ts +53 -0
  47. package/src/lib/famix/src/model/famix/index.ts +42 -0
  48. package/src/lib/famix/src/model/famix/indexed_file_anchor.ts +49 -0
  49. package/src/lib/famix/src/model/famix/inheritance.ts +42 -0
  50. package/src/lib/famix/src/model/famix/interface.ts +75 -0
  51. package/src/lib/famix/src/model/famix/invocation.ts +68 -0
  52. package/src/lib/famix/src/model/famix/method.ts +96 -0
  53. package/src/lib/famix/src/model/famix/module.ts +31 -0
  54. package/src/lib/famix/src/model/famix/named_entity.ts +98 -0
  55. package/src/lib/famix/src/model/famix/namespace.ts +28 -0
  56. package/src/lib/famix/src/model/famix/parameter.ts +29 -0
  57. package/src/lib/famix/src/model/famix/parameterizable_class.ts +31 -0
  58. package/src/lib/famix/src/model/famix/parameterizable_interface.ts +31 -0
  59. package/src/lib/famix/src/model/famix/parameterized_type.ts +40 -0
  60. package/src/lib/famix/src/model/famix/primitive_type.ts +15 -0
  61. package/src/lib/famix/src/model/famix/property.ts +54 -0
  62. package/src/lib/famix/src/model/famix/reference.ts +42 -0
  63. package/src/lib/famix/src/model/famix/scoping_entity.ts +31 -0
  64. package/src/lib/famix/src/model/famix/script_entity.ts +38 -0
  65. package/src/lib/famix/src/model/famix/source_anchor.ts +31 -0
  66. package/src/lib/famix/src/model/famix/source_language.ts +31 -0
  67. package/src/lib/famix/src/model/famix/sourced_entity.ts +70 -0
  68. package/src/lib/famix/src/model/famix/structural_entity.ts +44 -0
  69. package/src/lib/famix/src/model/famix/text_anchor.ts +49 -0
  70. package/src/lib/famix/src/model/famix/type.ts +88 -0
  71. package/src/lib/famix/src/model/famix/type_parameter.ts +33 -0
  72. package/src/lib/famix/src/model/famix/variable.ts +28 -0
  73. package/src/lib/famix/tsconfig.json +27 -0
  74. package/src/lib/famix/tslint.json +15 -0
  75. package/src/lib/ts-complex/cyclomatic-service.ts +85 -0
  76. package/src/ts2famix-cli.ts +26 -0
  77. package/src/ts2famix-tsconfig.ts +30 -0
  78. package/test/abstractClassWithComments.test.ts +58 -0
  79. package/test/abstracts.test.ts +53 -0
  80. package/test/access.test.ts +62 -0
  81. package/test/accesses.test.ts +42 -0
  82. package/test/accessorsWithDecorators.test.ts +98 -0
  83. package/test/alias.test.ts +39 -0
  84. package/test/classExtendsUndefinedClass.test.ts +41 -0
  85. package/test/classImplementsUndefinedInterface.test.ts +45 -0
  86. package/test/classWithDecorators.test.ts +65 -0
  87. package/test/entities.test.ts +232 -0
  88. package/test/entities_json.test.ts +48 -0
  89. package/test/enum.test.ts +55 -0
  90. package/test/functionReturnsFunction.test.ts +53 -0
  91. package/test/functionWithParameters.test.ts +38 -0
  92. package/test/functionWithVariables.test.ts +64 -0
  93. package/test/functions.test.ts +23 -0
  94. package/test/functionsInFunction.test.ts +40 -0
  95. package/test/functionsInMethod.test.ts +42 -0
  96. package/test/genericClass.test.ts +42 -0
  97. package/test/genericClassInheritsInterface.test.ts +47 -0
  98. package/test/genericInterface.test.ts +38 -0
  99. package/test/genericMethod.test.ts +65 -0
  100. package/test/genericWithInvocation.test.ts +71 -0
  101. package/test/generics.test.ts +68 -0
  102. package/test/inheritance.test.ts +50 -0
  103. package/test/interfaceInheritsInterface.test.ts +40 -0
  104. package/test/interfaceInheritsUndefinedInterface.test.ts +41 -0
  105. package/test/invocation.test.ts +94 -0
  106. package/test/invocationWithFunction.test.ts +42 -0
  107. package/test/invocationWithVariable.test.ts +46 -0
  108. package/test/invocation_json.test.ts +63 -0
  109. package/test/invocations.test.ts +131 -0
  110. package/test/jsDoc.test.ts +31 -0
  111. package/test/methodWithDecorator.test.ts +44 -0
  112. package/test/methods.test.ts +42 -0
  113. package/test/metrics.test.ts +51 -0
  114. package/test/module.test.ts +71 -0
  115. package/test/namespaces.test.ts +54 -0
  116. package/test/namespacesAndClasses.test.ts +66 -0
  117. package/test/parameterWithDecorators.test.ts +54 -0
  118. package/test/propertyWithDecorators.test.ts +80 -0
  119. package/test/sample.test.ts +13 -0
  120. package/test/simpleFunction.test.ts +32 -0
  121. package/test/simpleTest.test.ts +18 -0
  122. package/test/simpleTest2.test.ts +36 -0
  123. package/test/types.test.ts +58 -0
  124. package/test_src/sample.ts +103 -0
  125. package/test_src/sampleForModule.ts +10 -0
  126. package/test_src/sampleForModule2.ts +7 -0
  127. package/test_src/sampleForModule3.ts +2 -0
  128. package/tsconfig.json +70 -0
@@ -0,0 +1,85 @@
1
+ /*
2
+ Portions of this file:
3
+
4
+ MIT License
5
+
6
+ Copyright (c) 2018 Anand Undavia
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ */
14
+
15
+ import { forEachChild, SyntaxKind, createSourceFile } from 'typescript';
16
+ import { isFunctionWithBody } from 'tsutils';
17
+ import { existsSync, readFileSync } from 'fs';
18
+
19
+ const { isIdentifier } = require('typescript');
20
+
21
+ const getNodeName = (node) => {
22
+ const { name, pos, end } = node;
23
+ const key = name !== undefined && isIdentifier(name) ? name.text : JSON.stringify({ pos, end });
24
+ return key;
25
+ };
26
+
27
+ const increasesComplexity = (node) => {
28
+ /* eslint-disable indent */
29
+ switch (node.kind) {
30
+ case SyntaxKind.CaseClause:
31
+ return node.statements.length > 0;
32
+ case SyntaxKind.CatchClause:
33
+ case SyntaxKind.ConditionalExpression:
34
+ case SyntaxKind.DoStatement:
35
+ case SyntaxKind.ForStatement:
36
+ case SyntaxKind.ForInStatement:
37
+ case SyntaxKind.ForOfStatement:
38
+ case SyntaxKind.IfStatement:
39
+ case SyntaxKind.WhileStatement:
40
+ return true;
41
+
42
+ case SyntaxKind.BinaryExpression:
43
+ switch (node.operatorToken.kind) {
44
+ case SyntaxKind.BarBarToken:
45
+ case SyntaxKind.AmpersandAmpersandToken:
46
+ return true;
47
+ default:
48
+ return false;
49
+ }
50
+
51
+ default:
52
+ return false;
53
+ }
54
+ /* eslint-enable indent */
55
+ };
56
+
57
+ const calculateFromSource = (ctx) => {
58
+ let complexity = 0;
59
+ const output = {};
60
+ forEachChild(ctx, function cb(node) {
61
+ if (isFunctionWithBody(node)) {
62
+ const old = complexity;
63
+ complexity = 1;
64
+ forEachChild(node, cb);
65
+ const name = getNodeName(node)?.toString();
66
+ output[name] = complexity;
67
+ complexity = old;
68
+ } else {
69
+ if (increasesComplexity(node)) {
70
+ complexity += 1;
71
+ }
72
+ forEachChild(node, cb);
73
+ }
74
+ });
75
+ return output;
76
+ };
77
+
78
+ export const calculate = (filePath) => {
79
+ if (!existsSync(filePath)) {
80
+ throw new Error(`File "${filePath}" does not exists`);
81
+ }
82
+ const sourceText = readFileSync(filePath).toString();
83
+ const source = createSourceFile(filePath, sourceText, isIdentifier.ES2015);
84
+ return calculateFromSource(source);
85
+ };
@@ -0,0 +1,26 @@
1
+ import * as fs from "fs";
2
+ import yargs from "yargs";
3
+ import { Importer } from './analyze';
4
+
5
+ const importer = new Importer();
6
+
7
+ const argv = yargs
8
+ .example(`ts-node src/ts2famix-cli.ts -i "../path/to/project/**/*.ts" -o JSONModels/projectName.json`, 'creates a JSON-format model of a typescript project')
9
+ .alias('i', 'input')
10
+ .nargs('i', 1)
11
+ .alias('o', 'output')
12
+ .nargs('o', 1)
13
+ .demandOption('input').demandOption('output').parseSync();
14
+
15
+ const paths = new Array<string>();
16
+ paths.push(argv.input as string);
17
+
18
+ const famixRep = importer.famixRepFromPaths(paths);
19
+ const jsonOutput = famixRep.getJSON();
20
+ const jsonFilePath = argv.output as string;
21
+
22
+ fs.writeFile(jsonFilePath, jsonOutput, (err) => {
23
+ if (err) { throw err; }
24
+ });
25
+
26
+ console.info(`Created: ${jsonFilePath}`);
@@ -0,0 +1,30 @@
1
+ import * as fs from "fs";
2
+ import yargs from "yargs";
3
+ import { Importer } from "./analyze";
4
+ import { Project } from "ts-morph";
5
+
6
+ const importer = new Importer();
7
+
8
+ const argv = yargs
9
+ .example(`ts-node src/ts2famix-tsconfig.ts -i ../path/to/project/tsconfig.json -o JSONModels/projectName.json`, 'creates a JSON-format model of a typescript project')
10
+ .alias('i', 'input')
11
+ .nargs('i', 1)
12
+ .alias('o', 'output')
13
+ .nargs('o', 1)
14
+ .demandOption('input').demandOption('output').parseSync();
15
+
16
+ const tsConfigFilePath = argv.input as string;
17
+
18
+ const project = new Project({
19
+ tsConfigFilePath
20
+ });
21
+
22
+ const famixRep = importer.famixRepFromProject(project);
23
+ const jsonOutput = famixRep.getJSON();
24
+ const jsonFilePath = argv.output as string;
25
+
26
+ fs.writeFile(jsonFilePath, jsonOutput, (err) => {
27
+ if (err) { throw err; }
28
+ });
29
+
30
+ console.info(`Created: ${jsonFilePath}`);
@@ -0,0 +1,58 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Comment } from '../src/lib/famix/src/model/famix/comment';
3
+
4
+ const importer = new Importer();
5
+
6
+ const fmxRep = importer.famixRepFromSource("abstractClassWithComments",
7
+ '// before\n\
8
+ abstract class MyAbstractClass {} // a comment\n\
9
+ // after\n\
10
+ /* test */\n\
11
+ /**\n\
12
+ * test2\n\
13
+ */\n\
14
+ function tst() {}\n\
15
+ ');
16
+
17
+ describe('Tests for abstract class with comments', () => {
18
+
19
+ it("should contain one class", () => {
20
+ expect(fmxRep._getAllEntitiesWithType("Class").size).toBe(1);
21
+ });
22
+
23
+ const theAbstractClass = fmxRep._getFamixClass("MyAbstractClass");
24
+ it("should contain an abstract class MyAbstractClass", () => {
25
+ expect(theAbstractClass).toBeTruthy();
26
+ if (theAbstractClass) {
27
+ expect(theAbstractClass.getIsAbstract()).toBe(true);
28
+ }
29
+ });
30
+
31
+ it("should have two comments for the abstract class", () => {
32
+ expect(theAbstractClass?.getComments().size).toBe(2);
33
+ const comments = Array.from(theAbstractClass?.getComments() as Set<Comment>);
34
+ expect(comments[0]?.getContent()).toBe("// before");
35
+ expect(comments[0]?.getContainer()).toBe(theAbstractClass);
36
+ expect(comments[1]?.getContent()).toBe("// a comment");
37
+ expect(comments[1]?.getContainer()).toBe(theAbstractClass);
38
+ });
39
+
40
+ it("should contain one function", () => {
41
+ expect(fmxRep._getAllEntitiesWithType("Function").size).toBe(1);
42
+ });
43
+
44
+ const theFunction = fmxRep._getFamixFunction("tst");
45
+ it("should have three comments for the function", () => {
46
+ expect(theFunction?.getComments().size).toBe(3);
47
+ const comments = Array.from(theFunction?.getComments() as Set<Comment>);
48
+ expect(comments[0]?.getContent()).toBe("// after");
49
+ expect(comments[0]?.getContainer()).toBe(theFunction);
50
+ expect(comments[0]?.getIsJSDoc()).toBe(false);
51
+ expect(comments[1]?.getContent()).toBe("/* test */");
52
+ expect(comments[1]?.getContainer()).toBe(theFunction);
53
+ expect(comments[1]?.getIsJSDoc()).toBe(false);
54
+ expect(comments[2]?.getContent()).toBe("/**\n * test2\n */");
55
+ expect(comments[2]?.getContainer()).toBe(theFunction);
56
+ expect(comments[2]?.getIsJSDoc()).toBe(true);
57
+ });
58
+ });
@@ -0,0 +1,53 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Method } from '../src/lib/famix/src/model/famix';
3
+
4
+ const importer = new Importer();
5
+
6
+ const fmxRep = importer.famixRepFromSource("abstracts",
7
+ 'abstract class MyAbstractClass {\n\
8
+ public abstract abstractMethod1();\n\
9
+ public abstract abstractMethod2();\n\
10
+ public concreteMethod() {}\n\
11
+ }\n\
12
+ ');
13
+
14
+ describe('Abstract classes and methods', () => {
15
+
16
+ const theClass = fmxRep._getFamixClass("MyAbstractClass");
17
+ const theMethods = theClass?.getMethods();
18
+
19
+ it("should contain an abstract class MyAbstractClass", () => {
20
+ expect(theClass).toBeTruthy();
21
+ if (theClass) expect(theClass.getIsAbstract()).toBe(true);
22
+ });
23
+
24
+ it("should contain an abstract method MyAbstractClass.abstractMethod1", () => {
25
+ expect(theMethods).toBeTruthy();
26
+ if (theMethods) {
27
+ expect(theMethods.size).toBe(3);
28
+ if (theMethods) {
29
+ expect(theMethods.size).toBe(3);
30
+ const foundMethodName = fmxRep._getFamixMethod("abstractMethod1") as Method;
31
+ expect(foundMethodName.getIsAbstract()).toBe(true);
32
+ }
33
+ }
34
+ });
35
+
36
+ it("should contain an abstract method MyAbstractClass.abstractMethod2", () => {
37
+ expect(theMethods).toBeTruthy();
38
+ if (theMethods) {
39
+ expect(theMethods.size).toBe(3);
40
+ const foundMethodName = fmxRep._getFamixMethod("abstractMethod2") as Method;
41
+ expect(foundMethodName.getIsAbstract()).toBe(true);
42
+ }
43
+ });
44
+
45
+ it("should contain a concrete method MyAbstractClass.concreteMethod", () => {
46
+ expect(theMethods).toBeTruthy();
47
+ if (theMethods) {
48
+ expect(theMethods.size).toBe(3);
49
+ const foundMethodName = fmxRep._getFamixMethod("concreteMethod") as Method;
50
+ expect(foundMethodName.getIsAbstract()).toBe(false);
51
+ }
52
+ });
53
+ });
@@ -0,0 +1,62 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Property, Method } from "../src/lib/famix/src/model/famix";
3
+
4
+ const importer = new Importer();
5
+
6
+ const fmxRep = importer.famixRepFromSource("access",
7
+ 'class AccessClassForTesting {\n\
8
+ private privateAttribute;\n\
9
+ public publicAttribute;\n\
10
+ \n\
11
+ public returnAccessName() {\n\
12
+ return this.publicAttribute;\n\
13
+ }\n\
14
+ \n\
15
+ private privateMethod() {\n\
16
+ return this.privateAttribute;\n\
17
+ }\n\
18
+ }\n\
19
+ ');
20
+
21
+ describe('Accesses', () => {
22
+
23
+ const jsonOutput = fmxRep.getJSON();
24
+ const parsedModel = JSON.parse(jsonOutput);
25
+ let testAccessCls;
26
+ let accessClsMethods;
27
+ let accessClsAttributes;
28
+
29
+ it("should have a class with two methods and two attributes", () => {
30
+ const expectedAttributeNames: Array<string> = ['privateAttribute', 'publicAttribute'];
31
+ const expectedMethodNames: Array<string> = ['privateMethod', 'returnAccessName'];
32
+ testAccessCls = parsedModel.filter(el => (el.FM3 === "FamixTypeScript.Class" && el.name === "AccessClassForTesting"))[0];
33
+ expect(testAccessCls.properties.length).toBe(expectedAttributeNames.length);
34
+ expect(testAccessCls.methods.length).toBe(expectedMethodNames.length);
35
+ accessClsMethods = parsedModel.filter(e => testAccessCls.methods.some(m => m.ref === e.id));
36
+ expect(accessClsMethods.length).toBeGreaterThan(0);
37
+ const checkMethodName = accessClsMethods.every(m => expectedMethodNames.includes(m.name));
38
+ expect(checkMethodName).toBe(true);
39
+ accessClsAttributes = parsedModel.filter(e => testAccessCls.properties.some(a => a.ref === e.id));
40
+ expect(accessClsAttributes.length).toBeGreaterThan(0);
41
+ const checkAttributeName = accessClsAttributes.every(a => expectedAttributeNames.includes(a.name));
42
+ expect(checkAttributeName).toBe(true);
43
+ });
44
+
45
+ it("should have an access to privateAttribute in privateMethod", () => {
46
+ const famixAccess = parsedModel.filter(el =>
47
+ (el.accessor !== undefined && el.variable !== undefined && el.FM3 === "FamixTypeScript.Access"
48
+ && ((fmxRep.getFamixEntityById(el.accessor.ref) as Method).getName() === "privateMethod")
49
+ && ((fmxRep.getFamixEntityById(el.variable.ref) as Property).getName() === "privateAttribute")
50
+ ))[0];
51
+ expect(famixAccess).toBeTruthy();
52
+ });
53
+
54
+ it("should have an access to publicAttribute in returnAccessName", () => {
55
+ const famixAccess = parsedModel.filter(el =>
56
+ (el.accessor !== undefined && el.variable !== undefined && el.FM3 === "FamixTypeScript.Access"
57
+ && ((fmxRep.getFamixEntityById(el.accessor.ref) as Method).getName() === "returnAccessName")
58
+ && ((fmxRep.getFamixEntityById(el.variable.ref) as Property).getName() === "publicAttribute")
59
+ ))[0];
60
+ expect(famixAccess).toBeTruthy();
61
+ });
62
+ });
@@ -0,0 +1,42 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Access } from '../src/lib/famix/src/model/famix/access';
3
+ import { Parameter } from '../src/lib/famix/src/model/famix/parameter';
4
+ import { Variable } from '../src/lib/famix/src/model/famix/variable';
5
+
6
+ const importer = new Importer();
7
+
8
+ const fmxRep = importer.famixRepFromSource("accesses",
9
+ 'var b = 2;\n\
10
+ \n\
11
+ var x = b;\n\
12
+ \n\
13
+ class P {\n\
14
+ m(param) {\n\
15
+ var z = param;\n\
16
+ }\n\
17
+ }\n\
18
+ ');
19
+
20
+ describe('Tests for accesses', () => {
21
+
22
+ it("should contain one class", () => {
23
+ expect(fmxRep._getAllEntitiesWithType("Class").size).toBe(1);
24
+ });
25
+
26
+ const theFile = fmxRep._getFamixFile("accesses.ts");
27
+ const theMethod = fmxRep._getFamixMethod("m");
28
+
29
+ it("should contain one access to 'b'", () => {
30
+ const theVariable = Array.from(fmxRep._getAllEntitiesWithType("Variable") as Set<Variable>).find(v => v.getName() === "b");
31
+ const theAccess = Array.from(fmxRep._getAllEntitiesWithType("Access") as Set<Access>).find(a => a.getVariable() === theVariable && a.getAccessor() === theFile);
32
+ expect(theAccess).toBeTruthy();
33
+ expect(theAccess?.getIsWrite()).toBe(undefined);
34
+ });
35
+
36
+ it("should contain one access to 'param'", () => {
37
+ const theParam = Array.from(fmxRep._getAllEntitiesWithType("Parameter") as Set<Parameter>).find(v => v.getName() === "param");
38
+ const theAccess = Array.from(fmxRep._getAllEntitiesWithType("Access") as Set<Access>).find(a => a.getVariable() === theParam && a.getAccessor() === theMethod);
39
+ expect(theAccess).toBeTruthy();
40
+ expect(theAccess?.getIsWrite()).toBe(undefined);
41
+ });
42
+ });
@@ -0,0 +1,98 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Access } from '../src/lib/famix/src/model/famix/access';
3
+ import { Decorator } from '../src/lib/famix/src/model/famix/decorator';
4
+ import { Property } from '../src/lib/famix/src/model/famix/property';
5
+
6
+ const importer = new Importer();
7
+
8
+ const fmxRep = importer.famixRepFromSource("accessorsWithDecorators",
9
+ 'function configurable(value: boolean) {\n\
10
+ return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {\n\
11
+ descriptor.configurable = value;\n\
12
+ };\n\
13
+ }\n\
14
+ \n\
15
+ var b = function() {\n\
16
+ return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {};\n\
17
+ };\n\
18
+ \n\
19
+ var x = b();\n\
20
+ \n\
21
+ class Point {\n\
22
+ private _x: number;\n\
23
+ private _y: number;\n\
24
+ \n\
25
+ constructor(x: number, y: number) {\n\
26
+ this._x = x;\n\
27
+ this._y = y;\n\
28
+ }\n\
29
+ \n\
30
+ @x\n\
31
+ @b()\n\
32
+ @configurable(false)\n\
33
+ get x() {\n\
34
+ return this._x;\n\
35
+ }\n\
36
+ \n\
37
+ @x\n\
38
+ @b()\n\
39
+ @configurable(false)\n\
40
+ set y(y: number) {\n\
41
+ this._y = y;\n\
42
+ }\n\
43
+ }\n\
44
+ ');
45
+
46
+ describe('Tests for accessors with decorators', () => {
47
+
48
+ it("should contain one class", () => {
49
+ expect(fmxRep._getAllEntitiesWithType("Class").size).toBe(1);
50
+ });
51
+
52
+ it("should contain six decorators", () => {
53
+ expect(fmxRep._getAllEntitiesWithType("Decorator").size).toBe(6);
54
+ });
55
+
56
+ const theMethod1 = fmxRep._getFamixMethod("x");
57
+ const theMethod2 = fmxRep._getFamixMethod("y");
58
+ const d1 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).filter((d) => d.getName() === "@x");
59
+ const d2 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).filter((d) => d.getName() === "@b");
60
+ const d3 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).filter((d) => d.getName() === "@configurable");
61
+
62
+ it("should contain two accessors with three decorators for each one", () => {
63
+ expect(fmxRep._getAllEntitiesWithType("Accessor").size).toBe(2);
64
+ expect(fmxRep._getAllEntitiesWithType("Decorator").size).toBe(6);
65
+
66
+ expect(theMethod1?.getDecorators().size).toBe(3);
67
+ expect(d1[0]?.getDecoratedEntity()).toBe(theMethod1);
68
+ expect(d2[0]?.getDecoratedEntity()).toBe(theMethod1);
69
+ expect(d3[0]?.getDecoratedEntity()).toBe(theMethod1);
70
+
71
+ expect(theMethod2?.getDecorators().size).toBe(3);
72
+ expect(d1[1]?.getDecoratedEntity()).toBe(theMethod2);
73
+ expect(d2[1]?.getDecoratedEntity()).toBe(theMethod2);
74
+ expect(d3[1]?.getDecoratedEntity()).toBe(theMethod2);
75
+ });
76
+
77
+ it("should contain two accesses to '_x'", () => {
78
+ const theProperty = Array.from(fmxRep._getAllEntitiesWithType("Property") as Set<Property>).find(v => v.getName() === "_x");
79
+ const theMethod1 = fmxRep._getFamixMethod("constructor");
80
+ const theMethod2 = fmxRep._getFamixMethod("x");
81
+ expect(theMethod2?.getKind()).toBe("getter");
82
+ const theAccess1 = Array.from(fmxRep._getAllEntitiesWithType("Access") as Set<Access>).find(a => a.getVariable() === theProperty && a.getAccessor() === theMethod1);
83
+ const theAccess2 = Array.from(fmxRep._getAllEntitiesWithType("Access") as Set<Access>).find(a => a.getVariable() === theProperty && a.getAccessor() === theMethod2);
84
+ expect(theAccess1).toBeTruthy();
85
+ expect(theAccess2).toBeTruthy();
86
+ });
87
+
88
+ it("should contain two accesses to '_y'", () => {
89
+ const theProperty = Array.from(fmxRep._getAllEntitiesWithType("Property") as Set<Property>).find(v => v.getName() === "_y");
90
+ const theMethod1 = fmxRep._getFamixMethod("constructor");
91
+ const theMethod2 = fmxRep._getFamixMethod("y");
92
+ expect(theMethod2?.getKind()).toBe("setter");
93
+ const theAccess1 = Array.from(fmxRep._getAllEntitiesWithType("Access") as Set<Access>).find(a => a.getVariable() === theProperty && a.getAccessor() === theMethod1);
94
+ const theAccess2 = Array.from(fmxRep._getAllEntitiesWithType("Access") as Set<Access>).find(a => a.getVariable() === theProperty && a.getAccessor() === theMethod2);
95
+ expect(theAccess1).toBeTruthy();
96
+ expect(theAccess2).toBeTruthy();
97
+ });
98
+ });
@@ -0,0 +1,39 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Alias } from '../src/lib/famix/src/model/famix';
3
+ import { Type } from '../src/lib/famix/src/model/famix';
4
+
5
+ const importer = new Importer();
6
+
7
+ const fmxRep = importer.famixRepFromSource("alias",
8
+ 'type Point = {\n\
9
+ x: number;\n\
10
+ y: number;\n\
11
+ };\n\
12
+ ');
13
+
14
+ describe('Tests for alias', () => {
15
+
16
+ it("should contain one alias", () => {
17
+ expect(fmxRep._getAllEntitiesWithType("Alias").size).toBe(1);
18
+ });
19
+
20
+ const theAlias = Array.from(fmxRep._getAllEntitiesWithType("Alias") as Set<Alias>)[0];
21
+ it("should contain an alias Point", () => {
22
+ expect(theAlias.getName()).toBe("Point");
23
+ });
24
+
25
+ const theType = Array.from(fmxRep._getAllEntitiesWithType("Type") as Set<Type>)[0];
26
+ it("should contain a type Point", () => {
27
+ expect(theType.getName()).toBe("Point");
28
+ });
29
+
30
+ const theFile = fmxRep._getFamixFile("alias.ts");
31
+ it("should contain an alias on type Point", () => {
32
+ expect(theFile?.getAliases().size).toBe(1);
33
+ expect(Array.from(theFile?.getAliases() as Set<Alias>)[0]).toBe(theAlias);
34
+ expect(theAlias.getAliasedEntity()).toBe(theType);
35
+ expect(theAlias.getParentEntity()).toBe(theFile);
36
+ expect(theType.getTypeAliases().size).toBe(1);
37
+ expect(Array.from(theType.getTypeAliases())[0]).toBe(theAlias);
38
+ });
39
+ });
@@ -0,0 +1,41 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Interface } from '../src/lib/famix/src/model/famix';
3
+
4
+ const importer = new Importer();
5
+
6
+ const fmxRep = importer.famixRepFromSource("classExtendsUndefinedClass",
7
+ 'import {ClassDeclaration} from "ts-morph";\n\
8
+ \n\
9
+ class myClass extends ClassDeclaration {}\n\
10
+ ');
11
+
12
+ describe('Tests for class extends undefined class', () => {
13
+
14
+ it("should contain two class", () => {
15
+ expect(fmxRep._getAllEntitiesWithType("Class").size).toBe(2);
16
+ });
17
+
18
+ it("should contain a class myClass that extends a class ClassDeclaration", () => {
19
+ const cList = Array.from(fmxRep._getAllEntitiesWithType("Class") as Set<Interface>);
20
+ expect(cList).toBeTruthy();
21
+ const myClass1 = cList.find(p => p.getName() === "ClassDeclaration");
22
+ expect(myClass1).toBeTruthy();
23
+ expect(myClass1?.getIsStub()).toBe(true);
24
+ const myClass2 = cList.find(p => p.getName() === "myClass");
25
+ expect(myClass2).toBeTruthy();
26
+ if (myClass2) {
27
+ expect(myClass2.getSubInheritances().size).toBe(0);
28
+ expect(myClass2.getSuperInheritances().size).toBe(1);
29
+ const theInheritance = (Array.from(myClass2.getSuperInheritances())[0]);
30
+ expect(theInheritance.getSuperclass()).toBeTruthy();
31
+ expect(theInheritance.getSuperclass()).toBe(myClass1);
32
+ }
33
+ if (myClass1) {
34
+ expect(myClass1.getSubInheritances().size).toBe(1);
35
+ expect(myClass1.getSuperInheritances().size).toBe(0);
36
+ const theInheritance = (Array.from(myClass1.getSubInheritances())[0]);
37
+ expect(theInheritance.getSubclass()).toBeTruthy();
38
+ expect(theInheritance.getSubclass()).toBe(myClass2);
39
+ }
40
+ });
41
+ });
@@ -0,0 +1,45 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Class } from '../src/lib/famix/src/model/famix/class';
3
+ import { Interface } from '../src/lib/famix/src/model/famix/interface';
4
+
5
+ const importer = new Importer();
6
+
7
+ const fmxRep = importer.famixRepFromSource("classImplementsUndefinedInterface",
8
+ 'import {FileSystemHost} from "ts-morph";\n\
9
+ \n\
10
+ class myClass implements FileSystemHost {}\n\
11
+ ');
12
+
13
+ describe('Tests for class implements undefined interface', () => {
14
+
15
+ it("should contain one class and one interface", () => {
16
+ expect(fmxRep._getAllEntitiesWithType("Class").size).toBe(1);
17
+ expect(fmxRep._getAllEntitiesWithType("Interface").size).toBe(1);
18
+ });
19
+
20
+ it("should contain an interface myClass that extends an interface FileSystemHost", () => {
21
+ const cList = Array.from(fmxRep._getAllEntitiesWithType("Class") as Set<Class>);
22
+ const iList = Array.from(fmxRep._getAllEntitiesWithType("Interface") as Set<Interface>);
23
+ expect(cList).toBeTruthy();
24
+ expect(iList).toBeTruthy();
25
+ const myInterface1 = iList.find(p => p.getName() === "FileSystemHost");
26
+ expect(myInterface1).toBeTruthy();
27
+ expect(myInterface1?.getIsStub()).toBe(true);
28
+ const myClass = cList.find(p => p.getName() === "myClass");
29
+ expect(myClass).toBeTruthy();
30
+ if (myClass) {
31
+ expect(myClass.getSubInheritances().size).toBe(0);
32
+ expect(myClass.getSuperInheritances().size).toBe(1);
33
+ const theInheritance = (Array.from(myClass.getSuperInheritances())[0]);
34
+ expect(theInheritance.getSuperclass()).toBeTruthy();
35
+ expect(theInheritance.getSuperclass()).toBe(myInterface1);
36
+ }
37
+ if (myInterface1) {
38
+ expect(myInterface1.getSubInheritances().size).toBe(1);
39
+ expect(myInterface1.getSuperInheritances().size).toBe(0);
40
+ const theInheritance = (Array.from(myInterface1.getSubInheritances())[0]);
41
+ expect(theInheritance.getSubclass()).toBeTruthy();
42
+ expect(theInheritance.getSubclass()).toBe(myClass);
43
+ }
44
+ });
45
+ });
@@ -0,0 +1,65 @@
1
+ import { Importer } from '../src/analyze';
2
+ import { Decorator } from '../src/lib/famix/src/model/famix/decorator';
3
+
4
+ const importer = new Importer();
5
+
6
+ const fmxRep = importer.famixRepFromSource("classWithDecorators",
7
+ 'function sealed(constructor: Function) { // function can\'t take other parameters with constructor\n\
8
+ Object.seal(constructor);\n\
9
+ Object.seal(constructor.prototype);\n\
10
+ }\n\
11
+ \n\
12
+ function reportableClassDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {\n\
13
+ return class extends constructor {\n\
14
+ reportingURL = "http://www...";\n\
15
+ };\n\
16
+ }\n\
17
+ \n\
18
+ // the signature is different from the one of the other decorators\n\
19
+ var d = function decoratorWithParameter(parameter: string) {\n\
20
+ return function (target: Function) {\n\
21
+ // use parameter in decorator behavior\n\
22
+ console.log("Decorator parameter :", parameter);\n\
23
+ };\n\
24
+ };\n\
25
+ \n\
26
+ var c = d("param2");\n\
27
+ \n\
28
+ @c\n\
29
+ @d("param")\n\
30
+ @sealed\n\
31
+ @reportableClassDecorator\n\
32
+ class BugReport {\n\
33
+ type = "report";\n\
34
+ title: string;\n\
35
+ \n\
36
+ constructor(t: string) {\n\
37
+ this.title = t;\n\
38
+ }\n\
39
+ }\n\
40
+ ');
41
+
42
+ describe('Tests for class with decorators', () => {
43
+
44
+ it("should contain one class", () => {
45
+ expect(fmxRep._getAllEntitiesWithType("Class").size).toBe(1);
46
+ });
47
+
48
+ it("should contain four decorators", () => {
49
+ expect(fmxRep._getAllEntitiesWithType("Decorator").size).toBe(4);
50
+ });
51
+
52
+ const theClass = fmxRep._getFamixClass("BugReport");
53
+ const d1 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).find((d) => d.getName() === "@c");
54
+ const d2 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).find((d) => d.getName() === "@d");
55
+ const d3 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).find((d) => d.getName() === "@sealed");
56
+ const d4 = (Array.from(fmxRep._getAllEntitiesWithType("Decorator")) as Array<Decorator>).find((d) => d.getName() === "@reportableClassDecorator");
57
+
58
+ it("should contain a class with four decorators", () => {
59
+ expect(theClass?.getDecorators().size).toBe(4);
60
+ expect(d1?.getDecoratedEntity()).toBe(theClass);
61
+ expect(d2?.getDecoratedEntity()).toBe(theClass);
62
+ expect(d3?.getDecoratedEntity()).toBe(theClass);
63
+ expect(d4?.getDecoratedEntity()).toBe(theClass);
64
+ });
65
+ });