knip 2.40.0 → 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.
@@ -6,6 +6,7 @@ import type { PackageJson } from '@npmcli/package-json';
6
6
  type ConfigurationManagerOptions = {
7
7
  cwd: string;
8
8
  isProduction: boolean;
9
+ isStrict: boolean;
9
10
  };
10
11
  export type Workspace = {
11
12
  name: string;
@@ -19,6 +20,7 @@ export type Workspace = {
19
20
  export declare class ConfigurationChief {
20
21
  cwd: string;
21
22
  isProduction: boolean;
23
+ isStrict: boolean;
22
24
  config: Configuration;
23
25
  manifestPath?: string;
24
26
  manifest?: PackageJson;
@@ -36,7 +38,7 @@ export declare class ConfigurationChief {
36
38
  enabledWorkspaces: Workspace[];
37
39
  resolvedConfigFilePath?: string;
38
40
  rawConfig?: any;
39
- constructor({ cwd, isProduction }: ConfigurationManagerOptions);
41
+ constructor({ cwd, isProduction, isStrict }: ConfigurationManagerOptions);
40
42
  init(): Promise<void>;
41
43
  private loadResolvedConfigurationFile;
42
44
  getCompilers(): [SyncCompilers, AsyncCompilers];
@@ -51,6 +51,7 @@ const PLUGIN_NAMES = Object.keys(plugins);
51
51
  export class ConfigurationChief {
52
52
  cwd;
53
53
  isProduction = false;
54
+ isStrict = false;
54
55
  config;
55
56
  manifestPath;
56
57
  manifest;
@@ -65,9 +66,10 @@ export class ConfigurationChief {
65
66
  enabledWorkspaces = [];
66
67
  resolvedConfigFilePath;
67
68
  rawConfig;
68
- constructor({ cwd, isProduction }) {
69
+ constructor({ cwd, isProduction, isStrict }) {
69
70
  this.cwd = cwd;
70
71
  this.isProduction = isProduction;
72
+ this.isStrict = isStrict;
71
73
  this.config = defaultConfig;
72
74
  }
73
75
  async init() {
@@ -233,7 +235,10 @@ export class ConfigurationChief {
233
235
  : this.availableWorkspaceNames;
234
236
  const graph = this.workspacesGraph?.graph;
235
237
  const ws = new Set();
236
- if (graph && workspaceArg) {
238
+ if (workspaceArg && this.isStrict) {
239
+ ws.add(workspaceArg);
240
+ }
241
+ else if (graph && workspaceArg) {
237
242
  const seen = new Set();
238
243
  const initialWorkspaces = new Set(workspaceNames.map(name => join(this.cwd, name)));
239
244
  const workspaceDirsWithDependants = new Set(initialWorkspaces);
@@ -122,7 +122,8 @@ export class ProjectPrincipal {
122
122
  const resolvedModule = this.resolveModule(specifier, filePath);
123
123
  if (resolvedModule) {
124
124
  if (resolvedModule.isExternalLibraryImport) {
125
- external.add(specifier);
125
+ const sanitizedSpecifier = sanitizeSpecifier(specifier);
126
+ external.add(sanitizedSpecifier);
126
127
  }
127
128
  else {
128
129
  this.addEntryPath(resolvedModule.resolvedFileName, { skipExportsAnalysis: true });
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ import { WorkspaceWorker } from './WorkspaceWorker.js';
17
17
  export const main = async (unresolvedConfiguration) => {
18
18
  const { cwd, tsConfigFile, gitignore, isStrict, isProduction, isIgnoreInternal, isShowProgress, isIncludeEntryExports, } = unresolvedConfiguration;
19
19
  debugLogObject('*', 'Unresolved configuration (from CLI arguments)', unresolvedConfiguration);
20
- const chief = new ConfigurationChief({ cwd, isProduction });
20
+ const chief = new ConfigurationChief({ cwd, isProduction, isStrict });
21
21
  const deputy = new DependencyDeputy({ isStrict });
22
22
  const factory = new PrincipalFactory();
23
23
  const streamer = new ConsoleStreamer({ isEnabled: isShowProgress });
@@ -1,6 +1,6 @@
1
1
  import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
2
2
  export declare const NAME = "Node.js Test Runner";
3
- export declare const ENABLERS: string[];
3
+ export declare const ENABLERS = "This plugin is enabled when any script in `package.json` includes `node --test`";
4
4
  export declare const isEnabled: IsPluginEnabledCallback;
5
5
  export declare const ENTRY_FILE_PATTERNS: string[];
6
6
  export declare const findDependencies: GenericPluginCallback;
@@ -1,7 +1,7 @@
1
1
  import { timerify } from '../../util/Performance.js';
2
2
  import { toEntryPattern } from '../../util/protocols.js';
3
3
  export const NAME = 'Node.js Test Runner';
4
- export const ENABLERS = [''];
4
+ export const ENABLERS = 'This plugin is enabled when any script in `package.json` includes `node --test`';
5
5
  export const isEnabled = ({ manifest }) => {
6
6
  return Object.keys(manifest.scripts ?? {})
7
7
  .filter(s => /test/.test(s))
@@ -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
  }
@@ -51,4 +51,8 @@ export const getEntryPathFromManifest = (cwd, dir, manifest) => {
51
51
  }
52
52
  return _glob({ cwd, workingDir: dir, patterns: Array.from(entryPaths) });
53
53
  };
54
- export const sanitizeSpecifier = (specifier) => specifier.replace(/^([?!|-]+)?([^!?:]+).*/, '$2');
54
+ export const sanitizeSpecifier = (specifier) => {
55
+ if (specifier.startsWith('node:'))
56
+ return specifier;
57
+ return specifier.replace(/^([?!|-]+)?([^!?:]+).*/, '$2');
58
+ };
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.40.0";
1
+ export declare const version = "2.40.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.40.0';
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.0",
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",