Skip to content

Commit fca87f8

Browse files
committedNov 28, 2023
Exclude negated production patterns in default mode (resolves #352)
1 parent 7c80ad1 commit fca87f8

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed
 

‎packages/knip/fixtures/negated-production-paths/index.test.ts

Whitespace-only changes.

‎packages/knip/fixtures/negated-production-paths/index.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"entry": ["*.ts!", "!*.test.ts!"],
3+
"project": ["**/*.ts!", "!*.test.ts!"]
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "@fixtures/negated-production-paths"
3+
}

‎packages/knip/fixtures/workspaces/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"entry": "index.ts!",
2929
"project": [
3030
"**/*.ts!",
31-
"!ignored/*.ts!"
31+
"!ignored/*.ts"
3232
]
3333
}
3434
}

‎packages/knip/src/WorkspaceWorker.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,20 @@ export class WorkspaceWorker {
152152
getEntryFilePatterns() {
153153
const { entry } = this.config;
154154
if (entry.length === 0) return [];
155-
return [entry, this.negatedWorkspacePatterns].flat();
155+
const excludeProductionNegations = entry.filter(pattern => !(pattern.startsWith('!') && pattern.endsWith('!')));
156+
return [excludeProductionNegations, this.negatedWorkspacePatterns].flat();
156157
}
157158

158159
getProjectFilePatterns(testFilePatterns: string[]) {
159160
const { project } = this.config;
160161
if (project.length === 0) return [];
161162

163+
const excludeProductionNegations = project.filter(pattern => !(pattern.startsWith('!') && pattern.endsWith('!')));
162164
const negatedPluginConfigPatterns = this.getPluginConfigPatterns().map(negate);
163165
const negatedPluginProjectFilePatterns = this.getPluginProjectFilePatterns().map(negate);
164166

165167
return [
166-
project,
168+
excludeProductionNegations,
167169
negatedPluginConfigPatterns,
168170
negatedPluginProjectFilePatterns,
169171
testFilePatterns,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assert from 'node:assert/strict';
2+
import test from 'node:test';
3+
import { main } from '../src/index.js';
4+
import { resolve } from '../src/util/path.js';
5+
import baseArguments from './helpers/baseArguments.js';
6+
import baseCounters from './helpers/baseCounters.js';
7+
8+
const cwd = resolve('fixtures/negated-production-paths');
9+
10+
test('Exclude negated production paths', async () => {
11+
const { counters } = await main({
12+
...baseArguments,
13+
cwd,
14+
});
15+
16+
assert.deepEqual(counters, {
17+
...baseCounters,
18+
processed: 2,
19+
total: 2,
20+
});
21+
});
22+
23+
test('Respect negated paths (production)', async () => {
24+
const { counters } = await main({
25+
...baseArguments,
26+
cwd,
27+
isProduction: true,
28+
});
29+
30+
assert.deepEqual(counters, {
31+
...baseCounters,
32+
processed: 1,
33+
total: 1,
34+
});
35+
});

0 commit comments

Comments
 (0)
Please sign in to comment.