@@ -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 ) {
0 commit comments