Skip to content

Commit

Permalink
feat(wrap): add wrap function
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Dec 27, 2018
1 parent 844a361 commit c8fb78b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/wrapIgnore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// MIT © 2018 azu
"use strict";
import { ASTNodeTypes } from "@textlint/ast-node-types";
import RuleHelper from "./textlint-rule-helper.js";

/**
* @param {{ignoreNodeTypes:ASTNodeTypes[]}} options
* @param {object} nodeHandlers
*/
export function wrap(options, nodeHandlers = {}) {
const ignoreNodeTypes = options.ignoreNodeTypes || [];
const ruleHelper = new RuleHelper({});
Object.keys(nodeHandlers).forEach(nodeType => {
const nodeHandler = nodeHandlers[nodeType];
const wrappedNodeHandler = (node) => {
if (ruleHelper.isChildNode(node, ignoreNodeTypes)) {
return;
}
return nodeHandler(node);
};
nodeHandlers[nodeType] = wrappedNodeHandler
});
return nodeHandlers;
}
30 changes: 30 additions & 0 deletions test/wrapIgnore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// LICENSE : MIT
import assert from 'assert'
import { textlint } from "textlint"
import { wrap } from "../src/wrapIgnore.js";

describe("wrap", function() {
afterEach(function() {
textlint.resetRules();
});
describe("ignoreNodeTypes", () => {
it("should ignore multiple nodes", () => {
let isCalled = false;
textlint.setupRules({
"rule-key": function(context) {
return wrap({
ignoreNodeTypes: [context.Syntax.BlockQuote]
}, {
[context.Syntax.Str](node) {
isCalled = true
}
})
}
});
const text = `> This should be ignored.`;
return textlint.lintText(text, ".md").then(() => {
assert.ok(isCalled === false, "Str node handler should not be called");
});
});
});
});

0 comments on commit c8fb78b

Please sign in to comment.