knip 0.0.0-graph.1 → 0.0.0-graph.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.
@@ -25,6 +25,7 @@ export declare class ConfigurationChief {
25
25
  manifestWorkspaces: Map<string, string>;
26
26
  additionalWorkspaceNames: Set<string>;
27
27
  availableWorkspaceNames: string[];
28
+ availableWorkspacePkgNames: Set<string | undefined>;
28
29
  availableWorkspaceDirs: string[];
29
30
  availableWorkspaceManifests: {
30
31
  dir: string;
@@ -32,12 +33,16 @@ export declare class ConfigurationChief {
32
33
  }[];
33
34
  workspacesGraph: ReturnType<typeof createPkgGraph> | undefined;
34
35
  enabledWorkspaces: Workspace[];
35
- localWorkspaces: Set<string>;
36
36
  resolvedConfigFilePath?: string;
37
37
  constructor({ cwd, isProduction }: ConfigurationManagerOptions);
38
38
  init(): Promise<void>;
39
39
  getCompilers(): [SyncCompilers, AsyncCompilers];
40
40
  getRules(): import("./types/issues.js").Rules;
41
+ getFilters(): {
42
+ dir: string;
43
+ } | {
44
+ dir?: undefined;
45
+ };
41
46
  private normalize;
42
47
  private setWorkspaces;
43
48
  private getListedWorkspaces;
@@ -6,7 +6,7 @@ import { ConfigurationValidator } from './ConfigurationValidator.js';
6
6
  import { ROOT_WORKSPACE_NAME, DEFAULT_EXTENSIONS, KNIP_CONFIG_LOCATIONS } from './constants.js';
7
7
  import { defaultRules } from './issues/initializers.js';
8
8
  import * as plugins from './plugins/index.js';
9
- import { arrayify, compact } from './util/array.js';
9
+ import { arrayify } from './util/array.js';
10
10
  import parsedArgValues from './util/cli-arguments.js';
11
11
  import { partitionCompilers } from './util/compilers.js';
12
12
  import { ConfigurationError, LoaderError } from './util/errors.js';
@@ -58,11 +58,11 @@ export class ConfigurationChief {
58
58
  manifestWorkspaces = new Map();
59
59
  additionalWorkspaceNames = new Set();
60
60
  availableWorkspaceNames = [];
61
+ availableWorkspacePkgNames = new Set();
61
62
  availableWorkspaceDirs = [];
62
63
  availableWorkspaceManifests = [];
63
64
  workspacesGraph;
64
65
  enabledWorkspaces = [];
65
- localWorkspaces = new Set();
66
66
  resolvedConfigFilePath;
67
67
  constructor({ cwd, isProduction }) {
68
68
  this.cwd = cwd;
@@ -103,6 +103,11 @@ export class ConfigurationChief {
103
103
  getRules() {
104
104
  return this.config.rules;
105
105
  }
106
+ getFilters() {
107
+ if (this.workspacesGraph?.graph && workspaceArg)
108
+ return { dir: join(this.cwd, workspaceArg) };
109
+ return {};
110
+ }
106
111
  normalize(rawLocalConfig) {
107
112
  const initialWorkspaces = rawLocalConfig.workspaces ?? {
108
113
  [ROOT_WORKSPACE_NAME]: {
@@ -179,9 +184,9 @@ export class ConfigurationChief {
179
184
  .reverse()
180
185
  .map(dir => join(this.cwd, dir));
181
186
  this.availableWorkspaceManifests = this.getAvailableWorkspaceManifests(this.availableWorkspaceDirs);
187
+ this.availableWorkspacePkgNames = new Set(this.availableWorkspaceManifests.map(w => w.manifest.name));
182
188
  this.workspacesGraph = createPkgGraph(this.availableWorkspaceManifests);
183
189
  this.enabledWorkspaces = this.getEnabledWorkspaces();
184
- this.localWorkspaces = new Set(compact(this.enabledWorkspaces.map(w => w.pkgName)));
185
190
  }
186
191
  getListedWorkspaces() {
187
192
  return this.manifest?.workspaces
@@ -245,14 +250,15 @@ export class ConfigurationChief {
245
250
  const ws = new Set();
246
251
  if (graph && workspaceArg) {
247
252
  const seen = new Set();
248
- const workspaceDirsWithDependants = new Set(workspaceNames.map(name => join(this.cwd, name)));
253
+ const initialWorkspaces = new Set(workspaceNames.map(name => join(this.cwd, name)));
254
+ const workspaceDirsWithDependants = new Set(initialWorkspaces);
249
255
  const addDependents = (dir) => {
250
256
  seen.add(dir);
251
257
  const deps = graph[dir]?.dependencies ?? [];
252
- if (deps.length > 0 && Array.from(workspaceDirsWithDependants).some(dir => deps.includes(dir))) {
258
+ if (deps.length > 0 && Array.from(initialWorkspaces).some(dir => deps.includes(dir))) {
253
259
  workspaceDirsWithDependants.add(dir);
254
- deps.filter(dir => !seen.has(dir)).forEach(addDependents);
255
260
  }
261
+ deps.filter(dir => !seen.has(dir)).forEach(addDependents);
256
262
  };
257
263
  this.availableWorkspaceNames.map(name => join(this.cwd, name)).forEach(addDependents);
258
264
  workspaceDirsWithDependants.forEach(dir => ws.add(relative(this.cwd, dir) || ROOT_WORKSPACE_NAME));
@@ -1,16 +1,21 @@
1
1
  import type { ConfigurationHint, Issue, Rules } from './types/issues.js';
2
+ type Filters = Partial<{
3
+ dir: string;
4
+ }>;
2
5
  type IssueCollectorOptions = {
3
6
  cwd: string;
4
7
  rules: Rules;
8
+ filters: Filters;
5
9
  };
6
10
  export declare class IssueCollector {
7
11
  private cwd;
8
12
  private rules;
13
+ private filters;
9
14
  private issues;
10
15
  private counters;
11
16
  private referencedFiles;
12
17
  private configurationHints;
13
- constructor({ cwd, rules }: IssueCollectorOptions);
18
+ constructor({ cwd, rules, filters }: IssueCollectorOptions);
14
19
  addFileCounts({ processed, unused }: {
15
20
  processed: number;
16
21
  unused: number;
@@ -7,13 +7,15 @@ function objectInSet(set, obj) {
7
7
  export class IssueCollector {
8
8
  cwd;
9
9
  rules;
10
+ filters;
10
11
  issues = initIssues();
11
12
  counters = initCounters();
12
13
  referencedFiles = new Set();
13
14
  configurationHints = new Set();
14
- constructor({ cwd, rules }) {
15
+ constructor({ cwd, rules, filters }) {
15
16
  this.cwd = cwd;
16
17
  this.rules = rules;
18
+ this.filters = filters;
17
19
  }
18
20
  addFileCounts({ processed, unused }) {
19
21
  this.counters.processed += processed;
@@ -21,14 +23,18 @@ export class IssueCollector {
21
23
  }
22
24
  addFilesIssues(filePaths) {
23
25
  filePaths.forEach(filePath => {
24
- if (!this.referencedFiles.has(filePath)) {
25
- this.issues.files.add(filePath);
26
- this.counters.files++;
27
- this.counters.processed++;
28
- }
26
+ if (this.filters.dir && !filePath.startsWith(this.filters.dir + '/'))
27
+ return;
28
+ if (this.referencedFiles.has(filePath))
29
+ return;
30
+ this.issues.files.add(filePath);
31
+ this.counters.files++;
32
+ this.counters.processed++;
29
33
  });
30
34
  }
31
35
  addIssue(issue) {
36
+ if (this.filters.dir && !issue.filePath.startsWith(this.filters.dir + '/'))
37
+ return;
32
38
  const key = relative(this.cwd, issue.filePath);
33
39
  issue.severity = this.rules[issue.type];
34
40
  this.issues[issue.type][key] = this.issues[issue.type][key] ?? {};
package/dist/index.js CHANGED
@@ -28,13 +28,15 @@ export const main = async (unresolvedConfiguration) => {
28
28
  const workspaces = chief.getWorkspaces();
29
29
  const report = chief.getIssueTypesToReport();
30
30
  const rules = chief.getRules();
31
+ const filters = chief.getFilters();
31
32
  const isReportDependencies = report.dependencies || report.unlisted || report.unresolved;
32
33
  const isReportValues = report.exports || report.nsExports || report.classMembers;
33
34
  const isReportTypes = report.types || report.nsTypes || report.enumMembers;
34
- const collector = new IssueCollector({ cwd, rules });
35
+ const collector = new IssueCollector({ cwd, rules, filters });
35
36
  const enabledPluginsStore = new Map();
36
37
  deputy.addIgnored(chief.config.ignoreBinaries, chief.config.ignoreDependencies);
37
- debugLogObject('Included workspaces', workspaces);
38
+ debugLogObject('Included workspaces', workspaces.map(w => w.pkgName));
39
+ debugLogObject('Included workspace configs', workspaces.map(w => ({ name: w.name, pkgName: w.pkgName, config: w.config, ancestors: w.ancestors })));
38
40
  const handleReferencedDependency = ({ specifier, containingFilePath, principal, workspace, }) => {
39
41
  if (isInternal(specifier)) {
40
42
  const absSpecifier = toAbsolute(specifier, dirname(containingFilePath));
@@ -184,7 +186,7 @@ export const main = async (unresolvedConfiguration) => {
184
186
  exportedSymbols.set(filePath, exported);
185
187
  for (const [specifierFilePath, importItems] of internal.entries()) {
186
188
  const packageName = getPackageNameFromModuleSpecifier(importItems.specifier);
187
- if (packageName && chief.localWorkspaces.has(packageName)) {
189
+ if (packageName && chief.availableWorkspacePkgNames.has(packageName)) {
188
190
  external.add(packageName);
189
191
  if (_principal === principal) {
190
192
  const workspace = chief.findWorkspaceByFilePath(specifierFilePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "0.0.0-graph.1",
3
+ "version": "0.0.0-graph.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",