6
6
全角の大かっこを使用します
7
7
*/
8
8
import { isUserWrittenNode } from "./util/node-util" ;
9
- export default function ( context ) {
10
- let { Syntax, RuleError, report, getSource} = context ;
9
+ import { matchCaptureGroupAll } from "./util/match-index" ;
10
+ import regx from 'regx' ;
11
+ import { japaneseRegExp } from "./util/regexp" ;
12
+ const rx = regx ( "g" ) ;
13
+
14
+ const replaceSymbol = ( symbol ) => {
15
+ var newSymbol = {
16
+ "[" : "[" ,
17
+ "]" : "]"
18
+ } [ symbol ] ;
19
+ if ( ! newSymbol ) {
20
+ throw new Error ( "fail to replace symbol" ) ;
21
+ }
22
+ return newSymbol ;
23
+ } ;
24
+ function reporter ( context ) {
25
+ let { Syntax, RuleError, report, fixer, getSource} = context ;
11
26
return {
12
27
[ Syntax . Str ] ( node ) {
13
28
if ( ! isUserWrittenNode ( node , context ) ) {
14
29
return ;
15
30
}
16
- let text = getSource ( node ) ;
17
- // 半角の大かっこ[]は使用しない
18
- var matchHanQuestion = / [ \[ \] ] / ;
19
- var index = text . search ( matchHanQuestion ) ;
20
- if ( index !== - 1 ) {
21
- return report ( node , new RuleError ( "半角の大かっこ[]が使用されています。" , index + 1 ) )
22
- }
31
+ // 半角のかっこ[]は使用しないで全角のかっこを使用する
32
+ const text = getSource ( node ) ;
33
+ const matchRegExp = rx `(?:${ japaneseRegExp } )([\[\]])` ;
34
+ matchCaptureGroupAll ( text , matchRegExp ) . forEach ( match => {
35
+ const { index} = match ;
36
+ report ( node , new RuleError ( "半角の大かっこ[]が使用されています。全角のかっこ[]を使用してください。" , {
37
+ column : index ,
38
+ fix : fixer . replaceTextRange ( [ index , index + 1 ] , replaceSymbol ( match . text ) )
39
+ } ) ) ;
40
+ } ) ;
23
41
}
24
42
} ;
43
+ }
44
+ export default {
45
+ linter : reporter ,
46
+ fixer : reporter
25
47
}
0 commit comments