Skip to content

Commit 991c649

Browse files
authored
feat: support comment description (#12)
1 parent 5273aa6 commit 991c649

3 files changed

Lines changed: 251 additions & 13 deletions

File tree

src/parse-comment.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,22 @@ export function parseListConfig(string) {
4444
*/
4545
export function parseRuleIds(string) {
4646
return Object.keys(parseListConfig(string));
47-
}
47+
}
48+
49+
/**
50+
* Remove trailing description part that follows `--` in comment directives.
51+
* @param {string} string
52+
* @returns {string}
53+
* @example
54+
* removeCommentDescription(" ruleA -- because of reasons"); // => " ruleA "
55+
*/
56+
export function removeCommentDescription(string) {
57+
if (!string) {
58+
return string;
59+
}
60+
const index = string.indexOf("--");
61+
if (index === -1) {
62+
return string;
63+
}
64+
return string.slice(0, index);
65+
}

src/textlint-filter-rule-comments.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
33
import StatusManager from "./StatusManager";
4-
import {parseRuleIds, getValuesFromHTMLComment, isHTMLComment} from "./parse-comment";
4+
import {parseRuleIds, getValuesFromHTMLComment, isHTMLComment, removeCommentDescription} from "./parse-comment";
55
const defaultOptions = {
66
// enable comment directive
77
// if comment has the value, then enable textlint rule
@@ -23,7 +23,7 @@ module.exports = function(context, options = defaultOptions) {
2323
/*
2424
2525
This is wrong format.
26-
https://github.com/wooorm/remark treat as one html block.
26+
https://github.com/wooorm/remark treat as one html block.
2727
2828
<!-- textlint-disable -->
2929
This is ignored.
@@ -45,21 +45,21 @@ This is ignored.
4545
const comments = getValuesFromHTMLComment(nodeValue);
4646
comments.forEach(commentValue => {
4747
if (commentValue.indexOf(enablingComment) !== -1) {
48-
const configValue = commentValue.replace(enablingComment, "");
48+
const configValue = removeCommentDescription(commentValue.replace(enablingComment, ""));
4949
statusManager.enableReporting(node, parseRuleIds(configValue));
5050
} else if (commentValue.indexOf(disablingComment) !== -1) {
51-
const configValue = commentValue.replace(disablingComment, "");
51+
const configValue = removeCommentDescription(commentValue.replace(disablingComment, ""));
5252
statusManager.disableReporting(node, parseRuleIds(configValue));
5353
}
5454
});
5555
},
5656
[Syntax.Comment](node){
5757
const commentValue = node.value || "";
5858
if (commentValue.indexOf(enablingComment) !== -1) {
59-
const configValue = commentValue.replace(enablingComment, "");
59+
const configValue = removeCommentDescription(commentValue.replace(enablingComment, ""));
6060
statusManager.enableReporting(node, parseRuleIds(configValue));
6161
} else if (commentValue.indexOf(disablingComment) !== -1) {
62-
const configValue = commentValue.replace(disablingComment, "");
62+
const configValue = removeCommentDescription(commentValue.replace(disablingComment, ""));
6363
statusManager.disableReporting(node, parseRuleIds(configValue));
6464
}
6565
},
@@ -73,4 +73,4 @@ This is ignored.
7373
})
7474
}
7575
}
76-
};
76+
};

test/textlint-filter-rule-comments-test.js

Lines changed: 225 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ This is ignored.
6969
nodeTypes: [ASTNodeTypes.Str]
7070
}
7171
});
72-
72+
7373
textlint.setupFilterRules({
7474
filter: filterRule
7575
});
@@ -94,7 +94,7 @@ This is text.
9494
nodeTypes: [ASTNodeTypes.Str]
9595
}
9696
});
97-
97+
9898
textlint.setupFilterRules({
9999
filter: filterRule
100100
});
@@ -124,7 +124,7 @@ This is Error.
124124
nodeTypes: [ASTNodeTypes.Str]
125125
}
126126
});
127-
127+
128128
textlint.setupFilterRules({
129129
filter: filterRule
130130
});
@@ -147,7 +147,7 @@ This is text.
147147
nodeTypes: [ASTNodeTypes.Str]
148148
}
149149
});
150-
150+
151151
textlint.setupFilterRules({
152152
filter: filterRule
153153
});
@@ -176,7 +176,7 @@ This is text.
176176
nodeTypes: [ASTNodeTypes.Str]
177177
}
178178
});
179-
179+
180180
textlint.setupFilterRules({
181181
filter: filterRule
182182
});
@@ -195,5 +195,225 @@ This is Error of RuleA.
195195
});
196196
});
197197
});
198+
199+
context("description basics", function () {
200+
it("should ignore messages when using description in disable comment", function () {
201+
const textlint = new TextLintCore();
202+
textlint.setupRules({
203+
ruleA: reportRule
204+
}, {
205+
ruleA: {
206+
nodeTypes: [ASTNodeTypes.Str]
207+
}
208+
});
209+
210+
textlint.setupFilterRules({
211+
filter: filterRule
212+
});
213+
return textlint.lintMarkdown(`
214+
<!-- textlint-disable ruleA -- temporary -->
215+
216+
This is text.
217+
218+
<!-- textlint-enable ruleA -->
219+
`).then(({ messages }) => {
220+
assert.equal(messages.length, 0);
221+
});
222+
});
223+
224+
it("should ignore messages when disabling multiple rules with description", function () {
225+
const textlint = new TextLintCore();
226+
textlint.setupRules({
227+
ruleA: reportRule,
228+
ruleB: reportRule
229+
}, {
230+
ruleA: {
231+
nodeTypes: [ASTNodeTypes.Str]
232+
},
233+
ruleB: {
234+
nodeTypes: [ASTNodeTypes.Str]
235+
}
236+
});
237+
238+
textlint.setupFilterRules({
239+
filter: filterRule
240+
});
241+
return textlint.lintMarkdown(`
242+
<!-- textlint-disable ruleA,ruleB -- temporary -->
243+
244+
This is text.
245+
246+
<!-- textlint-enable -->
247+
`).then(({ messages }) => {
248+
assert.equal(messages.length, 0);
249+
});
250+
});
251+
252+
it("should re-enable ruleA when using description in enable comment", function () {
253+
const textlint = new TextLintCore();
254+
textlint.setupRules({
255+
ruleA: reportRule
256+
}, {
257+
ruleA: {
258+
nodeTypes: [ASTNodeTypes.Str]
259+
}
260+
});
261+
262+
textlint.setupFilterRules({
263+
filter: filterRule
264+
});
265+
return textlint.lintMarkdown(`
266+
<!-- textlint-disable ruleA -->
267+
268+
This is text.
269+
270+
<!-- textlint-enable ruleA -- done -->
271+
272+
This is Error.
273+
`).then(({ messages }) => {
274+
assert.equal(messages.length, 1);
275+
});
276+
});
277+
278+
it("should re-enable all when using description without rule ids in enable comment", function () {
279+
const textlint = new TextLintCore();
280+
textlint.setupRules({
281+
report: reportRule
282+
}, {
283+
report: {
284+
nodeTypes: [ASTNodeTypes.Str]
285+
}
286+
});
287+
288+
textlint.setupFilterRules({
289+
filter: filterRule
290+
});
291+
return textlint.lintMarkdown(`
292+
<!-- textlint-disable -->
293+
294+
This is text.
295+
296+
<!-- textlint-enable -- done -->
297+
298+
This is Error.
299+
`).then(({ messages }) => {
300+
assert.equal(messages.length, 1);
301+
});
302+
});
303+
});
304+
305+
context("edge cases for comment descriptions with --", function () {
306+
it("should ignore messages when `--` appears before directive", function () {
307+
const textlint = new TextLintCore();
308+
textlint.setupRules({
309+
ruleA: reportRule,
310+
ruleB: reportRule
311+
}, {
312+
ruleA: {
313+
nodeTypes: [ASTNodeTypes.Str]
314+
},
315+
ruleB: {
316+
nodeTypes: [ASTNodeTypes.Str]
317+
}
318+
});
319+
320+
textlint.setupFilterRules({
321+
filter: filterRule
322+
});
323+
return textlint.lintMarkdown(`
324+
<!-- -- textlint-disable ruleA -->
325+
326+
This is text.
327+
328+
<!-- textlint-enable -->
329+
`).then(({ messages }) => {
330+
assert.equal(messages.length, 0);
331+
});
332+
});
333+
334+
it("should ignore messages when '---' follows directive", function () {
335+
const textlint = new TextLintCore();
336+
textlint.setupRules({
337+
ruleA: reportRule,
338+
ruleB: reportRule
339+
}, {
340+
ruleA: {
341+
nodeTypes: [ASTNodeTypes.Str]
342+
},
343+
ruleB: {
344+
nodeTypes: [ASTNodeTypes.Str]
345+
}
346+
});
347+
348+
textlint.setupFilterRules({
349+
filter: filterRule
350+
});
351+
return textlint.lintMarkdown(`
352+
<!-- textlint-disable --- reason -->
353+
354+
This is text.
355+
356+
<!-- textlint-enable -->
357+
`).then(({ messages }) => {
358+
assert.equal(messages.length, 0);
359+
});
360+
});
361+
362+
it("should ignore messages when '----' follows a rule id", function () {
363+
const textlint = new TextLintCore();
364+
textlint.setupRules({
365+
ruleA: reportRule,
366+
ruleB: reportRule
367+
}, {
368+
ruleA: {
369+
nodeTypes: [ASTNodeTypes.Str]
370+
},
371+
ruleB: {
372+
nodeTypes: [ASTNodeTypes.Str]
373+
}
374+
});
375+
376+
textlint.setupFilterRules({
377+
filter: filterRule
378+
});
379+
return textlint.lintMarkdown(`
380+
<!-- textlint-disable ruleA ---- reason -->
381+
382+
This is text.
383+
384+
<!-- textlint-enable -->
385+
`).then(({ messages }) => {
386+
assert.equal(messages.length, 0);
387+
});
388+
});
389+
390+
it("should ignore messages when multiple '--' are present", function () {
391+
const textlint = new TextLintCore();
392+
textlint.setupRules({
393+
ruleA: reportRule,
394+
ruleB: reportRule
395+
}, {
396+
ruleA: {
397+
nodeTypes: [ASTNodeTypes.Str]
398+
},
399+
ruleB: {
400+
nodeTypes: [ASTNodeTypes.Str]
401+
}
402+
});
403+
404+
textlint.setupFilterRules({
405+
filter: filterRule
406+
});
407+
return textlint.lintMarkdown(`
408+
<!-- textlint-disable ruleA -- reason -- extra -->
409+
410+
This is text.
411+
412+
<!-- textlint-enable -->
413+
`).then(({ messages }) => {
414+
assert.equal(messages.length, 0);
415+
});
416+
});
417+
});
198418
});
199419
});

0 commit comments

Comments
 (0)