Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prh): Update [email protected] #17

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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