Skip to content

Commit

Permalink
fix(rule): support textlint 8.2.0 (#15)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: https://github.com/prh/prh/blob/master/CHANGELOG.md#300-2017-05-06
prh use `/regexp/u` by default.
Always add `u` flag
  • Loading branch information
azu authored May 21, 2017
1 parent 8ee7d0d commit 60f9bc2
Show file tree
Hide file tree
Showing 5 changed files with 2,818 additions and 27 deletions.
20 changes: 15 additions & 5 deletions src/prh-rule.js → src/textlint-rule-prh.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// LICENSE : MIT
"use strict";
import {RuleHelper} from "textlint-rule-helper";
import { RuleHelper } from "textlint-rule-helper";

const prh = require("prh");
const path = require("path");
const untildify = require('untildify');
Expand Down Expand Up @@ -90,11 +91,20 @@ const forEachChange = (changeSet, str, onChangeOfMatch) => {
delta += result.length - diff.matches[0].length;
});
};
const getConfigBaseDir = (context) => {
if (typeof context.getConfigBaseDir === "function") {
return context.getConfigBaseDir();
}
// Old fallback that use deprecated `config` value
// https://github.com/textlint/textlint/issues/294
const textlintRcFilePath = context.config ? context.config.configFile : null;
// .textlinrc directory
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
};
function reporter(context, options = {}) {
assertOptions(options);
const textlintRcFilePath = context.config ? context.config.configFile : null;
// .textlinrc directory
const textlintRCDir = textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
const textlintRCDir = getConfigBaseDir(context);
// create prh config
const rulePaths = options.rulePaths || [];
const ruleContents = options.ruleContents || [];
Expand All @@ -103,7 +113,7 @@ function reporter(context, options = {}) {
const prhEngineFiles = createPrhEngine(rulePaths, textlintRCDir);
const prhEngine = mergePrh(prhEngineFiles, prhEngineContent);
const helper = new RuleHelper(context);
const {Syntax, getSource, report, fixer, RuleError} = context;
const { Syntax, getSource, report, fixer, RuleError } = context;
return {
[Syntax.Str](node){
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
Expand All @@ -112,7 +122,7 @@ function reporter(context, options = {}) {
const text = getSource(node);
// to get position from index
const makeChangeSet = prhEngine.makeChangeSet(null, text);
forEachChange(makeChangeSet, text, ({matchStartIndex, matchEndIndex, actual, expected}) => {
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected }) => {
// If result is not changed, should not report
if (actual === expected) {
return;
Expand Down
2 changes: 1 addition & 1 deletion test/prh-rule-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"use strict";
import assert from "power-assert";
import {textlint} from "textlint";
import rule from "../src/prh-rule";
import rule from "../src/textlint-rule-prh";
describe("prh-rule-test", function () {
beforeEach(function () {
textlint.setupRules({
Expand Down
2 changes: 1 addition & 1 deletion test/prh-rule-tester-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var TextLintTester = require("textlint-tester");
var tester = new TextLintTester();
// rule
import rule from "../src/prh-rule";
import rule from "../src/textlint-rule-prh";
// ruleName, rule, { valid, invalid }
tester.run("prh", rule, {
valid: [
Expand Down
39 changes: 19 additions & 20 deletions test/textlintrc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
"use strict";
import assert from "power-assert";
import fs from "fs";
import {TextLintCore} from "textlint";
import rule from "../src/prh-rule";
import { TextLintEngine, TextLintCore } from "textlint";
import rule from "../src/textlint-rule-prh";
import path from "path";
describe(".textlinrc test", function () {
context("when use .textlintrc", function () {
it("should resolve path to rule.yaml", function () {
var textlint = new TextLintCore();
textlint.setupRules({
"prh": rule
}, {
"prh": {
"rulePaths": [path.join(__dirname, "fixtures", "rule.yaml"), path.join(__dirname, "fixtures", "imports.yml")]
}

describe(".textlinrc test", function() {
context("when use .textlintrc", function() {
it("should resolve path to rule.yaml", function() {
const engine = new TextLintEngine({
configFile: path.join(__dirname, "fixtures/.textlintrc"),
rulesBaseDirectory: path.join(__dirname, "../src")
});
return textlint.lintMarkdown("jquery").then(result => {
return engine.executeOnText("jquery").then(([result]) => {
assert(result.messages.length === 1);
assert(result.messages[0].line === 1);
assert(result.messages[0].column === 1);
});
});
it("should resolve path to rule.yaml", function () {
});
context("options", () => {
it("should resolve path to rule.yaml", function() {
var textlint = new TextLintCore();
textlint.setupRules({
"prh": rule
Expand All @@ -37,7 +36,7 @@ describe(".textlinrc test", function () {
assert(result.messages[0].column === 1);
});
});
it("should resolve yaml content", function () {
it("should resolve yaml content", function() {
var textlint = new TextLintCore();
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
textlint.setupRules({
Expand All @@ -53,7 +52,7 @@ describe(".textlinrc test", function () {
assert(result.messages[0].column === 1);
});
});
it("should resolve yaml file and content", function () {
it("should resolve yaml file and content", function() {
var textlint = new TextLintCore();
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
textlint.setupRules({
Expand All @@ -74,11 +73,11 @@ describe(".textlinrc test", function () {
assert(result.messages[1].column === 8);
});
});

});
context("prh features", function () {
describe("import", function () {
it("should work import directive", function () {

context("prh features", function() {
describe("import", function() {
it("should work import directive", function() {
var textlint = new TextLintCore();
textlint.setupRules({
"prh": rule
Expand Down
Loading

0 comments on commit 60f9bc2

Please sign in to comment.