ts2famix 1.0.1 → 1.0.5

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 (121) hide show
  1. package/dist/analyze.js +102 -0
  2. package/dist/analyze_functions/processAccesses.js +47 -0
  3. package/dist/analyze_functions/processFiles.js +540 -0
  4. package/dist/analyze_functions/processImportClauses.js +70 -0
  5. package/dist/analyze_functions/processInheritances.js +73 -0
  6. package/dist/analyze_functions/processInvocations.js +49 -0
  7. package/dist/famix2puml.js +125 -0
  8. package/dist/famix_functions/famix_functions.js +513 -0
  9. package/dist/famix_functions/famix_functions_associations.js +205 -0
  10. package/dist/famix_functions/famix_functions_index.js +62 -0
  11. package/dist/famix_functions/famix_functions_types.js +110 -0
  12. package/dist/fqn.js +126 -0
  13. package/dist/fqp_implementation.js +73 -0
  14. package/dist/lib/famix/src/famix_JSON_exporter.js +54 -0
  15. package/dist/lib/famix/src/famix_base_element.js +13 -0
  16. package/dist/lib/famix/src/famix_repository.js +187 -0
  17. package/dist/lib/famix/src/index.js +30 -0
  18. package/dist/lib/famix/src/model/famix/access.js +39 -0
  19. package/dist/lib/famix/src/model/famix/accessor.js +16 -0
  20. package/dist/lib/famix/src/model/famix/alias.js +32 -0
  21. package/dist/lib/famix/src/model/famix/association.js +36 -0
  22. package/dist/lib/famix/src/model/famix/behavioral_entity.js +81 -0
  23. package/dist/lib/famix/src/model/famix/c_source_language.js +16 -0
  24. package/dist/lib/famix/src/model/famix/class.js +70 -0
  25. package/dist/lib/famix/src/model/famix/comment.js +38 -0
  26. package/dist/lib/famix/src/model/famix/container_entity.js +125 -0
  27. package/dist/lib/famix/src/model/famix/custom_source_language.js +23 -0
  28. package/dist/lib/famix/src/model/famix/decorator.js +31 -0
  29. package/dist/lib/famix/src/model/famix/entity.js +16 -0
  30. package/dist/lib/famix/src/model/famix/enum.js +30 -0
  31. package/dist/lib/famix/src/model/famix/enum_value.js +24 -0
  32. package/dist/lib/famix/src/model/famix/function.js +16 -0
  33. package/dist/lib/famix/src/model/famix/implicit_variable.js +16 -0
  34. package/dist/lib/famix/src/model/famix/import_clause.js +39 -0
  35. package/dist/lib/famix/src/model/famix/index.js +87 -0
  36. package/dist/lib/famix/src/model/famix/indexed_file_anchor.js +37 -0
  37. package/dist/lib/famix/src/model/famix/inheritance.js +32 -0
  38. package/dist/lib/famix/src/model/famix/interface.js +63 -0
  39. package/dist/lib/famix/src/model/famix/invocation.js +53 -0
  40. package/dist/lib/famix/src/model/famix/method.js +66 -0
  41. package/dist/lib/famix/src/model/famix/module.js +30 -0
  42. package/dist/lib/famix/src/model/famix/named_entity.js +77 -0
  43. package/dist/lib/famix/src/model/famix/namespace.js +24 -0
  44. package/dist/lib/famix/src/model/famix/parameter.js +24 -0
  45. package/dist/lib/famix/src/model/famix/parameterizable_class.js +30 -0
  46. package/dist/lib/famix/src/model/famix/parameterizable_interface.js +30 -0
  47. package/dist/lib/famix/src/model/famix/parameterized_type.js +36 -0
  48. package/dist/lib/famix/src/model/famix/primitive_type.js +16 -0
  49. package/dist/lib/famix/src/model/famix/property.js +44 -0
  50. package/dist/lib/famix/src/model/famix/reference.js +32 -0
  51. package/dist/lib/famix/src/model/famix/scoping_entity.js +30 -0
  52. package/dist/lib/famix/src/model/famix/script_entity.js +30 -0
  53. package/dist/lib/famix/src/model/famix/source_anchor.js +26 -0
  54. package/dist/lib/famix/src/model/famix/source_language.js +30 -0
  55. package/dist/lib/famix/src/model/famix/sourced_entity.js +55 -0
  56. package/dist/lib/famix/src/model/famix/structural_entity.js +38 -0
  57. package/dist/lib/famix/src/model/famix/text_anchor.js +37 -0
  58. package/dist/lib/famix/src/model/famix/type.js +71 -0
  59. package/dist/lib/famix/src/model/famix/type_parameter.js +24 -0
  60. package/dist/lib/famix/src/model/famix/variable.js +23 -0
  61. package/dist/lib/ts-complex/cyclomatic-service.js +83 -0
  62. package/dist/ts2famix-cli.js +51 -0
  63. package/dist/ts2famix-tsconfig.js +53 -0
  64. package/doc-uml/.gitkeep +0 -0
  65. package/docs/.gitkeep +0 -0
  66. package/jest.config.json +10 -0
  67. package/package.json +1 -1
  68. package/tsconfig.json +1 -1
  69. package/.github/workflows/node.js.yml +0 -60
  70. package/doc-metamodel/skins.include.puml +0 -2
  71. package/test/abstractClassWithComments.test.ts +0 -58
  72. package/test/abstracts.test.ts +0 -53
  73. package/test/access.test.ts +0 -62
  74. package/test/accesses.test.ts +0 -42
  75. package/test/accessorsWithDecorators.test.ts +0 -98
  76. package/test/alias.test.ts +0 -39
  77. package/test/classExtendsUndefinedClass.test.ts +0 -41
  78. package/test/classImplementsUndefinedInterface.test.ts +0 -45
  79. package/test/classWithDecorators.test.ts +0 -65
  80. package/test/entities.test.ts +0 -232
  81. package/test/entities_json.test.ts +0 -48
  82. package/test/enum.test.ts +0 -55
  83. package/test/functionReturnsFunction.test.ts +0 -53
  84. package/test/functionWithParameters.test.ts +0 -38
  85. package/test/functionWithVariables.test.ts +0 -64
  86. package/test/functions.test.ts +0 -23
  87. package/test/functionsInFunction.test.ts +0 -40
  88. package/test/functionsInMethod.test.ts +0 -42
  89. package/test/genericClass.test.ts +0 -42
  90. package/test/genericClassInheritsInterface.test.ts +0 -47
  91. package/test/genericInterface.test.ts +0 -38
  92. package/test/genericMethod.test.ts +0 -65
  93. package/test/genericWithInvocation.test.ts +0 -71
  94. package/test/generics.test.ts +0 -68
  95. package/test/inheritance.test.ts +0 -50
  96. package/test/interfaceInheritsInterface.test.ts +0 -40
  97. package/test/interfaceInheritsUndefinedInterface.test.ts +0 -41
  98. package/test/invocation.test.ts +0 -94
  99. package/test/invocationWithFunction.test.ts +0 -42
  100. package/test/invocationWithVariable.test.ts +0 -46
  101. package/test/invocation_json.test.ts +0 -63
  102. package/test/invocations.test.ts +0 -131
  103. package/test/jsDoc.test.ts +0 -31
  104. package/test/methodWithDecorator.test.ts +0 -44
  105. package/test/methods.test.ts +0 -42
  106. package/test/metrics.test.ts +0 -51
  107. package/test/module.test.ts +0 -71
  108. package/test/namespaces.test.ts +0 -54
  109. package/test/namespacesAndClasses.test.ts +0 -66
  110. package/test/parameterWithDecorators.test.ts +0 -54
  111. package/test/propertyWithDecorators.test.ts +0 -80
  112. package/test/sample.test.ts +0 -13
  113. package/test/simpleFunction.test.ts +0 -32
  114. package/test/simpleTest.test.ts +0 -18
  115. package/test/simpleTest2.test.ts +0 -36
  116. package/test/types.test.ts +0 -58
  117. package/test_src/sample.ts +0 -103
  118. package/test_src/sampleForModule.ts +0 -10
  119. package/test_src/sampleForModule2.ts +0 -7
  120. package/test_src/sampleForModule3.ts +0 -2
  121. /package/{jest.config.ts → jest.config-old.ts} +0 -0
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ /*
3
+ Portions of this file:
4
+
5
+ MIT License
6
+
7
+ Copyright (c) 2018 Anand Undavia
8
+
9
+ 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:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+
13
+ 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.
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.calculate = void 0;
17
+ const typescript_1 = require("typescript");
18
+ const tsutils_1 = require("tsutils");
19
+ const fs_1 = require("fs");
20
+ const { isIdentifier } = require('typescript');
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
+ const increasesComplexity = (node) => {
27
+ /* eslint-disable indent */
28
+ switch (node.kind) {
29
+ case typescript_1.SyntaxKind.CaseClause:
30
+ return node.statements.length > 0;
31
+ case typescript_1.SyntaxKind.CatchClause:
32
+ case typescript_1.SyntaxKind.ConditionalExpression:
33
+ case typescript_1.SyntaxKind.DoStatement:
34
+ case typescript_1.SyntaxKind.ForStatement:
35
+ case typescript_1.SyntaxKind.ForInStatement:
36
+ case typescript_1.SyntaxKind.ForOfStatement:
37
+ case typescript_1.SyntaxKind.IfStatement:
38
+ case typescript_1.SyntaxKind.WhileStatement:
39
+ return true;
40
+ case typescript_1.SyntaxKind.BinaryExpression:
41
+ switch (node.operatorToken.kind) {
42
+ case typescript_1.SyntaxKind.BarBarToken:
43
+ case typescript_1.SyntaxKind.AmpersandAmpersandToken:
44
+ return true;
45
+ default:
46
+ return false;
47
+ }
48
+ default:
49
+ return false;
50
+ }
51
+ /* eslint-enable indent */
52
+ };
53
+ const calculateFromSource = (ctx) => {
54
+ let complexity = 0;
55
+ const output = {};
56
+ (0, typescript_1.forEachChild)(ctx, function cb(node) {
57
+ var _a;
58
+ if ((0, tsutils_1.isFunctionWithBody)(node)) {
59
+ const old = complexity;
60
+ complexity = 1;
61
+ (0, typescript_1.forEachChild)(node, cb);
62
+ const name = (_a = getNodeName(node)) === null || _a === void 0 ? void 0 : _a.toString();
63
+ output[name] = complexity;
64
+ complexity = old;
65
+ }
66
+ else {
67
+ if (increasesComplexity(node)) {
68
+ complexity += 1;
69
+ }
70
+ (0, typescript_1.forEachChild)(node, cb);
71
+ }
72
+ });
73
+ return output;
74
+ };
75
+ const calculate = (filePath) => {
76
+ if (!(0, fs_1.existsSync)(filePath)) {
77
+ throw new Error(`File "${filePath}" does not exists`);
78
+ }
79
+ const sourceText = (0, fs_1.readFileSync)(filePath).toString();
80
+ const source = (0, typescript_1.createSourceFile)(filePath, sourceText, isIdentifier.ES2015);
81
+ return calculateFromSource(source);
82
+ };
83
+ exports.calculate = calculate;
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __importDefault = (this && this.__importDefault) || function (mod) {
27
+ return (mod && mod.__esModule) ? mod : { "default": mod };
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ const fs = __importStar(require("fs"));
31
+ const yargs_1 = __importDefault(require("yargs"));
32
+ const analyze_1 = require("./analyze");
33
+ const importer = new analyze_1.Importer();
34
+ const argv = yargs_1.default
35
+ .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')
36
+ .alias('i', 'input')
37
+ .nargs('i', 1)
38
+ .alias('o', 'output')
39
+ .nargs('o', 1)
40
+ .demandOption('input').demandOption('output').parseSync();
41
+ const paths = new Array();
42
+ paths.push(argv.input);
43
+ const famixRep = importer.famixRepFromPaths(paths);
44
+ const jsonOutput = famixRep.getJSON();
45
+ const jsonFilePath = argv.output;
46
+ fs.writeFile(jsonFilePath, jsonOutput, (err) => {
47
+ if (err) {
48
+ throw err;
49
+ }
50
+ });
51
+ console.info(`Created: ${jsonFilePath}`);
@@ -0,0 +1,53 @@
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
+ const fs = __importStar(require("fs"));
30
+ const yargs_1 = __importDefault(require("yargs"));
31
+ const analyze_1 = require("./analyze");
32
+ const ts_morph_1 = require("ts-morph");
33
+ const importer = new analyze_1.Importer();
34
+ const argv = yargs_1.default
35
+ .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')
36
+ .alias('i', 'input')
37
+ .nargs('i', 1)
38
+ .alias('o', 'output')
39
+ .nargs('o', 1)
40
+ .demandOption('input').demandOption('output').parseSync();
41
+ const tsConfigFilePath = argv.input;
42
+ const project = new ts_morph_1.Project({
43
+ tsConfigFilePath
44
+ });
45
+ const famixRep = importer.famixRepFromProject(project);
46
+ const jsonOutput = famixRep.getJSON();
47
+ const jsonFilePath = argv.output;
48
+ fs.writeFile(jsonFilePath, jsonOutput, (err) => {
49
+ if (err) {
50
+ throw err;
51
+ }
52
+ });
53
+ console.info(`Created: ${jsonFilePath}`);
File without changes
package/docs/.gitkeep ADDED
File without changes
@@ -0,0 +1,10 @@
1
+ {
2
+ "collectCoverage": false,
3
+ "coverageDirectory": "coverage",
4
+ "coverageProvider": "babel",
5
+ "transform": {
6
+ "^.+\\.tsx?$": ["ts-jest", { "isolatedModules": true }]
7
+ },
8
+ "testEnvironment": "jest-environment-node",
9
+ "verbose": true
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts2famix",
3
- "version": "1.0.1",
3
+ "version": "1.0.5",
4
4
  "description": "Examples of the TypeScript compiler API usage",
5
5
  "main": "dist/ts2famix-cli.js",
6
6
  "scripts": {
package/tsconfig.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "compilerOptions": {
3
3
  /* Basic Options */
4
4
  "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
5
- "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
5
+ "module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6
6
  // "lib": [], /* Specify library files to be included in the compilation. */
7
7
  // "allowJs": true, /* Allow javascript files to be compiled. */
8
8
  // "checkJs": true, /* Report errors in .js files. */
@@ -1,60 +0,0 @@
1
- # This workflow will do a clean install of node dependencies, cache/restore them, and run tests across different versions of node
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
-
4
- name: Node.js CI
5
-
6
- permissions: write-all
7
-
8
- on:
9
- push:
10
- branches: [ master ]
11
- pull_request:
12
- branches: [ master ]
13
-
14
- # Allows you to run this workflow manually from the Actions tab
15
- workflow_dispatch:
16
-
17
- jobs:
18
- build:
19
-
20
- runs-on: ubuntu-latest
21
-
22
- strategy:
23
- matrix:
24
- # node-version: [12.x, 14.x, 16.x]
25
- node-version: [16.x]
26
- # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
27
-
28
- steps:
29
- - uses: actions/checkout@v3
30
- - uses: ts-graphviz/setup-graphviz@v1
31
- - name: Use Node.js ${{ matrix.node-version }}
32
- uses: actions/setup-node@v3
33
- with:
34
- node-version: ${{ matrix.node-version }}
35
- cache: 'npm'
36
- - run: npm ci
37
- - run: npm test
38
- - run: npm run uml
39
-
40
- - name: Init new repo in doc-uml folder and commit generated files
41
- run: |
42
- cd doc-uml/
43
- git init
44
- git add -A
45
- git config --local user.email "action@github.com"
46
- git config --local user.name "GitHub Action"
47
- git commit -m 'update doc'
48
-
49
- # Careful, this can kill your project, because it force pushes to the destination branch
50
- - name: Force push to destination branch
51
- uses: ad-m/github-push-action@master
52
- with:
53
- # Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}
54
- github_token: ${{ secrets.GITHUB_TOKEN }}
55
- force: true
56
- # Destination branch to push changes
57
- branch: v1/doc
58
- # We have to push from the folder where files were generated.
59
- # Same were the new repo was initialized in the previous step
60
- directory: ./doc-uml
@@ -1,2 +0,0 @@
1
- ' put skinparam customizations here
2
- hide members
@@ -1,58 +0,0 @@
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
- });
@@ -1,53 +0,0 @@
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
- });
@@ -1,62 +0,0 @@
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
- });
@@ -1,42 +0,0 @@
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
- });
@@ -1,98 +0,0 @@
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
- });
@@ -1,39 +0,0 @@
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
- });