Skip to content

Commit d81b138

Browse files
committed
refactor(rule): move all rule to index-based reporter
1 parent 238df08 commit d81b138

27 files changed

+48
-64
lines changed

src/1.1.2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function mixer(context) {
3131
if (index !== -1) {
3232
report(node, {
3333
message: "見出しの文末には、句点(。)を付けません。",
34-
column: index,
34+
index: index,
3535
fix: fixer.removeRange([index, index + 1])
3636
});
3737
}

src/1.2.1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const reporter = (context) => {
4444
const indexOfSymbol = match.index;
4545
report(node, {
4646
message: "句読点には全角の「、」と「。」を使います。和文の句読点としてピリオド(.)とカンマ(,)を使用しません。",
47-
column: indexOfSymbol,
47+
index: indexOfSymbol,
4848
fix: fixer.replaceTextRange([indexOfSymbol, indexOfSymbol + 1], symbol)
4949
});
5050
})

src/1.2.2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function report(context) {
2626
const symbol = replaceSymbol[text[index]];
2727
report(node, {
2828
message: "全角のピリオドとカンマは使用しません。",
29-
column: index,
29+
index: index,
3030
fix: fixer.replaceTextRange([index, index + 1], symbol)
3131
});
3232
}

src/2.1.10.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function reporter(context) {
2727
matchCaptureGroupAll(matchedString, strictMatchReg).forEach(subMatch => {
2828
const {index} = subMatch;
2929
report(node, new RuleError("小数点には「ピリオド」を使います。", {
30-
column: match.index + index,
30+
index: match.index + index,
3131
fix: fixer.replaceTextRange([match.index + index, match.index + index + 1], ".")
3232
}));
3333
});

src/2.1.2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export default function (context) {
2525
for (let index = 0; index < strArray.length; index++) {
2626
const item = strArray[index];
2727
if (kanjiRegExp.test(item) && !isJoyo(item)) {
28-
report(node, new RuleError("「" + item + "」は「常用漢字表」外の漢字です。", index));
28+
report(node, new RuleError("「" + item + "」は「常用漢字表」外の漢字です。", {
29+
index
30+
}));
2931
}
3032
}
3133
}

src/2.1.5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function reporter(context) {
4444
const {index, text} = match;
4545
report(node, {
4646
message: "カタカナは「全角」で表記します。",
47-
column: index,
47+
index: index,
4848
fix: fixer.replaceTextRange([index, index + text.length], toZenkaku(text))
4949
});
5050

src/2.1.8.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function reporter(context) {
2525
const {index, text} = match;
2626
report(node, {
2727
message: "算用数字は「半角」で表記します。",
28-
column: index,
28+
index: index,
2929
fix: fixer.replaceTextRange([index, index + text.length], toHankaku(text))
3030
})
3131
});

src/2.1.9.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function reporter(context) {
2525
const {index, text} = match;
2626
report(node, {
2727
message: "アルファベットは「半角」で表記します。",
28-
column: index,
28+
index: index,
2929
fix: fixer.replaceTextRange([index, index + text.length], toHankaku(text))
3030
})
3131
});

src/2.2.2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function reporter(context) {
125125
});
126126
const ruleError = new RuleError(`${matchedString} => ${expected}
127127
数量を表現し、数を数えられるものは算用数字を使用します。任意の数に置き換えても通用する語句がこれに該当します。`, {
128-
column: index,
128+
index: index,
129129
fix: fixer.replaceTextRange([index, index + matchedString.length], expected)
130130
});
131131
report(node, ruleError);
@@ -142,7 +142,7 @@ function reporter(context) {
142142
const index = match.index;
143143
report(node, new RuleError(`${matchedString} => ${expected}
144144
慣用的表現、熟語、概数、固有名詞、副詞など、漢数字を使用することが一般的な語句では漢数字を使います。`, {
145-
column: index,
145+
index: index,
146146
fix: fixer.replaceTextRange([index, index + matchedString.length], expected)
147147
}));
148148
};

