@@ -25,6 +25,45 @@ function isSandwichedMeishi({ before, token, after }) {
25
25
return before . pos === "名詞" && after . pos === "名詞" ;
26
26
}
27
27
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
+
28
67
/**
29
68
* @param {RuleContext } context
30
69
* @param {typeof defaultOptions } [options]
@@ -76,9 +115,17 @@ module.exports = function (context, options = {}) {
76
115
if ( surface === touten ) {
77
116
// 名詞に囲まわれている場合は例外とする
78
117
const isSandwiched = isSandwichedMeishi ( {
79
- before : tokens [ index - 1 ] ,
118
+ before : findSiblingMeaningToken ( {
119
+ tokens,
120
+ currentIndex : index ,
121
+ direction : "prev"
122
+ } ) ,
80
123
token : token ,
81
- after : tokens [ index + 1 ]
124
+ after : findSiblingMeaningToken ( {
125
+ tokens,
126
+ currentIndex : index ,
127
+ direction : "next"
128
+ } )
82
129
} ) ;
83
130
// strictなら例外を例外としない
84
131
if ( ! isStrict && isSandwiched ) {
0 commit comments