-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path1.1.3.js
146 lines (138 loc) · 5.67 KB
/
1.1.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// LICENSE : MIT
"use strict";
/*
1.1.3.箇条書き
基本的に本文の文体に合わせます。
ただし、本文が「敬体」である場合、箇条書きに「常体」または「体言止め」も使う場合があります。
一般読者向け の文書で、本文が敬体である場合、多くの場合、箇条書きでも敬体を使います。
本文が「常体」である場合、箇条書きには「常体」または「体言止め」を使います。「敬体」は使いません。
いずれの場合も、ひとまとまりの箇条書きでは、敬体と常体を混在させません。文末に句点(。)を付けるかどうかも統一します。
*/
import { analyzeDesumasu, analyzeDearu } from "analyze-desumasu-dearu";
module.exports = function (context) {
let { Syntax, RuleError, report, getSource } = context;
let desumasuList = [];
let dearuList = [];
// 。付きのListItem
let withPointList = [];
// 。なしのListItem
let withoutPointList = [];
function resetList() {
dearuList = [];
desumasuList = [];
withPointList = [];
withoutPointList = [];
}
function reportPointResult(nodeList, { shouldUsePoint }) {
nodeList.forEach((node) => {
let message;
if (shouldUsePoint) {
message = `箇条書きの文末に句点(。)を付けて下さい。\n箇条書きの文末に句点(。)を付けるかを統一します。`;
} else {
message = `箇条書きの文末から句点(。)を外して下さい。\n箇条書きの文末に句点(。)を付けるかを統一します。`;
}
report(node, new RuleError(message));
});
}
function reportDesumaruDearuResult(list, { desumasu, dearu }) {
list.forEach(({ node, matches }) => {
matches.forEach((match) => {
let message;
if (desumasu) {
message = `箇条書きを敬体(ですます調)に統一して下さい。\nひとまとまりの箇条書きでは、敬体と常体を混在させません。\n"${match.value}"が常体(である調)です。`;
} else if (dearu) {
message = `箇条書きを常体(である調)に統一して下さい。\nひとまとまりの箇条書きでは、敬体と常体を混在させません。\n"${match.value}"が敬体(ですます調)です。`;
}
report(
node,
new RuleError(message, {
line: match.lineNumber - 1,
column: match.columnIndex
})
);
});
});
}
// 末尾に。があるかが統一されているのチェック
function countingPoint(withPointList, withoutPointList) {
if (withPointList.length === 0 || withoutPointList.length === 0) {
return;
}
if (withPointList.length > withoutPointList.length) {
// 。ありに統一
reportPointResult(withoutPointList, {
shouldUsePoint: true
});
} else if (withPointList.length < withoutPointList.length) {
// 。なしに統一
reportPointResult(withPointList, {
shouldUsePoint: false
});
} else {
// 。ありに統一
reportPointResult(withoutPointList, {
shouldUsePoint: true
});
}
}
// 敬体(ですます調)あるいは常体(である調)なのかのチェック
function countingDesumasuDearu(desumasuList, dearuList) {
let desumasuCount = desumasuList.reduce((count, { matches }) => count + matches.length, 0);
let dearuCount = dearuList.reduce((count, { matches }) => count + matches.length, 0);
if (desumasuCount === 0 || dearuCount === 0) {
return;
}
// ですます優先
if (desumasuCount > dearuCount) {
reportDesumaruDearuResult(dearuList, {
desumasu: true
});
} else if (desumasuCount < dearuCount) {
// である優先
reportDesumaruDearuResult(desumasuList, {
dearu: true
});
} else {
// 同等の場合はですます優先
reportDesumaruDearuResult(dearuList, {
desumasu: true
});
}
}
return {
[Syntax.List](node) {
resetList();
},
[Syntax.ListItem](node) {
let text = getSource(node);
// 末尾に。があるかが統一されているのチェック
let matchPointReg = /。(\s*?)$/;
if (matchPointReg.test(text)) {
// 。あり
withPointList.push(node);
} else {
// 。なし
withoutPointList.push(node);
}
// 敬体(ですます調)あるいは常体(である調)なのかのチェック
let retDesumasu = analyzeDesumasu(text);
if (retDesumasu.length > 0) {
desumasuList.push({
node: node,
matches: retDesumasu
});
}
let retDearu = analyzeDearu(text);
if (retDearu.length > 0) {
dearuList.push({
node: node,
matches: retDearu
});
}
},
[`${Syntax.List}:exit`](node) {
countingPoint(withPointList, withoutPointList);
countingDesumasuDearu(desumasuList, dearuList);
}
};
};