src/3.1.1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function reporter(context) {
2222
const reportMatch = (match) => {
2323
const {index} = match;
2424
report(node, new RuleError("原則として、全角文字と半角文字の間にスペースを入れません。", {
25-
column: match.index,
25+
index: match.index,
2626
fix: fixer.replaceTextRange([index, index + 1], "")
2727
}));
2828
};

src/3.1.2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function reporter(context) {
3131
captureGroups.forEach(captureGroup => {
3232
const index = captureGroup.index;
3333
report(node, new RuleError("原則として、全角文字どうしの間にスペースを入れません。", {
34-
column: index,
34+
index: index,
3535
fix: fixer.replaceTextRange([index, index + 1], "")
3636
}));
3737
});

src/3.2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function (context) {
2222
const {text} = match;
2323
if (text === " ") {
2424
report(node, new RuleError("カタカナ語間は中黒(・)または半角スペースを用いてカタカナ語を区切ります", {
25-
column: match.index
25+
index: match.index
2626
}));
2727
}
2828
});

src/3.3.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ const brackets = [
1212
];
1313

1414
const leftBrackets = brackets.map(bracket => {
15-
return new RegExp("\(\\s\)" + bracket, "g");
15+
return new RegExp("\([  ]\)" + bracket, "g");
1616
});
1717
const rightBrackets = brackets.map(bracket => {
18-
return new RegExp(bracket + "\(\\s\)", "g");
18+
return new RegExp(bracket + "\([  ])", "g");
1919
});
2020
function reporter(context) {
2121
let {Syntax, RuleError, report, fixer, getSource} = context;
@@ -30,21 +30,21 @@ function reporter(context) {
3030
matchCaptureGroupAll(text, pattern).forEach(match => {
3131
const {index} = match;
3232
report(node, new RuleError("かっこの外側、内側ともにスペースを入れません。", {
33-
column: index,
33+
index: index,
3434
fix: fixer.replaceTextRange([index, index + 1], "")
3535
}));
36-
})
36+
});
3737
});
3838
// 右にスペース
3939
rightBrackets.forEach(pattern => {
4040
matchCaptureGroupAll(text, pattern).forEach(match => {
41-
const {index} = match;
41+
const {index, text} = match;
4242
report(node, new RuleError("かっこの外側、内側ともにスペースを入れません。", {
43-
column: index,
43+
index: index,
4444
fix: fixer.replaceTextRange([index, index + 1], "")
4545
}));
46-
})
47-
})
46+
});
47+
});
4848
}
4949
}
5050
}

src/4.1.1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var reporter = function reporter(context) {
2727
matchCaptureGroupAll(text, pattern).forEach(match => {
2828
const {index} = match;
2929
report(node, new RuleError("文中にかぎかっこが入る場合は、閉じかっこの前に句点を打ちません。", {
30-
column: index,
30+
index: index,
3131
fix: fixer.replaceTextRange([index, index + 1], "")
3232
}));
3333
});

src/4.1.3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function reporter(context) {
2323
matchCaptureGroupAll(text, matchReg).forEach(match => {
2424
const index = match.index;
2525
report(node, new RuleError("和文の句読点としてはピリオドを使用しません。", {
26-
column: index,
26+
index: index,
2727
fix: fixer.replaceTextRange([index, index + 1], "。")
2828
}));
2929
});

src/4.2.1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function reporter(context) {
2727
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
2828
const {index} = match;
2929
return report(node, new RuleError("感嘆符(!)を使用する場合は「全角」で表記します。", {
30-
column: index,
30+
index: index,
3131
fix: fixer.replaceTextRange([index, index + 1], "!")
3232
}));
3333
});
@@ -37,7 +37,7 @@ function reporter(context) {
3737
matchCaptureGroupAll(text, matchAfter).forEach(match => {
3838
const {index} = match;
3939
return report(node, new RuleError("文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。", {
40-
column: index,
40+
index: index,
4141
fix: fixer.replaceTextRange([index, index + 1], " ")
4242
}));
4343
});

src/4.2.2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function reporter(context) {
2828
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
2929
const {index} = match;
3030
return report(node, new RuleError("疑問符(?)を使用する場合は「全角」で表記します。", {
31-
column: index,
31+
index: index,
3232
fix: fixer.replaceTextRange([index, index + 1], "?")
3333
}));
3434
});
@@ -38,7 +38,7 @@ function reporter(context) {
3838
matchCaptureGroupAll(text, matchAfter).forEach(match => {
3939
const {index} = match;
4040
return report(node, new RuleError("文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。", {
41-
column: index,
41+
index: index,
4242
fix: fixer.replaceTextRange([index, index + 1], " ")
4343
}));
4444
});

src/4.2.4.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function reporter(context) {
2626
matchCaptureGroupAll(text, matchHanNakaguro).forEach(match => {
2727
const {index} = match;
2828
report(node, new RuleError("カタカナ複合語を区切る場合または同格の語句を並列する場合には全角の中黒(・)を使用します。", {
29-
column: index,
29+
index: index,
3030
fix: fixer.replaceTextRange([index, index + 1], "・")
3131
}));
3232
})

src/4.2.5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function reporter(context) {
2020
matchCaptureGroupAll(text, matchHanQuestion).forEach(match => {
2121
const {index} = match;
2222
report(node, new RuleError("数値の範囲を示す場合には全角の〜を使用します。", {
23-
column: index,
23+
index: index,
2424
fix: fixer.replaceTextRange([index, index + 1], "〜")
2525
}));
2626
});

src/4.2.6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function (context) {
3030
const {index} = match;
3131
report(node, new RuleError(`原則として和文ではハイフン(-)を使用しません。
3232
例外は、住所や電話番号の区切りに使う場合です。`, {
33-
column: index
33+
index: index
3434
}))
3535
});
3636
}

src/4.2.7.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function reporter(context) {
2525
matchCaptureGroupAll(text, matchHanQuestion).forEach(match => {
2626
const {index} = match;
2727
report(node, new RuleError("コロン(:)を使用する場合は「全角」で表記します。", {
28-
column: index,
28+
index: index,
2929
fix: fixer.replaceTextRange([index, index + 1], ":")
3030
}))
3131
})

src/4.2.8.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function (context) {
2323
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
2424
const {index} = match;
2525
report(node, new RuleError("原則として和文ではセミコロン(;)を使用しません。", {
26-
column: index
26+
index: index
2727
}));
2828
})
2929
}

src/4.2.9.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function (context) {
2727
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
2828
const {index} = match;
2929
report(node, new RuleError("原則として和文ではダッシュ(―)を使用しません。", {
30-
column: index
30+
index
3131
}));
3232
})
3333
}

