-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path4.2.6.js
More file actions
49 lines (49 loc) · 1.89 KB
/
4.2.6.js
File metadata and controls
49 lines (49 loc) · 1.89 KB
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
// LICENSE : MIT
"use strict";
/*
4.2.6.ハイフン(-)
原則として和文ではハイフン(-)を使用しません。
使用する場合は半角で表記します。原文でハイフンが使われている場合も、和文では使用しません。
例外は、住所や電話番号の区切りに使う場合です。
*/
import { isUserWrittenNode } from "./util/node-util";
import { matchCaptureGroupAll } from "match-index";
import regx from "regx";
import { japaneseRegExp } from "./util/regexp";
import mergeMatches from "./util/merge-matches";
const rx = regx("g");
module.exports = function (context) {
let { Syntax, RuleError, report, getSource } = context;
return {
[Syntax.Str](node) {
if (!isUserWrittenNode(node, context)) {
return;
}
let text = getSource(node);
// 和文ではハイフン(-)を使用しません
// right
const rightMatches = matchCaptureGroupAll(text, rx`${japaneseRegExp}(\-)`);
// left
const leftMatches = matchCaptureGroupAll(text, rx`(\-)${japaneseRegExp}`);
const matches = mergeMatches(leftMatches, rightMatches);
matches.forEach((match) => {
const { index } = match;
// 数値の符号としてのハイフンは許可する
const nextChar = text[index + 1];
if (nextChar && /[0-9]/.test(nextChar)) {
return;
}
report(
node,
new RuleError(
`原則として和文ではハイフン(-)を使用しません。
例外は、住所や電話番号の区切りに使う場合です。`,
{
index: index
}
)
);
});
}
};
};