Skip to content

Commit 87c735d

Browse files
committed
feat(4.3.2): fixer support
1 parent 1323c7f commit 87c735d

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/4.3.2.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 = /[\[\]]/;
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.2-test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ tester.run("4.3.2.大かっこ[]", rule, {
1111
],
1212
invalid: [
1313
{
14-
text: "半角[かっこ",// 半角かっこ Markdown的に意味を持つので片方ずつ
14+
// 半角かっこ Markdown的に意味を持つので片方ずつ
15+
text: "半角[かっこ",
16+
output: "半角[かっこ",
1517
errors: [
1618
{
17-
message: "半角の大かっこ[]が使用されています。",
18-
column: 4
19+
message: "半角の大かっこ[]が使用されています。全角のかっこ[]を使用してください。",
20+
column: 3
1921
}
2022
]
2123
},
2224
{
23-
text: "半角]かっこ",// 半角かっこ Markdown的に意味を持つので片方ずつ
25+
// 半角かっこ Markdown的に意味を持つので片方ずつ
26+
text: "半角]かっこ",
27+
output: "半角]かっこ",
2428
errors: [
2529
{
26-
message: "半角の大かっこ[]が使用されています。",
27-
column: 4
30+
message: "半角の大かっこ[]が使用されています。全角のかっこ[]を使用してください。",
31+
column: 3
2832
}
2933
]
3034
}

0 commit comments

Comments
 (0)