Skip to content

Commit 14f9af5

Browse files
committed
fix(rule): fix unnessary reporting error
1 parent bfdc268 commit 14f9af5

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"object-assign": "^4.0.1",
4848
"rousseau": "^1.0.0",
4949
"textlint-rule-helper": "^1.2.0",
50-
"textlint-util-to-string": "^1.2.0"
50+
"textlint-util-to-string": "^1.2.1",
51+
"unist-util-map": "^1.0.1"
5152
}
5253
}

src/textlint-rule-rousseau.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import {RuleHelper, IgnoreNodeManger} from "textlint-rule-helper";
44
const StringSource = require("textlint-util-to-string").default;
55
const rousseau = require("rousseau");
6+
const map = require("unist-util-map");
67
const ObjectAssign = require("object-assign");
78
const defaultOptions = {
89
// "suggestion", "warning", "error"
910
showLevels: ["suggestion", "warning", "error"],
1011
// ignore check type of https://github.com/GitbookIO/rousseau#checks
1112
ignoreTypes: [],
1213
// ignore textlint's node type
13-
ignoreInlineNodeTypes: undefined
14+
ignoreInlineNodeTypes: ["Code"]
1415
};
1516

1617
const mapNode = function (ast, mapFn) {
@@ -39,30 +40,30 @@ export default function textlintRousseau(context, options = defaultOptions) {
3940
return showLevels.indexOf(level) !== -1;
4041
};
4142
/*
42-
{
43-
// Type of check that output this suggestion
44-
type: "so",
43+
{
44+
// Type of check that output this suggestion
45+
type: "so",
4546
46-
// Level of importance
47-
// "suggestion", "warning", "error"
48-
level: "warning",
47+
// Level of importance
48+
// "suggestion", "warning", "error"
49+
level: "warning",
4950
50-
// Index in the text
51-
index: 10,
51+
// Index in the text
52+
index: 10,
5253
53-
// Size of the section in the text
54-
offset: 2,
54+
// Size of the section in the text
55+
offset: 2,
5556
56-
// Message to describe the suggestion
57-
message: "omit 'So' from the beginning of sentences",
57+
// Message to describe the suggestion
58+
message: "omit 'So' from the beginning of sentences",
5859
59-
// Replacements suggestion
60-
replacements: [
61-
{
62-
value: ""
63-
}
64-
]
65-
}
60+
// Replacements suggestion
61+
replacements: [
62+
{
63+
value: ""
64+
}
65+
]
66+
}
6667
*/
6768
const createSuggest = (replacements) => {
6869
if (replacements.length === 0) {
@@ -104,8 +105,21 @@ export default function textlintRousseau(context, options = defaultOptions) {
104105
// ignore if contain child node types
105106
ignoreNodeManager.ignoreChildrenByTypes(node, ignoreInlineNodeTypes);
106107
// check
107-
108-
const source = new StringSource(node);
108+
// replace code with dummy code
109+
// if you want to filter(remove) code, use https://github.com/eush77/unist-util-filter
110+
const filteredNode = map(node, (node) => {
111+
if (node.type === Syntax.Code) {
112+
// only change `value` to dummy
113+
return ObjectAssign({}, node, {
114+
value: new Array(node.value.length + 1).join("x")
115+
});
116+
}
117+
return node;
118+
});
119+
if (!filteredNode) {
120+
return;
121+
}
122+
const source = new StringSource(filteredNode);
109123
const text = source.toString();
110124
const reportSourceError = (results) => {
111125
reportError(node, source, results);

test/textlint-rule-rousseau-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ tester.run("rousseau", rule, {
1414
"This is **pen**.",
1515
"This is __pen__.",
1616
"`this` is pen.",
17+
{
18+
text: "Put the config of rules into `.textlintrc`",
19+
options: {
20+
"showLevels": [
21+
"warning",
22+
"error"
23+
],
24+
"ignoreTypes": [
25+
"sentence:uppercase",
26+
"passive",
27+
"weasel"
28+
]
29+
},
30+
},
1731
{
1832
text: "this is pen.",
1933
options: {

0 commit comments

Comments
 (0)