diff --git a/src/IgnoreNodeManager.js b/src/IgnoreNodeManager.js index 51445a1..2a45c90 100644 --- a/src/IgnoreNodeManager.js +++ b/src/IgnoreNodeManager.js @@ -14,6 +14,13 @@ export default class IgnoreNodeManager { this._ignoredRangeList = [] } + /** + * @returns {(number)[][]} + */ + get ignoredRanges() { + return this._ignoredRangeList; + } + /** * |.......| * ^ ^ @@ -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; }) } @@ -79,4 +86,4 @@ export default class IgnoreNodeManager { } }); } -} \ No newline at end of file +} diff --git a/test/IgnoreNodeManager-test.js b/test/IgnoreNodeManager-test.js index 210d695..8b23d36 100644 --- a/test/IgnoreNodeManager-test.js +++ b/test/IgnoreNodeManager-test.js @@ -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]]); }); }); });