From 6cf74e7ecb9e7e86ecf40b331d5d72b89b430b8d Mon Sep 17 00:00:00 2001 From: azu Date: Sat, 19 Mar 2016 19:00:24 +0900 Subject: [PATCH] fix(src): use map function instead of filter --- package.json | 1 + src/textlint-rule-rousseau.js | 30 +++++++++++++++++++++++++---- test/textlint-rule-rousseau-test.js | 11 +++++------ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 8aa3e20..e0882a7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/textlint-rule-rousseau.js b/src/textlint-rule-rousseau.js index 6a42fe5..5fe57f6 100644 --- a/src/textlint-rule-rousseau.js +++ b/src/textlint-rule-rousseau.js @@ -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"], @@ -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; }; @@ -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; diff --git a/test/textlint-rule-rousseau-test.js b/test/textlint-rule-rousseau-test.js index 2f4b16f..a532ad8 100644 --- a/test/textlint-rule-rousseau-test.js +++ b/test/textlint-rule-rousseau-test.js @@ -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: {