madge icon indicating copy to clipboard operation
madge copied to clipboard

Add support for the `--include` flag

Open danburzo opened this issue 2 years ago • 5 comments

Fixes #219. This is a redo of a previous PR:

  • https://github.com/pahen/madge/pull/220

danburzo avatar Jan 30 '23 09:01 danburzo

This might be too much to ask: could you please include some tests to show this PR works as intended? @danburzo

PabloLION avatar Jan 30 '23 09:01 PabloLION

No worries, I'll be glad to include some tests when I find a window of time!

In regards to your proposal to mirror .tsconfig conventions, I think because --exclude already works differently from .tsconfig.exclude, making an analogy is not possible.

Important: exclude only changes which files are included as a result of the include setting. A file specified by exclude can still become part of your codebase due to an import statement in your code, a types inclusion, a /// <reference directive, or being specified in the files list.

It is not a mechanism that prevents a file from being included in the codebase - it simply changes what the include setting finds.

I'll rethink the names of the CLI options to see if there's something better available.

danburzo avatar Jan 30 '23 09:01 danburzo

I agree. The current dependencyFilter is kinda messy (see #282) so it's probable that we change everything to mirror tsconfig. I'm not sure how current --exclude differs from tsconfig. For me they both exclude some "modules" to be parsed/checked by tsc/madge (because tsconfig has include default to "[] if files is specified, ** otherwise." )

One difference I noticed is the glob-pattern and RegExp. I can't decide which one is better:

  • RegExp is much more expressive
  • glob-pattern has less version / flavor, hence more stable

Maybe we can first keep everything the same to finish this PR? We welcome all to discuss about the new --include and --exclude in a new issue.

PabloLION avatar Jan 30 '23 11:01 PabloLION

I'm not sure how current --exclude differs from tsconfig

If I understand correctly (not a TS user), .tsconfig's include, exclude, and files options control the set of input files. All imports are then added to the list of files to process.

In contrast, include and exclude as defined in this PR (as well as the current definition of exclude) act together as a filter to what's rendered in the graph. The set of input files are defined as the operands to the CLI interface: madge file1 file2 ... and expanded based on their dependencies, then filtered by include/exclude: they have to match an include pattern and not be excluded by an exclude pattern.

danburzo avatar Jan 30 '23 11:01 danburzo

@danburzo, sorry 😅 I was too stupid. I just realized: in tsconfig, include and exclude only changes the entry points rather than files transpiled by typescript.

What I wanted to say at first was to give priorities to include and exclude: like tsconfig.json first add all files whose filename fits the include pattern, then remove those of exclude. Maybe we should implement something like this?

PabloLION avatar Jan 30 '23 23:01 PabloLION