Skip to content

Commit

Permalink
fix(ignoreNodeManger): igIgnoredIndex did ignored over range
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 20, 2018
1 parent 08bbc57 commit c73703a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/IgnoreNodeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export default class IgnoreNodeManager {
this._ignoredRangeList = []
}

/**
* @returns {(number)[][]}
*/
get ignoredRanges() {
return this._ignoredRangeList;
}

/**
* |.......|
* ^ ^
Expand All @@ -28,7 +35,7 @@ export default class IgnoreNodeManager {
isIgnoredIndex(index) {
return this._ignoredRangeList.some(range => {
const [start, end] = range;
return start <= index && index <= end;
return start <= index && index < end;
})
}

Expand Down Expand Up @@ -79,4 +86,4 @@ export default class IgnoreNodeManager {
}
});
}
}
}
115 changes: 92 additions & 23 deletions test/IgnoreNodeManager-test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,103 @@
// LICENSE : MIT
import assert from 'assert'
import { textlint } from "textlint"
import IgnoreNodeManager from "../src/IgnoreNodeManager";
import {textlint} from "textlint"
describe("IgnoreNodeManager", function () {
afterEach(function () {

describe("IgnoreNodeManager", function() {
afterEach(function() {
textlint.resetRules();
});
describe("#ignoreChildrenByTypes()", ()=> {
context("when the parent node is Paragraph, the child node is Str", ()=> {
it("should ignore range by index", () => {
const text = "text`code`text";
let isIgnored = false;
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function (context) {
const {Syntax, RuleError, report, getSource} = context;
return {
[Syntax.Paragraph](node) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
const text = getSource(node);
const codeIndex = text.search("code");
isIgnored = ignoreManager.isIgnoredIndex(codeIndex);
}
describe("#ignoreChildrenByTypes()", () => {
it("should ignore multiple nodes", () => {
const text = `
- This is \`ignored\`
- This is not ignored
This is **ignored**.
`;
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function(context) {
const { Syntax, RuleError, report, getSource } = context;
return {
[Syntax.Paragraph](node) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code, context.Syntax.Strong]);
}
}
}
});
return textlint.lintMarkdown(text).then(() => {
assert.deepStrictEqual(ignoreManager.ignoredRanges, [
[11, 20],
[
52,
63
]
]);
});
});
it("should ignore range by index", () => {
const text = "123`456`789";
const expectedList = [
{
name: "1",
ignored: false
},
{
name: "2",
ignored: false
},
{
name: "3",
ignored: false
},
{
name: "4",
ignored: true
},
{
name: "5",
ignored: true
},
{
name: "6",
ignored: true
},
{
name: "7",
ignored: false
},
{
name: "8",
ignored: false
},
{
name: "9",
ignored: false
},
];
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function(context) {
const { Syntax, RuleError, report, getSource } = context;
return {
[Syntax.Paragraph](node) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
const text = getSource(node);
expectedList.forEach(item => {
const index = text.search(item.name);
item["actual"] = ignoreManager.isIgnoredIndex(index);
})
}
}
}
});
return textlint.lintMarkdown(text).then(() => {
expectedList.forEach(item => {
assert.strictEqual(item.actual, item.ignored, `${item.name} should be ${item.ignored ? "ignored"
: "includes"}`);
});
return textlint.lintMarkdown(text).then(()=> {
assert.ok(isIgnored);
assert.deepStrictEqual(ignoreManager["_ignoredRangeList"], [[4, 10]]);
});
assert.deepStrictEqual(ignoreManager.ignoredRanges, [[3, 8]]);
});
});
});
Expand Down

0 comments on commit c73703a

Please sign in to comment.