@@ -15,47 +15,65 @@ export function checkPair(context, {left, right}) {
15
15
let { Syntax, RuleError, report, getSource} = context ;
16
16
let helper = new RuleHelper ( context ) ;
17
17
let isInParagraph = false ;
18
- let matchParentheses = [ ] ;
18
+ let currentStrInParagraph = [ ] ;
19
+ /**
20
+ * `Str` nodeの配列を受け取り、pairが見つからないnodeを返す
21
+ * @param {Object } currentStrInParagraph
22
+ * @returns {{node, index}[] }
23
+ */
24
+ const foundMissingPairNodes = ( currentStrInParagraph ) => {
25
+ let foundLeft = false ;
26
+ let matchParentheses = [ ] ;
27
+ currentStrInParagraph . forEach ( node => {
28
+ const text = getSource ( node ) ;
29
+ // left を探す
30
+ let leftIndex = - 1 ;
31
+ if ( ! foundLeft ) {
32
+ leftIndex = text . indexOf ( left ) ;
33
+ if ( leftIndex !== - 1 ) {
34
+ matchParentheses . push ( {
35
+ node,
36
+ index : leftIndex
37
+ } ) ;
38
+ foundLeft = true
39
+ }
40
+ }
41
+ // right を探す
42
+ let pairIndex = text . indexOf ( right , leftIndex + 1 ) ;
43
+ if ( pairIndex !== - 1 ) {
44
+ matchParentheses . pop ( ) ;
45
+ foundLeft = false ;
46
+ }
47
+ } ) ;
48
+ return matchParentheses ;
49
+ } ;
19
50
return {
20
51
[ Syntax . Paragraph ] ( node ) {
21
52
if ( helper . isChildNode ( node , [ Syntax . BlockQuote ] ) ) {
22
53
return ;
23
54
}
55
+ currentStrInParagraph = [ ] ;
24
56
isInParagraph = true
25
57
} ,
26
58
[ Syntax . Str ] ( node ) {
27
59
if ( ! isInParagraph ) {
28
60
return ;
29
61
}
30
- let text = getSource ( node ) ;
31
- // left を探す
32
- let index = text . indexOf ( left ) ;
33
- if ( index !== - 1 ) {
34
- matchParentheses . push ( {
35
- node,
36
- index
37
- } ) ;
38
- }
39
- // right を探す
40
- let pairIndex = text . indexOf ( right , index + 1 ) ;
41
- if ( pairIndex !== - 1 ) {
42
- matchParentheses . pop ( ) ;
43
- }
62
+ currentStrInParagraph . push ( node ) ;
44
63
} ,
45
- [ `${ Syntax . Paragraph } :exit` ] ( node ) {
64
+ [ `${ Syntax . Paragraph } :exit` ] ( ) {
65
+ const missingPairList = foundMissingPairNodes ( currentStrInParagraph ) ;
66
+ // 探索おわり
46
67
isInParagraph = false ;
47
68
// 全ての対が見つかったなら配列は空になる
48
- if ( matchParentheses . length === 0 ) {
69
+ if ( missingPairList . length === 0 ) {
49
70
return ;
50
71
}
51
-
52
- matchParentheses . forEach ( ( { node, index} ) => {
72
+ missingPairList . forEach ( ( { node, index} ) => {
53
73
report ( node , new RuleError ( `${ left } の対となる${ right } が見つかりません。${ left } ${ right } ` , {
54
74
index
55
75
} ) ) ;
56
76
} ) ;
57
- // clear state
58
- matchParentheses = [ ] ;
59
77
}
60
78
} ;
61
79
0 commit comments