sverweij / dependency-cruiser

Validate and visualize dependencies. Your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
https://npmjs.com/dependency-cruiser
MIT License
5.38k stars 251 forks source link

Feature request: Add option to filter out modules that do or do not pass rules #344

Open forivall opened 4 years ago

forivall commented 4 years ago

Context

I want to show only tsx files that actually require react in my dependency graph visualization.

see #343 for more context

For the given rule

/** @type {import('dependency-cruiser').IRuleSetType} */
module.exports = {
  required: [
    {
      name: 'tsx-must-require-react',
      comment: 'tsx modules must require react, otherwise, they should be .ts',
      severity: 'info',
      module: {
        path: '\\.[tj]sx$',
      },
      to: {
        path: 'node_modules/react/'
      }
    }
  ],

i'd like to only show modules that pass this rule

Expected Behavior

these non-react modules are hidden in the dependency graph

Current Behavior

the modules are shown in the dependency graph

Possible Solution

Update IReporterFiltersType

export interface IReporterFiltersType {
  exclude: IExcludeType;
  includeOnly: IIncludeOnlyFilterType;
  focus: IFocusType;
  pass: string | string[];
  /** this would include only those modules that fail rules */
  fail: string | string[];
}

where, in my example, i would use

  reporterOptions: { 
    dot: {
      filters: {
        pass: 'tsx-must-require-react'
      }
    }
  }

Considered alternatives

Current workaround is to run depcruise --config .dependency-cruiser.js src|grep tsx-must-require-react | cut -d: -f2 and paste that into reporterOptions.dot.filters.exclude. not the worst.

sverweij commented 4 years ago

Hi @forivall thanks for this suggestion (and the research), sorry for the slow reaction - I needed some time to reflect on this and see the general applicability and usability - and now see 'show me a graph focussing on all modules (& dependencies) that fail (/ pass) rule x' could be useful especially if the repo is larger.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.