Skip to content

Commit 2ed576e

Browse files
committed
feat(3.1.2): fixer support
1 parent cd4c8af commit 2ed576e

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/3.1.2.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
// LICENSE : MIT
22
"use strict";
33
import {isUserWrittenNode} from "./util/node-util";
4+
import {matchAll} from "./util/match-index";
5+
46
/*
57
3.1.2. 全角文字どうし
68
79
原則として、全角文字どうしの間にスペースを入れません。ただしカタカナ複合語の場合を除きます。
810
「2.1.7 カタカナ複合語」を参照してください。
911
*/
10-
export default function (context) {
11-
let {Syntax, RuleError, report, getSource} = context;
12+
function reporter(context) {
13+
let {Syntax, RuleError, report, fixer, getSource} = context;
1214
return {
1315
[Syntax.Str](node){
1416
if (!isUserWrittenNode(node, context)) {
1517
return;
1618
}
17-
let text = getSource(node);
19+
const text = getSource(node);
1820
// 全角同士の間は半角スペースを入れない
19-
let matchReg = /([\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--]) ([\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/g;
20-
let katakakana = /[-] [-]/;
21-
if (matchReg.test(text)) {
22-
var matches = text.match(matchReg);
23-
matches.forEach(match => {
24-
// ただし、カタカナ同士は例外
25-
if (!katakakana.test(match)) {
26-
report(node, new RuleError("原則として、全角文字どうしの間にスペースを入れません。"))
27-
}
28-
})
29-
}
21+
const matchReg = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])( )(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/g;
22+
const katakakana = /[-]( )[-]/;
23+
matchAll(text, matchReg).forEach(match => {
24+
const {input, captureGroups} = match;
25+
// ただしカタカナ複合語の場合を除きます。
26+
if (katakakana.test(input)) {
27+
return;
28+
}
29+
captureGroups.forEach(captureGroup => {
30+
const index = captureGroup.index;
31+
report(node, new RuleError("原則として、全角文字どうしの間にスペースを入れません。", {
32+
column: index,
33+
fix: fixer.replaceTextRange([index, index + 1], "")
34+
}));
35+
});
36+
});
3037
}
3138
}
39+
}
40+
export default {
41+
linter: reporter,
42+
fixer: reporter
3243
}

test/3.1.2-test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,29 @@ var tester = new TextLintTester();
66
tester.run("3.1.2. 全角文字どうし", rule, {
77
valid: [
88
"これは正解",
9+
"This is 大丈夫",
910
"This is a pen.",
10-
"ユーザー インターフェース"//例外
11+
"ユーザー インターフェース"//カタカナは例外
1112
],
1213
invalid: [
1314
{
1415
text: "これは ダメ",
16+
output: "これはダメ",
1517
errors: [
16-
{message: "原則として、全角文字どうしの間にスペースを入れません。"}
18+
{
19+
message: "原則として、全角文字どうしの間にスペースを入れません。",
20+
column: 4
21+
}
22+
]
23+
},
24+
{
25+
text: "これは どういうこと?",
26+
output: "これはどういうこと?",
27+
errors: [
28+
{
29+
message: "原則として、全角文字どうしの間にスペースを入れません。",
30+
column: 4
31+
}
1732
]
1833
}
1934
]

0 commit comments

Comments
 (0)