Skip to content

Commit 8b28131

Browse files
committed
feat(4.2.2): fixer support
1 parent 00f65fb commit 8b28131

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/4.2.2.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,38 @@
1111
文中に疑問符を使用する場合はスペースを挿入しません。
1212
*/
1313
import {isUserWrittenNode} from "./util/node-util";
14-
export default function (context) {
15-
let {Syntax, RuleError, report, getSource} = context;
14+
import {matchCaptureGroupAll} from "./util/match-index";
15+
function reporter(context) {
16+
let {Syntax, RuleError, report, fixer, getSource} = context;
1617
return {
1718
[Syntax.Str](node){
1819
if (!isUserWrittenNode(node, context)) {
1920
return;
2021
}
2122
let text = getSource(node);
2223
// 和文で半角の?は利用しない
23-
var matchHanQuestion = /([\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])\?/;
24-
var index = text.search(matchHanQuestion);
25-
if (index !== -1) {
26-
return report(node, new RuleError("疑問符(?)を使用する場合は「全角」で表記します。", index + 1))
27-
}
24+
const matchRegExp = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])(\?)/;
25+
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
26+
const {index} = match;
27+
return report(node, new RuleError("疑問符(?)を使用する場合は「全角」で表記します。", {
28+
column: index,
29+
fix: fixer.replaceTextRange([index, index + 1], "?")
30+
}));
31+
});
32+
// ?の後ろは全角スペースが推奨
33+
// 半角スペースである場合はエラーとする
34+
const matchAfter = /( )[^\n]/;
35+
matchCaptureGroupAll(text, matchAfter).forEach(match => {
36+
const {index} = match;
37+
return report(node, new RuleError("文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。", {
38+
column: index,
39+
fix: fixer.replaceTextRange([index, index + 1], " ")
40+
}));
41+
});
2842
}
2943
};
44+
}
45+
export default {
46+
linter: reporter,
47+
fixer: reporter
3048
}

test/4.2.2-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@ tester.run("4.2.2.疑問符(?)", rule, {
88
"【原文】Does the reader understand the document?",
99
"【訳文】読者は文書の内容を理解しているでしょうか。",
1010
"オプションを変更しますか?",
11-
"A 社の成功の秘密とは? この本ではそれをご紹介します。",
11+
"A 社の成功の秘密とは? この本ではそれをご紹介します。",
1212
"どう操作したらよいのか?というユーザーの疑問に答えます。"
1313
],
1414
invalid: [
1515
{
1616
text: "半角疑問符?",
17+
output: "半角疑問符?",
1718
errors: [
1819
{
1920
message: "疑問符(?)を使用する場合は「全角」で表記します。",
2021
column: 6
2122
}
2223
]
24+
},
25+
{
26+
text: "驚きの速さ!? これが新製品のキャッチコピーでした? これは問題なし",
27+
output: "驚きの速さ!? これが新製品のキャッチコピーでした? これは問題なし",
28+
errors: [
29+
{
30+
message: "文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。",
31+
column: 8
32+
}
33+
]
2334
}
2435
]
2536
});

0 commit comments

Comments
 (0)