cognitive-complexity-ts 0.5.0 → 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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cognitive-complexity-ts",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "typescript",