Skip to content

Commit ae890e6

Browse files
committed
feat(4.2.1): fixer support
1 parent 49b1a26 commit ae890e6

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/4.2.1.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,39 @@
1212
文中に感嘆符を使用する場合はスペースを挿入しません。下記を参考にしてください。
1313
*/
1414
import {isUserWrittenNode} from "./util/node-util";
15-
export default function (context) {
16-
let {Syntax, RuleError, report, getSource} = context;
15+
import {matchCaptureGroupAll} from "./util/match-index";
16+
17+
function reporter(context) {
18+
let {Syntax, RuleError, report, fixer, getSource} = context;
1719
return {
1820
[Syntax.Str](node){
1921
if (!isUserWrittenNode(node, context)) {
2022
return;
2123
}
2224
let text = getSource(node);
2325
// 半角の!は利用しない
24-
if (/([\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])!/.test(text)) {
25-
return report(node, new RuleError("感嘆符(!)を使用する場合は「全角」で表記します。"))
26-
}
27-
// !の後ろは全角スペース
28-
// 半角スペースではない
29-
if (/ [^\n]/.test(text)) {
30-
return report(node, new RuleError("文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。"))
31-
}
26+
const matchRegExp = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])(!)/;
27+
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
28+
const {index} = match;
29+
return report(node, new RuleError("感嘆符(!)を使用する場合は「全角」で表記します。", {
30+
column: index,
31+
fix: fixer.replaceTextRange([index, index + 1], "!")
32+
}));
33+
});
34+
// !の後ろは全角スペースが推奨
35+
// 半角スペースである場合
36+
const matchAfter = /( )[^\n]/;
37+
matchCaptureGroupAll(text, matchAfter).forEach(match => {
38+
const {index} = match;
39+
return report(node, new RuleError("文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。", {
40+
column: index,
41+
fix: fixer.replaceTextRange([index, index + 1], " ")
42+
}));
43+
});
3244
}
3345
};
46+
}
47+
export default {
48+
linter: reporter,
49+
fixer: reporter
3450
}

test/4.2.1-test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,22 @@ tester.run("4.2.1.感嘆符(!)", rule, {
1313
invalid: [
1414
{
1515
text: "半角感嘆符!",
16+
output: "半角感嘆符!",
1617
errors: [
17-
{message: "感嘆符(!)を使用する場合は「全角」で表記します。"}
18+
{
19+
message: "感嘆符(!)を使用する場合は「全角」で表記します。",
20+
column: 6
21+
}
1822
]
1923
},
2024
{
2125
text: "驚きの速さ! これが新製品のキャッチコピーでした。半角 ",
26+
output: "驚きの速さ! これが新製品のキャッチコピーでした。半角 ",
2227
errors: [
23-
{message: "文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。"}
28+
{
29+
message: "文末に感嘆符を使用し、後に別の文が続く場合は、直後に全角スペースを挿入します。",
30+
column: 7
31+
}
2432
]
2533
}
2634
]

0 commit comments

Comments
 (0)