textlint-rule-helper
Advanced tools
Comparing version 2.0.1 to 2.1.0
// LICENSE : MIT | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var visit = require('unist-util-visit'); | ||
@@ -28,25 +9,21 @@ /** | ||
*/ | ||
var IgnoreNodeManager = | ||
/*#__PURE__*/ | ||
function () { | ||
function IgnoreNodeManager() { | ||
_classCallCheck(this, IgnoreNodeManager); | ||
var IgnoreNodeManager = /** @class */ (function () { | ||
function IgnoreNodeManager() { | ||
/** | ||
* @type {[number,number][]} | ||
* @private | ||
*/ | ||
this._ignoredRangeList = []; | ||
} | ||
Object.defineProperty(IgnoreNodeManager.prototype, "ignoredRanges", { | ||
/** | ||
* @returns {(number)[][]} | ||
*/ | ||
get: function () { | ||
return this._ignoredRangeList; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
* @type {[number,number][]} | ||
* @private | ||
*/ | ||
this._ignoredRangeList = []; | ||
} | ||
/** | ||
* @returns {(number)[][]} | ||
*/ | ||
_createClass(IgnoreNodeManager, [{ | ||
key: "isIgnoredIndex", | ||
/** | ||
* |.......| | ||
@@ -62,22 +39,16 @@ * ^ ^ | ||
*/ | ||
value: function isIgnoredIndex(index) { | ||
return this._ignoredRangeList.some(function (range) { | ||
var _range = _slicedToArray(range, 2), | ||
start = _range[0], | ||
end = _range[1]; | ||
return start <= index && index < end; | ||
}); | ||
} | ||
IgnoreNodeManager.prototype.isIgnoredIndex = function (index) { | ||
return this._ignoredRangeList.some(function (range) { | ||
var start = range[0], end = range[1]; | ||
return start <= index && index < end; | ||
}); | ||
}; | ||
/** | ||
* @param {[number, number]} aRange | ||
* @returns {boolean} | ||
* @deprecated This method will be removed | ||
*/ | ||
}, { | ||
key: "isIgnoredRange", | ||
value: function isIgnoredRange(aRange) { | ||
var index = aRange[0]; | ||
return this.isIgnoredIndex(index); | ||
} | ||
IgnoreNodeManager.prototype.isIgnoredRange = function (aRange) { | ||
return this.isIgnoredIndex(aRange[0]) || this.isIgnoredIndex(aRange[1]); | ||
}; | ||
/** | ||
@@ -87,9 +58,6 @@ * @param {Object} node | ||
*/ | ||
}, { | ||
key: "isIgnored", | ||
value: function isIgnored(node) { | ||
var index = node.index; | ||
return this.isIgnoredIndex(index); | ||
} | ||
IgnoreNodeManager.prototype.isIgnored = function (node) { | ||
var index = node.index; | ||
return this.isIgnoredIndex(index); | ||
}; | ||
/** | ||
@@ -99,8 +67,5 @@ * add node to ignore range list | ||
*/ | ||
}, { | ||
key: "ignore", | ||
value: function ignore(node) { | ||
this.ignoreRange(node.range); | ||
} | ||
IgnoreNodeManager.prototype.ignore = function (node) { | ||
this.ignoreRange(node.range); | ||
}; | ||
/** | ||
@@ -110,8 +75,5 @@ * add range to ignore range list | ||
*/ | ||
}, { | ||
key: "ignoreRange", | ||
value: function ignoreRange(range) { | ||
this._ignoredRangeList.push(range); | ||
} | ||
IgnoreNodeManager.prototype.ignoreRange = function (range) { | ||
this._ignoredRangeList.push(range); | ||
}; | ||
/** | ||
@@ -123,25 +85,13 @@ * ignore children node of `node`, | ||
*/ | ||
}, { | ||
key: "ignoreChildrenByTypes", | ||
value: function ignoreChildrenByTypes(targetNode, ignoredNodeTypes) { | ||
var _this = this; | ||
visit(targetNode, function (visitedNode) { | ||
if (ignoredNodeTypes.indexOf(visitedNode.type) !== -1) { | ||
_this.ignore(visitedNode); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "ignoredRanges", | ||
get: function get() { | ||
return this._ignoredRangeList; | ||
} | ||
}]); | ||
return IgnoreNodeManager; | ||
}(); | ||
IgnoreNodeManager.prototype.ignoreChildrenByTypes = function (targetNode, ignoredNodeTypes) { | ||
var _this = this; | ||
visit(targetNode, function (visitedNode) { | ||
if (ignoredNodeTypes.indexOf(visitedNode.type) !== -1) { | ||
_this.ignore(visitedNode); | ||
} | ||
}); | ||
}; | ||
return IgnoreNodeManager; | ||
}()); | ||
exports.default = IgnoreNodeManager; | ||
//# sourceMappingURL=IgnoreNodeManager.js.map |
@@ -1,14 +0,12 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
var _textlintRuleHelper = _interopRequireDefault(require("./textlint-rule-helper")); | ||
var _IgnoreNodeManager = _interopRequireDefault(require("./IgnoreNodeManager")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
module.exports = { | ||
IgnoreNodeManager: _IgnoreNodeManager.default, | ||
RuleHelper: _textlintRuleHelper.default | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var textlint_rule_helper_1 = __importDefault(require("./textlint-rule-helper")); | ||
exports.RuleHelper = textlint_rule_helper_1.default; | ||
var IgnoreNodeManager_1 = __importDefault(require("./IgnoreNodeManager")); | ||
exports.IgnoreNodeManager = IgnoreNodeManager_1.default; | ||
var wrap_report_handler_1 = require("./wrap-report-handler"); | ||
exports.wrapReportHandler = wrap_report_handler_1.wrapReportHandler; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
// LICENSE : MIT | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -7,49 +7,26 @@ * RuleHelper is helper class for textlint. | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var RuleHelper = | ||
/*#__PURE__*/ | ||
function () { | ||
/** | ||
* Initialize RuleHelper with RuleContext object. | ||
* @param {RuleContext} ruleContext the ruleContext is context object of the rule. | ||
*/ | ||
function RuleHelper(ruleContext) { | ||
_classCallCheck(this, RuleHelper); | ||
this.ruleContext = ruleContext; | ||
} | ||
/** | ||
* Get parents of node. | ||
* The parent nodes are returned in order from the closest parent to the outer ones. | ||
* {@link node} is not contained in the results. | ||
* @param {TxtNode} node the node is start point. | ||
* @returns {TxtNode[]} | ||
*/ | ||
_createClass(RuleHelper, [{ | ||
key: "getParents", | ||
value: function getParents(node) { | ||
var result = []; | ||
var parent = node.parent; | ||
while (parent != null) { | ||
result.push(parent); | ||
parent = parent.parent; | ||
} | ||
return result; | ||
var RuleHelper = /** @class */ (function () { | ||
/** | ||
* Initialize RuleHelper with RuleContext object. | ||
* @param {RuleContext} ruleContext the ruleContext is context object of the rule. | ||
*/ | ||
function RuleHelper(ruleContext) { | ||
this._ruleContext = ruleContext; | ||
} | ||
/** | ||
* Get parents of node. | ||
* The parent nodes are returned in order from the closest parent to the outer ones. | ||
* {@link node} is not contained in the results. | ||
* @param {TxtNode} node the node is start point. | ||
*/ | ||
RuleHelper.prototype.getParents = function (node) { | ||
var result = []; | ||
var parent = node.parent; | ||
while (parent != null) { | ||
result.push(parent); | ||
parent = parent.parent; | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Return true if `node` is wrapped any one of node {@link types}. | ||
@@ -60,22 +37,16 @@ * @param {TxtNode} node is target node | ||
*/ | ||
}, { | ||
key: "isChildNode", | ||
value: function isChildNode(node, types) { | ||
var parents = this.getParents(node); | ||
var parentsTypes = parents.map(function (parent) { | ||
return parent.type; | ||
}); | ||
return types.some(function (type) { | ||
return parentsTypes.some(function (parentType) { | ||
return parentType === type; | ||
RuleHelper.prototype.isChildNode = function (node, types) { | ||
var parents = this.getParents(node); | ||
var parentsTypes = parents.map(function (parent) { | ||
return parent.type; | ||
}); | ||
}); | ||
} | ||
}]); | ||
return RuleHelper; | ||
}(); | ||
return types.some(function (type) { | ||
return parentsTypes.some(function (parentType) { | ||
return parentType === type; | ||
}); | ||
}); | ||
}; | ||
return RuleHelper; | ||
}()); | ||
exports.default = RuleHelper; | ||
//# sourceMappingURL=textlint-rule-helper.js.map |
{ | ||
"name": "textlint-rule-helper", | ||
"version": "2.1.0", | ||
"description": "Helper for textlint rule.", | ||
"version": "2.0.1", | ||
"homepage": "https://github.com/textlint/textlint-rule-helper/", | ||
"bugs": { | ||
"url": "https://github.com/textlint/textlint-rule-helper/issues" | ||
}, | ||
"repository": { | ||
@@ -10,3 +13,4 @@ "type": "git", | ||
}, | ||
"main": "lib/index.js", | ||
"license": "MIT", | ||
"author": "azu", | ||
"files": [ | ||
@@ -16,26 +20,29 @@ "lib", | ||
], | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
"build": "babel src/ --out-dir lib/ --source-maps", | ||
"watch": "babel src/ --out-dir lib/ --watch --source-maps", | ||
"test": "mocha test/", | ||
"prepublish": "npm run build" | ||
"build": "cross-env NODE_ENV=production tsc -p .", | ||
"prepublish": "npm run --if-present build", | ||
"test": "mocha \"test/**/*.{js,ts}\"", | ||
"watch": "tsc -p . --watch" | ||
}, | ||
"author": "azu", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/textlint/textlint-rule-helper/issues" | ||
"dependencies": { | ||
"@textlint/ast-node-types": "^4.2.1", | ||
"@textlint/types": "^1.1.2", | ||
"structured-source": "^3.0.2", | ||
"unist-util-visit": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/register": "^7.0.0", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.18", | ||
"@types/structured-source": "^3.0.0", | ||
"cross-env": "^5.2.0", | ||
"markdown-to-ast": "^6.0.3", | ||
"mocha": "^5.2.0", | ||
"textlint": "^11.0.0", | ||
"txt-ast-traverse": "^2.0.4" | ||
}, | ||
"dependencies": { | ||
"unist-util-visit": "^1.1.0" | ||
"textlint": "^11.2.1", | ||
"ts-node": "^7.0.1", | ||
"ts-node-test-register": "^4.0.0", | ||
"txt-ast-traverse": "^2.0.4", | ||
"typescript": "^3.2.2" | ||
} | ||
} |
@@ -92,3 +92,3 @@ # textlint-rule-helper [![Build Status](https://travis-ci.org/textlint/textlint-rule-helper.svg?branch=master)](https://travis-ci.org/textlint/textlint-rule-helper) | ||
## Example | ||
### RuleHelper and IgnoreNodeManager Example | ||
@@ -98,4 +98,4 @@ A rule for [textlint](https://github.com/textlint/textlint "textlint"). | ||
```js | ||
var RuleHelper = require("textlint-rule-helper").RuleHelper; | ||
var IgnoreNodeManager = require("textlint-rule-helper").IgnoreNodeManager; | ||
import { RuleHelper } from "textlint-rule-helper"; | ||
import { IgnoreNodeManager } from "textlint-rule-helper"; | ||
module.exports = function(context) { | ||
@@ -133,2 +133,53 @@ var helper = new RuleHelper(context); | ||
## `wrapReportHandler(context, options, handler): TextlintRuleReportHandler` | ||
**Params** | ||
- context `TextlintRuleContent` - rule context object | ||
- options `{{ignoreNodeTypes: TxtNodeType[]}}` - options | ||
- handler `(report: (node: AnyTxtNode, ruleError: TextlintRuleError) => void) => any` - handler should return a object | ||
`wrapReportHandler` is high level API that use `RuleHelper` and `IgnoreNodeManager`. | ||
It aim to easy to ignore some Node type for preventing unnecessary error report. | ||
Example: ignore `BlockQuote` and `Code` node. | ||
```js | ||
import { wrapReportHandler } from "textlint-rule-helper"; | ||
const reporter = function (context) { | ||
const { Syntax, getSource } = context; | ||
return wrapReportHandler({ | ||
ignoreNodeTypes: [Syntax.BlockQuote, Syntax.Code] | ||
}, context, report => { // <= wrap version of context.report | ||
// handler should return a rule handler object | ||
return { | ||
[Syntax.Paragraph](node) { | ||
const text = getSource(node); | ||
const index = text.search("code"); | ||
/* | ||
* Following text is matched, but it will not reported. | ||
* ---- | ||
* This is `code`. | ||
* > code | ||
* ---- | ||
*/ | ||
if(index === -1){ | ||
return; | ||
} | ||
report(node, new context.RuleError(item.name, { | ||
index | ||
})); | ||
} | ||
} | ||
}); | ||
}; | ||
module.exports = reporter; | ||
``` | ||
The Mechanism of `wrapReportHandler`: ` | ||
- Ignore all parent nodes that are matched with `ignoreNodeTypes`. | ||
- Ignore all children nodes that are matched with `ignoreNodeTypes`. | ||
- `wrapReportHandler` create custom `report` function that ignore matched node | ||
## Use-Case | ||
@@ -135,0 +186,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40729
26
697
215
4
11
1
+ Added@textlint/types@^1.1.2
+ Addedstructured-source@^3.0.2
+ Added@textlint/ast-node-types@4.4.3(transitive)
+ Added@textlint/types@1.5.5(transitive)
+ Addedboundary@1.0.1(transitive)
+ Addedstructured-source@3.0.2(transitive)