Skip to content

Commit d3d2c68

Browse files
committed
fix: 名詞同士の比較で、括弧記号はスキップするように変更
fix #12
1 parent 4872a6d commit d3d2c68

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/max-ten.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,45 @@ function isSandwichedMeishi({ before, token, after }) {
2525
return before.pos === "名詞" && after.pos === "名詞";
2626
}
2727

28+
/**
29+
* 括弧のトークンかどうか
30+
* @param token
31+
* @returns {boolean}
32+
*/
33+
function is括弧(token) {
34+
if (token.pos === "記号" && /^/.test(token.pos_detail_1)) {
35+
return true;
36+
}
37+
if (token.surface_form === "(" || token.surface_form === ")") {
38+
return true;
39+
}
40+
return false;
41+
}
42+
/**
43+
* 括弧などの記号的なTokenはスキップとして隣接するTokenを探す
44+
* @see https://github.com/textlint-ja/textlint-rule-max-ten/issues/12
45+
* @param {*[]} tokens
46+
* @param {number} currentIndex
47+
* @param {"prev"|"next"} direction
48+
* @returns {undefined | *}
49+
*/
50+
function findSiblingMeaningToken({ tokens, currentIndex, direction }) {
51+
const delta = direction === "prev" ? -1 : 1;
52+
const sibilingToken = tokens[currentIndex + delta];
53+
if (!sibilingToken) {
54+
return;
55+
}
56+
// 括弧はスキップして、隣接Nodeを探す
57+
if (is括弧(sibilingToken)) {
58+
return findSiblingMeaningToken({
59+
tokens,
60+
currentIndex: currentIndex + delta,
61+
direction
62+
});
63+
}
64+
return sibilingToken;
65+
}
66+
2867
/**
2968
* @param {RuleContext} context
3069
* @param {typeof defaultOptions} [options]
@@ -76,9 +115,17 @@ module.exports = function (context, options = {}) {
76115
if (surface === touten) {
77116
// 名詞に囲まわれている場合は例外とする
78117
const isSandwiched = isSandwichedMeishi({
79-
before: tokens[index - 1],
118+
before: findSiblingMeaningToken({
119+
tokens,
120+
currentIndex: index,
121+
direction: "prev"
122+
}),
80123
token: token,
81-
after: tokens[index + 1]
124+
after: findSiblingMeaningToken({
125+
tokens,
126+
currentIndex: index,
127+
direction: "next"
128+
})
82129
});
83130
// strictなら例外を例外としない
84131
if (!isStrict && isSandwiched) {

test/max-ten-test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ tester.run("max-ten", rule, {
1515
"名詞、名詞、名詞、名詞、名詞の場合は例外",
1616
"ビスケットの主な材料は(1)小麦粉、(2)牛乳、(3)ショートニング、(4)バター、(5)砂糖である。",
1717
"これは、TaskA、TaskB、TaskC、TaskDが処理するものです。",
18+
// 括弧は無視されるため、名詞の連続として扱う
19+
"変数の名前は、半角のアルファベットである`A`から`Z`(大文字)と`a`から`z`(小文字)、`_`(アンダースコア)、`$`(ダラー)、数字の`0`から`9`を組み合わせた名前にします。",
1820
{
1921
text: textIncludeTen(3)
2022
},
@@ -147,15 +149,16 @@ tester.run("max-ten", rule, {
147149
// 3行目が、の問題
148150
text: `このパラグラフはOKです。
149151
150-
変数の名前は、半角のアルファベットであるAからZ(大文字)とaからz(小文字)、_(アンダースコア)、$(ダラー)、数字の0から9を組み合わせた名前にします
152+
変数の名前は、名前のルールが決まっていて、そのルールは複雑で、たくさんのルールがあるので、箇条書きしましょう
151153
152154
3つめのパラグラフはもOKです。
153155
154156
`,
155157
errors: [
156158
{
157159
message: '一つの文で"、"を4つ以上使用しています',
158-
line: 3
160+
line: 3,
161+
column: 45
159162
}
160163
]
161164
}

0 commit comments

Comments
 (0)