Skip to content

Commit 60f9bc2

Browse files
authored
fix(rule): support textlint 8.2.0 (#15)
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
1 parent 8ee7d0d commit 60f9bc2

File tree

5 files changed

+2818
-27
lines changed

5 files changed

+2818
-27
lines changed

src/prh-rule.js renamed to src/textlint-rule-prh.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
3-
import {RuleHelper} from "textlint-rule-helper";
3+
import { RuleHelper } from "textlint-rule-helper";
4+
45
const prh = require("prh");
56
const path = require("path");
67
const untildify = require('untildify');
@@ -90,11 +91,20 @@ const forEachChange = (changeSet, str, onChangeOfMatch) => {
9091
delta += result.length - diff.matches[0].length;
9192
});
9293
};
94+
const getConfigBaseDir = (context) => {
95+
if (typeof context.getConfigBaseDir === "function") {
96+
return context.getConfigBaseDir();
97+
}
98+
// Old fallback that use deprecated `config` value
99+
// https://github.com/textlint/textlint/issues/294
100+
const textlintRcFilePath = context.config ? context.config.configFile : null;
101+
// .textlinrc directory
102+
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
103+
};
93104
function reporter(context, options = {}) {
94105
assertOptions(options);
95-
const textlintRcFilePath = context.config ? context.config.configFile : null;
96106
// .textlinrc directory
97-
const textlintRCDir = textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
107+
const textlintRCDir = getConfigBaseDir(context);
98108
// create prh config
99109
const rulePaths = options.rulePaths || [];
100110
const ruleContents = options.ruleContents || [];
@@ -103,7 +113,7 @@ function reporter(context, options = {}) {
103113
const prhEngineFiles = createPrhEngine(rulePaths, textlintRCDir);
104114
const prhEngine = mergePrh(prhEngineFiles, prhEngineContent);
105115
const helper = new RuleHelper(context);
106-
const {Syntax, getSource, report, fixer, RuleError} = context;
116+
const { Syntax, getSource, report, fixer, RuleError } = context;
107117
return {
108118
[Syntax.Str](node){
109119
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
@@ -112,7 +122,7 @@ function reporter(context, options = {}) {
112122
const text = getSource(node);
113123
// to get position from index
114124
const makeChangeSet = prhEngine.makeChangeSet(null, text);
115-
forEachChange(makeChangeSet, text, ({matchStartIndex, matchEndIndex, actual, expected}) => {
125+
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected }) => {
116126
// If result is not changed, should not report
117127
if (actual === expected) {
118128
return;

test/prh-rule-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33
import assert from "power-assert";
44
import {textlint} from "textlint";
5-
import rule from "../src/prh-rule";
5+
import rule from "../src/textlint-rule-prh";
66
describe("prh-rule-test", function () {
77
beforeEach(function () {
88
textlint.setupRules({

test/prh-rule-tester-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var TextLintTester = require("textlint-tester");
44
var tester = new TextLintTester();
55
// rule
6-
import rule from "../src/prh-rule";
6+
import rule from "../src/textlint-rule-prh";
77
// ruleName, rule, { valid, invalid }
88
tester.run("prh", rule, {
99
valid: [

test/textlintrc-test.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
22
"use strict";
33
import assert from "power-assert";
44
import fs from "fs";
5-
import {TextLintCore} from "textlint";
6-
import rule from "../src/prh-rule";
5+
import { TextLintEngine, TextLintCore } from "textlint";
6+
import rule from "../src/textlint-rule-prh";
77
import path from "path";
8-
describe(".textlinrc test", function () {
9-
context("when use .textlintrc", function () {
10-
it("should resolve path to rule.yaml", function () {
11-
var textlint = new TextLintCore();
12-
textlint.setupRules({
13-
"prh": rule
14-
}, {
15-
"prh": {
16-
"rulePaths": [path.join(__dirname, "fixtures", "rule.yaml"), path.join(__dirname, "fixtures", "imports.yml")]
17-
}
8+
9+
describe(".textlinrc test", function() {
10+
context("when use .textlintrc", function() {
11+
it("should resolve path to rule.yaml", function() {
12+
const engine = new TextLintEngine({
13+
configFile: path.join(__dirname, "fixtures/.textlintrc"),
14+
rulesBaseDirectory: path.join(__dirname, "../src")
1815
});
19-
return textlint.lintMarkdown("jquery").then(result => {
16+
return engine.executeOnText("jquery").then(([result]) => {
2017
assert(result.messages.length === 1);
2118
assert(result.messages[0].line === 1);
2219
assert(result.messages[0].column === 1);
2320
});
2421
});
25-
it("should resolve path to rule.yaml", function () {
22+
});
23+
context("options", () => {
24+
it("should resolve path to rule.yaml", function() {
2625
var textlint = new TextLintCore();
2726
textlint.setupRules({
2827
"prh": rule
@@ -37,7 +36,7 @@ describe(".textlinrc test", function () {
3736
assert(result.messages[0].column === 1);
3837
});
3938
});
40-
it("should resolve yaml content", function () {
39+
it("should resolve yaml content", function() {
4140
var textlint = new TextLintCore();
4241
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
4342
textlint.setupRules({
@@ -53,7 +52,7 @@ describe(".textlinrc test", function () {
5352
assert(result.messages[0].column === 1);
5453
});
5554
});
56-
it("should resolve yaml file and content", function () {
55+
it("should resolve yaml file and content", function() {
5756
var textlint = new TextLintCore();
5857
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
5958
textlint.setupRules({
@@ -74,11 +73,11 @@ describe(".textlinrc test", function () {
7473
assert(result.messages[1].column === 8);
7574
});
7675
});
77-
7876
});
79-
context("prh features", function () {
80-
describe("import", function () {
81-
it("should work import directive", function () {
77+
78+
context("prh features", function() {
79+
describe("import", function() {
80+
it("should work import directive", function() {
8281
var textlint = new TextLintCore();
8382
textlint.setupRules({
8483
"prh": rule

0 commit comments

Comments
 (0)