textlint-rule-prh
Advanced tools
@@ -12,87 +12,95 @@ // LICENSE : MIT | ||
if (RegExp.prototype.flags === undefined) { | ||
Object.defineProperty(RegExp.prototype, "flags", { | ||
configurable: true, | ||
get: function get() { | ||
return this.toString().match(/[gimuy]*$/)[0]; | ||
} | ||
}); | ||
Object.defineProperty(RegExp.prototype, "flags", { | ||
configurable: true, | ||
get: function get() { | ||
return this.toString().match(/[gimuy]*$/)[0]; | ||
} | ||
}); | ||
} | ||
var prh = require("prh"); | ||
var path = require("path"); | ||
var untildify = require("untildify"); | ||
var defaultOptions = { | ||
checkLink: false, | ||
checkBlockQuote: false, | ||
checkEmphasis: false, | ||
checkHeader: true | ||
checkLink: false, | ||
checkBlockQuote: false, | ||
checkEmphasis: false, | ||
checkHeader: true | ||
}; | ||
function createPrhEngine(rulePaths, baseDir) { | ||
if (rulePaths.length === 0) { | ||
return null; | ||
} | ||
var expandedRulePaths = rulePaths.map(function (rulePath) { | ||
return untildify(rulePath); | ||
}); | ||
var prhEngine = prh.fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0])); | ||
expandedRulePaths.slice(1).forEach(function (ruleFilePath) { | ||
var config = prh.fromYAMLFilePath(path.resolve(baseDir, ruleFilePath)); | ||
prhEngine.merge(config); | ||
}); | ||
return prhEngine; | ||
if (rulePaths.length === 0) { | ||
return null; | ||
} | ||
var expandedRulePaths = rulePaths.map(function (rulePath) { | ||
return untildify(rulePath); | ||
}); | ||
var prhEngine = prh.fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0])); | ||
expandedRulePaths.slice(1).forEach(function (ruleFilePath) { | ||
var config = prh.fromYAMLFilePath(path.resolve(baseDir, ruleFilePath)); | ||
prhEngine.merge(config); | ||
}); | ||
return prhEngine; | ||
} | ||
function createPrhEngineFromContents(yamlContents) { | ||
if (yamlContents.length === 0) { | ||
return null; | ||
} | ||
var dummyFilePath = ""; | ||
var prhEngine = prh.fromYAML(dummyFilePath, yamlContents[0]); | ||
yamlContents.slice(1).forEach(function (content) { | ||
var config = prh.fromYAML(dummyFilePath, content); | ||
prhEngine.merge(config); | ||
}); | ||
return prhEngine; | ||
if (yamlContents.length === 0) { | ||
return null; | ||
} | ||
var dummyFilePath = ""; | ||
var prhEngine = prh.fromYAML(dummyFilePath, yamlContents[0]); | ||
yamlContents.slice(1).forEach(function (content) { | ||
var config = prh.fromYAML(dummyFilePath, content); | ||
prhEngine.merge(config); | ||
}); | ||
return prhEngine; | ||
} | ||
function mergePrh() { | ||
for (var _len = arguments.length, engines = Array(_len), _key = 0; _key < _len; _key++) { | ||
engines[_key] = arguments[_key]; | ||
} | ||
for (var _len = arguments.length, engines = new Array(_len), _key = 0; _key < _len; _key++) { | ||
engines[_key] = arguments[_key]; | ||
} | ||
var engines_ = engines.filter(function (engine) { | ||
return !!engine; | ||
}); | ||
var mainEngine = engines_[0]; | ||
engines_.slice(1).forEach(function (engine) { | ||
mainEngine.merge(engine); | ||
}); | ||
return mainEngine; | ||
var engines_ = engines.filter(function (engine) { | ||
return !!engine; | ||
}); | ||
var mainEngine = engines_[0]; | ||
engines_.slice(1).forEach(function (engine) { | ||
mainEngine.merge(engine); | ||
}); | ||
return mainEngine; | ||
} | ||
var assertOptions = function assertOptions(options) { | ||
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") { | ||
throw new Error("textlint-rule-prh require Rule Options.\nPlease set .textlinrc:\n{\n \"rules\": {\n \"prh\": {\n \"rulePaths\" :[\"path/to/prh.yml\"]\n }\n }\n}\n"); | ||
} | ||
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") { | ||
throw new Error("textlint-rule-prh require Rule Options.\nPlease set .textlinrc:\n{\n \"rules\": {\n \"prh\": {\n \"rulePaths\" :[\"path/to/prh.yml\"]\n }\n }\n}\n"); | ||
} | ||
}; | ||
var createIgnoreNodeTypes = function createIgnoreNodeTypes(options, Syntax) { | ||
var nodeTypes = []; | ||
if (!options.checkLink) { | ||
nodeTypes.push(Syntax.Link); | ||
} | ||
if (!options.checkBlockQuote) { | ||
nodeTypes.push(Syntax.BlockQuote); | ||
} | ||
if (!options.checkEmphasis) { | ||
nodeTypes.push(Syntax.Emphasis); | ||
} | ||
if (!options.checkHeader) { | ||
nodeTypes.push(Syntax.Header); | ||
} | ||
return nodeTypes; | ||
var nodeTypes = []; | ||
if (!options.checkLink) { | ||
nodeTypes.push(Syntax.Link); | ||
} | ||
if (!options.checkBlockQuote) { | ||
nodeTypes.push(Syntax.BlockQuote); | ||
} | ||
if (!options.checkEmphasis) { | ||
nodeTypes.push(Syntax.Emphasis); | ||
} | ||
if (!options.checkHeader) { | ||
nodeTypes.push(Syntax.Header); | ||
} | ||
return nodeTypes; | ||
}; | ||
/** | ||
@@ -109,101 +117,106 @@ * for each diff of changeSet | ||
*/ | ||
var forEachChange = function forEachChange(changeSet, str, onChangeOfMatch) { | ||
var sortedDiffs = changeSet.diffs.sort(function (a, b) { | ||
return a.index - b.index; | ||
var sortedDiffs = changeSet.diffs.sort(function (a, b) { | ||
return a.index - b.index; | ||
}); | ||
var delta = 0; | ||
sortedDiffs.forEach(function (diff) { | ||
var result = diff.expected.replace(/\$([0-9]{1,2})/g, function (match, g1) { | ||
var index = parseInt(g1); | ||
if (index === 0 || diff.matches.length - 1 < index) { | ||
return match; | ||
} | ||
return diff.matches[index] || ""; | ||
}); // matchStartIndex/matchEndIndex value is original position, not replaced position | ||
// textlint use original position | ||
var matchStartIndex = diff.index; | ||
var matchEndIndex = matchStartIndex + diff.matches[0].length; // actual => expected | ||
var actual = str.slice(diff.index + delta, diff.index + delta + diff.matches[0].length); | ||
var prh = diff.rule.raw.prh || null; | ||
onChangeOfMatch({ | ||
matchStartIndex: matchStartIndex, | ||
matchEndIndex: matchEndIndex, | ||
actual: actual, | ||
expected: result, | ||
prh: prh | ||
}); | ||
var delta = 0; | ||
sortedDiffs.forEach(function (diff) { | ||
var result = diff.expected.replace(/\$([0-9]{1,2})/g, function (match, g1) { | ||
var index = parseInt(g1); | ||
if (index === 0 || diff.matches.length - 1 < index) { | ||
return match; | ||
} | ||
return diff.matches[index] || ""; | ||
}); | ||
// matchStartIndex/matchEndIndex value is original position, not replaced position | ||
// textlint use original position | ||
var matchStartIndex = diff.index; | ||
var matchEndIndex = matchStartIndex + diff.matches[0].length; | ||
// actual => expected | ||
var actual = str.slice(diff.index + delta, diff.index + delta + diff.matches[0].length); | ||
var prh = diff.rule.raw.prh || null; | ||
onChangeOfMatch({ | ||
matchStartIndex: matchStartIndex, | ||
matchEndIndex: matchEndIndex, | ||
actual: actual, | ||
expected: result, | ||
prh: prh | ||
}); | ||
str = str.slice(0, diff.index + delta) + result + str.slice(diff.index + delta + diff.matches[0].length); | ||
delta += result.length - diff.matches[0].length; | ||
}); | ||
str = str.slice(0, diff.index + delta) + result + str.slice(diff.index + delta + diff.matches[0].length); | ||
delta += result.length - diff.matches[0].length; | ||
}); | ||
}; | ||
var getConfigBaseDir = function getConfigBaseDir(context) { | ||
if (typeof context.getConfigBaseDir === "function") { | ||
return context.getConfigBaseDir() || process.cwd(); | ||
} | ||
// Old fallback that use deprecated `config` value | ||
// https://github.com/textlint/textlint/issues/294 | ||
var textlintRcFilePath = context.config ? context.config.configFile : null; | ||
// .textlinrc directory | ||
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd(); | ||
if (typeof context.getConfigBaseDir === "function") { | ||
return context.getConfigBaseDir() || process.cwd(); | ||
} // Old fallback that use deprecated `config` value | ||
// https://github.com/textlint/textlint/issues/294 | ||
var textlintRcFilePath = context.config ? context.config.configFile : null; // .textlinrc directory | ||
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd(); | ||
}; | ||
function reporter(context) { | ||
var userOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var userOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
assertOptions(userOptions); | ||
var options = Object.assign({}, defaultOptions, userOptions); // .textlinrc directory | ||
assertOptions(userOptions); | ||
var options = Object.assign({}, defaultOptions, userOptions); | ||
// .textlinrc directory | ||
var textlintRCDir = getConfigBaseDir(context); | ||
// create prh config | ||
var rulePaths = options.rulePaths || []; | ||
var ruleContents = options.ruleContents || []; | ||
// yaml file + yaml contents | ||
var prhEngineContent = createPrhEngineFromContents(ruleContents); | ||
var prhEngineFiles = createPrhEngine(rulePaths, textlintRCDir); | ||
var prhEngine = mergePrh(prhEngineFiles, prhEngineContent); | ||
var helper = new _textlintRuleHelper.RuleHelper(context); | ||
var Syntax = context.Syntax, | ||
getSource = context.getSource, | ||
report = context.report, | ||
fixer = context.fixer, | ||
RuleError = context.RuleError; | ||
var textlintRCDir = getConfigBaseDir(context); // create prh config | ||
var ignoreNodeTypes = createIgnoreNodeTypes(options, Syntax); | ||
return _defineProperty({}, Syntax.Str, function (node) { | ||
if (helper.isChildNode(node, ignoreNodeTypes)) { | ||
return; | ||
} | ||
var text = getSource(node); | ||
// to get position from index | ||
// https://github.com/prh/prh/issues/29 | ||
var dummyFilePath = ""; | ||
var makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text); | ||
forEachChange(makeChangeSet, text, function (_ref) { | ||
var matchStartIndex = _ref.matchStartIndex, | ||
matchEndIndex = _ref.matchEndIndex, | ||
actual = _ref.actual, | ||
expected = _ref.expected, | ||
prh = _ref.prh; | ||
var rulePaths = options.rulePaths || []; | ||
var ruleContents = options.ruleContents || []; // yaml file + yaml contents | ||
// If result is not changed, should not report | ||
if (actual === expected) { | ||
return; | ||
} | ||
var prhEngineContent = createPrhEngineFromContents(ruleContents); | ||
var prhEngineFiles = createPrhEngine(rulePaths, textlintRCDir); | ||
var prhEngine = mergePrh(prhEngineFiles, prhEngineContent); | ||
var helper = new _textlintRuleHelper.RuleHelper(context); | ||
var Syntax = context.Syntax, | ||
getSource = context.getSource, | ||
report = context.report, | ||
fixer = context.fixer, | ||
RuleError = context.RuleError; | ||
var ignoreNodeTypes = createIgnoreNodeTypes(options, Syntax); | ||
return _defineProperty({}, Syntax.Str, function (node) { | ||
if (helper.isChildNode(node, ignoreNodeTypes)) { | ||
return; | ||
} | ||
var suffix = prh !== null ? "\n" + prh : ""; | ||
var messages = actual + " => " + expected + suffix; | ||
report(node, new RuleError(messages, { | ||
index: matchStartIndex, | ||
fix: fixer.replaceTextRange([matchStartIndex, matchEndIndex], expected) | ||
})); | ||
}); | ||
var text = getSource(node); // to get position from index | ||
// https://github.com/prh/prh/issues/29 | ||
var dummyFilePath = ""; | ||
var makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text); | ||
forEachChange(makeChangeSet, text, function (_ref) { | ||
var matchStartIndex = _ref.matchStartIndex, | ||
matchEndIndex = _ref.matchEndIndex, | ||
actual = _ref.actual, | ||
expected = _ref.expected, | ||
prh = _ref.prh; | ||
// If result is not changed, should not report | ||
if (actual === expected) { | ||
return; | ||
} | ||
var suffix = prh !== null ? "\n" + prh : ""; | ||
var messages = actual + " => " + expected + suffix; | ||
report(node, new RuleError(messages, { | ||
index: matchStartIndex, | ||
fix: fixer.replaceTextRange([matchStartIndex, matchEndIndex], expected) | ||
})); | ||
}); | ||
}); | ||
} | ||
module.exports = { | ||
linter: reporter, | ||
fixer: reporter | ||
linter: reporter, | ||
fixer: reporter | ||
}; | ||
//# sourceMappingURL=textlint-rule-prh.js.map |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "5.2.0", | ||
"version": "5.2.1", | ||
"description": "textlint rule for prh.", | ||
@@ -26,8 +26,6 @@ "main": "lib/textlint-rule-prh.js", | ||
"scripts": { | ||
"build": "NODE_ENV=production babel src --out-dir lib --source-maps", | ||
"watch": "babel src --out-dir lib --watch --source-maps", | ||
"build": "textlint-scripts build", | ||
"watch": "textlint-scripts build --watch", | ||
"prepublish": "npm run --if-present build", | ||
"test": "mocha", | ||
"precommit": "lint-staged", | ||
"postcommit": "git reset", | ||
"test": "textlint-scripts test", | ||
"prettier": "prettier --write '**/*.{js,jsx,ts,tsx,css}'" | ||
@@ -41,22 +39,12 @@ }, | ||
"prh": "^5.4.3", | ||
"textlint-rule-helper": "^2.0.0", | ||
"untildify": "^3.0.2" | ||
"textlint-rule-helper": "^2.1.1", | ||
"untildify": "^3.0.3" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-jsdoc-to-assert": "^4.0.0", | ||
"babel-preset-power-assert": "^2.0.0", | ||
"babel-register": "^6.26.0", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^7.0.0", | ||
"mocha": "^5.0.0", | ||
"power-assert": "^1.4.4", | ||
"prettier": "^1.7.0", | ||
"textlint-tester": "^4.0.0", | ||
"textlint": "^11.0.0" | ||
"husky": "2.4.1", | ||
"lint-staged": "8.2.1", | ||
"prettier": "1.15.3", | ||
"textlint": "11.2.5", | ||
"textlint-scripts": "^2.1.0" | ||
}, | ||
"peerDependencies": { | ||
"textlint": ">= 5.5.0" | ||
}, | ||
"prettier": { | ||
@@ -71,3 +59,9 @@ "printWidth": 120, | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"post-commit": "git reset", | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
# textlint-rule-prh [](https://textlint.github.io/) [](https://travis-ci.org/textlint-rule/textlint-rule-prh) | ||
[](https://greenkeeper.io/) | ||
[textlint](https://github.com/textlint-rule/textlint "textlint") rule for [prh/prh: proofreading helper](https://github.com/prh/prh "prh/prh: proofreading helper"). | ||
@@ -6,0 +4,0 @@ |
Sorry, the diff of this file is not supported yet
34381
0.6%3
-25%5
-58.33%364
-2.15%190
-1.04%