knip 2.1.1 → 2.1.3

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/README.md CHANGED
@@ -72,14 +72,15 @@ file. Let's say you are using `.ts` files excusively and have all source files o
72
72
  The `entry` files target the starting point(s) to resolve the rest of the imported code. The `project` files should
73
73
  contain all files to match against the files resolved from the entry files, including potentially unused files.
74
74
 
75
- Places where Knip looks for configuration:
75
+ Places where Knip looks for configuration (ordered by priority):
76
76
 
77
77
  - `knip.json`
78
78
  - `knip.jsonc`
79
79
  - `.knip.json`
80
80
  - `.knip.jsonc`
81
- - `knip.js`
82
81
  - `knip.ts`
82
+ - `knip.js`
83
+ - `package.json#knip`
83
84
 
84
85
  So you can use a dynamic `knip.ts` with TypeScript if you prefer:
85
86
 
@@ -510,6 +511,9 @@ Such variables and types can be marked with the JSDoc `@public` tag:
510
511
  */
511
512
 
512
513
  export const merge = function () {};
514
+
515
+ /** @public */
516
+ export const split = function () {};
513
517
  ```
514
518
 
515
519
  Knip does not report public exports and types as unused.
@@ -1,5 +1,6 @@
1
1
  import parse from 'bash-parser';
2
2
  import parseArgs from 'minimist';
3
+ import { debugLogObject } from '../util/debug.js';
3
4
  import * as FallbackResolver from './resolvers/fallback.js';
4
5
  import * as KnownResolvers from './resolvers/index.js';
5
6
  const spawningBinaries = ['cross-env', 'dotenv'];
@@ -52,6 +53,12 @@ export const getBinariesFromScript = (script, { cwd, manifest, knownGlobalsOnly
52
53
  return [];
53
54
  }
54
55
  });
55
- const parsed = parse(script);
56
- return parsed?.commands ? getBinariesFromNodes(parsed.commands) : [];
56
+ try {
57
+ const parsed = parse(script);
58
+ return parsed?.commands ? getBinariesFromNodes(parsed.commands) : [];
59
+ }
60
+ catch (error) {
61
+ debugLogObject('Bash parser error', error);
62
+ return [];
63
+ }
57
64
  };
package/dist/cli.js CHANGED
@@ -3,7 +3,7 @@ import './util/register.js';
3
3
  import prettyMilliseconds from 'pretty-ms';
4
4
  import reporters from './reporters/index.js';
5
5
  import parsedArgs, { helpText } from './util/cli-arguments.js';
6
- import { ConfigurationError, LoaderError } from './util/errors.js';
6
+ import { isKnownError, ConfigurationError } from './util/errors.js';
7
7
  import { _load } from './util/loader.js';
8
8
  import { cwd, resolve } from './util/path.js';
9
9
  import { Performance } from './util/performance.js';
@@ -46,15 +46,12 @@ const run = async () => {
46
46
  }
47
47
  }
48
48
  catch (error) {
49
- if (error instanceof ConfigurationError) {
50
- console.error(error.message + '\n');
51
- console.log(helpText);
52
- process.exit(1);
53
- }
54
- else if (error instanceof LoaderError) {
49
+ if (error instanceof Error && isKnownError(error)) {
55
50
  console.error(error.message);
56
51
  if (error.cause instanceof Error)
57
52
  console.error('Reason:', error.cause.message);
53
+ if (error instanceof ConfigurationError)
54
+ console.log('\n' + helpText);
58
55
  process.exit(1);
59
56
  }
60
57
  throw error;
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { IssueCollector } from './issue-collector.js';
6
6
  import { PrincipalFactory } from './principal-factory.js';
7
7
  import { compact } from './util/array.js';
8
8
  import { debugLogObject, debugLogArray, debugLog } from './util/debug.js';
9
- import { ConfigurationError } from './util/errors.js';
9
+ import { LoaderError } from './util/errors.js';
10
10
  import { findFile } from './util/fs.js';
11
11
  import { _glob } from './util/glob.js';
12
12
  import { getPackageNameFromFilePath, getPackageNameFromModuleSpecifier } from './util/modules.js';
@@ -38,7 +38,7 @@ export const main = async (unresolvedConfiguration) => {
38
38
  const manifestPath = isRoot ? chief.manifestPath : findFile(dir, 'package.json');
39
39
  const manifest = isRoot ? chief.manifest : manifestPath && _require(manifestPath);
40
40
  if (!manifestPath || !manifest)
41
- throw new ConfigurationError(`Unable to load package.json for ${name}`);
41
+ throw new LoaderError(`Unable to load package.json for ${name}`);
42
42
  deputy.addWorkspace({ name, dir, manifestPath, manifest, ignoreDependencies });
43
43
  const compilerOptions = await loadCompilerOptions(join(dir, tsConfigFile ?? 'tsconfig.json'));
44
44
  const principal = factory.getPrincipal({ cwd: dir, report: report, paths, compilerOptions, compilers });
@@ -2,4 +2,4 @@ import { hasDependency } from '../../util/plugin.js';
2
2
  export const NAME = 'Tailwind';
3
3
  export const ENABLERS = ['tailwindcss'];
4
4
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
5
- export const CONFIG_FILE_PATTERNS = ['tailwind.config.js'];
5
+ export const CONFIG_FILE_PATTERNS = ['tailwind.config.{js,cjs,mjs,ts}'];
@@ -6,7 +6,7 @@ type Paths = ts.CompilerOptions['paths'];
6
6
  type Principal = {
7
7
  principal: ProjectPrincipal;
8
8
  cwds: Set<string>;
9
- paths: Set<string>;
9
+ pathKeys: Set<string>;
10
10
  };
11
11
  type Principals = Set<Principal>;
12
12
  type Options = {
@@ -1,12 +1,11 @@
1
1
  import { ProjectPrincipal } from './project-principal.js';
2
2
  import { join, isAbsolute } from './util/path.js';
3
- const mergePaths = (cwd, compilerOptions = {}, paths = {}) => {
4
- const mergedPaths = { ...compilerOptions.paths, ...paths };
5
- const baseUrl = compilerOptions.baseUrl ?? '.';
6
- compilerOptions.paths = Object.keys(mergedPaths).reduce((paths, key) => {
7
- paths[key] = mergedPaths[key].map(entry => isAbsolute(entry) ? entry : isAbsolute(baseUrl) ? join(baseUrl, entry) : join(cwd, baseUrl, entry));
8
- return paths;
3
+ const mergePaths = (cwd, compilerOptions, paths = {}) => {
4
+ const overridePaths = Object.keys(paths).reduce((overridePaths, key) => {
5
+ overridePaths[key] = paths[key].map(entry => (isAbsolute(entry) ? entry : join(cwd, entry)));
6
+ return overridePaths;
9
7
  }, {});
8
+ compilerOptions.paths = { ...compilerOptions.paths, ...overridePaths };
10
9
  return compilerOptions;
11
10
  };
12
11
  export class PrincipalFactory {
@@ -26,22 +25,22 @@ export class PrincipalFactory {
26
25
  const workspacePaths = compilerOptions?.paths ? Object.keys(compilerOptions.paths) : [];
27
26
  const principal = Array.from(this.principals).find(principal => {
28
27
  if (compilerOptions.baseUrl === principal.principal.compilerOptions.baseUrl) {
29
- return workspacePaths.every(p => !principal.paths.has(p));
28
+ return workspacePaths.every(p => !principal.pathKeys.has(p));
30
29
  }
31
30
  return !compilerOptions.baseUrl;
32
31
  });
33
32
  return principal;
34
33
  }
35
34
  linkPrincipal(principal, cwd, paths) {
36
- Object.keys(paths ?? {}).forEach(p => principal.paths.add(p));
35
+ Object.keys(paths ?? {}).forEach(p => principal.pathKeys.add(p));
37
36
  principal.principal.compilerOptions.paths = { ...principal.principal.compilerOptions.paths, ...paths };
38
37
  principal.cwds.add(cwd);
39
38
  }
40
39
  addNewPrincipal({ cwd, compilerOptions, report, compilers }) {
41
40
  const principal = new ProjectPrincipal({ cwd, compilerOptions, report, compilers });
42
- const paths = new Set(Object.keys(compilerOptions?.paths ?? {}));
41
+ const pathKeys = new Set(Object.keys(compilerOptions?.paths ?? {}));
43
42
  compilerOptions.baseUrl = join(cwd, compilerOptions.baseUrl ?? '.');
44
- this.principals.add({ principal, cwds: new Set([cwd]), paths });
43
+ this.principals.add({ principal, cwds: new Set([cwd]), pathKeys });
45
44
  return principal;
46
45
  }
47
46
  getPrincipals() {
@@ -2,3 +2,4 @@ export declare class ConfigurationError extends Error {
2
2
  }
3
3
  export declare class LoaderError extends Error {
4
4
  }
5
+ export declare const isKnownError: (error: Error) => boolean;
@@ -2,3 +2,4 @@ export class ConfigurationError extends Error {
2
2
  }
3
3
  export class LoaderError extends Error {
4
4
  }
5
+ export const isKnownError = (error) => error instanceof ConfigurationError || error instanceof LoaderError;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.1.1";
1
+ export declare const version = "2.1.3";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.1.1';
1
+ export const version = '2.1.3';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
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",
@@ -66,7 +66,7 @@
66
66
  "@types/js-yaml": "4.0.5",
67
67
  "@types/micromatch": "4.0.2",
68
68
  "@types/minimist": "1.2.2",
69
- "@types/node": "18.15.7",
69
+ "@types/node": "18.15.10",
70
70
  "@types/npmcli__map-workspaces": "3.0.0",
71
71
  "@types/webpack": "5.28.0",
72
72
  "@typescript-eslint/eslint-plugin": "5.56.0",
@@ -77,11 +77,11 @@
77
77
  "eslint-plugin-import": "2.27.5",
78
78
  "globstar": "1.0.0",
79
79
  "prettier": "2.8.7",
80
- "release-it": "15.9.1",
80
+ "release-it": "15.9.3",
81
81
  "remark-cli": "11.0.0",
82
82
  "remark-preset-webpro": "0.0.2",
83
83
  "tsx": "3.12.6",
84
- "type-fest": "3.7.0"
84
+ "type-fest": "3.7.1"
85
85
  },
86
86
  "engines": {
87
87
  "node": ">=16.17.0 <17 || >=18.6.0"