Skip to content

Commit 8f8fb38

Browse files
committed
feat(4.2.7): fixer support
1 parent d127267 commit 8f8fb38

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/4.2.7.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,32 @@
77
ただし和文でも、見出し語とその説明の間にコロンを使う場合があります。使用する場合は全角で表記します。
88
*/
99
import {isUserWrittenNode} from "./util/node-util";
10-
export default function (context) {
11-
let {Syntax, RuleError, report, getSource} = context;
10+
import {matchCaptureGroupAll} from "./util/match-index";
11+
import regx from 'regx';
12+
import {japaneseRegExp} from "./util/regexp";
13+
const rx = regx("g");
14+
function reporter(context) {
15+
let {Syntax, RuleError, report, fixer, getSource} = context;
1216
return {
1317
[Syntax.Str](node){
1418
if (!isUserWrittenNode(node, context)) {
1519
return;
1620
}
17-
let text = getSource(node);
21+
const text = getSource(node);
1822
// "和文:" というような半角:は使用しない
19-
var matchHanQuestion = /([\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--]):/;
20-
var index = text.search(matchHanQuestion);
21-
if (index !== -1) {
22-
return report(node, new RuleError("コロン(:)を使用する場合は「全角」で表記します。", index + 1))
23-
}
23+
24+
const matchHanQuestion = rx`(?:${japaneseRegExp})(:)`;
25+
matchCaptureGroupAll(text, matchHanQuestion).forEach(match => {
26+
const {index} = match;
27+
report(node, new RuleError("コロン(:)を使用する場合は「全角」で表記します。", {
28+
column: index,
29+
fix: fixer.replaceTextRange([index, index + 1], ":")
30+
}))
31+
})
2432
}
2533
};
34+
}
35+
export default {
36+
linter: reporter,
37+
fixer: reporter
2638
}

test/4.2.7-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ tester.run("4.2.7.コロン(:)", rule, {
77
valid: [
88
"日時:3月16日午後 1 時",
99
"例:〜",
10-
"1: これはペンです"// [^和文]: のパターン
10+
"1: これはペンです",// [^和文]: のパターン,
11+
"`this::method`"
1112
],
1213
invalid: [
1314
{
1415
text: "例:〜",
16+
output: "例:〜",
1517
errors: [
1618
{
1719
message: "コロン(:)を使用する場合は「全角」で表記します。",

0 commit comments

Comments
 (0)