@@ -5,7 +5,6 @@ const assert = require("assert");
5
5
全角文字と半角文字の間にスペースを入れるかどうか
6
6
*/
7
7
import { RuleHelper } from "textlint-rule-helper" ;
8
- import { matchCaptureGroupAll } from "match-index" ;
9
8
import { matchPatterns } from "@textlint/regexp-string-matcher" ;
10
9
11
10
const PunctuationRegExp = / [ 。 、 ] / ;
@@ -71,7 +70,7 @@ function reporter(context, options = {}) {
71
70
/**
72
71
* `text`を対象に例外オプションを取り除くfilter関数を返す
73
72
* @param {string } text テスト対象のテキスト全体
74
- * @param {number } padding +1 or -1
73
+ * @param {number } padding 例外文字までのオフセット
75
74
* @returns {function(*, *) }
76
75
*/
77
76
const createFilter = ( text , padding ) => {
@@ -101,20 +100,20 @@ function reporter(context, options = {}) {
101
100
} ;
102
101
// Never: アルファベットと全角の間はスペースを入れない
103
102
const noSpaceBetween = ( node , text ) => {
104
- const betweenHanAndZen = matchCaptureGroupAll ( text , new RegExp ( `[A-Za-z0-9]([ ])(?:${ ZenRegExpStr } )` ) ) ;
105
- const betweenZenAndHan = matchCaptureGroupAll ( text , new RegExp ( `(?:${ ZenRegExpStr } )([ ])[A-Za-z0-9]` ) ) ;
103
+ const betweenHanAndZen = text . matchAll ( new RegExp ( `[A-Za-z0-9]([ ])(?:${ ZenRegExpStr } )` , "g" ) ) ;
104
+ const betweenZenAndHan = text . matchAll ( new RegExp ( `(?:${ ZenRegExpStr } )([ ])[A-Za-z0-9]` , "g" ) ) ;
106
105
const reportMatch = ( match ) => {
107
- const { index } = match ;
106
+ const indexOneBased = match . index + 1 ;
108
107
report (
109
108
node ,
110
109
new RuleError ( "原則として、全角文字と半角文字の間にスペースを入れません。" , {
111
- index : match . index ,
112
- fix : fixer . replaceTextRange ( [ index , index + 1 ] , "" )
110
+ index : indexOneBased ,
111
+ fix : fixer . replaceTextRange ( [ indexOneBased , indexOneBased + 1 ] , "" )
113
112
} )
114
113
) ;
115
114
} ;
116
- betweenHanAndZen . filter ( createFilter ( text , 1 ) ) . forEach ( reportMatch ) ;
117
- betweenZenAndHan . filter ( createFilter ( text , - 1 ) ) . forEach ( reportMatch ) ;
115
+ Array . from ( betweenHanAndZen ) . filter ( createFilter ( text , 2 ) ) . forEach ( reportMatch ) ;
116
+ Array . from ( betweenZenAndHan ) . filter ( createFilter ( text , 0 ) ) . forEach ( reportMatch ) ;
118
117
} ;
119
118
120
119
// Always: アルファベットと全角の間はスペースを入れる
@@ -136,27 +135,27 @@ function reporter(context, options = {}) {
136
135
expStr = `(${ ZenRegExpStr } )[${ alphabets } ${ numbers } ]` ;
137
136
}
138
137
139
- return new RegExp ( expStr ) ;
138
+ return new RegExp ( expStr , "g" ) ;
140
139
} ;
141
140
142
141
const betweenHanAndZenRegExp = generateRegExp ( options ) ;
143
142
const betweenZenAndHanRegExp = generateRegExp ( options , false ) ;
144
143
const errorMsg = "原則として、全角文字と半角文字の間にスペースを入れます。" ;
145
144
146
- const betweenHanAndZen = matchCaptureGroupAll ( text , betweenHanAndZenRegExp ) ;
147
- const betweenZenAndHan = matchCaptureGroupAll ( text , betweenZenAndHanRegExp ) ;
145
+ const betweenHanAndZen = text . matchAll ( betweenHanAndZenRegExp ) ;
146
+ const betweenZenAndHan = text . matchAll ( betweenZenAndHanRegExp ) ;
148
147
const reportMatch = ( match ) => {
149
148
const { index } = match ;
150
149
report (
151
150
node ,
152
151
new RuleError ( errorMsg , {
153
- index : match . index ,
152
+ index : index ,
154
153
fix : fixer . replaceTextRange ( [ index + 1 , index + 1 ] , " " )
155
154
} )
156
155
) ;
157
156
} ;
158
- betweenHanAndZen . filter ( createFilter ( text , 1 ) ) . forEach ( reportMatch ) ;
159
- betweenZenAndHan . filter ( createFilter ( text , 0 ) ) . forEach ( reportMatch ) ;
157
+ Array . from ( betweenHanAndZen ) . filter ( createFilter ( text , 1 ) ) . forEach ( reportMatch ) ;
158
+ Array . from ( betweenZenAndHan ) . filter ( createFilter ( text , 0 ) ) . forEach ( reportMatch ) ;
160
159
} ;
161
160
return {
162
161
[ Syntax . Str ] ( node ) {
0 commit comments