Skip to content

Commit 6cf74e7

Browse files
committed
fix(src): use map function instead of filter
1 parent be9c323 commit 6cf74e7

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"textlint-tester": "^1.1.0"
4545
},
4646
"dependencies": {
47+
"object-assign": "^4.0.1",
4748
"rousseau": "github:azu/rousseau#fix-for-strict-mode",
4849
"textlint-rule-helper": "^1.1.5",
4950
"textlint-util-to-string": "^1.2.0",

src/textlint-rule-rousseau.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const {RuleHelper} = require("textlint-rule-helper");
44
const StringSource = require("textlint-util-to-string").default;
55
const rousseau = require("rousseau");
6-
const filter = require('unist-util-filter');
6+
const ObjectAssign = require("object-assign");
77
const defaultOptions = {
88
// "suggestion", "warning", "error"
99
showLevels: ["suggestion", "warning", "error"],
@@ -12,12 +12,25 @@ const defaultOptions = {
1212
// ignore textlint's node type
1313
ignoreInlineNodeTypes: undefined
1414
};
15+
16+
const mapNode = function (ast, mapFn) {
17+
return (function preorder(node, index, parent) {
18+
const newNode = ObjectAssign({}, mapFn(node, index, parent));
19+
if (node.children) {
20+
newNode.children = node.children.map(function (child, index) {
21+
return preorder(child, index, node);
22+
});
23+
}
24+
return newNode;
25+
}(ast, null, null));
26+
};
27+
1528
export default function textlintRousseau(context, options = defaultOptions) {
1629
const helper = new RuleHelper(context);
1730
const {Syntax, RuleError, report, getSource} = context;
1831
const showLevels = options.showLevels || defaultOptions.showLevels;
1932
const ignoreTypes = options.ignoreTypes || defaultOptions.ignoreTypes;
20-
const ignoreInlineNodeTypes = options.ignoreInlineNodeTypes || [Syntax.Image, Syntax.Code, Syntax.Link];
33+
const ignoreInlineNodeTypes = options.ignoreInlineNodeTypes || [Syntax.Code];
2134
const isShowType = (type)=> {
2235
return ignoreTypes.indexOf(type) === -1;
2336
};
@@ -82,8 +95,17 @@ export default function textlintRousseau(context, options = defaultOptions) {
8295
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
8396
return;
8497
}
85-
const filteredNode = filter(node, (node) => {
86-
return ignoreInlineNodeTypes.indexOf(node.type) === -1;
98+
const filteredNode = mapNode(node, (node) => {
99+
const index = ignoreInlineNodeTypes.indexOf(node.type);
100+
if (index === -1) {
101+
return node;
102+
}
103+
/*
104+
`xxx` => code
105+
*/
106+
return ObjectAssign({}, node, {
107+
value: node.type.toLocaleLowerCase()
108+
});
87109
});
88110
if (!filteredNode) {
89111
return;

test/textlint-rule-rousseau-test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import rule from "../src/textlint-rule-rousseau";
77
// ruleName, rule, { valid, invalid }
88
tester.run("rousseau", rule, {
99
valid: [
10-
// ignore Link
11-
"[So the cat was stolen.](http://example.com)",
12-
// ignore Image
13-
"![So the cat was stolen.](http://example.com)",
14-
// ignore Code
15-
"`So the cat was stolen.`",
10+
// Code
11+
"This is `var cat = 'is stolen'`.",// => This is code.
1612
"This is pen.",
13+
"This is *pen*.",
14+
"This is **pen**.",
15+
"This is __pen__.",
1716
{
1817
text: "this is pen.",
1918
options: {

0 commit comments

Comments
 (0)