File size: 5,939 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentDirectives = void 0;
const COMPUTED = Symbol();
const ALL = Symbol();
class CommentDirectives {
    constructor(options) {
        this.lineDisableDirectives = new Map();
        this.blockDirectives = new Map();
        this.ruleId = options.ruleId;
        this.reportUnusedDisableDirectives = Boolean(options?.reportUnusedDisableDirectives);
    }
    filterMessages(messages) {
        const { lineDisableDirectives, blockDirectives, reportUnusedDisableDirectives } = this;
        const usedDirectives = new Set();
        if (reportUnusedDisableDirectives) {
            const allBlocks = [];
            for (const bs of blockDirectives.values()) {
                for (const b of bs) {
                    allBlocks.push(b);
                }
            }
            const blockEnableDirectives = new Set();
            for (const block of allBlocks.sort((a, b) => comparePos(b.loc, a.loc))) {
                if (block.kind === 'enable') {
                    if (block.targetRule === ALL) {
                        blockEnableDirectives.clear();
                    }
                    blockEnableDirectives.add(block);
                }
                else if (block.kind === 'disable') {
                    if (block.targetRule === ALL) {
                        for (const b of blockEnableDirectives) {
                            usedDirectives.add(b);
                        }
                        blockEnableDirectives.clear();
                    }
                    else {
                        for (const b of [...blockEnableDirectives]) {
                            if (block.targetRule === b.targetRule || b.targetRule === ALL) {
                                usedDirectives.add(b);
                                blockEnableDirectives.delete(b);
                            }
                        }
                    }
                }
            }
        }
        let filteredMessages = messages.filter(isEnable);
        if (reportUnusedDisableDirectives) {
            const usedDirectiveKeys = new Set([...usedDirectives].map((d) => locToKey(d.define.loc)));
            filteredMessages = filteredMessages.filter((m) => {
                if (m.ruleId !== this.ruleId) {
                    return true;
                }
                if (usedDirectiveKeys.has(messageToKey(m))) {
                    return false;
                }
                return true;
            });
        }
        return filteredMessages;
        /** Checks wether given rule is enable */
        function isEnable(message) {
            if (!message.ruleId) {
                // Maybe fatal error
                return true;
            }
            for (const disableLines of getFromRule(lineDisableDirectives, message.ruleId)) {
                for (const disableLine of disableLines.get(message.line) ?? []) {
                    if (!disableLine.rule(message.ruleId)) {
                        continue;
                    }
                    usedDirectives.add(disableLine);
                    return false;
                }
            }
            const blocks = getFromRule(blockDirectives, message.ruleId)
                .reduce((p, c) => p.concat(c), [])
                .sort((a, b) => comparePos(b.loc, a.loc));
            for (const block of blocks) {
                if (comparePos(message, block.loc) < 0) {
                    continue;
                }
                if (!block.rule(message.ruleId)) {
                    continue;
                }
                if (block.kind === 'enable') {
                    return true;
                }
                // block.kind === "disable"
                usedDirectives.add(block);
                return false;
            }
            return true;
        }
        /** Compare locations */
        function comparePos(a, b) {
            return a.line - b.line || a.column - b.column;
        }
    }
    disableLine(line, rule, define) {
        const key = typeof rule === 'string' ? rule : COMPUTED;
        let disableLines = this.lineDisableDirectives.get(key);
        if (!disableLines) {
            disableLines = new Map();
            this.lineDisableDirectives.set(key, disableLines);
        }
        let disableLine = disableLines.get(line);
        if (!disableLine) {
            disableLine = [];
            disableLines.set(line, disableLine);
        }
        const disable = {
            line,
            rule: typeof rule === 'string' ? (id) => id === rule : rule,
            define
        };
        disableLine.push(disable);
    }
    disableBlock(pos, rule, define) {
        this.block(pos, rule, define, 'disable');
    }
    enableBlock(pos, rule, define) {
        this.block(pos, rule, define, 'enable');
    }
    block(pos, rule, define, kind) {
        const key = typeof rule === 'string' ? rule : COMPUTED;
        let blocks = this.blockDirectives.get(key);
        if (!blocks) {
            blocks = [];
            this.blockDirectives.set(key, blocks);
        }
        const disable = {
            loc: pos,
            rule: typeof rule === 'string' ? (id) => id === rule : rule,
            kind,
            targetRule: typeof rule === 'string' ? rule : ALL,
            define
        };
        blocks.push(disable);
    }
}
exports.CommentDirectives = CommentDirectives;
/** Get the list of directives from given rule  */
function getFromRule(map, ruleId) {
    return [map.get(ruleId), map.get(COMPUTED)].filter((a) => Boolean(a));
}
/**
 * Gets the key of location
 */
function locToKey(location) {
    return `line:${location.line},column${location.column}`;
}
/**
 * Gets the key of message location
 */
function messageToKey(message) {
    return `line:${message.line},column${
    // -1 because +1 by ESLint's `report-translator`.
    message.column - 1}`;
}