File size: 8,249 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const shared_1 = require("../shared");
const utils_1 = require("../utils");
const compat_1 = require("../utils/compat");
// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------
const COMMENT_DIRECTIVE_B = /^\s*(eslint-(?:en|dis)able)(?:\s+|$)/;
const COMMENT_DIRECTIVE_L = /^\s*(eslint-disable(?:-next)?-line)(?:\s+|$)/;
// eslint-disable-next-line func-style -- ignore
const ALL_RULES = () => true;
/**
 * Remove the ignored part from a given directive comment and trim it.
 * @param {string} value The comment text to strip.
 * @returns {string} The stripped text.
 */
function stripDirectiveComment(value) {
    return value.split(/\s-{2,}\s/u)[0];
}
exports.default = (0, utils_1.createRule)('comment-directive', {
    meta: {
        docs: {
            description: 'support comment-directives in HTML template',
            category: 'System',
            recommended: 'base'
        },
        schema: [
            {
                type: 'object',
                properties: {
                    reportUnusedDisableDirectives: {
                        type: 'boolean'
                    }
                },
                additionalProperties: false
            }
        ],
        messages: {
            unused: 'Unused {{kind}} directive (no problems were reported).',
            unusedRule: "Unused {{kind}} directive (no problems were reported from '{{rule}}').",
            unusedEnable: 'Unused {{kind}} directive (reporting is not suppressed).',
            unusedEnableRule: "Unused {{kind}} directive (reporting from '{{rule}}' is not suppressed)."
        },
        type: 'problem'
    },
    create(context) {
        const shared = (0, shared_1.getShared)((0, compat_1.getFilename)(context));
        if (!shared)
            return {};
        const options = context.options[0] || {};
        const reportUnusedDisableDirectives = Boolean(options.reportUnusedDisableDirectives);
        const directives = shared.newCommentDirectives({
            ruleId: 'svelte/comment-directive',
            reportUnusedDisableDirectives
        });
        const sourceCode = (0, compat_1.getSourceCode)(context);
        /**
         * Parse a given comment.
         */
        function parse(pattern, comment) {
            const text = stripDirectiveComment(comment.value);
            const match = pattern.exec(text);
            if (match == null) {
                return null;
            }
            const type = match[1];
            const rules = [];
            const rulesRe = /([^\s,]+)[\s,]*/g;
            let startIndex = match[0].length;
            rulesRe.lastIndex = startIndex;
            let res;
            while ((res = rulesRe.exec(text))) {
                const ruleId = res[1].trim();
                const commentStart = comment.range[0] + 4; /* <!-- */
                const start = sourceCode.getLocFromIndex(commentStart + startIndex);
                const end = sourceCode.getLocFromIndex(commentStart + startIndex + ruleId.length);
                rules.push({
                    ruleId,
                    loc: {
                        start,
                        end
                    }
                });
                startIndex = rulesRe.lastIndex;
            }
            return { type, rules };
        }
        /**
         * Process a given comment token.
         * If the comment is `eslint-disable` or `eslint-enable` then it reports the comment.
         */
        function processBlock(directives, comment) {
            const parsed = parse(COMMENT_DIRECTIVE_B, comment);
            if (parsed != null) {
                if (parsed.type === 'eslint-disable') {
                    if (parsed.rules.length) {
                        for (const rule of parsed.rules) {
                            if (reportUnusedDisableDirectives) {
                                context.report({
                                    loc: rule.loc,
                                    messageId: 'unusedRule',
                                    data: { rule: rule.ruleId, kind: parsed.type }
                                });
                            }
                            directives.disableBlock(comment.loc.end, rule.ruleId, {
                                loc: rule.loc.start
                            });
                        }
                    }
                    else {
                        if (reportUnusedDisableDirectives) {
                            context.report({
                                loc: comment.loc,
                                messageId: 'unused',
                                data: { kind: parsed.type }
                            });
                        }
                        directives.disableBlock(comment.loc.end, ALL_RULES, {
                            loc: comment.loc.start
                        });
                    }
                }
                else if (parsed.rules.length) {
                    for (const rule of parsed.rules) {
                        if (reportUnusedDisableDirectives) {
                            context.report({
                                loc: rule.loc,
                                messageId: 'unusedEnableRule',
                                data: { rule: rule.ruleId, kind: parsed.type }
                            });
                        }
                        directives.enableBlock(comment.loc.start, rule.ruleId, {
                            loc: rule.loc.start
                        });
                    }
                }
                else {
                    if (reportUnusedDisableDirectives) {
                        context.report({
                            loc: comment.loc,
                            messageId: 'unusedEnable',
                            data: { kind: parsed.type }
                        });
                    }
                    directives.enableBlock(comment.loc.start, ALL_RULES, {
                        loc: comment.loc.start
                    });
                }
            }
        }
        /**
         * Process a given comment token.
         * If the comment is `eslint-disable-line` or `eslint-disable-next-line` then it reports the comment.
         */
        function processLine(directives, comment) {
            const parsed = parse(COMMENT_DIRECTIVE_L, comment);
            if (parsed != null && comment.loc.start.line === comment.loc.end.line) {
                const line = comment.loc.start.line + (parsed.type === 'eslint-disable-line' ? 0 : 1);
                if (parsed.rules.length) {
                    for (const rule of parsed.rules) {
                        if (reportUnusedDisableDirectives) {
                            context.report({
                                loc: rule.loc,
                                messageId: 'unusedRule',
                                data: { rule: rule.ruleId, kind: parsed.type }
                            });
                        }
                        directives.disableLine(line, rule.ruleId, {
                            loc: rule.loc.start
                        });
                    }
                }
                else {
                    if (reportUnusedDisableDirectives) {
                        context.report({
                            loc: comment.loc,
                            messageId: 'unused',
                            data: { kind: parsed.type }
                        });
                    }
                    directives.disableLine(line, ALL_RULES, {
                        loc: comment.loc.start
                    });
                }
            }
        }
        return {
            SvelteHTMLComment(node) {
                processBlock(directives, node);
                processLine(directives, node);
            },
            SvelteScriptElement(node) {
                directives.enableBlock(node.startTag.loc.end, ALL_RULES, {
                    loc: node.loc.start
                });
            }
        };
    }
});