Skip to content

Commit fa16808

Browse files
dokmicGerrit0
authored andcommitted
fix: the bug with the negated/commented exclude patterns (TypeStrong#1025)
* Fix negated patterns support (TypeStrong#1024) * Fix commented patterns support (TypeStrong#1024) * Fix assert messages (TypeStrong#1024)
1 parent cde478a commit fa16808

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/lib/utils/paths.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,30 @@ import { Minimatch, IMinimatch } from 'minimatch';
33

44
const unix = Path.sep === '/';
55

6+
function normalize(pattern: string) {
7+
if (pattern.startsWith('!') || pattern.startsWith('#')) {
8+
return pattern[0] + normalize(pattern.substr(1));
9+
}
10+
11+
if (unix) { pattern = pattern.replace(/[\\]/g, '/').replace(/^\w:/, ''); }
12+
13+
// pattern paths not starting with '**' are resolved even if it is an
14+
// absolute path, to ensure correct format for the current OS
15+
if (pattern.substr(0, 2) !== '**') {
16+
pattern = Path.resolve(pattern);
17+
}
18+
19+
// On Windows we transform `\` to `/` to unify the way paths are intepreted
20+
if (!unix) { pattern = pattern.replace(/[\\]/g, '/'); }
21+
22+
return pattern;
23+
}
24+
625
/**
726
* Convert array of glob patterns to array of minimatch instances.
827
*
928
* Handle a few Windows-Unix path gotchas.
1029
*/
1130
export function createMinimatch(patterns: string[]): IMinimatch[] {
12-
return patterns.map((pattern: string): IMinimatch => {
13-
// Ensure correct pathing on unix, by transforming `\` to `/` and removing any `X:/` from the path
14-
if (unix) { pattern = pattern.replace(/[\\]/g, '/').replace(/^\w:/, ''); }
15-
16-
// pattern paths not starting with '**' are resolved even if it is an
17-
// absolute path, to ensure correct format for the current OS
18-
if (pattern.substr(0, 2) !== '**') {
19-
pattern = Path.resolve(pattern);
20-
}
21-
22-
// On Windows we transform `\` to `/` to unify the way paths are intepreted
23-
if (!unix) { pattern = pattern.replace(/[\\]/g, '/'); }
24-
25-
// Unify the path slashes before creating the minimatch, for more reliable matching
26-
return new Minimatch(pattern, { dot: true });
27-
});
31+
return patterns.map(pattern => new Minimatch(normalize(pattern), { dot: true }));
2832
}

src/test/utils.paths.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,19 @@ describe('Paths', () => {
5858
Assert(mm.match(absolutePath('/some/path/.dot/dir')), 'Didn\'t match .dot path');
5959
Assert(mm.match(absolutePath('/some/path/normal/dir')), 'Didn\'t match normal path');
6060
});
61+
62+
it('Minimatch matches negated expressions', () => {
63+
const paths = ['!./some/path', '!!./some/path'];
64+
const mms = createMinimatch(paths);
65+
66+
Assert(!mms[0].match(Path.resolve('some/path')), 'Matched a negated expression');
67+
Assert(mms[1].match(Path.resolve('some/path')), "Didn't match a doubly negated expression");
68+
});
69+
70+
it('Minimatch does not match commented expressions', () => {
71+
const [mm] = createMinimatch(['#/some/path']);
72+
73+
Assert(!mm.match('#/some/path'), 'Matched a commented expression');
74+
});
6175
});
6276
});

0 commit comments

Comments
 (0)