Skip to content

Commit 69f21f7

Browse files
authored
feat(rule): add allow option (#2)
無視する単語を設定できる `allow` オプションを追加 ```js { "rules": { "max-kanji-continuous-len": { // 連続できる漢字の文字数 // Allow max continuous length of kanji // If {current} > max(5), report Error. max: 5, // "倍精度浮動小数点数"という単語は例外として無視します allow: ["倍精度浮動小数点数"] } } } ```
1 parent 83ff52b commit 69f21f7

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ textlint --rule max-kanji-continuous-len README.md
3131

3232
## Options
3333

34-
- `max`
34+
- `max`: `number`
3535
- default: 5
36+
- 連続できる漢字の文字数
3637
- `一二三四五六`は6文字なのでエラーとなります。
37-
- 最大の漢字長
38-
38+
- `allow`: `string[]`
39+
- default: `[]`
40+
- 無視する単語の配列
3941

4042
```js
4143
{
4244
"rules": {
4345
"max-kanji-continuous-len": {
44-
// 最大の漢字長
46+
// 連続できる漢字の文字数
4547
// Allow max continuous length of kanji
4648
// If {current} > max(5), report Error.
47-
max: 5
49+
max: 5,
50+
// "倍精度浮動小数点数"という単語は例外として無視します
51+
allow: ["倍精度浮動小数点数"]
4852
}
4953
}
5054
}

src/textlint-rule-max-kanji-continuous-len.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,40 @@ import {matchCaptureGroupAll} from "match-index"
55

66
const KanjiRegExp = /((?:[\u3400-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF])+)/g;
77
const defaultOptions = {
8-
// 最大の漢字連続長
9-
// Allow max continuous length of kanji
8+
// 連続できる最大の文字数
109
// If {current} > max(5), report Error.
11-
max: 5
10+
max: 5,
11+
// 許可する単語のリスト
12+
allow: []
1213
};
1314

1415
module.exports = function reporter(context, options = defaultOptions) {
1516
const {Syntax, RuleError, report, fixer, getSource} = context;
1617
const helper = new RuleHelper(context);
1718
const maxLength = options.max || defaultOptions.max;
19+
const allowWords = options.allow || defaultOptions.allow;
1820
return {
1921
[Syntax.Str](node){
2022
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
2123
return;
2224
}
2325
const text = getSource(node);
2426
matchCaptureGroupAll(text, KanjiRegExp).forEach(({text, index}) => {
25-
// max より 大きい場合はエラー
26-
if (text.length > maxLength) {
27+
// max以下であるなら無視する
28+
if (text.length <= maxLength) {
29+
return;
30+
}
31+
// 辞書にある単語は無視する
32+
if (allowWords.indexOf(text) !== -1) {
33+
return;
34+
}
35+
// maxより長い場合はエラーとなる
2736
const ruleError = new RuleError(`漢字が${maxLength + 1}つ以上連続しています: ${text}`, {
2837
index
2938
});
3039
report(node, ruleError);
3140
}
32-
});
41+
);
3342
}
3443
}
3544
};

test/textlint-rule-max-kanji-continuous-len-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ tester.run("max-kanji-continuous-len", rule, {
1313
options: {
1414
max: 6 // 6 is ok, 7 is ng
1515
}
16+
},
17+
{
18+
text: "まず倍精度浮動小数点数とは",
19+
options: {
20+
max: 6,
21+
allow: ["倍精度浮動小数点数"]
22+
}
1623
}
1724
],
1825
invalid: [

0 commit comments

Comments
 (0)