cognitive-complexity-ts 0.4.4 → 0.6.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.
@@ -40,12 +40,12 @@ class Scope {
40
40
  object.push(variableBeingDefined + "." + name);
41
41
  }
42
42
  }
43
- const maybeName = node_naming_1.getNameIfObjectMember(node);
44
- if (maybeName !== undefined) {
45
- object.push("this." + maybeName);
43
+ const maybeExpression = node_naming_1.getExpressionToAccessObjectMember(node);
44
+ if (maybeExpression !== undefined) {
45
+ object.push(maybeExpression);
46
46
  if (variableBeingDefined) {
47
- object.push(variableBeingDefined + "." + maybeName);
48
- local.push(variableBeingDefined + "." + maybeName);
47
+ object.push(variableBeingDefined + "." + maybeExpression);
48
+ local.push(variableBeingDefined + "." + maybeExpression);
49
49
  }
50
50
  }
51
51
  return {
@@ -33,7 +33,7 @@ function getColumnAndLine(node) {
33
33
  exports.getColumnAndLine = getColumnAndLine;
34
34
  function getIdentifier(node) {
35
35
  for (const child of node.getChildren()) {
36
- if (ts.isIdentifier(child)) {
36
+ if (ts.isIdentifier(child) || ts.isComputedPropertyName(child)) {
37
37
  return child.getText();
38
38
  }
39
39
  }
@@ -3,4 +3,4 @@ export declare function chooseContainerName(node: ts.Node, variableBeingDefined:
3
3
  export declare function getIntroducedLocalName(node: ts.Node): string | undefined;
4
4
  export declare function getNameIfCalledNode(node: ts.Node): string | undefined;
5
5
  export declare function getNameOfAssignment(node: ts.Node): string | undefined;
6
- export declare function getNameIfObjectMember(node: ts.Node): string | undefined;
6
+ export declare function getExpressionToAccessObjectMember(node: ts.Node): string | undefined;
@@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.getNameIfObjectMember = exports.getNameOfAssignment = exports.getNameIfCalledNode = exports.getIntroducedLocalName = exports.chooseContainerName = void 0;
22
+ exports.getExpressionToAccessObjectMember = exports.getNameOfAssignment = exports.getNameIfCalledNode = exports.getIntroducedLocalName = exports.chooseContainerName = void 0;
23
23
  const ts = __importStar(require("typescript"));
24
24
  const node_util_1 = require("../util/node-util");
25
25
  const node_inspection_1 = require("./node-inspection");
@@ -113,18 +113,44 @@ function getNameOfAssignment(node) {
113
113
  return undefined;
114
114
  }
115
115
  exports.getNameOfAssignment = getNameOfAssignment;
116
- function getNameIfObjectMember(node) {
116
+ function getExpressionToAccessObjectMember(node) {
117
117
  if (ts.isMethodDeclaration(node)) {
118
- return getMethodDeclarationName(node);
118
+ const [name, requiresDot] = getMethodDeclarationName(node);
119
+ if (requiresDot) {
120
+ return "this." + name;
121
+ }
122
+ else {
123
+ return "this" + name;
124
+ }
119
125
  }
120
126
  if (ts.isAccessor(node)) {
121
- return getFirstIdentifierName(node);
127
+ const [name, requiresDot] = getAccessorIdentifierName(node);
128
+ if (requiresDot) {
129
+ return "this." + name;
130
+ }
131
+ else {
132
+ return "this" + name;
133
+ }
122
134
  }
123
135
  return undefined;
124
136
  }
125
- exports.getNameIfObjectMember = getNameIfObjectMember;
137
+ exports.getExpressionToAccessObjectMember = getExpressionToAccessObjectMember;
138
+ /**
139
+ * @return [name, requires dot syntax]
140
+ */
141
+ function getAccessorIdentifierName(node) {
142
+ for (const child of node.getChildren()) {
143
+ if (ts.isIdentifier(child)) {
144
+ return [child.getText(), true];
145
+ }
146
+ if (ts.isComputedPropertyName(child)) {
147
+ return [child.getText(), false];
148
+ }
149
+ }
150
+ throw new node_util_1.UnreachableNodeState(node, "The accessor was expected to have an identifier or computed property name.");
151
+ }
126
152
  function getIdentifierDespiteBrackets(node) {
127
- if (ts.isIdentifier(node)) {
153
+ if (ts.isIdentifier(node) || ts.isElementAccessExpression(node)) {
128
154
  return node.getText();
129
155
  }
130
156
  if (ts.isParenthesizedExpression(node)) {
@@ -148,7 +174,8 @@ function getClassExpressionName(node, variableBeingDefined = undefined) {
148
174
  }
149
175
  function getFunctionNodeName(func) {
150
176
  if (ts.isAccessor(func)) {
151
- return getFirstIdentifierName(func);
177
+ const [name] = getAccessorIdentifierName(func);
178
+ return name;
152
179
  }
153
180
  if (ts.isArrowFunction(func)) {
154
181
  return undefined;
@@ -168,8 +195,8 @@ function getFunctionNodeName(func) {
168
195
  return undefined;
169
196
  }
170
197
  }
171
- const name = getMethodDeclarationName(func);
172
- if (name !== undefined) {
198
+ if (ts.isMethodDeclaration(func)) {
199
+ const [name] = getMethodDeclarationName(func);
173
200
  return name;
174
201
  }
175
202
  throw new node_util_1.UnreachableNodeState(func, "FunctionNode is not of a recognised type.");
@@ -177,14 +204,6 @@ function getFunctionNodeName(func) {
177
204
  function getInterfaceDeclarationName(node) {
178
205
  return node.getChildAt(1).getText();
179
206
  }
180
- function getFirstIdentifierName(node) {
181
- const name = node.getChildren()
182
- .find(child => ts.isIdentifier(child));
183
- if (name === undefined) {
184
- throw new node_util_1.UnreachableNodeState(node, "Node was expected to have an identifier.");
185
- }
186
- return name.getText();
187
- }
188
207
  function getModuleDeclarationName(node) {
189
208
  const moduleKeywordIndex = node.getChildren()
190
209
  .findIndex(node => node.kind === ts.SyntaxKind.NamespaceKeyword
@@ -195,10 +214,17 @@ function getModuleDeclarationName(node) {
195
214
  const moduleIdentifier = node.getChildAt(moduleKeywordIndex + 1);
196
215
  return moduleIdentifier.getText();
197
216
  }
217
+ /**
218
+ * @return [name, requires dot syntax]
219
+ */
198
220
  function getMethodDeclarationName(node) {
199
- const name = node_inspection_1.getIdentifier(node);
200
- if (name !== undefined) {
201
- return name;
221
+ for (const child of node.getChildren()) {
222
+ if (ts.isIdentifier(child)) {
223
+ return [child.getText(), true];
224
+ }
225
+ if (ts.isComputedPropertyName(child)) {
226
+ return [child.getText(), false];
227
+ }
202
228
  }
203
229
  throw new node_util_1.UnreachableNodeState(node, "Method has no identifier.");
204
230
  }
@@ -7,6 +7,7 @@ import { FileOutput, FolderOutput } from "../../shared/types";
7
7
  */
8
8
  export declare function getFileOrFolderOutput(entryPath: string): Promise<FileOutput | FolderOutput>;
9
9
  export declare function getFileOutput(filePath: string): Promise<FileOutput>;
10
+ export declare function getSourceOutput(sourceCode: string, fileName?: string): FileOutput;
10
11
  export declare function getFolderOutput(folderPath: string): Promise<FolderOutput>;
11
12
  /**
12
13
  * @param entryPath Relative to cwd
@@ -22,7 +22,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  return result;
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.programOutput = exports.getFolderOutput = exports.getFileOutput = exports.getFileOrFolderOutput = void 0;
25
+ exports.programOutput = exports.getFolderOutput = exports.getSourceOutput = exports.getFileOutput = exports.getFileOrFolderOutput = void 0;
26
26
  const fs_1 = require("fs");
27
27
  const path = __importStar(require("path"));
28
28
  const ts = __importStar(require("typescript"));
@@ -45,11 +45,16 @@ exports.getFileOrFolderOutput = getFileOrFolderOutput;
45
45
  // API
46
46
  async function getFileOutput(filePath) {
47
47
  const fileContent = (await fs_1.promises.readFile(filePath)).toString();
48
- const parsedFile = ts.createSourceFile(path.basename(filePath), fileContent, ts.ScriptTarget.Latest, true);
49
- return cognitive_complexity_1.fileCost(parsedFile);
48
+ return getSourceOutput(fileContent, path.basename(filePath));
50
49
  }
51
50
  exports.getFileOutput = getFileOutput;
52
51
  // API
52
+ function getSourceOutput(sourceCode, fileName = "") {
53
+ const parsedFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest, true);
54
+ return cognitive_complexity_1.fileCost(parsedFile);
55
+ }
56
+ exports.getSourceOutput = getSourceOutput;
57
+ // API
53
58
  async function getFolderOutput(folderPath) {
54
59
  const folderContents = await fs_1.promises.readdir(folderPath, { withFileTypes: true });
55
60
  return util_1.createObjectOfPromisedValues(folderContents, entry => entry.name, (entry) => {
@@ -24,6 +24,7 @@ const fs_1 = require("fs");
24
24
  const http = __importStar(require("http"));
25
25
  const path = __importStar(require("path"));
26
26
  const util_1 = require("../util/util");
27
+ const DEV_MODE = !!process.env["DEV"];
27
28
  const sourcePath = __dirname + "/../../..";
28
29
  const cssPath = path.normalize(sourcePath + "/ui/ts");
29
30
  const indexFilePath = path.normalize(sourcePath + "/ui/html/index.html");
@@ -91,7 +92,7 @@ async function handleRequest(req, res, combinedOutputsJson) {
91
92
  res.write(await fs_1.promises.readFile(targetFile));
92
93
  return;
93
94
  }
94
- if (url.endsWith(".js.map")) {
95
+ if (DEV_MODE && url.endsWith(".js.map")) {
95
96
  const targetFile = jsPath + "/" + url;
96
97
  if (!doesFileExistInFolder(targetFile, jsPath)) {
97
98
  return endWith404(res);
@@ -101,7 +102,7 @@ async function handleRequest(req, res, combinedOutputsJson) {
101
102
  res.write(await fs_1.promises.readFile(targetFile));
102
103
  return;
103
104
  }
104
- if (url.endsWith(".ts")) {
105
+ if (DEV_MODE && url.endsWith(".ts")) {
105
106
  const targetFile = tsPath + "/" + url;
106
107
  if (!doesFileExistInFolder(targetFile, tsPath)) {
107
108
  return endWith404(res);
package/build/src/ui.js CHANGED
@@ -8,17 +8,23 @@ const open_1 = __importDefault(require("open"));
8
8
  const util_1 = require("./util/util");
9
9
  const output_1 = require("./cognitive-complexity/output");
10
10
  const ui_server_1 = require("./ui-server/ui-server");
11
+ const helpText = "Arguments: [-h | --help] [--port <NUMBER>] [FILE]...";
11
12
  main();
12
13
  async function main() {
13
14
  const args = minimist_1.default(process.argv.slice(2));
14
15
  if (args["h"] || args["help"]) {
15
- printHelp();
16
+ console.log(helpText);
16
17
  return;
17
18
  }
18
19
  const givenPort = parseInt(args["port"]);
19
20
  const port = util_1.nonNaN(givenPort, 5678);
20
21
  const url = `http://localhost:${port}`;
21
22
  const inputFiles = args["_"];
23
+ if (inputFiles.length === 0) {
24
+ console.error("No files given.");
25
+ console.error(helpText);
26
+ return;
27
+ }
22
28
  const combinedOutputsJson = await generateComplexityJson(inputFiles);
23
29
  const server = ui_server_1.createUiServer(combinedOutputsJson);
24
30
  server.listen(port, () => {
@@ -30,7 +36,4 @@ async function generateComplexityJson(inputFiles) {
30
36
  const combinedOutputs = await util_1.keysToAsyncValues(inputFiles, file => output_1.getFileOrFolderOutput(file));
31
37
  return JSON.stringify(combinedOutputs);
32
38
  }
33
- function printHelp() {
34
- console.log("Arguments: [-h | --help] [--port <NUMBER>] [FILE]...");
35
- }
36
39
  //# sourceMappingURL=ui.js.map
@@ -30,7 +30,6 @@ export class ComplexityController {
30
30
  this.view.expandAll();
31
31
  }
32
32
  sort() {
33
- console.log("sort");
34
33
  if (this.sortMethod === Sort.inOrder) {
35
34
  if (this.include === Include.containers) {
36
35
  sortProgramByName(this.complexity);
@@ -49,7 +48,6 @@ export class ComplexityController {
49
48
  this.model.overwriteComplexity(this.complexity);
50
49
  }
51
50
  filter() {
52
- console.log("filter");
53
51
  this.complexity = cloneSortedOutput(this.initialComplexity);
54
52
  const removeWhat = this.include === Include.folders
55
53
  ? () => false
@@ -25,9 +25,6 @@ export class Tree {
25
25
  if (this.containerMap.has(containerOutput.id)) {
26
26
  return this.containerMap.get(containerOutput.id);
27
27
  }
28
- else {
29
- console.log("container");
30
- }
31
28
  const container = new Container(containerOutput, containerOutput.inner.map(inner => this.makeContainer(inner)));
32
29
  this.containerMap.set(containerOutput.id, container);
33
30
  return container;
@@ -36,9 +33,6 @@ export class Tree {
36
33
  if (this.fileMap.has(fileOutput.id)) {
37
34
  return this.fileMap.get(fileOutput.id);
38
35
  }
39
- else {
40
- console.log("file");
41
- }
42
36
  const children = [];
43
37
  for (const containerOutput of fileOutput.inner) {
44
38
  children.push(this.makeContainer(containerOutput));
@@ -51,9 +45,6 @@ export class Tree {
51
45
  if (this.folderContentsMap.has(folderOutput.id)) {
52
46
  return this.folderContentsMap.get(folderOutput.id);
53
47
  }
54
- else {
55
- console.log("folder contents");
56
- }
57
48
  const folderContents = new FolderContents(folderOutput.inner.map((folderEntry) => {
58
49
  const folderEntryComponent = isSortedContainerOutput(folderEntry)
59
50
  ? this.makeContainer(folderEntry)
@@ -69,9 +60,6 @@ export class Tree {
69
60
  if (this.folderMap.has(folderOutput.id)) {
70
61
  return this.folderMap.get(folderOutput.id);
71
62
  }
72
- else {
73
- console.log("folder");
74
- }
75
63
  const folder = new Folder(folderOutput, this.makeFolderContents(folderOutput));
76
64
  this.folderMap.set(folderOutput.id, folder);
77
65
  return folder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cognitive-complexity-ts",
3
- "version": "0.4.4",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "typescript",
@@ -15,7 +15,7 @@
15
15
  "build-tools": "tsc -i -p ./tools",
16
16
  "build-ui": "npm run build && tsc -i -p ./ui",
17
17
  "json": "npm run build && node -r source-map-support/register ./build/src/json",
18
- "ui": "npm run build && npm run build-ui && node -r source-map-support/register ./build/src/ui",
18
+ "ui": "npm run build && npm run build-ui && DEV=true node -r source-map-support/register ./build/src/ui",
19
19
  "test": "npm run build && node -r source-map-support/register ./build/test/main",
20
20
  "what": "npm run build-tools && node -r source-map-support/register ./build/tools/what-is-it",
21
21
  "prepublishOnly": "rm -rf ./build && npm run build && npm run build-ui && npm run test"