• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// give it a pattern, and it'll be able to tell you if
2// a given path should be ignored.
3// Ignoring a path ignores its children if the pattern ends in /**
4// Ignores are always parsed in dot:true mode
5import { Minimatch } from 'minimatch';
6import { Pattern } from './pattern.js';
7const defaultPlatform = typeof process === 'object' &&
8    process &&
9    typeof process.platform === 'string'
10    ? process.platform
11    : 'linux';
12/**
13 * Class used to process ignored patterns
14 */
15export class Ignore {
16    relative;
17    relativeChildren;
18    absolute;
19    absoluteChildren;
20    constructor(ignored, { nobrace, nocase, noext, noglobstar, platform = defaultPlatform, }) {
21        this.relative = [];
22        this.absolute = [];
23        this.relativeChildren = [];
24        this.absoluteChildren = [];
25        const mmopts = {
26            dot: true,
27            nobrace,
28            nocase,
29            noext,
30            noglobstar,
31            optimizationLevel: 2,
32            platform,
33            nocomment: true,
34            nonegate: true,
35        };
36        // this is a little weird, but it gives us a clean set of optimized
37        // minimatch matchers, without getting tripped up if one of them
38        // ends in /** inside a brace section, and it's only inefficient at
39        // the start of the walk, not along it.
40        // It'd be nice if the Pattern class just had a .test() method, but
41        // handling globstars is a bit of a pita, and that code already lives
42        // in minimatch anyway.
43        // Another way would be if maybe Minimatch could take its set/globParts
44        // as an option, and then we could at least just use Pattern to test
45        // for absolute-ness.
46        // Yet another way, Minimatch could take an array of glob strings, and
47        // a cwd option, and do the right thing.
48        for (const ign of ignored) {
49            const mm = new Minimatch(ign, mmopts);
50            for (let i = 0; i < mm.set.length; i++) {
51                const parsed = mm.set[i];
52                const globParts = mm.globParts[i];
53                /* c8 ignore start */
54                if (!parsed || !globParts) {
55                    throw new Error('invalid pattern object');
56                }
57                /* c8 ignore stop */
58                const p = new Pattern(parsed, globParts, 0, platform);
59                const m = new Minimatch(p.globString(), mmopts);
60                const children = globParts[globParts.length - 1] === '**';
61                const absolute = p.isAbsolute();
62                if (absolute)
63                    this.absolute.push(m);
64                else
65                    this.relative.push(m);
66                if (children) {
67                    if (absolute)
68                        this.absoluteChildren.push(m);
69                    else
70                        this.relativeChildren.push(m);
71                }
72            }
73        }
74    }
75    ignored(p) {
76        const fullpath = p.fullpath();
77        const fullpaths = `${fullpath}/`;
78        const relative = p.relative() || '.';
79        const relatives = `${relative}/`;
80        for (const m of this.relative) {
81            if (m.match(relative) || m.match(relatives))
82                return true;
83        }
84        for (const m of this.absolute) {
85            if (m.match(fullpath) || m.match(fullpaths))
86                return true;
87        }
88        return false;
89    }
90    childrenIgnored(p) {
91        const fullpath = p.fullpath() + '/';
92        const relative = (p.relative() || '.') + '/';
93        for (const m of this.relativeChildren) {
94            if (m.match(relative))
95                return true;
96        }
97        for (const m of this.absoluteChildren) {
98            if (m.match(fullpath))
99                return true;
100        }
101        return false;
102    }
103}
104//# sourceMappingURL=ignore.js.map