-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path3.3.js
98 lines (97 loc) · 4.05 KB
/
3.3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// LICENSE : MIT
"use strict";
/*
3.3. かっこ類と隣接する文字の間のスペースの有無
かっこの外側、内側ともにスペースを入れません。
*/
import { isUserWrittenNode } from "./util/node-util";
import { matchCaptureGroupAll } from "match-index";
import { japaneseRegExp } from "./util/regexp";
const brackets = ["\\(", "\\)", "\\[", "\\]", "(", ")", "[", "]", "「", "」", "『", "』"];
const leftBrackets = brackets.map((bracket) => {
return new RegExp("([ ])" + bracket, "g");
});
const rightBrackets = brackets.map((bracket) => {
return new RegExp(bracket + "([ ])", "g");
});
const leftHalfParentheses = new RegExp(`${japaneseRegExp.source}(\\()`, "g");
const rightHalfParentheses = new RegExp(`(\\))${japaneseRegExp.source}`, "g");
const defaultOptions = {
allowOutsideHalfParentheses: true,
requireOutsideHalfParentheses: false
};
function reporter(context, options) {
let { Syntax, RuleError, report, fixer, getSource } = context;
const allowOutsideHalfParentheses =
options.allowOutsideHalfParentheses ?? defaultOptions.allowOutsideHalfParentheses;
const requireOutsideHalfParentheses =
options.requireOutsideHalfParentheses ?? defaultOptions.requireOutsideHalfParentheses;
return {
[Syntax.Str](node) {
if (!isUserWrittenNode(node, context)) {
return;
}
const text = getSource(node);
// 左にスペース
leftBrackets.forEach((pattern) => {
matchCaptureGroupAll(text, pattern).forEach((match) => {
const { index } = match;
if (allowOutsideHalfParentheses && text.substring(index, index + 2) === " (") {
return;
}
report(
node,
new RuleError("かっこの外側、内側ともにスペースを入れません。", {
index: index,
fix: fixer.replaceTextRange([index, index + 1], "")
})
);
});
});
// 右にスペース
rightBrackets.forEach((pattern) => {
matchCaptureGroupAll(text, pattern).forEach((match) => {
const { index } = match;
if (allowOutsideHalfParentheses && text.substring(index - 1, index + 1) === ") ") {
return;
}
report(
node,
new RuleError("かっこの外側、内側ともにスペースを入れません。", {
index: index,
fix: fixer.replaceTextRange([index, index + 1], "")
})
);
});
});
if (requireOutsideHalfParentheses) {
// 左にスペース必須
matchCaptureGroupAll(text, leftHalfParentheses).forEach((match) => {
const { index } = match;
report(
node,
new RuleError("半角かっこの外側に半角スペースが必要です。", {
index,
fix: fixer.replaceTextRange([index, index + 1], " " + match.text)
})
);
});
// 右にスペース必須
matchCaptureGroupAll(text, rightHalfParentheses).forEach((match) => {
const { index } = match;
report(
node,
new RuleError("半角かっこの外側に半角スペースが必要です。", {
index,
fix: fixer.replaceTextRange([index, index + 1], match.text + " ")
})
);
});
}
}
};
}
module.exports = {
linter: reporter,
fixer: reporter
};