-
Notifications
You must be signed in to change notification settings - Fork 18
/
3.3.js
58 lines (56 loc) · 2.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
// LICENSE : MIT
"use strict";
/*
3.3. かっこ類と隣接する文字の間のスペースの有無
かっこの外側、内側ともにスペースを入れません。
*/
import { isUserWrittenNode } from "./util/node-util";
import { matchCaptureGroupAll } from "match-index";
const brackets = ["\\[", "\\]", "(", ")", "[", "]", "「", "」", "『", "』"];
const leftBrackets = brackets.map((bracket) => {
return new RegExp("([ ])" + bracket, "g");
});
const rightBrackets = brackets.map((bracket) => {
return new RegExp(bracket + "([ ])", "g");
});
function reporter(context) {
let { Syntax, RuleError, report, fixer, getSource } = context;
return {
[Syntax.Str](node) {
if (!isUserWrittenNode(node, context)) {
return;
}
const text = getSource(node);
// 左にスペース
leftBrackets.forEach((pattern) => {
matchCaptureGroupAll(text, pattern).forEach((match) => {
const { index } = match;
report(
node,
new RuleError("かっこの外側、内側ともにスペースを入れません。", {
index: index,
fix: fixer.replaceTextRange([index, index + 1], "")
})
);
});
});
// 右にスペース
rightBrackets.forEach((pattern) => {
matchCaptureGroupAll(text, pattern).forEach((match) => {
const { index, text } = match;
report(
node,
new RuleError("かっこの外側、内側ともにスペースを入れません。", {
index: index,
fix: fixer.replaceTextRange([index, index + 1], "")
})
);
});
});
}
};
}
module.exports = {
linter: reporter,
fixer: reporter
};