Skip to content

Commit f715a00

Browse files
committed
feat(4.3.1): fixer support
1 parent aebbbf8 commit f715a00

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/4.3.1.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,42 @@
66
全角のかっこを使用します
77
*/
88
import {isUserWrittenNode} from "./util/node-util";
9-
export default function (context) {
10-
let {Syntax, RuleError, report, getSource} = context;
9+
import {matchCaptureGroupAll} from "./util/match-index";
10+
import regx from 'regx';
11+
import {japaneseRegExp} from "./util/regexp";
12+
const rx = regx("g");
13+
14+
const replaceSymbol = (symbol) => {
15+
var newSymbol = {
16+
"(": "(",
17+
")": ")"
18+
}[symbol];
19+
if (!newSymbol) {
20+
throw new Error("fail to replace symbol");
21+
}
22+
return newSymbol;
23+
};
24+
function reporter(context) {
25+
let {Syntax, RuleError, report, fixer, getSource} = context;
1126
return {
1227
[Syntax.Str](node){
1328
if (!isUserWrittenNode(node, context)) {
1429
return;
1530
}
16-
let text = getSource(node);
17-
// 半角のかっこ()は使用しない
18-
var matchHanQuestion = /([\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])[\(\)]/;
19-
var index = text.search(matchHanQuestion);
20-
if (index !== -1) {
21-
return report(node, new RuleError("半角のかっこ()が使用されています。", index + 1))
22-
}
31+
// 半角のかっこ()は使用しないで全角のかっこを使用する
32+
const text = getSource(node);
33+
const matchRegExp = rx`(?:${japaneseRegExp})([\(\)])`;
34+
matchCaptureGroupAll(text, matchRegExp).forEach(match => {
35+
const {index} = match;
36+
report(node, new RuleError("半角のかっこ()が使用されています。全角のかっこ()を使用してください。", {
37+
column: index,
38+
fix: fixer.replaceTextRange([index, index + 1], replaceSymbol(match.text))
39+
}));
40+
});
2341
}
2442
};
43+
}
44+
export default {
45+
linter: reporter,
46+
fixer: reporter
2547
}

test/4.3.1-test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,31 @@ tester.run("4.3.1.丸かっこ()", rule, {
99
],
1010
invalid: [
1111
{
12-
text: "クォーク(物質の素粒子)",// 半角かっこ
12+
// 半角かっこ
13+
text: "クォーク(物質の素粒子)",
14+
output: "クォーク(物質の素粒子)",
1315
errors: [
1416
{
15-
message: "半角のかっこ()が使用されています。",
17+
message: "半角のかっこ()が使用されています。全角のかっこ()を使用してください。",
1618
column: 5
19+
},
20+
{
21+
message: "半角のかっこ()が使用されています。全角のかっこ()を使用してください。",
22+
column: 12
23+
}
24+
]
25+
},
26+
{
27+
// 半角かっこ
28+
text: "例)test",
29+
output: "例)test",
30+
errors: [
31+
{
32+
message: "半角のかっこ()が使用されています。全角のかっこ()を使用してください。",
33+
column: 2
1734
}
1835
]
1936
}
37+
2038
]
2139
});

0 commit comments

Comments
 (0)