knip 2.40.1 → 2.40.2

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.
@@ -126,9 +126,7 @@ export class ProjectPrincipal {
126
126
  external.add(sanitizedSpecifier);
127
127
  }
128
128
  else {
129
- const isIgnored = this.isGitIgnored(resolvedModule.resolvedFileName);
130
- if (!isIgnored)
131
- this.addEntryPath(resolvedModule.resolvedFileName, { skipExportsAnalysis: true });
129
+ this.addEntryPath(resolvedModule.resolvedFileName, { skipExportsAnalysis: true });
132
130
  }
133
131
  }
134
132
  else {
@@ -13,12 +13,6 @@ export declare function isImportCall(node: ts.Node): node is ts.ImportCall;
13
13
  export declare function isRequireCall(callExpression: ts.Node): callExpression is ts.CallExpression;
14
14
  export declare function isPropertyAccessCall(node: ts.Node, identifier: string): node is ts.CallExpression;
15
15
  export declare function getAccessExpressionName(node: ts.PropertyAccessExpression | ts.ElementAccessExpression): string;
16
- type LiteralLikeElementAccessExpression = ts.ElementAccessExpression & ts.Declaration & {
17
- readonly argumentExpression: ts.StringLiteralLike | ts.NumericLiteral;
18
- };
19
- export declare function isModuleExportsAccessExpression(node: ts.Node): node is LiteralLikeElementAccessExpression & {
20
- expression: ts.Identifier;
21
- };
22
16
  export declare function stripQuotes(name: string): string;
23
17
  export declare function findAncestor<T>(node: ts.Node | undefined, callback: (element: ts.Node) => boolean | 'STOP'): T | undefined;
24
18
  export declare function findDescendants<T>(node: ts.Node | undefined, callback: (element: ts.Node) => boolean | 'STOP'): T[];
@@ -36,12 +36,6 @@ export function isPropertyAccessCall(node, identifier) {
36
36
  export function getAccessExpressionName(node) {
37
37
  return 'argumentExpression' in node ? stripQuotes(node.argumentExpression.getText()) : node.name.getText();
38
38
  }
39
- export function isModuleExportsAccessExpression(node) {
40
- return ((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) &&
41
- ts.isIdentifier(node.expression) &&
42
- node.expression.escapedText === 'module' &&
43
- getAccessExpressionName(node) === 'exports');
44
- }
45
39
  export function stripQuotes(name) {
46
40
  const length = name.length;
47
41
  if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && isQuoteOrBacktick(name.charCodeAt(0))) {
@@ -103,7 +97,8 @@ export const isInModuleBlock = (node) => {
103
97
  };
104
98
  export const getJSDocTags = (node) => {
105
99
  const tags = new Set();
106
- for (const tagNode of ts.getJSDocTags(node)) {
100
+ const declaration = ts.isExportSpecifier(node) || ts.isBindingElement(node) ? node.parent.parent : node;
101
+ for (const tagNode of ts.getJSDocTags(declaration)) {
107
102
  const match = tagNode.getText()?.match(/@\S+/);
108
103
  if (match)
109
104
  tags.add(match[0]);
@@ -122,11 +122,14 @@ export const getImportsAndExports = (sourceFile, options) => {
122
122
  [results].flat().forEach(addImport);
123
123
  }
124
124
  }
125
- for (const visitor of visitors.export) {
126
- if (visitor) {
127
- const results = visitor(node, options);
128
- if (results)
129
- [results].flat().forEach(addExport);
125
+ const isTopLevel = node.parent === sourceFile || node.parent?.parent === sourceFile;
126
+ if (isTopLevel) {
127
+ for (const visitor of visitors.export) {
128
+ if (visitor) {
129
+ const results = visitor(node, options);
130
+ if (results)
131
+ [results].flat().forEach(addExport);
132
+ }
130
133
  }
131
134
  }
132
135
  for (const visitor of visitors.script) {
@@ -1,30 +1,43 @@
1
1
  import ts from 'typescript';
2
2
  import { SymbolType } from '../../../types/issues.js';
3
- import { isModuleExportsAccessExpression, stripQuotes } from '../../ast-helpers.js';
3
+ import { stripQuotes } from '../../ast-helpers.js';
4
4
  import { isJS } from '../helpers.js';
5
5
  import { exportVisitor as visit } from '../index.js';
6
6
  export default visit(isJS, node => {
7
- if (isModuleExportsAccessExpression(node)) {
8
- const parent = node.parent;
9
- if (ts.isPropertyAccessExpression(parent)) {
10
- const identifier = parent.name.getText();
11
- const pos = parent.name.getStart();
12
- return { node, identifier, type: SymbolType.UNKNOWN, pos };
13
- }
14
- else if (ts.isElementAccessExpression(parent)) {
15
- const identifier = stripQuotes(parent.argumentExpression.getText());
16
- const pos = parent.argumentExpression.getStart();
17
- return { node, identifier, type: SymbolType.UNKNOWN, pos };
18
- }
19
- else if (ts.isBinaryExpression(parent)) {
20
- const expr = parent.right;
21
- if (ts.isObjectLiteralExpression(expr) && expr.properties.every(ts.isShorthandPropertyAssignment)) {
22
- return expr.properties.map(node => {
23
- return { node, identifier: node.getText(), type: SymbolType.UNKNOWN, pos: node.pos };
24
- });
7
+ if (ts.isExpressionStatement(node)) {
8
+ if (ts.isBinaryExpression(node.expression)) {
9
+ if (ts.isPropertyAccessExpression(node.expression.left) &&
10
+ ts.isPropertyAccessExpression(node.expression.left.expression) &&
11
+ ts.isIdentifier(node.expression.left.expression.expression) &&
12
+ node.expression.left.expression.expression.escapedText === 'module' &&
13
+ node.expression.left.expression.name.escapedText === 'exports') {
14
+ const identifier = node.expression.left.name.getText();
15
+ const pos = node.expression.left.name.pos;
16
+ return { node, identifier, type: SymbolType.UNKNOWN, pos };
17
+ }
18
+ else if (ts.isElementAccessExpression(node.expression.left) &&
19
+ ts.isPropertyAccessExpression(node.expression.left.expression) &&
20
+ ts.isIdentifier(node.expression.left.expression.name) &&
21
+ ts.isIdentifier(node.expression.left.expression.expression) &&
22
+ node.expression.left.expression.expression.escapedText === 'module' &&
23
+ node.expression.left.expression.name.escapedText === 'exports') {
24
+ const identifier = stripQuotes(node.expression.left.argumentExpression.getText());
25
+ const pos = node.expression.left.argumentExpression.pos;
26
+ return { node, identifier, type: SymbolType.UNKNOWN, pos };
25
27
  }
26
- else {
27
- return { node, identifier: 'default', type: SymbolType.UNKNOWN, pos: node.getStart() };
28
+ else if (ts.isPropertyAccessExpression(node.expression.left) &&
29
+ ts.isIdentifier(node.expression.left.expression) &&
30
+ node.expression.left.expression.escapedText === 'module' &&
31
+ node.expression.left.name.escapedText === 'exports') {
32
+ const expr = node.expression.right;
33
+ if (ts.isObjectLiteralExpression(expr) && expr.properties.every(ts.isShorthandPropertyAssignment)) {
34
+ return expr.properties.map(node => {
35
+ return { node, identifier: node.getText(), type: SymbolType.UNKNOWN, pos: node.pos };
36
+ });
37
+ }
38
+ else {
39
+ return { node, identifier: 'default', type: SymbolType.UNKNOWN, pos: expr.pos };
40
+ }
28
41
  }
29
42
  }
30
43
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.40.1";
1
+ export declare const version = "2.40.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.40.1';
1
+ export const version = '2.40.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.40.1",
3
+ "version": "2.40.2",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://github.com/webpro/knip",
6
6
  "repository": "github:webpro/knip",