Skip to content

Commit 3a786a3

Browse files
Lekoazu
authored andcommitted
feat(rule): add prh to message (#42)
Fixed #5 ## Changes Added `prh` field to message if exists. ### Example (prh field exists): ```yml version: 1 rules: - expected: デフォルト pattern: ディフォルト prh: 表記をデフォルトに統一してください ``` ``` ディフォルト => デフォルト 表記をデフォルトに統一してください ``` ### Example (prh not exists): ```yml version: 1 rules: - expected: jQuery ``` ``` JQUERY => jQuery ```
1 parent c8f3103 commit 3a786a3

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/textlint-rule-prh.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ const forEachChange = (changeSet, str, onChangeOfMatch) => {
121121
const matchEndIndex = matchStartIndex + diff.matches[0].length;
122122
// actual => expected
123123
const actual = str.slice(diff.index + delta, diff.index + delta + diff.matches[0].length);
124+
const prh = diff.rule.raw.prh || null;
124125
onChangeOfMatch({
125126
matchStartIndex,
126127
matchEndIndex,
127128
actual: actual,
128-
expected: result
129+
expected: result,
130+
prh
129131
});
130132
str = str.slice(0, diff.index + delta) + result + str.slice(diff.index + delta + diff.matches[0].length);
131133
delta += result.length - diff.matches[0].length;
@@ -167,13 +169,14 @@ function reporter(context, userOptions = {}) {
167169
// https://github.com/prh/prh/issues/29
168170
const dummyFilePath = "";
169171
const makeChangeSet = prhEngine.makeChangeSet(dummyFilePath, text);
170-
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected }) => {
172+
forEachChange(makeChangeSet, text, ({ matchStartIndex, matchEndIndex, actual, expected, prh }) => {
171173
// If result is not changed, should not report
172174
if (actual === expected) {
173175
return;
174176
}
175177

176-
const messages = actual + " => " + expected;
178+
const suffix = prh !== null ? "\n" + prh : "";
179+
const messages = actual + " => " + expected + suffix;
177180
report(
178181
node,
179182
new RuleError(messages, {

test/fixtures/rule.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rules:
2828
# 表現の統一を図る
2929
- expected: デフォルト
3030
pattern: ディフォルト
31+
prh: 表記をデフォルトに統一してください
3132

3233
# patternは複数記述可能
3334
- expected: ハードウェア

test/prh-rule-tester-test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ tester.run("prh", rule, {
5757
{
5858
type: "lint",
5959
ruleId: "prh",
60-
message: "行 => おこな",
60+
message: "行 => おこな\n「行う」「行なう」は開く。",
6161
index: 0,
6262
line: 1,
6363
column: 1,
@@ -70,7 +70,7 @@ tester.run("prh", rule, {
7070
{
7171
type: "lint",
7272
ruleId: "prh",
73-
message: "行な => おこな",
73+
message: "行な => おこな\n「行う」「行なう」は開く。",
7474
index: 3,
7575
line: 1,
7676
column: 4,
@@ -83,7 +83,7 @@ tester.run("prh", rule, {
8383
{
8484
type: "lint",
8585
ruleId: "prh",
86-
message: "行 => おこな",
86+
message: "行 => おこな\n「行う」「行なう」は開く。",
8787
index: 12,
8888
line: 1,
8989
column: 13,
@@ -96,7 +96,7 @@ tester.run("prh", rule, {
9696
{
9797
type: "lint",
9898
ruleId: "prh",
99-
message: "行 => おこな",
99+
message: "行 => おこな\n「行う」「行なう」は開く。",
100100
index: 16,
101101
line: 1,
102102
column: 17,
@@ -109,7 +109,7 @@ tester.run("prh", rule, {
109109
{
110110
type: "lint",
111111
ruleId: "prh",
112-
message: "行な => おこな",
112+
message: "行な => おこな\n「行う」「行なう」は開く。",
113113
index: 21,
114114
line: 1,
115115
column: 22,
@@ -178,7 +178,7 @@ tester.run("prh", rule, {
178178
{
179179
type: "lint",
180180
ruleId: "prh",
181-
message: "ディフォルト => デフォルト",
181+
message: "ディフォルト => デフォルト\n表記をデフォルトに統一してください",
182182
index: 1,
183183
line: 1,
184184
column: 2,
@@ -200,7 +200,7 @@ tester.run("prh", rule, {
200200
{
201201
type: "lint",
202202
ruleId: "prh",
203-
message: "ディフォルト => デフォルト",
203+
message: "ディフォルト => デフォルト\n表記をデフォルトに統一してください",
204204
index: 2,
205205
line: 1,
206206
column: 3,

0 commit comments

Comments
 (0)