Skip to content

Commit

Permalink
fix(prh): Update [email protected] (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Sep 27, 2017
1 parent fcc5ea2 commit 2116502
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 126 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@
"prh"
],
"dependencies": {
"prh": "^3.0.1",
"prh": "^5.4.3",
"textlint-rule-helper": "^2.0.0",
"untildify": "^3.0.2"
},
"devDependencies": {
"babel-cli": "^6.7.5",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-jsdoc-to-assert": "^4.0.0",
"babel-preset-power-assert": "^1.0.0",
"babel-register": "^6.7.2",
"mocha": "^3.1.2",
"power-assert": "^1.3.1",
"textlint": "^8.0.0",
"textlint-tester": "^2.0.0"
"babel-register": "^6.26.0",
"mocha": "^3.5.3",
"power-assert": "^1.4.4",
"textlint": "^8.2.1",
"textlint-tester": "^2.2.4"
},
"peerDependencies": {
"textlint": ">= 5.5.0"
Expand Down
17 changes: 13 additions & 4 deletions src/textlint-rule-prh.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RuleHelper } from "textlint-rule-helper";
const prh = require("prh");
const path = require("path");
const untildify = require('untildify');

function createPrhEngine(rulePaths, baseDir) {
if (rulePaths.length === 0) {
return null;
Expand All @@ -17,17 +18,20 @@ function createPrhEngine(rulePaths, baseDir) {
});
return prhEngine;
}

function createPrhEngineFromContents(yamlContents) {
if (yamlContents.length === 0) {
return null;
}
const prhEngine = prh.fromYAML(null, yamlContents[0]);
const dummyFilePath = "";
const prhEngine = prh.fromYAML(dummyFilePath, yamlContents[0]);
yamlContents.slice(1).forEach(content => {
const config = prh.fromYAML(null, content);
const config = prh.fromYAML(dummyFilePath, content);
prhEngine.merge(config);
});
return prhEngine;
}

function mergePrh(...engines) {
const engines_ = engines.filter(engine => !!engine);
const mainEngine = engines_[0];
Expand All @@ -36,6 +40,7 @@ function mergePrh(...engines) {
});
return mainEngine;
}

const assertOptions = (options) => {
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") {
throw new Error(`textlint-rule-prh require Rule Options.
Expand Down Expand Up @@ -101,6 +106,7 @@ const getConfigBaseDir = (context) => {
// .textlinrc directory
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
};

function reporter(context, options = {}) {
assertOptions(options);
// .textlinrc directory
Expand All @@ -115,13 +121,15 @@ function reporter(context, options = {}) {
const helper = new RuleHelper(context);
const { Syntax, getSource, report, fixer, RuleError } = context;
return {
[Syntax.Str](node){
[Syntax.Str](node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
const text = getSource(node);
// to get position from index
const makeChangeSet = prhEngine.makeChangeSet(null, text);
// https://github.com/prh/prh/issues/29
const dummyFilePath = "";
const makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text);
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected }) => {
// If result is not changed, should not report
if (actual === expected) {
Expand All @@ -137,6 +145,7 @@ function reporter(context, options = {}) {
}
}
}

module.exports = {
linter: reporter,
fixer: reporter
Expand Down
Loading

0 comments on commit 2116502

Please sign in to comment.