Skip to content

Commit 317e50d

Browse files
committed
Merge pull request #3 from azu/fix-1
fix(rule): don't modify ast, use filter function
2 parents ca23e40 + bfa5066 commit 317e50d

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sudo: false
2+
language: node_js
3+
node_js: "stable"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# textlint-rule-rousseau
1+
# textlint-rule-rousseau [![Build Status](https://travis-ci.org/azu/textlint-rule-rousseau.svg?branch=master)](https://travis-ci.org/azu/textlint-rule-rousseau)
22

33
A [textlint](https://github.com/textlint/textlint "textlint") rule check english sentence using [rousseau](https://github.com/GitbookIO/rousseau "rousseau").
44

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
"babel-preset-es2015": "^6.5.0",
4141
"babel-register": "^6.5.2",
4242
"mocha": "^2.4.5",
43-
"textlint": "^5.2.0",
44-
"textlint-tester": "^0.4.1"
43+
"textlint": "^6.0.1",
44+
"textlint-tester": "^1.1.0"
4545
},
4646
"dependencies": {
4747
"rousseau": "github:azu/rousseau#fix-for-strict-mode",
4848
"textlint-rule-helper": "^1.1.5",
49-
"textlint-util-to-string": "^1.2.0"
49+
"textlint-util-to-string": "^1.2.0",
50+
"unist-util-filter": "^0.2.1"
5051
}
5152
}

src/textlint-rule-rousseau.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
// LICENSE : MIT
22
"use strict";
3-
import {RuleHelper} from "textlint-rule-helper"
4-
import StringSource from "textlint-util-to-string";
5-
import rousseau from "rousseau";
3+
const {RuleHelper} = require("textlint-rule-helper");
4+
const StringSource = require("textlint-util-to-string").default;
5+
const rousseau = require("rousseau");
6+
const filter = require('unist-util-filter');
67
const defaultOptions = {
78
// "suggestion", "warning", "error"
89
showLevels: ["suggestion", "warning", "error"],
10+
// ignore check type of https://github.com/GitbookIO/rousseau#checks
911
ignoreTypes: [],
10-
fakeInlineCode: true
12+
// ignore textlint's node type
13+
ignoreInlineNodeTypes: undefined
1114
};
1215
export default function textlintRousseau(context, options = defaultOptions) {
1316
const helper = new RuleHelper(context);
1417
const {Syntax, RuleError, report, getSource} = context;
1518
const showLevels = options.showLevels || defaultOptions.showLevels;
1619
const ignoreTypes = options.ignoreTypes || defaultOptions.ignoreTypes;
17-
const fakeInlineCode = options.fakeInlineCode || defaultOptions.fakeInlineCode;
20+
const ignoreInlineNodeTypes = options.ignoreInlineNodeTypes || [Syntax.Image, Syntax.Code, Syntax.Link];
1821
const isShowType = (type)=> {
1922
return ignoreTypes.indexOf(type) === -1;
2023
};
@@ -74,29 +77,23 @@ export default function textlintRousseau(context, options = defaultOptions) {
7477
});
7578
report(node, ruleError);
7679
};
80+
7781
return {
7882
[Syntax.Paragraph](node){
7983
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
8084
return;
8185
}
82-
// fake `code`
83-
if (fakeInlineCode) {
84-
node.children = node.children.map(childNode => {
85-
if (childNode.type === Syntax.Code) {
86-
return {
87-
type: Syntax.Str,
88-
value: "code",
89-
raw: "code",
90-
loc: childNode.loc,
91-
range: childNode.range
92-
}
93-
}
94-
return childNode;
95-
});
86+
const filteredNode = filter(node, (node) => {
87+
return ignoreInlineNodeTypes.indexOf(node.type) === -1;
88+
});
89+
if (!filteredNode) {
90+
return;
9691
}
97-
const source = new StringSource(node);
92+
const source = new StringSource(filteredNode);
9893
const text = source.toString();
99-
const reportSourceError = reportError.bind(null, node, source);
94+
const reportSourceError = (results) => {
95+
reportError(node, source, results);
96+
};
10097
rousseau(text, function (err, results) {
10198
if (err) {
10299
throw err;

test/textlint-rule-rousseau-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +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.`",
1016
"This is pen.",
1117
{
1218
text: "this is pen.",

0 commit comments

Comments
 (0)