Skip to content

Commit

Permalink
fix(src): use map function instead of filter
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Mar 19, 2016
1 parent be9c323 commit 6cf74e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"textlint-tester": "^1.1.0"
},
"dependencies": {
"object-assign": "^4.0.1",
"rousseau": "github:azu/rousseau#fix-for-strict-mode",
"textlint-rule-helper": "^1.1.5",
"textlint-util-to-string": "^1.2.0",
Expand Down
30 changes: 26 additions & 4 deletions src/textlint-rule-rousseau.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const {RuleHelper} = require("textlint-rule-helper");
const StringSource = require("textlint-util-to-string").default;
const rousseau = require("rousseau");
const filter = require('unist-util-filter');
const ObjectAssign = require("object-assign");
const defaultOptions = {
// "suggestion", "warning", "error"
showLevels: ["suggestion", "warning", "error"],
Expand All @@ -12,12 +12,25 @@ const defaultOptions = {
// ignore textlint's node type
ignoreInlineNodeTypes: undefined
};

const mapNode = function (ast, mapFn) {
return (function preorder(node, index, parent) {
const newNode = ObjectAssign({}, mapFn(node, index, parent));
if (node.children) {
newNode.children = node.children.map(function (child, index) {
return preorder(child, index, node);
});
}
return newNode;
}(ast, null, null));
};

export default function textlintRousseau(context, options = defaultOptions) {
const helper = new RuleHelper(context);
const {Syntax, RuleError, report, getSource} = context;
const showLevels = options.showLevels || defaultOptions.showLevels;
const ignoreTypes = options.ignoreTypes || defaultOptions.ignoreTypes;
const ignoreInlineNodeTypes = options.ignoreInlineNodeTypes || [Syntax.Image, Syntax.Code, Syntax.Link];
const ignoreInlineNodeTypes = options.ignoreInlineNodeTypes || [Syntax.Code];
const isShowType = (type)=> {
return ignoreTypes.indexOf(type) === -1;
};
Expand Down Expand Up @@ -82,8 +95,17 @@ export default function textlintRousseau(context, options = defaultOptions) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
const filteredNode = filter(node, (node) => {
return ignoreInlineNodeTypes.indexOf(node.type) === -1;
const filteredNode = mapNode(node, (node) => {
const index = ignoreInlineNodeTypes.indexOf(node.type);
if (index === -1) {
return node;
}
/*
`xxx` => code
*/
return ObjectAssign({}, node, {
value: node.type.toLocaleLowerCase()
});
});
if (!filteredNode) {
return;
Expand Down
11 changes: 5 additions & 6 deletions test/textlint-rule-rousseau-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import rule from "../src/textlint-rule-rousseau";
// ruleName, rule, { valid, invalid }
tester.run("rousseau", rule, {
valid: [
// ignore Link
"[So the cat was stolen.](http://example.com)",
// ignore Image
"![So the cat was stolen.](http://example.com)",
// ignore Code
"`So the cat was stolen.`",
// Code
"This is `var cat = 'is stolen'`.",// => This is code.
"This is pen.",
"This is *pen*.",
"This is **pen**.",
"This is __pen__.",
{
text: "this is pen.",
options: {
Expand Down

0 comments on commit 6cf74e7

Please sign in to comment.