-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathjp-reactjs-org-lint.js
102 lines (88 loc) · 3.08 KB
/
jp-reactjs-org-lint.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
const toString = require('mdast-util-to-string');
module.exports = context => {
return {
// [context.Syntax.Header]: node => {},
[context.Syntax.Paragraph]: node => {
// Checks against paragraph-based rules.
// Convert a paragraph to plain text, stripping any markups
const text = toString(node);
enforceSpaceAfterQuestionOrExclamation(node, text, context);
noHanPunctSpace(node, text, context);
enforceZenHanSpace(node, text, context);
noLineEndSpace(node, text, context);
noConflictMarker(node, text, context);
},
[context.Syntax.Str]: node => {
const text = toString(node);
const index = text.indexOf(' ');
if (index >= 0) {
context.report(
node,
new context.RuleError(
'いかなる場合も全角スペースは使用しないでください。',
{index},
),
);
}
},
};
};
const enforceSpaceAfterQuestionOrExclamation = (node, text, context) => {
const markReg = /[!?](\u3000|[A-Za-z0-9]|[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/;
if (markReg.exec(text)) {
context.report(
node,
new context.RuleError(
'全角の「!」「?」と次の文の間には半角スペースを挿入してください。',
),
);
}
};
const noHanPunctSpace = (node, text, context) => {
const match = /(.{0,3})[、。]( |\u3000)/.exec(text);
if (match) {
context.report(
node,
new context.RuleError(
`全角の句読点の直後にスペースを入れてはいけません("${match[0]}")。`,
),
);
}
};
const enforceZenHanSpace = (node, text, context) => {
const hanZen = /[A-Za-z0-9](?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/;
const zenHan = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])[A-Za-z0-9]/;
const hanZenMatch = hanZen.exec(text);
const zenHanMatch = zenHan.exec(text);
if (hanZenMatch || zenHanMatch) {
const matched = hanZenMatch ? hanZenMatch[0] : zenHanMatch[0];
context.report(
node,
new context.RuleError(
`全角文字と半角英数字とが隣接しています("${matched}")。` +
`半角スペースを挿入してください。`,
),
);
}
};
const noLineEndSpace = (node, text, context) => {
// This detects a line-end space *only when* the line contains Japanese characters.
const reg = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ]).?(\s|\u3000)$/;
if (reg.exec(text)) {
context.report(
node,
new context.RuleError('行末にスペース文字を入れないでください。'),
);
}
};
const noConflictMarker = (node, text, context) => {
const reg = /<<<<<<< HEAD/;
if (reg.exec(text)) {
context.report(
node,
new context.RuleError(
'コンフリクトマーカーが残っています。コンフリクトを解消してください。',
),
);
}
};