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