Skip to content

Commit

Permalink
Fix errors with space-separated include/exclude arg
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 29, 2023
1 parent f0ae9e7 commit ec64c07
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
36 changes: 16 additions & 20 deletions packages/knip/src/util/get-included-issue-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,36 @@ type Options = {
exports?: boolean;
};

const normalize = (values: string[]) => values.map(value => value.split(',')).flat();

export const getIncludedIssueTypes = (
cliArgs: CLIArguments,
{ include = [], exclude = [], isProduction = false }: Options = {}
) => {
[...cliArgs.include, ...cliArgs.exclude, ...include, ...exclude].forEach(type => {
// Allow space-separated argument values (--include files,dependencies)
let incl = normalize(cliArgs.include);
let excl = normalize(cliArgs.exclude);

// Naming is hard...
[...incl, ...excl, ...include, ...exclude].forEach(type => {
// @ts-expect-error The point is that we're checking for invalid issue types
if (!ISSUE_TYPES.includes(type)) throw new ConfigurationError(`Invalid issue type: ${type}`);
});

// CLI arguments override local options
const excludes = exclude.filter(exclude => !incl.includes(exclude));
const includes = include.filter(include => !excl.includes(include));

if (cliArgs.dependencies) {
cliArgs.include = [
...cliArgs.include,
'dependencies',
'optionalPeerDependencies',
'unlisted',
'binaries',
'unresolved',
];
incl = [...incl, 'dependencies', 'optionalPeerDependencies', 'unlisted', 'binaries', 'unresolved'];
}
if (cliArgs.exports) {
const exports = ['exports', 'nsExports', 'classMembers', 'types', 'nsTypes', 'enumMembers', 'duplicates'];
cliArgs.include = [...cliArgs.include, ...exports];
incl = [...incl, ...exports];
}

// Allow space-separated argument values (--include files,dependencies)
const normalizedIncludesArg = cliArgs.include.map(value => value.split(',')).flat();
const normalizedExcludesArg = cliArgs.exclude.map(value => value.split(',')).flat();

// CLI arguments override local options
const excludes = exclude.filter(exclude => !normalizedIncludesArg.includes(exclude));
const includes = include.filter(include => !normalizedExcludesArg.includes(include));

const _include = [normalizedIncludesArg, includes].flat();
const _exclude = [normalizedExcludesArg, excludes].flat();
const _include = [...incl, ...includes];
const _exclude = [...excl, ...excludes];

if (isProduction) {
// Ignore devDependencies when analyzing production code
Expand Down
32 changes: 32 additions & 0 deletions packages/knip/test/cli-include.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { resolve } from '../src/util/path.js';
import { execFactory } from './helpers/exec.js';

const cwd = resolve('fixtures/cli');

const exec = execFactory(cwd);

test('knip --include files,dependencies', () => {
const { stdout, status } = exec('knip --include files,dependencies');
assert.equal(stdout, '');
assert.equal(status, 0);
});

test('knip --include files --include dependencies', () => {
const { stdout, status } = exec('knip --include files --include dependencies');
assert.equal(stdout, '');
assert.equal(status, 0);
});

test('knip --include file,dep', () => {
const { stderr, status } = exec('knip --include file,dep');
assert.match(stderr, /Invalid issue type: file/);
assert.equal(status, 2);
});

test('knip --include files --include deps', () => {
const { stderr, status } = exec('knip --include files,deps');
assert.match(stderr, /Invalid issue type: deps/);
assert.equal(status, 2);
});

0 comments on commit ec64c07

Please sign in to comment.