cognitive-complexity-ts 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,20 @@
1
+ export type NodeKind = "class" | "file" | "function" | "module" | "type";
1
2
  export interface ColumnAndLine {
2
3
  column: number;
3
4
  line: number;
4
5
  }
5
6
  export interface FunctionNodeInfo extends ColumnAndLine {
7
+ kind: "function";
6
8
  name: string;
7
9
  }
8
- export interface ContainerOutput extends FunctionNodeInfo {
10
+ export interface ContainerOutput extends ColumnAndLine {
11
+ kind: "class" | "function" | "module" | "type";
12
+ name: string;
9
13
  score: number;
10
14
  inner: ContainerOutput[];
11
15
  }
12
16
  export interface FileOutput {
17
+ kind: "file";
13
18
  score: number;
14
19
  inner: ContainerOutput[];
15
20
  }
@@ -50,7 +50,10 @@ function fileCost(file) {
50
50
  precedingOperator: undefined,
51
51
  precedingTypeOperator: undefined,
52
52
  };
53
- return nodeCost(file, initialContext, initialMutableContext);
53
+ return {
54
+ kind: "file",
55
+ ...nodeCost(file, initialContext, initialMutableContext),
56
+ };
54
57
  }
55
58
  function aggregateCostOfChildren(children, ctx, mutCtx) {
56
59
  let score = 0;
@@ -65,6 +68,7 @@ function aggregateCostOfChildren(children, ctx, mutCtx) {
65
68
  const name = (0, node_naming_1.chooseContainerName)(child, ctx.variableBeingDefined);
66
69
  if (name !== undefined) {
67
70
  inner.push({
71
+ kind: (0, node_inspection_1.getNodeKind)(child),
68
72
  ...(0, node_inspection_1.getColumnAndLine)(child),
69
73
  ...childCost,
70
74
  name,
@@ -5,6 +5,7 @@ export type FunctionNode = ts.AccessorDeclaration | ts.ArrowFunction | ts.Functi
5
5
  export declare function getColumnAndLine(node: ts.Node): ColumnAndLine;
6
6
  export declare function getIdentifier(node: ts.Node): string | undefined;
7
7
  export declare function getFirstNonParenthesizedAncestor(node: ts.Node): ts.Node;
8
+ export declare function getNodeKind(node: ts.Node): "class" | "function" | "module" | "type" | undefined;
8
9
  export declare function getTextWithoutBrackets(node: ts.Node): string;
9
10
  export declare function isBreakOrContinueToLabel(node: ts.Node): boolean;
10
11
  export declare function isContainer(node: ts.Node): boolean;
@@ -36,6 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.getColumnAndLine = getColumnAndLine;
37
37
  exports.getIdentifier = getIdentifier;
38
38
  exports.getFirstNonParenthesizedAncestor = getFirstNonParenthesizedAncestor;
39
+ exports.getNodeKind = getNodeKind;
39
40
  exports.getTextWithoutBrackets = getTextWithoutBrackets;
40
41
  exports.isBreakOrContinueToLabel = isBreakOrContinueToLabel;
41
42
  exports.isContainer = isContainer;
@@ -76,6 +77,21 @@ function getFirstNonParenthesizedAncestor(node) {
76
77
  }
77
78
  return firstNonParenthesisAncestor;
78
79
  }
80
+ function getNodeKind(node) {
81
+ if (isFunctionNode(node)) {
82
+ return "function";
83
+ }
84
+ if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) {
85
+ return "class";
86
+ }
87
+ if (ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node)) {
88
+ return "type";
89
+ }
90
+ if (ts.isModuleDeclaration(node)) {
91
+ return "module";
92
+ }
93
+ return undefined;
94
+ }
79
95
  function getTextWithoutBrackets(node) {
80
96
  if (ts.isParenthesizedExpression(node)) {
81
97
  return node.getChildren()
@@ -98,7 +114,6 @@ function isBreakOrContinueToLabel(node) {
98
114
  function isContainer(node) {
99
115
  return isFunctionNode(node)
100
116
  || ts.isClassDeclaration(node)
101
- || ts.isConstructorDeclaration(node)
102
117
  || ts.isInterfaceDeclaration(node)
103
118
  || ts.isModuleDeclaration(node)
104
119
  || ts.isTypeAliasDeclaration(node)
@@ -115,6 +130,7 @@ function isFunctionNode(node) {
115
130
  || ts.isFunctionDeclaration(node)
116
131
  || ts.isFunctionExpression(node)
117
132
  || ts.isMethodDeclaration(node)
133
+ || ts.isConstructorDeclaration(node)
118
134
  || ts.isAccessor(node);
119
135
  }
120
136
  function isSyntaxList(node) {
@@ -52,9 +52,6 @@ function chooseContainerName(node, variableBeingDefined) {
52
52
  if (ts.isClassExpression(node)) {
53
53
  return getClassExpressionName(node, variableBeingDefined);
54
54
  }
55
- if (ts.isConstructorDeclaration(node)) {
56
- return "constructor";
57
- }
58
55
  if (ts.isInterfaceDeclaration(node)) {
59
56
  return getInterfaceDeclarationName(node);
60
57
  }
@@ -193,6 +190,9 @@ function getFunctionNodeName(func) {
193
190
  if (ts.isArrowFunction(func)) {
194
191
  return undefined;
195
192
  }
193
+ if (ts.isConstructorDeclaration(func)) {
194
+ return "constructor";
195
+ }
196
196
  if (ts.isFunctionDeclaration(func)) {
197
197
  const functionKeywordIndex = func.getChildren()
198
198
  .findIndex(node => node.kind === ts.SyntaxKind.FunctionKeyword);
@@ -77,7 +77,7 @@ async function getFolderOutput(folderPath) {
77
77
  return getFolderOutput(folderPath + "/" + entry.name);
78
78
  }
79
79
  // correct extension
80
- if (entry.name.match(/.*\.[tj]sx?$/) !== null) {
80
+ if (entry.name.match(/.*\.[cm]?[tj]sx?$/) !== null) {
81
81
  return getFileOutput(folderPath + "/" + entry.name);
82
82
  }
83
83
  return undefined;
@@ -4,16 +4,34 @@ export var Include;
4
4
  (function (Include) {
5
5
  Include[Include["folders"] = 1] = "folders";
6
6
  Include[Include["files"] = 2] = "files";
7
- Include[Include["containers"] = 3] = "containers";
7
+ Include[Include["namespaces"] = 3] = "namespaces";
8
+ Include[Include["classes"] = 4] = "classes";
9
+ Include[Include["functionsAndTypes"] = 5] = "functionsAndTypes";
8
10
  })(Include || (Include = {}));
9
11
  export var Sort;
10
12
  (function (Sort) {
11
13
  Sort[Sort["inOrder"] = 1] = "inOrder";
12
14
  Sort[Sort["complexity"] = 2] = "complexity";
13
15
  })(Sort || (Sort = {}));
16
+ const nodeKindIncludeMap = {
17
+ class: Include.classes,
18
+ file: Include.files,
19
+ function: Include.functionsAndTypes,
20
+ module: Include.namespaces,
21
+ type: Include.functionsAndTypes,
22
+ };
23
+ export function convertNodeKindToInclude(kind) {
24
+ return nodeKindIncludeMap[kind];
25
+ }
14
26
  export class ComplexityController {
15
27
  constructor(progComp, model, view) {
16
- this.include = Include.folders;
28
+ this.include = {
29
+ [Include.folders]: true,
30
+ [Include.files]: true,
31
+ [Include.namespaces]: true,
32
+ [Include.classes]: true,
33
+ [Include.functionsAndTypes]: true,
34
+ };
17
35
  this.sortMethod = Sort.inOrder;
18
36
  this.model = model;
19
37
  this.view = view;
@@ -31,7 +49,8 @@ export class ComplexityController {
31
49
  }
32
50
  sort() {
33
51
  if (this.sortMethod === Sort.inOrder) {
34
- if (this.include === Include.containers) {
52
+ const noFilesOrFolders = !this.include[Include.folders] && !this.include[Include.files];
53
+ if (noFilesOrFolders) {
35
54
  sortProgramByName(this.complexity);
36
55
  }
37
56
  else {
@@ -49,17 +68,41 @@ export class ComplexityController {
49
68
  }
50
69
  filter() {
51
70
  this.complexity = cloneSortedOutput(this.initialComplexity);
52
- const removeWhat = this.include === Include.folders
53
- ? () => false
54
- : this.include === Include.files
55
- ? data => isSortedFolderOutput(data)
56
- : data => !isSortedContainerOutput(data);
71
+ const removeWhat = (data) => {
72
+ if (!this.include[Include.folders]) {
73
+ if (isSortedFolderOutput(data)) {
74
+ return true;
75
+ }
76
+ }
77
+ if (!this.include[Include.files]) {
78
+ if (isSortedFileOutput(data)) {
79
+ return true;
80
+ }
81
+ }
82
+ if (!isSortedContainerOutput(data)) {
83
+ return false;
84
+ }
85
+ if (!this.include[Include.namespaces]) {
86
+ if (data.kind === "module") {
87
+ return true;
88
+ }
89
+ }
90
+ if (!this.include[Include.classes]) {
91
+ if (data.kind === "class") {
92
+ return true;
93
+ }
94
+ }
95
+ return false;
96
+ };
57
97
  this.moveComplexityNodes(this.complexity.inner, removeWhat);
58
98
  this.reDepth();
59
99
  this.sort();
60
100
  this.model.overwriteComplexity(this.complexity);
61
101
  }
62
102
  moveComplexityNodes(inner, removeWhat) {
103
+ for (const child of inner) {
104
+ this.moveComplexityNodes(child.inner, removeWhat);
105
+ }
63
106
  const removed = removeAll(inner, removeWhat);
64
107
  if (removed.length > 0) {
65
108
  inner.push(...removed.flatMap(removedElem => removedElem.inner));
@@ -75,15 +118,14 @@ export class ComplexityController {
75
118
  this.model.updateContainer(removedElem);
76
119
  }
77
120
  }
78
- this.moveComplexityNodes(inner, removeWhat);
79
121
  }
80
122
  }
81
123
  reDepth(complexity = this.complexity, depth = 0) {
82
124
  complexity.depth = depth;
83
125
  complexity.inner.forEach(child => this.reDepth(child, depth + 1));
84
126
  }
85
- setInclude(include) {
86
- this.include = include;
127
+ setInclude(include, value) {
128
+ this.include[include] = value;
87
129
  this.filter();
88
130
  }
89
131
  }
@@ -1,32 +1,29 @@
1
1
  import { Include } from "../complexity-tree/ComplexityController.js";
2
2
  import { ToggleControl } from "./controls/ToggleControl.js";
3
3
  export function Filterers(controller) {
4
- function updateFilter() {
5
- if (includeFolders.getState()) {
6
- controller.setInclude(Include.folders);
7
- }
8
- else if (includeFiles.getState()) {
9
- controller.setInclude(Include.files);
10
- }
11
- else {
12
- controller.setInclude(Include.containers);
13
- }
14
- }
15
4
  const includeFolders = new ToggleControl(true, "Include Folders", (state) => {
16
5
  if (state) {
17
6
  includeFiles.setState(true);
18
7
  }
19
- updateFilter();
8
+ controller.setInclude(Include.folders, state);
20
9
  });
21
10
  const includeFiles = new ToggleControl(true, "Include Files", (state) => {
22
11
  if (!state) {
23
12
  includeFolders.setState(false);
24
13
  }
25
- updateFilter();
14
+ controller.setInclude(Include.files, state);
15
+ });
16
+ const includeNamespaces = new ToggleControl(true, "Include Namespaces", (state) => {
17
+ controller.setInclude(Include.namespaces, state);
18
+ });
19
+ const includeClasses = new ToggleControl(true, "Include Classes", (state) => {
20
+ controller.setInclude(Include.classes, state);
26
21
  });
27
22
  return [
28
23
  includeFolders.dom,
29
24
  includeFiles.dom,
25
+ includeNamespaces.dom,
26
+ includeClasses.dom,
30
27
  ];
31
28
  }
32
29
  //# sourceMappingURL=Filterers.js.map
@@ -3,13 +3,13 @@ import { isFileOutput } from "./output.js";
3
3
  import { concatFilePath } from "./path.js";
4
4
  ;
5
5
  export function isSortedFileOutput(output) {
6
- return output.score !== undefined && output.line === undefined;
6
+ return ("score" in output) && !("line" in output);
7
7
  }
8
8
  export function isSortedFolderOutput(output) {
9
- return output.score === undefined;
9
+ return !("score" in output);
10
10
  }
11
11
  export function isSortedContainerOutput(output) {
12
- return output.line !== undefined;
12
+ return "line" in output;
13
13
  }
14
14
  export function cloneSortedOutput(output) {
15
15
  return JSON.parse(JSON.stringify(output));
@@ -61,6 +61,7 @@ export function convertToSortedOutput(programOutput) {
61
61
  function convertToSortedContainer(path, depth, containerOutput) {
62
62
  return {
63
63
  id: UniqueId.next(),
64
+ kind: containerOutput.kind,
64
65
  column: containerOutput.column,
65
66
  line: containerOutput.line,
66
67
  name: containerOutput.name,
@@ -78,6 +79,7 @@ function convertToSortedFile(path, name, depth, fileOutput) {
78
79
  }
79
80
  return {
80
81
  id: UniqueId.next(),
82
+ kind: "file",
81
83
  name,
82
84
  path,
83
85
  depth,
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "cognitive-complexity-ts",
3
- "version": "0.7.0",
3
+ "kind": "function",
4
+ "version": "0.8.0",
4
5
  "description": "",
5
6
  "keywords": [
6
7
  "typescript",