Skip to content

Commit 378d68f

Browse files
committed
feat(3.3): 半角かっこのときに外側のスペースを必須にするオプションを追加
1 parent ea6a54f commit 378d68f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/3.3.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
import { isUserWrittenNode } from "./util/node-util";
88
import { matchCaptureGroupAll } from "match-index";
9+
import { japaneseRegExp } from "./util/regexp";
910

1011
const brackets = ["\\(", "\\)", "\\[", "\\]", "(", ")", "[", "]", "「", "」", "『", "』"];
1112
const leftBrackets = brackets.map((bracket) => {
@@ -14,13 +15,17 @@ const leftBrackets = brackets.map((bracket) => {
1415
const rightBrackets = brackets.map((bracket) => {
1516
return new RegExp(bracket + "([  ])", "g");
1617
});
18+
const leftHalfParentheses = new RegExp(`${japaneseRegExp.source}(\\()`, "g");
19+
const rightHalfParentheses = new RegExp(`(\\))${japaneseRegExp.source}`, "g");
1720
const defaultOptions = {
1821
allowOutsideHalfParentheses: true
1922
};
2023
function reporter(context, options) {
2124
let { Syntax, RuleError, report, fixer, getSource } = context;
2225
const allowOutsideHalfParentheses =
2326
options.allowOutsideHalfParentheses ?? defaultOptions.allowOutsideHalfParentheses;
27+
const requireOutsideHalfParentheses =
28+
options.requireOutsideHalfParentheses ?? defaultOptions.requireOutsideHalfParentheses;
2429
return {
2530
[Syntax.Str](node) {
2631
if (!isUserWrittenNode(node, context)) {
@@ -59,6 +64,30 @@ function reporter(context, options) {
5964
);
6065
});
6166
});
67+
if (requireOutsideHalfParentheses) {
68+
// 左にスペース必須
69+
matchCaptureGroupAll(text, leftHalfParentheses).forEach((match) => {
70+
const { index } = match;
71+
report(
72+
node,
73+
new RuleError("半角かっこの外側に半角スペースが必要です。", {
74+
index,
75+
fix: fixer.replaceTextRange([index, index + 1], " " + match.text)
76+
})
77+
);
78+
});
79+
// 右にスペース必須
80+
matchCaptureGroupAll(text, rightHalfParentheses).forEach((match) => {
81+
const { index } = match;
82+
report(
83+
node,
84+
new RuleError("半角かっこの外側に半角スペースが必要です。", {
85+
index,
86+
fix: fixer.replaceTextRange([index, index + 1], match.text + " ")
87+
})
88+
);
89+
});
90+
}
6291
}
6392
};
6493
}

test/3.3-test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ tester.run("3.3.かっこ類と隣接する文字の間のスペースの有無"
1313
そのため、特別な実装は必要なく
1414
「拡張する時は\`calculator.prototype\`の代わりに\`calculator.fn\`を拡張してください」
1515
というルールがあるだけとも言えます。
16-
`
16+
`,
17+
{
18+
text: "Page(s)",
19+
options: {
20+
requireOutsideHalfParentheses: true
21+
}
22+
}
1723
],
1824
invalid: [
1925
{
@@ -67,6 +73,25 @@ tester.run("3.3.かっこ類と隣接する文字の間のスペースの有無"
6773
}
6874
]
6975
},
76+
{
77+
text: "これはダメ(test)です",
78+
output: "これはダメ (test) です",
79+
options: {
80+
requireOutsideHalfParentheses: true
81+
},
82+
errors: [
83+
{
84+
message: "半角かっこの外側に半角スペースが必要です。",
85+
line: 1,
86+
column: 6
87+
},
88+
{
89+
message: "半角かっこの外側に半角スペースが必要です。",
90+
line: 1,
91+
column: 11
92+
}
93+
]
94+
},
7095
{
7196
text: `TEST
7297

0 commit comments

Comments
 (0)