Skip to content

Commit

Permalink
fix: allow to use multiple comment
Browse files Browse the repository at this point in the history
Before:

This filter rule go unrecognized.

<!-- textlint-disable preset-ja-technical-writing/max-ten -->
<!-- Test Text -->
  • Loading branch information
azu committed Dec 25, 2016
1 parent 3ee37f5 commit b96db71
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/parse-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ const HTML_COMMENT_REGEXP = /<!--((?:.|\s)*?)-->/g;
export function isHTMLComment(htmlString) {
return HTML_COMMENT_REGEXP.test(htmlString);
}

/**
* get comment value from html comment tag
* @param {string} commentValue <!-- comment -->
* @returns {string[]}
*/
export function getValueFromHTMLComment(commentValue) {
return commentValue.replace(HTML_COMMENT_REGEXP, "$1");
export function getValuesFromHTMLComment(commentValue) {
const results = [];
commentValue.replace(HTML_COMMENT_REGEXP, function(all, comment){
results.push(comment);
});
return results;
}
/**
* Parses a config of values separated by comma.
* @param {string} string The string to parse.
* @returns {Object} Result map of values and true values
*/
export function parseListConfig(string) {
var items = {};
const items = {};

// Collapse whitespace around ,
string = string.replace(/\s*,\s*/g, ",");

string.split(/,+/).forEach(function (name) {
name = name.trim();
if (!name) {
Expand Down
22 changes: 12 additions & 10 deletions src/textlint-filter-rule-comments.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// LICENSE : MIT
"use strict";
import StatusManager from "./StatusManager";
import {parseRuleIds, getValueFromHTMLComment, isHTMLComment} from "./parse-comment";
import {parseRuleIds, getValuesFromHTMLComment, isHTMLComment} from "./parse-comment";
const defaultOptions = {
// enable comment directive
// if comment has the value, then enable textlint rule
Expand All @@ -10,7 +10,7 @@ const defaultOptions = {
// if comment has the value, then disable textlint rule
"disablingComment": "textlint-disable"
};
module.exports = function (context, options = defaultOptions) {
module.exports = function(context, options = defaultOptions) {
const {Syntax, shouldIgnore, getSource} = context;

const enablingComment = options.enablingComment || defaultOptions.enablingComment;
Expand Down Expand Up @@ -42,14 +42,16 @@ This is ignored.
if (!isHTMLComment(nodeValue)) {
return;
}
const commentValue = getValueFromHTMLComment(nodeValue);
if (commentValue.indexOf(enablingComment) !== -1) {
const configValue = commentValue.replace(enablingComment, "");
statusManager.enableReporting(node, parseRuleIds(configValue));
} else if (commentValue.indexOf(disablingComment) !== -1) {
const configValue = commentValue.replace(disablingComment, "");
statusManager.disableReporting(node, parseRuleIds(configValue));
}
const comments = getValuesFromHTMLComment(nodeValue);
comments.forEach(commentValue => {
if (commentValue.indexOf(enablingComment) !== -1) {
const configValue = commentValue.replace(enablingComment, "");
statusManager.enableReporting(node, parseRuleIds(configValue));
} else if (commentValue.indexOf(disablingComment) !== -1) {
const configValue = commentValue.replace(disablingComment, "");
statusManager.disableReporting(node, parseRuleIds(configValue));
}
});
},
[Syntax.Comment](node){
const commentValue = node.value || "";
Expand Down
25 changes: 25 additions & 0 deletions test/textlint-filter-rule-comments-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ This is ignored.
});
});
});
context("when multi-line comments", function () {
it("should be ignored", function () {
const textlint = new TextLintCore();
textlint.setupRules({
report: reportRule
}, {
report: {
nodeTypes: [TextLintNodeType.Str]
}
});
textlint.setupFilterRules({
filter: filterRule
});
return textlint.lintMarkdown(`
<!-- textlint-disable -->
<!-- This is comment -->
This is ignored.
<!-- textlint-enable -->
`).then(({messages}) => {
assert.equal(messages.length, 0);
});
});
});
context("when during disable -- enable", function () {
it("should messages is ignored between disable and enable", function () {
const textlint = new TextLintCore();
Expand Down

0 comments on commit b96db71

Please sign in to comment.