Skip to content

Commit 5159710

Browse files
committed
feat(src): add IgnoreNodeManager
1 parent a2aa2bb commit 5159710

File tree

6 files changed

+133
-5
lines changed

6 files changed

+133
-5
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"src"
1414
],
1515
"scripts": {
16-
"build": "babel src --out-dir lib --source-maps inline",
16+
"build": "NODE_ENV=production babel src --out-dir lib --source-maps inline",
1717
"watch": "babel src --out-dir lib --watch --source-maps inline",
1818
"docs": "jsdoc2md -t template/README.template.md lib/textlint-rule-helper.js > README.md",
1919
"test": "npm run build && mocha test/*.js"
@@ -36,5 +36,8 @@
3636
"source-map-support": "^0.2.8",
3737
"textlint": "^5.0.2",
3838
"txt-ast-traverse": "^1.2.0"
39+
},
40+
"dependencies": {
41+
"unist-util-visit": "^1.1.0"
3942
}
4043
}

src/IgnoreNodeManger.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
const visit = require('unist-util-visit');
4+
/**
5+
* Ignore node manager that manager ignored ranges.
6+
*
7+
*/
8+
export default class IgnoreManger {
9+
constructor() {
10+
/**
11+
* @type {[number,number][]}
12+
* @private
13+
*/
14+
this._ignoredRangeList = []
15+
}
16+
17+
/**
18+
* |.......|
19+
* ^ ^
20+
* Ignored Range
21+
*
22+
* |........|
23+
* ^
24+
* index
25+
* @param {number} index
26+
* @returns {boolean}
27+
*/
28+
isIgnoredIndex(index) {
29+
return this._ignoredRangeList.some(range => {
30+
const [start, end] = range;
31+
return start <= index && index <= end;
32+
})
33+
}
34+
35+
/**
36+
* @param {[number, number]} aRange
37+
* @returns {boolean}
38+
*/
39+
isIgnoredRange(aRange) {
40+
const index = aRange[0];
41+
return this.isIgnoredIndex(index);
42+
}
43+
44+
/**
45+
* @param {Object} node
46+
* @returns {boolean}
47+
*/
48+
isIgnored(node) {
49+
const index = node.index;
50+
return this.isIgnoredIndex(index);
51+
}
52+
53+
/**
54+
* add node to ignore range list
55+
* @param {Object} node
56+
*/
57+
ignore(node) {
58+
this.ignoreRange(node.range);
59+
}
60+
61+
/**
62+
* add range to ignore range list
63+
* @param {[number, number]} range
64+
*/
65+
ignoreRange(range) {
66+
this._ignoredRangeList.push(range);
67+
}
68+
69+
/**
70+
* ignore children node of `node`,
71+
* if the children node has the type that is included in `ignoredNodeTypes`.
72+
* @param {Node} targetNode
73+
* @param {string[]} ignoredNodeTypes
74+
*/
75+
ignoreChildrenByTypes(targetNode, ignoredNodeTypes) {
76+
visit(targetNode, visitedNode => {
77+
if (ignoredNodeTypes.indexOf(visitedNode.type) !== -1) {
78+
this.ignore(visitedNode);
79+
}
80+
});
81+
}
82+
}

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import RuleHelper from "./textlint-rule-helper";
4+
import IgnoreManager from "./IgnoreNodeManger"
5+
module.exports = {
6+
IgnoreManager,
7+
RuleHelper
8+
};

src/textlint-rule-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* RuleHelper is helper class for textlint.
55
* @class RuleHelper
66
*/
7-
export class RuleHelper {
7+
export default class RuleHelper {
88
/**
99
* Initialize RuleHelper with RuleContext object.
1010
* @param {RuleContext} ruleContext the ruleContext is context object of the rule.

test/IgnoreNodeManger-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import assert from 'power-assert'
4+
import IgnoreNodeManger from "../src/IgnoreNodeManger";
5+
import {textlint} from "textlint"
6+
describe("IgnoreNodeManger", function () {
7+
afterEach(function () {
8+
textlint.resetRules();
9+
});
10+
describe("#ignoreChildrenByTypes()", ()=> {
11+
context("when the parent node is Paragraph, the child node is Str", ()=> {
12+
it("should ignore range by index", () => {
13+
var text = "text`code`text";
14+
var isIgnored = false;
15+
const ignoreManager = new IgnoreNodeManger();
16+
textlint.setupRules({
17+
"rule-key": function (context) {
18+
const {Syntax, RuleError, report, getSource} = context;
19+
return {
20+
[Syntax.Paragraph](node) {
21+
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
22+
const text = getSource(node);
23+
const codeIndex = text.search("code");
24+
isIgnored = ignoreManager.isIgnoredIndex(codeIndex);
25+
}
26+
}
27+
}
28+
});
29+
return textlint.lintMarkdown(text).then(()=> {
30+
assert.ok(isIgnored);
31+
assert.deepEqual(ignoreManager._ignoredRangeList, [[4, 10]]);
32+
33+
});
34+
});
35+
});
36+
});
37+
});

test/textlint-rule-helper-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
3-
import {parse,Syntax} from "markdown-to-ast"
4-
import {traverse} from "txt-ast-traverse"
53
import assert from 'power-assert'
6-
import {RuleHelper} from "../src/textlint-rule-helper";
4+
import RuleHelper from "../src/textlint-rule-helper";
75
import {textlint} from "textlint"
86
describe("textlint-rule-helper-test", function () {
97
afterEach(function () {

0 commit comments

Comments
 (0)