-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); | ||
}); |