src/4.3.1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function reporter(context) {
3434
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
3535
const {index} = match;
3636
report(node, new RuleError("半角のかっこ()が使用されています。全角のかっこ()を使用してください。", {
37-
column: index,
37+
index: index,
3838
fix: fixer.replaceTextRange([index, index + 1], replaceSymbol(match.text))
3939
}));
4040
});

src/4.3.2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function reporter(context) {
3434
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
3535
const {index} = match;
3636
report(node, new RuleError("半角の大かっこ[]が使用されています。全角のかっこ[]を使用してください。", {
37-
column: index,
37+
index: index,
3838
fix: fixer.replaceTextRange([index, index + 1], replaceSymbol(match.text))
3939
}));
4040
});

src/util/pair-checker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
import assert from "assert";
1111
import {RuleHelper} from "textlint-rule-helper";
12-
export function checkPair(context, { left, right }) {
12+
export function checkPair(context, {left, right}) {
1313
assert(left);
1414
assert(right);
1515
let {Syntax, RuleError, report, getSource} = context;
@@ -50,7 +50,9 @@ export function checkPair(context, { left, right }) {
5050
}
5151

5252
matchParentheses.forEach(({node, index}) => {
53-
report(node, new RuleError(`${left}の対となる${right}が見つかりません。${left}${right}`, index));
53+
report(node, new RuleError(`${left}の対となる${right}が見つかりません。${left}${right}`, {
54+
index
55+
}));
5456
});
5557
// clear state
5658
matchParentheses = [];

test/3.3-test.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ var tester = new TextLintTester();
66
tester.run("3.3.かっこ類と隣接する文字の間のスペースの有無", rule, {
77
valid: [
88
"「良い」",
9-
"テスト[文章]です"
9+
"テスト[文章]です",
10+
`
11+
実装をみてもらうと分かりますが、JavaScriptの\`prototype\`の仕組みをそのまま利用しています。
12+
そのため、特別な実装は必要なく
13+
「拡張する時は\`calculator.prototype\`の代わりに\`calculator.fn\`を拡張してください」
14+
というルールがあるだけとも言えます。
15+
`
1016
],
1117
invalid: [
1218
{
@@ -26,32 +32,6 @@ tester.run("3.3.かっこ類と隣接する文字の間のスペースの有無"
2632
column: 4
2733
}
2834
]
29-
},
30-
{
31-
text: // 改行はスペースではないとする
32-
`
33-
実装をみてもらうと分かりますが、JavaScriptの\`prototype\`の仕組みをそのまま利用しています。
34-
そのため、特別な実装は必要なく
35-
「拡張する時は\`calculator.prototype\`の代わりに\`calculator.fn\`を拡張してください」
36-
というルールがあるだけとも言えます。
37-
`,
38-
output: `
39-
実装をみてもらうと分かりますが、JavaScriptの\`prototype\`の仕組みをそのまま利用しています。
40-
そのため、特別な実装は必要なく「拡張する時は\`calculator.prototype\`の代わりに\`calculator.fn\`を拡張してください」というルールがあるだけとも言えます。
41-
`,
42-
errors: [
43-
{
44-
message: "かっこの外側、内側ともにスペースを入れません。",
45-
line: 2,
46-
column: 15
47-
},
48-
{
49-
message: "かっこの外側、内側ともにスペースを入れません。",
50-
line: 4,
51-
column: 15
52-
}
53-
54-
]
5535
}
5636
]
5737
});

0 commit comments

Comments
 (0)