Skip to content

Commit 84b2e03

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

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/3.3.js

Lines changed: 31 additions & 1 deletion
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,18 @@ 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 = {
18-
allowOutsideHalfParentheses: true
21+
allowOutsideHalfParentheses: true,
22+
requireOutsideHalfParentheses: false
1923
};
2024
function reporter(context, options) {
2125
let { Syntax, RuleError, report, fixer, getSource } = context;
2226
const allowOutsideHalfParentheses =
2327
options.allowOutsideHalfParentheses ?? defaultOptions.allowOutsideHalfParentheses;
28+
const requireOutsideHalfParentheses =
29+
options.requireOutsideHalfParentheses ?? defaultOptions.requireOutsideHalfParentheses;
2430
return {
2531
[Syntax.Str](node) {
2632
if (!isUserWrittenNode(node, context)) {
@@ -59,6 +65,30 @@ function reporter(context, options) {
5965
);
6066
});
6167
});
68+
if (requireOutsideHalfParentheses) {
69+
// 左にスペース必須
70+
matchCaptureGroupAll(text, leftHalfParentheses).forEach((match) => {
71+
const { index } = match;
72+
report(
73+
node,
74+
new RuleError("半角かっこの外側に半角スペースが必要です。", {
75+
index,
76+
fix: fixer.replaceTextRange([index, index + 1], " " + match.text)
77+
})
78+
);
79+
});
80+
// 右にスペース必須
81+
matchCaptureGroupAll(text, rightHalfParentheses).forEach((match) => {
82+
const { index } = match;
83+
report(
84+
node,
85+
new RuleError("半角かっこの外側に半角スペースが必要です。", {
86+
index,
87+
fix: fixer.replaceTextRange([index, index + 1], match.text + " ")
88+
})
89+
);
90+
});
91+
}
6292
}
6393
};
6494
}

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)