Skip to content

Commit c8fb78b

Browse files
committed
feat(wrap): add wrap function
1 parent 844a361 commit c8fb78b

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/wrapIgnore.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// MIT © 2018 azu
2+
"use strict";
3+
import { ASTNodeTypes } from "@textlint/ast-node-types";
4+
import RuleHelper from "./textlint-rule-helper.js";
5+
6+
/**
7+
* @param {{ignoreNodeTypes:ASTNodeTypes[]}} options
8+
* @param {object} nodeHandlers
9+
*/
10+
export function wrap(options, nodeHandlers = {}) {
11+
const ignoreNodeTypes = options.ignoreNodeTypes || [];
12+
const ruleHelper = new RuleHelper({});
13+
Object.keys(nodeHandlers).forEach(nodeType => {
14+
const nodeHandler = nodeHandlers[nodeType];
15+
const wrappedNodeHandler = (node) => {
16+
if (ruleHelper.isChildNode(node, ignoreNodeTypes)) {
17+
return;
18+
}
19+
return nodeHandler(node);
20+
};
21+
nodeHandlers[nodeType] = wrappedNodeHandler
22+
});
23+
return nodeHandlers;
24+
}

test/wrapIgnore-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// LICENSE : MIT
2+
import assert from 'assert'
3+
import { textlint } from "textlint"
4+
import { wrap } from "../src/wrapIgnore.js";
5+
6+
describe("wrap", function() {
7+
afterEach(function() {
8+
textlint.resetRules();
9+
});
10+
describe("ignoreNodeTypes", () => {
11+
it("should ignore multiple nodes", () => {
12+
let isCalled = false;
13+
textlint.setupRules({
14+
"rule-key": function(context) {
15+
return wrap({
16+
ignoreNodeTypes: [context.Syntax.BlockQuote]
17+
}, {
18+
[context.Syntax.Str](node) {
19+
isCalled = true
20+
}
21+
})
22+
}
23+
});
24+
const text = `> This should be ignored.`;
25+
return textlint.lintText(text, ".md").then(() => {
26+
assert.ok(isCalled === false, "Str node handler should not be called");
27+
});
28+
});
29+
});
30+
});

0 commit comments

Comments
 (0)