UNPKG

textlint-filter-rule-comments

Version:
63 lines (62 loc) 3.93 kB
// LICENSE : MIT "use strict"; var _StatusManager = _interopRequireDefault(require("./StatusManager")); var _parseComment = require("./parse-comment"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var defaultOptions = { // enable comment directive // if comment has the value, then enable textlint rule "enablingComment": "textlint-enable", // disable comment directive // if comment has the value, then disable textlint rule "disablingComment": "textlint-disable" }; module.exports = function (context) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOptions; var Syntax = context.Syntax, shouldIgnore = context.shouldIgnore, getSource = context.getSource; var enablingComment = options.enablingComment || defaultOptions.enablingComment; var disablingComment = options.disablingComment || defaultOptions.disablingComment; var content = getSource(); var statusManager = new _StatusManager["default"](content.length); // Get comment value return _defineProperty(_defineProperty(_defineProperty({}, Syntax.Html, function (node) { var nodeValue = getSource(node); if (!(0, _parseComment.isHTMLComment)(nodeValue)) { return; } var comments = (0, _parseComment.getValuesFromHTMLComment)(nodeValue); comments.forEach(function (commentValue) { if (commentValue.indexOf(enablingComment) !== -1) { var configValue = (0, _parseComment.removeCommentDescription)(commentValue.replace(enablingComment, "")); statusManager.enableReporting(node, (0, _parseComment.parseRuleIds)(configValue)); } else if (commentValue.indexOf(disablingComment) !== -1) { var _configValue = (0, _parseComment.removeCommentDescription)(commentValue.replace(disablingComment, "")); statusManager.disableReporting(node, (0, _parseComment.parseRuleIds)(_configValue)); } }); }), Syntax.Comment, function (node) { var commentValue = node.value || ""; if (commentValue.indexOf(enablingComment) !== -1) { var configValue = (0, _parseComment.removeCommentDescription)(commentValue.replace(enablingComment, "")); statusManager.enableReporting(node, (0, _parseComment.parseRuleIds)(configValue)); } else if (commentValue.indexOf(disablingComment) !== -1) { var _configValue2 = (0, _parseComment.removeCommentDescription)(commentValue.replace(disablingComment, "")); statusManager.disableReporting(node, (0, _parseComment.parseRuleIds)(_configValue2)); } }), "".concat(Syntax.Document, ":exit"), function exit() { var ignoringMessages = statusManager.getIgnoringMessages(); ignoringMessages.forEach(function (message) { var range = [message.startIndex, message.endIndex]; shouldIgnore(range, { ruleId: message.ruleId || "*" }); }); }); }; //# sourceMappingURL=textlint-filter-rule-comments.js.map