knip 2.16.1 → 2.17.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.
package/dist/index.js CHANGED
@@ -181,7 +181,7 @@ export const main = async (unresolvedConfiguration) => {
181
181
  const analyzeSourceFile = (filePath) => {
182
182
  const workspace = chief.findWorkspaceByFilePath(filePath);
183
183
  if (workspace) {
184
- const { imports, exports, scripts } = principal.analyzeSourceFile(filePath, { skipTypeOnly: !isReportTypes });
184
+ const { imports, exports, scripts } = principal.analyzeSourceFile(filePath, { skipTypeOnly: isProduction });
185
185
  const { internal, external, unresolved } = imports;
186
186
  const { exported, duplicate } = exports;
187
187
  if (exported.size > 0)
@@ -202,8 +202,6 @@ export const main = async (unresolvedConfiguration) => {
202
202
  importedModule.isReExport = importItems.isReExport;
203
203
  importedModule.isReExportedBy.add(filePath);
204
204
  }
205
- if (importItems.isDynamic)
206
- importedModule.isDynamic = importItems.isDynamic;
207
205
  if (importItems.isStar)
208
206
  importedModule.isStar = importItems.isStar;
209
207
  }
@@ -1,4 +1,5 @@
1
1
  import { compact } from '../../util/array.js';
2
+ import { join } from '../../util/path.js';
2
3
  import { timerify } from '../../util/Performance.js';
3
4
  import { hasDependency, load } from '../../util/plugin.js';
4
5
  import { getDependenciesFromConfig } from '../babel/index.js';
@@ -53,13 +54,13 @@ const findWebpackDependencies = async (configFilePath, { manifest, isProduction
53
54
  const cfg = typeof config === 'function' ? config(env, argv) : config;
54
55
  return [cfg].flat().flatMap(config => {
55
56
  const dependencies = (config.module?.rules?.flatMap(resolveRuleSetDependencies) ?? []).map(loader => loader.replace(/\?.*/, ''));
56
- const entries = cfg.entry
57
+ const entries = (cfg.entry
57
58
  ? typeof cfg.entry === 'string'
58
59
  ? [cfg.entry]
59
60
  : Array.isArray(cfg.entry)
60
61
  ? cfg.entry
61
62
  : Object.values(cfg.entry).map(entry => (typeof entry === 'string' ? entry : entry.filename))
62
- : [];
63
+ : []).map(entry => (config.context ? join(config.context, entry) : entry));
63
64
  return [...dependencies, ...entries];
64
65
  });
65
66
  });
@@ -1,6 +1,6 @@
1
1
  type FileExtension = string;
2
- export type SyncCompilerFn = (source: string) => string;
3
- export type AsyncCompilerFn = (source: string) => Promise<string>;
2
+ export type SyncCompilerFn = (source: string, path: string) => string;
3
+ export type AsyncCompilerFn = (source: string, path: string) => Promise<string>;
4
4
  export type SyncCompilers = Map<FileExtension, SyncCompilerFn>;
5
5
  export type AsyncCompilers = Map<FileExtension, AsyncCompilerFn>;
6
6
  export {};
@@ -8,7 +8,6 @@ export type ImportedModule = {
8
8
  isStar: boolean;
9
9
  isReExport: boolean;
10
10
  isReExportedBy: Set<string>;
11
- isDynamic: boolean;
12
11
  };
13
12
  export type Imports = Map<FilePath, ImportedModule>;
14
13
  export {};
@@ -23,7 +23,7 @@ export class SourceFileManager {
23
23
  throw new Error(`Unable to read ${filePath}`);
24
24
  const ext = extname(filePath);
25
25
  const compiler = this.syncCompilers?.get(ext);
26
- const compiled = compiler ? compiler(contents) : contents;
26
+ const compiled = compiler ? compiler(contents, filePath) : contents;
27
27
  if (compiler)
28
28
  debugLog(`Compiled ${filePath}`);
29
29
  return this.createSourceFile(filePath, compiled);
@@ -45,7 +45,7 @@ export class SourceFileManager {
45
45
  const ext = extname(filePath);
46
46
  const compiler = this.asyncCompilers?.get(ext);
47
47
  if (compiler) {
48
- const compiled = await compiler(contents);
48
+ const compiled = await compiler(contents, filePath);
49
49
  debugLog(`Compiled ${filePath}`);
50
50
  this.createSourceFile(filePath, compiled);
51
51
  }
@@ -7,7 +7,6 @@ interface ValidImportTypeNode extends ts.ImportTypeNode {
7
7
  export declare function isValidImportTypeNode(node: ts.Node): node is ValidImportTypeNode;
8
8
  export declare function isPrivateMember(node: ts.MethodDeclaration | ts.PropertyDeclaration): boolean;
9
9
  export declare function isDefaultImport(node: ts.ImportDeclaration | ts.ImportEqualsDeclaration | ts.ExportDeclaration): boolean;
10
- export declare function isVariableDeclarationList(node: ts.Node): node is ts.VariableDeclarationList;
11
10
  export declare function isAccessExpression(node: ts.Node): node is ts.PropertyAccessExpression | ts.ElementAccessExpression;
12
11
  export declare function isImportCall(node: ts.Node): node is ts.ImportCall;
13
12
  export declare function isRequireCall(callExpression: ts.Node): callExpression is ts.CallExpression;
@@ -8,9 +8,6 @@ export function isPrivateMember(node) {
8
8
  export function isDefaultImport(node) {
9
9
  return node.kind === ts.SyntaxKind.ImportDeclaration && !!node.importClause && !!node.importClause.name;
10
10
  }
11
- export function isVariableDeclarationList(node) {
12
- return node.kind === ts.SyntaxKind.VariableDeclarationList;
13
- }
14
11
  export function isAccessExpression(node) {
15
12
  return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node);
16
13
  }
@@ -10,7 +10,6 @@ export type AddImportOptions = {
10
10
  specifier: string;
11
11
  symbol?: ts.Symbol;
12
12
  identifier?: string;
13
- isDynamic?: boolean;
14
13
  isReExport?: boolean;
15
14
  };
16
15
  export type AddExportOptions = ExportItem & {
@@ -22,7 +22,7 @@ export const getImportsAndExports = (sourceFile, options) => {
22
22
  const importedInternalSymbols = new Map();
23
23
  const visitors = getVisitors(sourceFile);
24
24
  const addInternalImport = (options) => {
25
- const { identifier, specifier, symbol, filePath, isDynamic, isReExport } = options;
25
+ const { identifier, specifier, symbol, filePath, isReExport } = options;
26
26
  const isStar = identifier === '*';
27
27
  const internalImport = getOrSet(internalImports, filePath, {
28
28
  specifier,
@@ -30,7 +30,6 @@ export const getImportsAndExports = (sourceFile, options) => {
30
30
  isReExport,
31
31
  isReExportedBy: new Set(),
32
32
  symbols: new Set(),
33
- isDynamic,
34
33
  });
35
34
  if (isReExport) {
36
35
  internalImport.isReExport = isReExport;
@@ -42,11 +41,9 @@ export const getImportsAndExports = (sourceFile, options) => {
42
41
  internalImport.symbols.add(identifier);
43
42
  if (isStar && symbol)
44
43
  importedInternalSymbols.set(symbol, filePath);
45
- if (isDynamic)
46
- internalImport.isDynamic = isDynamic;
47
44
  };
48
45
  const addImport = (options) => {
49
- const { specifier, symbol, identifier = '__anonymous', isDynamic = false, isReExport = false } = options;
46
+ const { specifier, symbol, identifier = '__anonymous', isReExport = false } = options;
50
47
  if (isBuiltin(specifier))
51
48
  return;
52
49
  const module = sourceFile.resolvedModules?.get(specifier, undefined);
@@ -55,7 +52,7 @@ export const getImportsAndExports = (sourceFile, options) => {
55
52
  if (filePath) {
56
53
  if (module.resolvedModule.isExternalLibraryImport) {
57
54
  if (!isInNodeModules(filePath)) {
58
- addInternalImport({ identifier, specifier, symbol, filePath, isDynamic, isReExport });
55
+ addInternalImport({ identifier, specifier, symbol, filePath, isReExport });
59
56
  }
60
57
  if (!isMaybePackageName(specifier))
61
58
  return;
@@ -67,7 +64,7 @@ export const getImportsAndExports = (sourceFile, options) => {
67
64
  }
68
65
  }
69
66
  else {
70
- addInternalImport({ identifier, specifier, symbol, filePath, isDynamic, isReExport });
67
+ addInternalImport({ identifier, specifier, symbol, filePath, isReExport });
71
68
  }
72
69
  }
73
70
  }
@@ -1,40 +1,11 @@
1
1
  import ts from 'typescript';
2
- import { isImportCall, isAccessExpression, isVariableDeclarationList, findDescendants } from '../../ast-helpers.js';
2
+ import { isImportCall } from '../../ast-helpers.js';
3
3
  import { importVisitor as visit } from '../index.js';
4
4
  export default visit(() => true, node => {
5
5
  if (isImportCall(node)) {
6
6
  if (node.arguments[0] && ts.isStringLiteralLike(node.arguments[0])) {
7
7
  const specifier = node.arguments[0].text;
8
- if (isAccessExpression(node.parent)) {
9
- return { specifier, isDynamic: true };
10
- }
11
- let _ancestor = node.parent?.parent?.parent;
12
- if (_ancestor && isAccessExpression(_ancestor)) {
13
- return { specifier, isDynamic: true };
14
- }
15
- while (_ancestor) {
16
- if (_ancestor) {
17
- if (isVariableDeclarationList(_ancestor)) {
18
- return findDescendants(_ancestor, ts.isVariableDeclaration).flatMap(variableDeclaration => {
19
- if (ts.isIdentifier(variableDeclaration.name)) {
20
- return { identifier: 'default', specifier };
21
- }
22
- else {
23
- const binds = findDescendants(variableDeclaration, _node => ts.isBindingElement(_node) && ts.isIdentifier(_node.name));
24
- return binds.flatMap(element => {
25
- const symbol = element.propertyName?.getText() || element.name.getText();
26
- return { identifier: symbol, specifier };
27
- });
28
- }
29
- });
30
- }
31
- if (ts.isPropertyAssignment(_ancestor)) {
32
- return { identifier: 'default', specifier };
33
- }
34
- }
35
- _ancestor = _ancestor.parent;
36
- }
37
- return { specifier };
8
+ return { specifier, identifier: 'default' };
38
9
  }
39
10
  }
40
11
  });
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.16.1";
1
+ export declare const version = "2.17.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.16.1';
1
+ export const version = '2.17.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.16.1",
3
+ "version": "2.17.0",
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",
@@ -22,10 +22,11 @@
22
22
  "lint": "eslint scripts src tests",
23
23
  "lint:fix": "eslint scripts src tests --fix",
24
24
  "format": "prettier scripts src tests schema.json --with-node-modules --write --config .prettierrc",
25
- "test": "glob -c \"node --no-warnings --loader tsx --test\" \"tests/**/*.test.ts\"",
26
- "coverage": "c8 npm test",
25
+ "pretest": "node rmdir.js tmp && swc src -d tmp/src && swc tests -d tmp/tests",
26
+ "test": "node --no-warnings --test tmp",
27
+ "coverage": "c8 --reporter html node --no-warnings --loader tsx --test tests/*.test.ts tests/*/*.test.ts",
27
28
  "watch": "tsc --watch",
28
- "prebuild": "node -e \"require('fs').rmSync('dist', { force: true, recursive: true })\"",
29
+ "prebuild": "node rmdir.js dist",
29
30
  "build": "tsc",
30
31
  "docs": "npm run docs:cli && npm run docs:plugins && npm run docs:format",
31
32
  "docs:cli": "tsx ./scripts/update-cli-usage-in-readme.ts",
@@ -63,6 +64,8 @@
63
64
  "@jest/types": "29.6.1",
64
65
  "@npmcli/package-json": "4.0.1",
65
66
  "@release-it/bumper": "5.0.0",
67
+ "@swc/cli": "0.1.62",
68
+ "@swc/core": "1.3.70",
66
69
  "@types/eslint": "8.44.0",
67
70
  "@types/js-yaml": "4.0.5",
68
71
  "@types/micromatch": "4.0.2",
@@ -77,7 +80,6 @@
77
80
  "eslint-import-resolver-typescript": "3.5.5",
78
81
  "eslint-plugin-import": "2.27.5",
79
82
  "eslint-plugin-n": "16.0.1",
80
- "glob": "10.3.3",
81
83
  "prettier": "3.0.0",
82
84
  "release-it": "16.1.3",
83
85
  "remark-cli": "11.0.0",